我坚持我的代码,蛇的头部移动 [英] Im stuck on my code, the head of the snake dosent move
本文介绍了我坚持我的代码,蛇的头部移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我启动程序时,蛇会移动,但是很有效,我不知道为什么!
when i start the program the snake shold move but is dosent and i dont know why!
Public Class Form1
#Region "Snake"
Dim snake(1000) As PictureBox
Dim comprimento_da_cobra As Integer = -1
Dim esquerda_direita As Integer = 0
Dim cima_baixo As Integer = 0
Dim r As Random
Private Sub cabeça()
comprimento_da_cobra += 1
snake(comprimento_da_cobra) = New PictureBox
With snake(comprimento_da_cobra)
.Height = 10
.Width = 10
.BackColor = Color.Black
.Top = (pb1.Top + pb1.Bottom) / 2
.Left = (pb1.Left + pb1.Right) / 2
End With
Me.Controls.Add(snake(comprimento_da_cobra))
snake(comprimento_da_cobra).BringToFront()
End Sub
Private Sub Form1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
Select Case e.KeyChar
Case "d"
esquerda_direita = 10
cima_baixo = 0
Case "a"
esquerda_direita = -10
cima_baixo = 0
Case "w"
esquerda_direita = 0
cima_baixo = 10
Case "s"
esquerda_direita = 0
cima_baixo = -10
End Select
End Sub
Private Sub tsnake_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
snake(0).Top += cima_baixo
snake(0).Left += esquerda_direita
End Sub
#End Region
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
cabeça()
Timer1.Start()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
End Sub
End Class
这是代码
和grafic代码是
this is the code
and the grafic code is
<global.microsoft.visualbasic.compilerservices.designergenerated()> _
Partial Class Form1
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<system.diagnostics.debuggernonusercode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<system.diagnostics.debuggerstepthrough()> _
Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container()
Me.pb1 = New System.Windows.Forms.PictureBox()
Me.Label1 = New System.Windows.Forms.Label()
Me.Label2 = New System.Windows.Forms.Label()
Me.Label3 = New System.Windows.Forms.Label()
Me.Button1 = New System.Windows.Forms.Button()
Me.Label6 = New System.Windows.Forms.Label()
Me.Label7 = New System.Windows.Forms.Label()
Me.Button2 = New System.Windows.Forms.Button()
Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
CType(Me.pb1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'pb1
'
Me.pb1.BackColor = System.Drawing.Color.White
Me.pb1.Location = New System.Drawing.Point(22, 22)
Me.pb1.Name = "pb1"
Me.pb1.Size = New System.Drawing.Size(750, 358)
Me.pb1.TabIndex = 0
Me.pb1.TabStop = False
Me.pb1.WaitOnLoad = True
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label1.Location = New System.Drawing.Point(612, 423)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(51, 15)
Me.Label1.TabIndex = 1
Me.Label1.Text = "Pontos"
'
'Label2
'
Me.Label2.AutoSize = True
Me.Label2.Font = New System.Drawing.Font("Microsoft Sans Serif", 14.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label2.Location = New System.Drawing.Point(31, 401)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(112, 24)
Me.Label2.TabIndex = 2
Me.Label2.Text = "Instruçoes:"
'
'Label3
'
Me.Label3.AutoSize = True
Me.Label3.Font = New System.Drawing.Font("Microsoft Sans Serif", 72.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label3.Location = New System.Drawing.Point(201, 410)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(375, 108)
Me.Label3.TabIndex = 3
Me.Label3.Text = "SNAKE"
'
'Button1
'
Me.Button1.Font = New System.Drawing.Font("Microsoft Sans Serif", 15.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Button1.Location = New System.Drawing.Point(643, 476)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(129, 48)
Me.Button1.TabIndex = 5
Me.Button1.Text = "Começar"
Me.Button1.UseVisualStyleBackColor = True
'
'Label6
'
Me.Label6.BackColor = System.Drawing.Color.White
Me.Label6.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.Label6.Location = New System.Drawing.Point(687, 410)
Me.Label6.Name = "Label6"
Me.Label6.Size = New System.Drawing.Size(85, 28)
Me.Label6.TabIndex = 7
'
'Label7
'
Me.Label7.BackColor = System.Drawing.Color.Red
Me.Label7.Location = New System.Drawing.Point(32, 425)
Me.Label7.Name = "Label7"
Me.Label7.Size = New System.Drawing.Size(76, 50)
Me.Label7.TabIndex = 8
Me.Label7.Text = "Teclas W,S,A,D para mover"
'
'Button2
'
Me.Button2.Font = New System.Drawing.Font("Microsoft Sans Serif", 15.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Button2.Location = New System.Drawing.Point(14, 478)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(129, 46)
Me.Button2.TabIndex = 9
Me.Button2.Text = "Sair"
Me.Button2.UseVisualStyleBackColor = True
'
'Timer1
'
Me.Timer1.Enabled = True
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.BackColor = System.Drawing.Color.Red
Me.ClientSize = New System.Drawing.Size(789, 546)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Label7)
Me.Controls.Add(Me.Label6)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.pb1)
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.pb1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents pb1 As System.Windows.Forms.PictureBox
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Label6 As System.Windows.Forms.Label
Friend WithEvents Label7 As System.Windows.Forms.Label
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents Timer1 As System.Windows.Forms.Timer
End Class
推荐答案
你是否真的想要创建多达1000个PictureBoxes?不要那样做!!你创建的盒子越多,你的代码就会越来越慢,到你的应用程序将无法使用的时候。
你真的不得不废弃这些代码,改写。我建议在表单上删除一个Panel控件并订阅它的Paint事件。你必须使用由Paint事件args传入的Graphics对象自己为你的蛇绘制框。
你的蛇在没有移动的原因应用程序启动是你的速度变量在应用程序启动时都为0。
将来,将你的代码片段包装在< PRE> ...代码< / PRE>用于保持格式化并保持代码可读的标记。此外,您不必包含所有设计器生成的代码。
Are you seriously trying to create up to 1,000 PictureBoxes?? DO NOT DO THAT!! Your code will get slower and slower with the more boxes you create, to the point where you're app will be unusable.
You REALLY have to scrap this code and rewrite. I suggest dropping a Panel control on the form and subscribing to its Paint event. You have to Draw the boxes for your snake yourself using the Graphics object that is passed in by the Paint event args.
The reason your snake doesn't move when the app starts is your velocity variables are both 0 when the app starts.
In the future, wrap your code snippets in <PRE> ... code </PRE> tags to keep the formatting and keep the code readable. Also, you don't have to include all the designer generated code.
这篇关于我坚持我的代码,蛇的头部移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文