Pictureboxen Koordinaten abfragen?

  • VB.NET

Es gibt 4 Antworten in diesem Thema. Der letzte Beitrag () ist von Niko Ortner.

    Pictureboxen Koordinaten abfragen?

    Hallo zusammen,

    als Grundlage für ein Spiel wie Snake suche ich eine Möglichkeit, die Koordinaten des Standpunktes einer Picturebox abzufragen.

    Wie kann ich die Koordinaten dieser Picturebox abfragen?

    Mein jetziger Code:

    VB.NET-Quellcode

    1. Public Class frmPicboxen
    2. Private Sub frmPicboxen_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    3. If e.KeyCode = Keys.Up Then
    4. picBox.Top -= 50
    5. End If
    6. If e.KeyCode = Keys.Down Then
    7. picBox.Top += 50
    8. End If
    9. If e.KeyCode = Keys.Left Then
    10. picBox.Left -= 50
    11. End If
    12. If e.KeyCode = Keys.Right Then
    13. picBox.Left += 50
    14. End If
    15. End Sub
    16. End Class


    Kann ich vielleicht zwei Variablen deklarieren (As Integer) und dann jedes Mal wenn die Picbox bewegt werden soll, wird die neue Position in diesen Variablen gespeichert? Ist das zu umständlich? Man könnte ja auch versuchen, die Koordinaten vor den If-Abfragen, ob die Koordinaten größer/kleiner als x sind, in den Variablen abzuspeichern und diese dann abzufragen, oder?
    Oder es gibt eine variablenunabhängige Lösung?

    Sorry für die vlt. blöden Fragen, ich kenn mich nicht so gut aus mit VB.
    Hö und wenn ich da das unter Declaration kopiere dann ist irgendwas falsch...

    EDIT:
    Es klappt jetzt. Hab ich mal selbst ausprobiert:

    VB.NET-Quellcode

    1. Public Class frmPicboxen
    2. Dim Loc As Point
    3. Private Sub frmPicboxen_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    4. If e.KeyCode = Keys.Up Then
    5. Loc = picBox.Location
    6. If Loc.Y <= 0 Then MsgBox("Nicht möglich") Else picBox.Top -= 50
    7. End If
    8. If e.KeyCode = Keys.Down Then
    9. Loc = picBox.Location
    10. If Loc.Y >= 450 Then MsgBox("Nicht möglich") Else picBox.Top += 50
    11. End If
    12. If e.KeyCode = Keys.Left Then
    13. Loc = picBox.Location
    14. If Loc.X <= 0 Then MsgBox("Nicht möglich") Else picBox.Left -= 50
    15. End If
    16. If e.KeyCode = Keys.Right Then
    17. Loc = picBox.Location
    18. If Loc.X >= 450 Then MsgBox("Nicht möglich") Else picBox.Left += 50
    19. End If
    20. End Sub
    21. End Class

    Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von „ct5010“ ()

    Mach es wesentlich kürzer:

    VB.NET-Quellcode

    1. Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    2. Dim top As Integer = 0
    3. Dim left As Integer = 0
    4. Select Case e.KeyCode
    5. Case Keys.Up
    6. top = -50
    7. Case Keys.Down
    8. top = 50
    9. Case Keys.Left
    10. left = -50
    11. Case Keys.Right
    12. left = 50
    13. End Select
    14. Dim pt = picBox.Location
    15. picBox.Location = New Point(pt.X + left, pt.Y + top)
    16. End Sub
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Oder noch kürzer:

    VB.NET-Quellcode

    1. Private Sub KeyDownDingens(ByVal sender As Object, ByVal e As KeyEventArgs) Handles MyBase.KeyDown
    2. Select Case e.KeyCode
    3. Case Keys.Up
    4. PicBox.Top -= 50
    5. Case Keys.Down
    6. PicBox.Top += 50
    7. Case Keys.Left
    8. PicBox.Left -= 50
    9. Case Keys.Right
    10. PicBox.Left += 50
    11. End Select
    12. End Sub



    Oder anständig:

    VB.NET-Quellcode

    1. Dim WithEvents Timerbla As New Timer With {.Interval = 30, .Enabled = True}
    2. Dim Player As New PlayerClass
    3. Private Sub SetKeys(ByVal sender As Object, ByVal e As KeyEventArgs) Handles MyBase.KeyDown
    4. If e.KeyCode = Keys.Up AndAlso Not Player.MoveDown Then
    5. Player.MoveUp = True
    6. End If
    7. If e.KeyCode = Keys.Down AndAlso Not Player.MoveUp Then
    8. Player.MoveDown = True
    9. End If
    10. If e.KeyCode = Keys.Left AndAlso Not Player.MoveRight Then
    11. Player.MoveLeft = True
    12. End If
    13. If e.KeyCode = Keys.Right AndAlso Not Player.MoveLeft Then
    14. Player.MoveRight = True
    15. End If
    16. End Sub
    17. Private Sub RemoveKeys(ByVal sender As Object, ByVal e As KeyEventArgs) Handles MyBase.KeyUp
    18. If e.KeyCode = Keys.Up Then
    19. Player.MoveUp = False
    20. End If
    21. If e.KeyCode = Keys.Down Then
    22. Player.MoveDown = False
    23. End If
    24. If e.KeyCode = Keys.Left Then
    25. Player.MoveLeft = False
    26. End If
    27. If e.KeyCode = Keys.Right Then
    28. Player.MoveRight = False
    29. End If
    30. End Sub
    31. Private Sub GameTick() Handles Timerbla.Tick
    32. Player.Tick()
    33. Me.Invalidate()
    34. End Sub
    35. Private Sub PaintMahStuff(ByVal sender As Object, ByVal e As PaintEventArgs) Handles Me.Paint
    36. Player.DrawToGraphics(e.Graphics)
    37. End Sub
    38. Class PlayerClass
    39. Public Property MoveUp As Boolean = False
    40. Public Property MoveDown As Boolean = False
    41. Public Property MoveLeft As Boolean = False
    42. Public Property MoveRight As Boolean = False
    43. Public Property PositionX As Double = 0
    44. Public Property PositionY As Double = 0
    45. Public Property Speed As Double = 5
    46. Public Sub Tick()
    47. If MoveUp Then
    48. PositionY -= Speed
    49. End If
    50. If MoveDown Then
    51. PositionY += Speed
    52. End If
    53. If MoveLeft Then
    54. PositionX -= Speed
    55. End If
    56. If MoveRight Then
    57. PositionX += Speed
    58. End If
    59. End Sub
    60. Public Sub DrawToGraphics(ByVal g As Graphics)
    61. 'Hier der Code zum Zeichnen der Grafiken
    62. 'Ein Beispiel:
    63. g.DrawRectangle(Pens.Red, CSng(PositionX), CSng(PositionY), 50, 50)
    64. End Sub
    65. End Class
    "Luckily luh... luckily it wasn't poi-"
    -- Brady in Wonderland, 23. Februar 2015, 1:56
    Desktop Pinner | ApplicationSettings | OnUtils