如何将图片框(在我的例子中为项目符号)设置在其自己的轴上,与角色的运动分开?

问题描述 投票:0回答:1

我对编码非常非常陌生,并在 Visual Studio 和 .NET Windows 窗体应用程序中的 Visual Basic 语言中开始了一门课程。所以在这一点上,请放宽我的要求。

我正在尝试制作一个小游戏,你可以移动你的角色并仅向 4 个方向(左、右、上、下)射击一两个怪物。好吧,问题是当我尝试使用 WASD 键移动我的角色时,我的子弹会移动。这很可能是因为我将 WASD 键设置为角色的移动以及子弹 (PictureBox) 射击的方向。我尝试过进行布尔开关,但我对编码不熟悉,似乎在解决这个问题时遇到了一些麻烦。我会放弃目前为止所拥有的:

Public Class Form1
Dim Health As Integer = 7
Dim UUp As Boolean = True
Dim UDown As Boolean = True
Dim ULeft As Boolean = True
Dim URight As Boolean = True
Dim EUp As Boolean = True
Dim EDown As Boolean = True
Dim ELeft As Boolean = True
Dim ERight As Boolean = True
Dim Bullets(-1) As Bullet
Dim intCount As Integer
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
    'movement of character player controls
    If (e.KeyCode = Keys.W And UUp = True) Then
        pbIssac.Top -= 5
    ElseIf (e.KeyCode = Keys.A And ULeft = True) Then
        pbIssac.Left -= 5
    ElseIf (e.KeyCode = Keys.D And URight = True) Then
        pbIssac.Left += 5
    ElseIf (e.KeyCode = Keys.S And UDown = True) Then
        pbIssac.Top += 5
    ElseIf (e.KeyCode = Keys.Escape) Then
        End
    End If
    'creation of bullet? code from internet!
    Select Case e.KeyCode
        Case Keys.Space
            ReDim Preserve Bullets(intCount)
            Dim bullet1 As New Bullets
            Controls.Add(bullet1)
            Bullets(intCount) = bullet1
            intCount += 1
            tmrShoot.Enabled = True
    End Select

    'direction character is facing determines direction of bullet
    If (e.KeyCode = Keys.A) Then
        My.Settings.Keypressed = 1
        My.Settings.Save()
    ElseIf (e.KeyCode = Keys.D) Then
        My.Settings.Keypressed = 2
        My.Settings.Save()
    ElseIf (e.KeyCode = Keys.W) Then
        My.Settings.Keypressed = 3
        My.Settings.Save()
    ElseIf (e.KeyCode = Keys.S) Then
        My.Settings.Keypressed = 4
        My.Settings.Save()
    End If

子弹课堂(来自互联网!)

Public Class Bullet
Inherits PictureBox
Public Sub New()
    With Me
        .Size = New Size(10, 30)
        .Location = Form1.pbIssac.Location
        .BackgroundImageLayout = ImageLayout.Stretch
        .BackgroundImage = My.Resources.green
    End With
End Sub

Public Sub Shoot()
    If (My.Settings.Keypressed = 1) Then
        Me.Left -= 3
        My.Settings.Save()
    ElseIf (My.Settings.Keypressed = 2) Then
        Me.Left += 3
        My.Settings.Save()
    ElseIf (My.Settings.Keypressed = 3) Then
        Me.Top -= 3
        My.Settings.Save()
    ElseIf (My.Settings.Keypressed = 4) Then
        Me.Top += 4
        My.Settings.Save()
    End If
End Sub
End Class
vb.net
1个回答
0
投票

这就是我在那种情况下会做的事情:

Public Class Form1

    Private characterPosition As Point

    'Set a default direction for the character.
    Private characterDirection As Direction = Direction.Up

    Private ReadOnly bullets As New List(Of Bullet)

    Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
        Select Case e.KeyCode
            Case Keys.W
                characterDirection = Direction.Up
                'Move character up.
            Case Keys.S
                characterDirection = Direction.Down
                'Move character down.
            Case Keys.A
                characterDirection = Direction.Left
                'Move character left.
            Case Keys.D
                characterDirection = Direction.Right
                'Move character right.
            Case Keys.Space
                bullets.Add(New Bullet(characterPosition, characterDirection))
        End Select
    End Sub

End Class


Public Enum Direction
    Up
    Down
    Left
    Right
End Enum


Public Class Bullet

    Private direction As Direction

    Public Sub New(location As Point, direction As Direction)
        '...
        Me.direction = direction
    End Sub

    Public Sub Move()
        Select Case direction
            Case Direction.Up
                'Move bullet up.
            Case Direction.Down
                'Move bullet down.
            Case Direction.Left
                'Move bullet left.
            Case Direction.Right
                'Move bullet right.
        End Select
    End Sub

End Class

请注意,每颗子弹都会记住自己的方向,因此您只需告诉它移动,它就会移动。

© www.soinside.com 2019 - 2024. All rights reserved.