Visual Basic 2010 Fussballspiel programmieren SelMcKenzie Selzer-McKenzie
Author D.Selzer-McKenzie
Heute zeige ich Ihnen, wie man ein bewegliches Fussballspiel programmieren kann. Sie kennen das ja von den sogenannten Ping Pong Spielen, wo am Seitenrand ein Spieler läuft und den Ball antippt und schon bleibt der Ball im Spielfeld.
Hier habe ich das auch so gemacht mit lediglich 2 Spielern, aber das können Sie ja entsprechend erweitern.
Los geht’s mit Taste Leertaste, dann mit der A oder B Taste hoch runter für Spieler 1 und der Pfeiltaste für hoch runter Spieler 2.
Den Code habe ich bereirs geschrieben, aber so gross, dass der auf dem Video lesbar ist. Sie müssten notfalls das Video anhalten um den Code in Ruhe abschreiben zu können.
Das Spiel funktioniert bestens, ich habe auch drei Geschwindigkeiten einprogrammiert sowie Musik.
Good Luck.
Selzer-McKenzie
Der Code lautet:
Public Class Form1
Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Integer) As Short
Const VK_A = &H41
Const VK_Z = &H5A
Const VK_LSHIFT = &HA0
Const VK_RSHIFT = &HA1
Const VK_LCONTROL = &HA2
Const VK_RCONTROL = &HA3
Const VK_LMENU = &HA4
Const VK_RMENU = &HA5
Const VK_DOWN = &H28
Const VK_UP = &H26
Const VK_LEFT = &H25
Const VK_RIGHT = &H27
Const VK_SPACE = &H20
Dim xChange, yChange As Integer
Dim keypressed As String
Dim x, y, direction, ballSpeed As Double
Dim keyesc, keyA, KeyZ As Boolean
Dim isLoaded, isPaused As Boolean
Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
keypressed = e.KeyChar
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer2.Enabled = False
NewDirection()
ballSpeed = 0.05
End Sub
Sub NewGame()
boing()
PictureBox3.Left = 230
PictureBox3.Top = 150
NewDirection()
Timer1.Enabled = True
Timer2.Enabled = True
End Sub
Sub NewDirection()
Dim r As Integer
x = pictureBox3.Left
y = pictureBox3.Top
Randomize()
r = Int(Rnd() * 14) + 1
If r = 1 Then direction = 0
If r = 2 Then direction = 23
If r = 3 Then direction = 45
If r = 4 Then direction = 68
If r = 5 Then direction = 113
If r = 6 Then direction = 135
If r = 7 Then direction = 158
If r = 8 Then direction = 180
If r = 9 Then direction = 203
If r = 10 Then direction = 225
If r = 11 Then direction = 248
If r = 12 Then direction = 293
If r = 13 Then direction = 315
If r = 14 Then direction = 338
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If GetAsyncKeyState(VK_SPACE) Then
NewGame()
End If
If GetAsyncKeyState(VK_A) Then
If pictureBox1.Top <= 25 Then
pictureBox1.Top = 25
Exit Sub
End If
pictureBox1.Top = pictureBox1.Top - 10
End If
If GetAsyncKeyState(VK_Z) Then
If pictureBox1.Top >= 273 Then
pictureBox1.Top = 273
Exit Sub
End If
pictureBox1.Top = pictureBox1.Top + 10
End If
If GetAsyncKeyState(VK_UP) Then
If pictureBox2.Top <= 25 Then
pictureBox2.Top = 25
Exit Sub
End If
pictureBox2.Top = pictureBox2.Top - 10
End If
If GetAsyncKeyState(VK_DOWN) Then
If pictureBox2.Top >= 273 Then
pictureBox2.Top = 273
Exit Sub
End If
pictureBox2.Top = pictureBox2.Top + 10
End If
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
If direction = 338 Then
xChange = 92
yChange = 38
End If
If direction = 315 Then
xChange = 71
yChange = 71
End If
If direction = 293 Then
xChange = 38
yChange = 92
End If
If direction = 270 Then
xChange = 0
yChange = 100
End If
If direction = 248 Then
xChange = -38
yChange = 92
End If
If direction = 225 Then
xChange = -71
yChange = 71
End If
If direction = 203 Then
xChange = -92
yChange = 38
End If
If direction = 180 Then
xChange = -100
yChange = 0
End If
If direction = 158 Then
xChange = -92
yChange = -38
End If
If direction = 135 Then
xChange = -71
yChange = -71
End If
If direction = 113 Then
xChange = -38
yChange = -92
End If
If direction = 90 Then
xChange = 0
yChange = -100
End If
If direction = 68 Then
xChange = 38
yChange = -92
End If
If direction = 45 Then
xChange = 71
yChange = -71
End If
If direction = 23 Then
xChange = 92
yChange = -38
End If
If direction = 0 Then
xChange = 100
yChange = 0
End If
If y < 26 Then
boing()
If direction = 158 Then direction = 203
If direction = 135 Then direction = 225
If direction = 113 Then direction = 248
If direction = 68 Then direction = 293
If direction = 45 Then direction = 315
If direction = 23 Then direction = 338
y = 26
End If
If y > 300 Then
boing()
If direction = 203 Then direction = 158
If direction = 225 Then direction = 135
If direction = 248 Then direction = 113
If direction = 293 Then direction = 68
If direction = 315 Then direction = 45
If direction = 338 Then direction = 23
y = 300
End If
x = x + ballSpeed * xChange
y = y + ballSpeed * yChange
pictureBox3.Left = x
pictureBox3.Top = y
If pictureBox3.Left > pictureBox2.Left - 14 _
And pictureBox3.Left < pictureBox2.Left + 10 Then
If pictureBox3.Top >= pictureBox2.Top - 10 _
And pictureBox3.Top <= pictureBox2.Top + 2 Then
direction = 113
End If
If pictureBox3.Top >= pictureBox2.Top + 1 _
And pictureBox3.Top <= pictureBox2.Top + 8 Then
direction = 135
End If
If pictureBox3.Top >= pictureBox2.Top + 9 _
And pictureBox3.Top <= pictureBox2.Top + 15 Then
direction = 158
End If
If pictureBox3.Top >= pictureBox2.Top + 16 _
And pictureBox3.Top <= pictureBox2.Top + 22 Then
direction = 180
End If
If pictureBox3.Top >= pictureBox2.Top + 23 _
And pictureBox3.Top <= pictureBox2.Top + 29 Then
direction = 203
End If
If pictureBox3.Top >= pictureBox2.Top + 30 _
And pictureBox3.Top <= pictureBox2.Top + 36 Then
direction = 225
End If
If pictureBox3.Top >= pictureBox2.Top + 37 _
And pictureBox3.Top <= pictureBox2.Top + 42 Then
direction = 248
End If
End If
If pictureBox3.Left >= pictureBox1.Left - 14 _
And pictureBox3.Left <= pictureBox1.Left + 10 Then
If pictureBox3.Top >= pictureBox1.Top - 10 _
And pictureBox3.Top <= pictureBox1.Top + 2 Then
direction = 68
End If
If pictureBox3.Top >= pictureBox1.Top + 1 _
And pictureBox3.Top <= pictureBox1.Top + 8 Then
direction = 45
End If
If pictureBox3.Top >= pictureBox1.Top + 9 _
And pictureBox3.Top <= pictureBox1.Top + 15 Then
direction = 23
End If
If pictureBox3.Top >= pictureBox1.Top + 16 _
And pictureBox3.Top <= pictureBox1.Top + 22 Then
direction = 0
End If
If pictureBox3.Top >= pictureBox1.Top + 23 _
And pictureBox3.Top <= pictureBox1.Top + 29 Then
direction = 338
End If
If pictureBox3.Top >= pictureBox1.Top + 30 _
And pictureBox3.Top <= pictureBox2.Top + 36 Then
direction = 315
End If
If pictureBox3.Top >= pictureBox1.Top + 37 _
And pictureBox3.Top <= pictureBox2.Top + 42 Then
direction = 293
End If
End If
Static player2Score As Integer
If (pictureBox3.Left > 476) Then
Timer2.Enabled = False
player2Score = player2Score + 1
Dim p2score As String
p2score = player2Score.ToString()
label5.Text = p2score
End If
Static player1Score As Integer
If (pictureBox3.Left < -20) Then
Timer2.Enabled = False
player1Score = player1Score + 1
Dim p1score As String
p1score = player1Score.ToString()
label3.Text = p1score
End If
End Sub
Sub boing()
Keine Kommentare:
Kommentar veröffentlichen
Hinweis: Nur ein Mitglied dieses Blogs kann Kommentare posten.