Hallo Community,
ich bin gerade dabei ein 2D-Game zu programmieren. Ich habe gerade damit angefangen den Spieler zu programmieren, und stoße beim Zeichnen gleich auf ein Problem. Da ich ja nur den Player neu zeichnen möchte, zeichne ich mit Invalidate() nur den Teil neu, wo die Position des Spielers ist. Das Problem ist jetzt, dass der Spieler an die neue Position gezeichnet wird, aber die Positionen, an denen er vorher war, immernoch gezeichnet sind.
Klasse Player.vb:
Spoiler anzeigen
Klasse Form1.vb:
Spoiler anzeigen
Mit Me.Invalidate() funktioniert es, aber ich möchte nicht mehr neu zeichnen als nötig, außerdem flackert es dann. Ich hoffe ihr könnt mir helfen.
TheVBTutorialsVB
ich bin gerade dabei ein 2D-Game zu programmieren. Ich habe gerade damit angefangen den Spieler zu programmieren, und stoße beim Zeichnen gleich auf ein Problem. Da ich ja nur den Player neu zeichnen möchte, zeichne ich mit Invalidate() nur den Teil neu, wo die Position des Spielers ist. Das Problem ist jetzt, dass der Spieler an die neue Position gezeichnet wird, aber die Positionen, an denen er vorher war, immernoch gezeichnet sind.
Klasse Player.vb:
VB.NET-Quellcode
- Option Strict On
- Public Class Player
- Private _point As Point
- Private _size As Size
- Private _brush As Brush
- Public Enum Direction
- Top
- Bottom
- Left
- Right
- End Enum
- Public Sub New(ByVal point As Point, ByVal size As Size, ByVal brush As Brush)
- _point = point
- _size = size
- _brush = brush
- End Sub
- Public Property Location As Point
- Get
- Return _point
- End Get
- Set(value As Point)
- _point = value
- End Set
- End Property
- Public ReadOnly Property Size As Size
- Get
- Return _size
- End Get
- End Property
- Public Property Brush As Brush
- Get
- Return _brush
- End Get
- Set(value As Brush)
- _brush = value
- End Set
- End Property
- Public ReadOnly Property Rectangle As Rectangle
- Get
- Return New Rectangle(_point, _size)
- End Get
- End Property
- Public Sub DrawPlayer(ByVal e As PaintEventArgs)
- With e.Graphics
- .FillRectangle(_brush, New Rectangle(_point, _size))
- End With
- End Sub
- Public Sub DoStep(ByVal stepsize As Integer, ByVal direction As Direction)
- Select Case direction
- Case Player.Direction.Top
- _point = New Point(_point.X, _point.Y - stepsize)
- Case Player.Direction.Bottom
- _point = New Point(_point.X, _point.Y + stepsize)
- Case Player.Direction.Right
- _point = New Point(_point.X + stepsize, _point.Y)
- Case Player.Direction.Left
- _point = New Point(_point.X - stepsize, _point.Y)
- End Select
- End Sub
- End Class
Klasse Form1.vb:
VB.NET-Quellcode
- Option Strict On
- Public Class Form1
- Private p As New Player(New Point(10, 10), New Size(50, 50), Brushes.Red)
- Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
- DoubleBuffered = True
- End Sub
- Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
- Select Case e.KeyCode
- Case Keys.W
- p.DoStep(10, Player.Direction.Top)
- Invalidate(p.Rectangle)
- Case Keys.S
- p.DoStep(10, Player.Direction.Bottom)
- Invalidate(p.Rectangle)
- Case Keys.A
- p.DoStep(10, Player.Direction.Left)
- Invalidate(p.Rectangle)
- Case Keys.D
- p.DoStep(10, Player.Direction.Right)
- Invalidate(p.Rectangle)
- End Select
- End Sub
- Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
- p.DrawPlayer(e)
- End Sub
- End Class
Mit Me.Invalidate() funktioniert es, aber ich möchte nicht mehr neu zeichnen als nötig, außerdem flackert es dann. Ich hoffe ihr könnt mir helfen.
TheVBTutorialsVB