图片框只是淡出而不是移动。 (破碎的运动系统)

时间:2018-10-03 11:06:06

标签: vb.net winforms

我正在尝试使用VB从头开始制作Win形式的游戏,我知道这是个坏主意,但我喜欢挑战。因此,我一直在测试移动系统,以便可以选择自己喜欢的系统,但是在尝试移动图片框时遇到了一个问题,因为当我按下移动键时,图片框开始按照我想要的方向擦除图像移动直到消失。

我正在使用

Public Class Form1
Dim RightM As Boolean
Dim LeftM As Boolean
Dim UpM As Boolean
Dim DownM As Boolean

Sub Movement()
    Do While UpM = True
        PictureBox1.Top += -5
        Threading.Thread.Sleep(20)
    Loop
    Do While LeftM = True
        PictureBox1.Left += -5
        Threading.Thread.Sleep(20)
    Loop
    Do While DownM = True
        PictureBox1.Top += 5
        Threading.Thread.Sleep(20)
    Loop
    Do While RightM = True
        PictureBox1.Left += 5
        Threading.Thread.Sleep(20)
    Loop
    Do While (UpM = True) And (RightM = True)
        PictureBox1.Top += -5
        PictureBox1.Left += 5
        Threading.Thread.Sleep(20)
    Loop
    Do While (UpM = True) And (LeftM = True)
        PictureBox1.Top += -5
        PictureBox1.Left += -5
        Threading.Thread.Sleep(20)
    Loop
    Do While (DownM = True) And (RightM = True)
        PictureBox1.Top += 5
        PictureBox1.Left += 5
        Threading.Thread.Sleep(20)
    Loop
    Do While (DownM = True) And (LeftM = True)
        PictureBox1.Top += 5
        PictureBox1.Left += -5
    Loop
End Sub

Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
    If e.KeyCode = Keys.A Then
        LeftM = True
        Movement()
    ElseIf e.KeyCode = Keys.D Then
        RightM = True
        Movement()
    ElseIf e.KeyCode = Keys.W Then
        UpM = True
        Movement()
    ElseIf e.KeyCode = Keys.S Then
        DownM = True
        Movement()
    End If
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub

Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
    If e.KeyCode = Keys.A Then
        LeftM = False
        Movement()
    ElseIf e.KeyCode = Keys.D Then
        RightM = False
        Movement()
    ElseIf e.KeyCode = Keys.W Then
        UpM = False
        Movement()
    ElseIf e.KeyCode = Keys.S Then
        DownM = False
        Movement()
    End If
End Sub

结束班级

1 个答案:

答案 0 :(得分:0)

这个问题使我回到Z80年代,我有点怀旧,所以决定发布一个简化的答案。

'Position of PictureBox
Private Plocation As Point

'PictureBox Movement Boundries
Private XL As Integer 'X - Left
Private XR As Integer 'X - Right
Private YT As Integer 'Y - Top
Private YB As Integer 'Y - Bottom

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    'Set Boundries
    XL = 0
    XR = Me.ClientSize.Width - PictureBox1.Width
    YT = 0
    YB = Me.ClientSize.Height - PictureBox1.Height

    'Position PictureBox roughly in the centre of the Form
    Plocation = New Point((Me.ClientSize.Width / 2) - (PictureBox1.Width / 2), (Me.ClientSize.Height / 2) - (PictureBox1.Height / 2))
    PictureBox1.Location = Plocation
End Sub

Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
    'Change X,Y depending on KeyPress
    If e.KeyCode = Keys.A Then Plocation.X -= 5
    If e.KeyCode = Keys.D Then Plocation.X += 5
    If e.KeyCode = Keys.W Then Plocation.Y -= 5
    If e.KeyCode = Keys.S Then Plocation.Y += 5

    'Check for X,Y boundries
    If Plocation.X < XL Then Plocation.X = XL
    If Plocation.X > XR Then Plocation.X = XR
    If Plocation.Y < YT Then Plocation.Y = YT
    If Plocation.Y > YB Then Plocation.Y = YB

    'Update Position
    PictureBox1.Location = Plocation
End Sub

我特意逐个检查所有按键,以帮助进行对角线移动。这有点断断续续,但可能会给您一些想法。编程愉快...