Statt Buttons soll sich PictureBox bewegen :o

  • VB.NET

Es gibt 24 Antworten in diesem Thema. Der letzte Beitrag () ist von RodFromGermany.

    Statt Buttons soll sich PictureBox bewegen :o

    Hei Leute!

    Ich habe eine Frage an euch.
    Gerade habe ich es geschafft, dass sich meine Picturebox mithilfe von Pfeiltasten bewegt. Danach habe ich Buttons hinzugefügt, jetzt werden die statt der Picturebox ausgewählt.

    was kann ich tun? :o

    Mfg SveFriRo
    @SveFriRo Willkommen im Forum. :thumbup:
    So was:

    VB.NET-Quellcode

    1. PictureBox1.Location = New Point(PictureBox1.Location.X, PictureBox1.Location.Y + 10)

    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!
    müssen für die schule n spiel machen und als figur halt jene picturebox, die sich bewegen soll. hat auch funktioniert, bis die buttons kamen..


    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    If e.KeyCode = Keys.Left Then
    Dim new_location As Point = New Point(PictureBox30.Location.X - 2, PictureBox30.Location.Y)
    PictureBox30.Location = new_location
    End If
    If e.KeyCode = Keys.Right Then
    Dim new_location As Point = New Point(PictureBox30.Location.X + 2, PictureBox30.Location.Y)
    PictureBox30.Location = new_location
    End If
    If e.KeyCode = Keys.Up Then
    Dim new_location As Point = New Point(PictureBox30.Location.X, PictureBox30.Location.Y - 2)
    PictureBox30.Location = new_location
    End If

    If e.KeyCode = Keys.Down Then
    Dim new_location As Point = New Point(PictureBox30.Location.X, PictureBox30.Location.Y + 2)
    PictureBox30.Location = new_location
    End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_close.Click
    Me.Close()
    End Sub
    ich will das aber durchs ganze bild führen.
    ich würde das auch gerne so lassen wie es ist, es hat ja wunderbar funktioniert, bis die buttons kamen.
    meine frage ist bloß, wie ich die pfeiltasten jetzt wieder auf die picturebox lenke, anstatt dass die die buttons auswählen.
    wisst ihr das?

    VB.NET-Quellcode

    1. Public Class Form1
    2. Dim picrect As New Rectangle(50, 50, 40, 40)
    3. Dim flag As Boolean = True
    4. Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
    5. If flag Then
    6. e.Graphics.FillRectangle(Brushes.Black, picrect)
    7. flag = False
    8. End If
    9. End Sub
    10. Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
    11. Select Case e.KeyCode
    12. Case Keys.Left
    13. picrect.X = picrect.X - 5
    14. Case Keys.Right
    15. picrect.X = picrect.X + 5
    16. Case Keys.Up
    17. picrect.Y = picrect.Y - 5
    18. Case Keys.Down
    19. picrect.Y = picrect.Y + 5
    20. End Select
    21. flag = True
    22. Invalidate()
    23. End Sub
    24. End Class


    Hier mal mit GDI+. Ich denke nicht, dass das allzu schwierig ist.

    8-) faxe1008 8-)

    VB.NET-Quellcode

    1. Option Strict On
    2. Public Class Form1
    3. Public Sub New()
    4. ' Dieser Aufruf ist für den Designer erforderlich.
    5. InitializeComponent()
    6. SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.OptimizedDoubleBuffer Or ControlStyles.UserPaint Or ControlStyles.ResizeRedraw, True)
    7. UpdateStyles()
    8. Me.Size = New Size(1000, 500)
    9. newGame = New Game(Me.Size)
    10. ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.
    11. End Sub
    12. Private WithEvents newGame As Game
    13. Private Sub newGame_StartRender(sender As Object, e As EventArgs) Handles newGame.StartRender
    14. Me.Invalidate()
    15. End Sub
    16. Private Sub Form1_Disposed(sender As Object, e As EventArgs) Handles Me.Disposed
    17. newGame.Running = False
    18. End Sub
    19. Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
    20. If Not newGame.Pressing.Contains(e.KeyCode) Then newGame.Pressing.Add(e.KeyCode)
    21. newGame.CheckForCollision(newGame.Pressing(newGame.Pressing.Count - 1))
    22. End Sub
    23. Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
    24. If newGame.Pressing.Contains(e.KeyCode) Then newGame.Pressing.Remove(e.KeyCode)
    25. End Sub
    26. Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
    27. Dim element As GameObject = Nothing
    28. Dim touched As Boolean = False
    29. Dim lDistance As New Size
    30. For i = newGame.GameObjects.Count - 1 To 0 Step -1
    31. Dim item = newGame.GameObjects(i)
    32. item.Render(e.Graphics)
    33. If TypeOf item Is IMovable Then
    34. lDistance = DirectCast(item, IMovable).Gravity
    35. For el = newGame.GameObjects.Count - 1 To 0 Step -1
    36. Dim elem = newGame.GameObjects(el)
    37. If TypeOf elem Is ICollision AndAlso elem IsNot item Then
    38. element = item
    39. If elem.Body.IntersectsWith(item.Offset(New Size(0, lDistance.Height))) Then
    40. touched = True
    41. Do Until Not elem.Body.IntersectsWith(item.Offset(New Size(0, lDistance.Height)))
    42. lDistance.Height -= 1
    43. Loop
    44. If TypeOf elem Is Finish AndAlso newGame.NextLevel(newGame.CurrentLevel) Then
    45. Return
    46. End If
    47. End If
    48. End If
    49. Next
    50. For Each asd In newGame.Pressing
    51. newGame.CheckForCollision(asd)
    52. Next
    53. End If
    54. newGame.CheckWindow(Me)
    55. Next
    56. If Not newGame.Pressing.Contains(Keys.Space) Then DirectCast(element, IMovable).Move(New Size(0, lDistance.Height))
    57. End Sub
    58. End Class
    59. 'Spielobjekt
    60. Public MustInherit Class GameObject
    61. Public Property Body As Rectangle
    62. Get
    63. Return _body
    64. End Get
    65. Set(value As Rectangle)
    66. _body = value
    67. End Set
    68. End Property
    69. Protected _body As Rectangle
    70. Public Sub Restart(loc As Point)
    71. _body = New Rectangle(loc, Me.Body.Size)
    72. End Sub
    73. Public Sub New(ByVal pBody As Rectangle)
    74. Me.Body = pBody
    75. End Sub
    76. Public Function Offset(size As Size) As Rectangle
    77. Return New Rectangle(Me.Body.Location + size, Me.Body.Size)
    78. End Function
    79. Public MustOverride Sub Render(ByVal e As Graphics)
    80. Protected MustOverride Sub Initialise()
    81. End Class
    82. Public Class Wall
    83. Inherits GameObject
    84. Implements ICollision
    85. Public Sub New(ByVal pBody As Rectangle)
    86. MyBase.New(pBody)
    87. End Sub
    88. Protected Overrides Sub Initialise()
    89. 'Wand initialisieren
    90. End Sub
    91. Public Overrides Sub Render(e As Graphics)
    92. With e
    93. .DrawRectangle(Pens.Black, Body)
    94. End With
    95. End Sub
    96. Public Function Collided(pBody As Rectangle) As Boolean Implements ICollision.Collided
    97. Return Me.Body.IntersectsWith(pBody)
    98. End Function
    99. End Class
    100. 'Bewegbare Objekte
    101. Public Interface IMovable
    102. Sub Move(distance As Size)
    103. Function CheckInput(e As Keys) As Size
    104. Property MoveAction As Dictionary(Of Keys, Size)
    105. Property Gravity As Size
    106. End Interface
    107. Public Interface ICollision
    108. Function Collided(pBody As Rectangle) As Boolean
    109. End Interface
    110. 'Ball
    111. Public Class Ball
    112. Inherits GameObject
    113. Implements IMovable, ICollision
    114. Public Sub New(pBody As Rectangle)
    115. MyBase.New(pBody)
    116. Initialise()
    117. End Sub
    118. Protected Overrides Sub Initialise()
    119. Gravity = New Size(4, 7)
    120. MoveAction = New Dictionary(Of Keys, Size)
    121. MoveAction.Add(Keys.A, New Size(-Gravity.Width, 0))
    122. MoveAction.Add(Keys.D, New Size(Gravity.Width, 0))
    123. MoveAction.Add(Keys.Space, New Size(0, -Gravity.Height))
    124. End Sub
    125. Public Overrides Sub Render(ByVal e As Graphics)
    126. With e
    127. .DrawEllipse(Pens.Black, Me.Body)
    128. End With
    129. End Sub
    130. Public Sub Move(distance As Size) Implements IMovable.Move
    131. Me._body.Location += distance
    132. End Sub
    133. Public Function CheckInput(e As Keys) As Size Implements IMovable.CheckInput
    134. If MoveAction.ContainsKey(e) Then
    135. Return MoveAction(e)
    136. End If
    137. Return New Size(0, 0)
    138. End Function
    139. Public Property MoveAction As Dictionary(Of Keys, Size) Implements IMovable.MoveAction
    140. Public Function Collided(pBody As Rectangle) As Boolean Implements ICollision.Collided
    141. Return Me.Body.IntersectsWith(pBody)
    142. End Function
    143. Public Property Gravity As Size Implements IMovable.Gravity
    144. End Class
    145. Public Class Game
    146. Public Property GameObjects As New List(Of GameObject)
    147. Public Property Running As Boolean = True
    148. Public Property Pressing As New List(Of Keys)
    149. Public Property AllLevels As New List(Of Action)
    150. Public Property CurrentLevel As Integer = 0
    151. Public Sub New(ByVal pBody As Size)
    152. 'Eventuell ein Algorithmus zur zufälligen Erstellung von Level's implementieren
    153. InitialiseLevel(pBody)
    154. NextLevel(CurrentLevel)
    155. Dim beginLoopAction As Action = AddressOf StartGameLoop
    156. beginLoopAction.BeginInvoke(AddressOf Game_End, Nothing)
    157. End Sub
    158. Private Sub InitialiseLevel(pBody As Size)
    159. 'Levels intialisieren
    160. AllLevels.Add(Sub()
    161. GameObjects.Add(New Ball(New Rectangle(50, 50, 25, 25)))
    162. GameObjects.Add(New Wall(New Rectangle(0, 0, 10, pBody.Height - 27)))
    163. GameObjects.Add(New Wall(New Rectangle(0, 0, pBody.Width - 27, 10)))
    164. GameObjects.Add(New Wall(New Rectangle(pBody.Width - 27, 0, 10, pBody.Height - 27)))
    165. GameObjects.Add(New Wall(New Rectangle(0, pBody.Height - 50, pBody.Width - 27, 10)))
    166. GameObjects.Add(New Wall(New Rectangle(350, CInt(pBody.Height / 3) + 40, 500, 40)))
    167. GameObjects.Add(New Wall(New Rectangle(450, CInt(pBody.Height / 3) + 150, 156, 40)))
    168. GameObjects.Add(New Wall(New Rectangle(50, 100 + 150, 75, 120)))
    169. End Sub)
    170. End Sub
    171. Public Function NextLevel(ByRef currentLevel As Integer) As Boolean
    172. NextLevel = Not AllLevels.Count <= currentLevel
    173. If NextLevel Then
    174. GameObjects.Clear()
    175. AllLevels(currentLevel).Invoke()
    176. currentLevel += 1
    177. End If
    178. End Function
    179. Public Sub StartGameLoop()
    180. Do
    181. Render()
    182. Pause()
    183. Loop While Running
    184. End Sub
    185. Public Sub CheckForCollision(e As Keys)
    186. Dim touched As Boolean = False
    187. Dim element As IMovable = Nothing
    188. Dim lDistance As Size
    189. For ise = GameObjects.Where(Function(i) TypeOf i Is IMovable).Cast(Of IMovable)().Count - 1 To 0 Step -1
    190. Dim item = GameObjects.Where(Function(i) TypeOf i Is IMovable).Cast(Of IMovable)()(ise)
    191. lDistance = item.CheckInput(e)
    192. For el = GameObjects.Count - 1 To 0 Step -1
    193. Dim elem = GameObjects(el)
    194. If TypeOf elem Is ICollision AndAlso elem IsNot item Then
    195. If Not e = Keys.Space Then
    196. If CType(elem, ICollision).Collided(CType(item, GameObject).Offset(New Size(lDistance.Width, 0))) Then
    197. touched = True
    198. Do Until Not CType(elem, ICollision).Collided(CType(item, GameObject).Offset(New Size(lDistance.Width, 0)))
    199. If lDistance.Width < 0 Then
    200. lDistance.Width += 1
    201. ElseIf lDistance.Width > 0 Then
    202. lDistance.Width -= 1
    203. End If
    204. Loop
    205. End If
    206. Else
    207. If CType(elem, ICollision).Collided(CType(item, GameObject).Offset(New Size(0, lDistance.Height))) Then
    208. touched = True
    209. Do Until Not CType(elem, ICollision).Collided(CType(item, GameObject).Offset(New Size(0, lDistance.Height)))
    210. If lDistance.Height < 0 Then
    211. lDistance.Height += 1
    212. End If
    213. Loop
    214. End If
    215. End If
    216. End If
    217. element = item
    218. Next
    219. Next
    220. element.Move(lDistance)
    221. End Sub
    222. Public Sub CheckWindow(ByVal instance As Form)
    223. For Each item In GameObjects
    224. If Not instance.ClientRectangle.IntersectsWith(item.Body) Then
    225. If TypeOf item Is IMovable Then
    226. RestartBall(item)
    227. End If
    228. End If
    229. Next
    230. End Sub
    231. Protected Sub RestartBall(ByVal item As GameObject)
    232. item.Restart(New Point(50, CInt(50)))
    233. Pressing.Clear()
    234. End Sub
    235. Protected Sub Render()
    236. RaiseEvent StartRender(Me, EventArgs.Empty)
    237. End Sub
    238. Protected Sub Game_End(iasy As IAsyncResult)
    239. 'Ressourcen freigeben
    240. End Sub
    241. Protected Sub Pause()
    242. Threading.Thread.Sleep(16)
    243. End Sub
    244. Public Event StartRender As EventHandler(Of EventArgs)
    245. End Class
    246. Public Class Finish
    247. Inherits GameObject
    248. Implements ICollision
    249. Public Property ObjectImage As Image
    250. Public Sub New(pBody As Rectangle, ByVal img As Image)
    251. MyBase.New(pBody)
    252. Me.ObjectImage = img
    253. End Sub
    254. Protected Overrides Sub Initialise()
    255. 'Ziel initialisieren
    256. End Sub
    257. Public Overrides Sub Render(e As Graphics)
    258. e.DrawImage(Me.ObjectImage, Me.Body)
    259. End Sub
    260. Public Function Collided(pBody As Rectangle) As Boolean Implements ICollision.Collided
    261. Return Me.Body.IntersectsWith(pBody)
    262. End Function
    263. End Class


    Kannst du mal ausprobieren. (Geht direkt Copy & Paste)
    Halt als Beispiel wies mit GDI aussieht.
    Steuerung:

    Space - Fliegen
    A - Links
    D - Rechts

    SveFriRo schrieb:

    grad am verzweifeln
    Was genau geht nicht?
    Dein Code von Post #5 funktioniert, sofern Du der Form KeyPreview = True gibst.
    Hast Du tatsächlich 30 PictureBoxen auf der Form?
    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!