Statt Buttons soll sich PictureBox bewegen :o
- VB.NET
Sie verwenden einen veralteten Browser (%browser%) mit Sicherheitsschwachstellen und können nicht alle Funktionen dieser Webseite nutzen.
Hier erfahren Sie, wie einfach Sie Ihren Browser aktualisieren können.
Hier erfahren Sie, wie einfach Sie Ihren Browser aktualisieren können.
Es gibt 24 Antworten in diesem Thema. Der letzte Beitrag () ist von RodFromGermany.
-
-
Zunächst könntest du uns deinen Code zeigen.
-
-
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 -
RushDen schrieb:
spiele macht man nicht mit pictureboxen. -
-
-
-
-
-
-
-
VB.NET-Quellcode
- Public Class Form1
- Dim picrect As New Rectangle(50, 50, 40, 40)
- Dim flag As Boolean = True
- Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
- If flag Then
- e.Graphics.FillRectangle(Brushes.Black, picrect)
- flag = False
- End If
- End Sub
- Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
- Select Case e.KeyCode
- Case Keys.Left
- picrect.X = picrect.X - 5
- Case Keys.Right
- picrect.X = picrect.X + 5
- Case Keys.Up
- picrect.Y = picrect.Y - 5
- Case Keys.Down
- picrect.Y = picrect.Y + 5
- End Select
- flag = True
- Invalidate()
- End Sub
- End Class
Hier mal mit GDI+. Ich denke nicht, dass das allzu schwierig ist.
faxe1008
-
VB.NET-Quellcode
- Option Strict On
- Public Class Form1
- Public Sub New()
- ' Dieser Aufruf ist für den Designer erforderlich.
- InitializeComponent()
- SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.OptimizedDoubleBuffer Or ControlStyles.UserPaint Or ControlStyles.ResizeRedraw, True)
- UpdateStyles()
- Me.Size = New Size(1000, 500)
- newGame = New Game(Me.Size)
- ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.
- End Sub
- Private WithEvents newGame As Game
- Private Sub newGame_StartRender(sender As Object, e As EventArgs) Handles newGame.StartRender
- Me.Invalidate()
- End Sub
- Private Sub Form1_Disposed(sender As Object, e As EventArgs) Handles Me.Disposed
- newGame.Running = False
- End Sub
- Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
- If Not newGame.Pressing.Contains(e.KeyCode) Then newGame.Pressing.Add(e.KeyCode)
- newGame.CheckForCollision(newGame.Pressing(newGame.Pressing.Count - 1))
- End Sub
- Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
- If newGame.Pressing.Contains(e.KeyCode) Then newGame.Pressing.Remove(e.KeyCode)
- End Sub
- Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
- Dim element As GameObject = Nothing
- Dim touched As Boolean = False
- Dim lDistance As New Size
- For i = newGame.GameObjects.Count - 1 To 0 Step -1
- Dim item = newGame.GameObjects(i)
- item.Render(e.Graphics)
- If TypeOf item Is IMovable Then
- lDistance = DirectCast(item, IMovable).Gravity
- For el = newGame.GameObjects.Count - 1 To 0 Step -1
- Dim elem = newGame.GameObjects(el)
- If TypeOf elem Is ICollision AndAlso elem IsNot item Then
- element = item
- If elem.Body.IntersectsWith(item.Offset(New Size(0, lDistance.Height))) Then
- touched = True
- Do Until Not elem.Body.IntersectsWith(item.Offset(New Size(0, lDistance.Height)))
- lDistance.Height -= 1
- Loop
- If TypeOf elem Is Finish AndAlso newGame.NextLevel(newGame.CurrentLevel) Then
- Return
- End If
- End If
- End If
- Next
- For Each asd In newGame.Pressing
- newGame.CheckForCollision(asd)
- Next
- End If
- newGame.CheckWindow(Me)
- Next
- If Not newGame.Pressing.Contains(Keys.Space) Then DirectCast(element, IMovable).Move(New Size(0, lDistance.Height))
- End Sub
- End Class
- 'Spielobjekt
- Public MustInherit Class GameObject
- Public Property Body As Rectangle
- Get
- Return _body
- End Get
- Set(value As Rectangle)
- _body = value
- End Set
- End Property
- Protected _body As Rectangle
- Public Sub Restart(loc As Point)
- _body = New Rectangle(loc, Me.Body.Size)
- End Sub
- Public Sub New(ByVal pBody As Rectangle)
- Me.Body = pBody
- End Sub
- Public Function Offset(size As Size) As Rectangle
- Return New Rectangle(Me.Body.Location + size, Me.Body.Size)
- End Function
- Public MustOverride Sub Render(ByVal e As Graphics)
- Protected MustOverride Sub Initialise()
- End Class
- Public Class Wall
- Inherits GameObject
- Implements ICollision
- Public Sub New(ByVal pBody As Rectangle)
- MyBase.New(pBody)
- End Sub
- Protected Overrides Sub Initialise()
- 'Wand initialisieren
- End Sub
- Public Overrides Sub Render(e As Graphics)
- With e
- .DrawRectangle(Pens.Black, Body)
- End With
- End Sub
- Public Function Collided(pBody As Rectangle) As Boolean Implements ICollision.Collided
- Return Me.Body.IntersectsWith(pBody)
- End Function
- End Class
- 'Bewegbare Objekte
- Public Interface IMovable
- Sub Move(distance As Size)
- Function CheckInput(e As Keys) As Size
- Property MoveAction As Dictionary(Of Keys, Size)
- Property Gravity As Size
- End Interface
- Public Interface ICollision
- Function Collided(pBody As Rectangle) As Boolean
- End Interface
- 'Ball
- Public Class Ball
- Inherits GameObject
- Implements IMovable, ICollision
- Public Sub New(pBody As Rectangle)
- MyBase.New(pBody)
- Initialise()
- End Sub
- Protected Overrides Sub Initialise()
- Gravity = New Size(4, 7)
- MoveAction = New Dictionary(Of Keys, Size)
- MoveAction.Add(Keys.A, New Size(-Gravity.Width, 0))
- MoveAction.Add(Keys.D, New Size(Gravity.Width, 0))
- MoveAction.Add(Keys.Space, New Size(0, -Gravity.Height))
- End Sub
- Public Overrides Sub Render(ByVal e As Graphics)
- With e
- .DrawEllipse(Pens.Black, Me.Body)
- End With
- End Sub
- Public Sub Move(distance As Size) Implements IMovable.Move
- Me._body.Location += distance
- End Sub
- Public Function CheckInput(e As Keys) As Size Implements IMovable.CheckInput
- If MoveAction.ContainsKey(e) Then
- Return MoveAction(e)
- End If
- Return New Size(0, 0)
- End Function
- Public Property MoveAction As Dictionary(Of Keys, Size) Implements IMovable.MoveAction
- Public Function Collided(pBody As Rectangle) As Boolean Implements ICollision.Collided
- Return Me.Body.IntersectsWith(pBody)
- End Function
- Public Property Gravity As Size Implements IMovable.Gravity
- End Class
- Public Class Game
- Public Property GameObjects As New List(Of GameObject)
- Public Property Running As Boolean = True
- Public Property Pressing As New List(Of Keys)
- Public Property AllLevels As New List(Of Action)
- Public Property CurrentLevel As Integer = 0
- Public Sub New(ByVal pBody As Size)
- 'Eventuell ein Algorithmus zur zufälligen Erstellung von Level's implementieren
- InitialiseLevel(pBody)
- NextLevel(CurrentLevel)
- Dim beginLoopAction As Action = AddressOf StartGameLoop
- beginLoopAction.BeginInvoke(AddressOf Game_End, Nothing)
- End Sub
- Private Sub InitialiseLevel(pBody As Size)
- 'Levels intialisieren
- AllLevels.Add(Sub()
- GameObjects.Add(New Ball(New Rectangle(50, 50, 25, 25)))
- GameObjects.Add(New Wall(New Rectangle(0, 0, 10, pBody.Height - 27)))
- GameObjects.Add(New Wall(New Rectangle(0, 0, pBody.Width - 27, 10)))
- GameObjects.Add(New Wall(New Rectangle(pBody.Width - 27, 0, 10, pBody.Height - 27)))
- GameObjects.Add(New Wall(New Rectangle(0, pBody.Height - 50, pBody.Width - 27, 10)))
- GameObjects.Add(New Wall(New Rectangle(350, CInt(pBody.Height / 3) + 40, 500, 40)))
- GameObjects.Add(New Wall(New Rectangle(450, CInt(pBody.Height / 3) + 150, 156, 40)))
- GameObjects.Add(New Wall(New Rectangle(50, 100 + 150, 75, 120)))
- End Sub)
- End Sub
- Public Function NextLevel(ByRef currentLevel As Integer) As Boolean
- NextLevel = Not AllLevels.Count <= currentLevel
- If NextLevel Then
- GameObjects.Clear()
- AllLevels(currentLevel).Invoke()
- currentLevel += 1
- End If
- End Function
- Public Sub StartGameLoop()
- Do
- Render()
- Pause()
- Loop While Running
- End Sub
- Public Sub CheckForCollision(e As Keys)
- Dim touched As Boolean = False
- Dim element As IMovable = Nothing
- Dim lDistance As Size
- For ise = GameObjects.Where(Function(i) TypeOf i Is IMovable).Cast(Of IMovable)().Count - 1 To 0 Step -1
- Dim item = GameObjects.Where(Function(i) TypeOf i Is IMovable).Cast(Of IMovable)()(ise)
- lDistance = item.CheckInput(e)
- For el = GameObjects.Count - 1 To 0 Step -1
- Dim elem = GameObjects(el)
- If TypeOf elem Is ICollision AndAlso elem IsNot item Then
- If Not e = Keys.Space Then
- If CType(elem, ICollision).Collided(CType(item, GameObject).Offset(New Size(lDistance.Width, 0))) Then
- touched = True
- Do Until Not CType(elem, ICollision).Collided(CType(item, GameObject).Offset(New Size(lDistance.Width, 0)))
- If lDistance.Width < 0 Then
- lDistance.Width += 1
- ElseIf lDistance.Width > 0 Then
- lDistance.Width -= 1
- End If
- Loop
- End If
- Else
- If CType(elem, ICollision).Collided(CType(item, GameObject).Offset(New Size(0, lDistance.Height))) Then
- touched = True
- Do Until Not CType(elem, ICollision).Collided(CType(item, GameObject).Offset(New Size(0, lDistance.Height)))
- If lDistance.Height < 0 Then
- lDistance.Height += 1
- End If
- Loop
- End If
- End If
- End If
- element = item
- Next
- Next
- element.Move(lDistance)
- End Sub
- Public Sub CheckWindow(ByVal instance As Form)
- For Each item In GameObjects
- If Not instance.ClientRectangle.IntersectsWith(item.Body) Then
- If TypeOf item Is IMovable Then
- RestartBall(item)
- End If
- End If
- Next
- End Sub
- Protected Sub RestartBall(ByVal item As GameObject)
- item.Restart(New Point(50, CInt(50)))
- Pressing.Clear()
- End Sub
- Protected Sub Render()
- RaiseEvent StartRender(Me, EventArgs.Empty)
- End Sub
- Protected Sub Game_End(iasy As IAsyncResult)
- 'Ressourcen freigeben
- End Sub
- Protected Sub Pause()
- Threading.Thread.Sleep(16)
- End Sub
- Public Event StartRender As EventHandler(Of EventArgs)
- End Class
- Public Class Finish
- Inherits GameObject
- Implements ICollision
- Public Property ObjectImage As Image
- Public Sub New(pBody As Rectangle, ByVal img As Image)
- MyBase.New(pBody)
- Me.ObjectImage = img
- End Sub
- Protected Overrides Sub Initialise()
- 'Ziel initialisieren
- End Sub
- Public Overrides Sub Render(e As Graphics)
- e.DrawImage(Me.ObjectImage, Me.Body)
- End Sub
- Public Function Collided(pBody As Rectangle) As Boolean Implements ICollision.Collided
- Return Me.Body.IntersectsWith(pBody)
- End Function
- 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
Dein Code von Post #5 funktioniert, sofern Du der FormKeyPreview = 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!
-
Tags
-
Ähnliche Themen
-
enIking1998 - - Multimedia- und Spieleprogrammierung
-
Michi M - - Sonstige Problemstellungen
-
Keks3000 - - Multimedia- und Spieleprogrammierung