Nechci Vás zklamat, ale nefunguje to. Formulář se mi sice vykreslí, ale objekt nejde dát do pohybu, když mačkám šipky. Programuju ve VB.net 2010, jestli to pomůže. Zde je upravený kód :
Public Class Form1
Dim b As Bitmap
Dim g As Graphics
'směry pohybu stíhačky
Enum SMERY
Nahoru = 1
Dolu = 2
Doleva = 3
Doprava = 4
Zadne = 5
End Enum
'souřadnice
Structure XY
Dim x As Integer
Dim y As Integer
End Structure
'směry stíhačky
Dim Smer As SMERY
'součastná pozice stíhačky
Dim Space As XY
Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyCode
Case Keys.Up
Smer = SMERY.Nahoru
Case Keys.Down
Smer = SMERY.Dolu
Case Keys.Right
Smer = SMERY.Doprava
Case Keys.Left
Smer = SMERY.Doleva
End Select
End Sub
Private Sub Form1_KeyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
Select Case e.KeyCode
Case Keys.Up
Smer = SMERY.Zadne
Case Keys.Down
Smer = SMERY.Zadne
Case Keys.Right
Smer = SMERY.Zadne
Case Keys.Left
Smer = SMERY.Zadne
End Select
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Timer1.Enabled = True
b = New Bitmap(750, 750)
g = Graphics.FromImage(b)
PictureBox1.Image = b
'nastavení pozice stíhačky
Space.x = 12 * 30
Space.y = 20 * 30
End Sub
Private Sub PictureBox1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
'pozadí vybarvit na bílou barvu
e.Graphics.Clear(Color.White)
'vykreslit stíhačku
e.Graphics.DrawImage(ImageList1.Images(0), 12 * 30, 20 * 30)
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
PictureBox1.Invalidate() 'obnovit vykresleni obrazku v pictureboxu
g.FillRectangle(Brushes.White, Space.x, Space.y, 30, 30)
Select Case Smer
Case SMERY.Nahoru
Space.y = Space.y - 1
Case SMERY.Dolu
Space.y = Space.y + 1
Case SMERY.Doprava
Space.x = Space.x + 1
Case SMERY.Doleva
Space.x = Space.x - 1
Case SMERY.Zadne
Space.x = Space.x
Space.y = Space.y
End Select
g.DrawImage(ImageList1.Images(0), Space.x, Space.y)
End Sub
End Class
|