Visual Basic 2010 Strategiespiel programmieren SelMcKenzie Selzer-McKenzie
Author D.Selzer-McKenzie
Heute zeige ich Ihnen, wie man ein komplettes Strategiespiel programmiert. Ich habe hier ein Snowboardspiel auf einer Skipiste genommen, aber man kann es natürlich genausogut als Rennwagenspiel oder sonstwas programmieren.
Den Code habe ich so geschrieben, dass er gut lesbar ist und Sie müssten das Video jeweils anhalten, um den Code abschreiben zu können.
Sie sehen, das Spiel funktioniert wunderbar.
Selzer-McKenzie
Der Code lautet:
Public Class Form1
Inherits System.Windows.Forms.Form
Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Integer) As Short
Dim x(30), y(30), mogulx(7), moguly(7), rockx(6), rocky(6) As Integer
Dim mykeyup, mykeyleft, mykeyright, mykeydown As Boolean
Dim contact As String
Dim speedx, speedy As Integer
Dim switcharm, rightcount, leftcount As Integer
Dim direction As String
Dim whichKey As Integer
Dim santax, santay As Integer
Dim SantaCrash As Boolean
Dim santaCrashCount As Integer
Dim santaCrashx, santaCrashy As Integer
Dim pinkSkierDirection, greenSkierDirection, santaDirection As String
Dim greenSkierCrash, pinkSkiercrash As Boolean
Dim greenSkierCrashCount, pinkSkierCrashCount As Integer
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
whichKey = e.KeyCode
If whichKey = 39 Then
mykeyright = True
mykeyup = False
mykeyleft = False
mykeydown = False
End If
If whichKey = 37 Then
mykeyright = False
mykeyup = False
mykeyleft = True
mykeydown = False
End If
If whichKey = 38 Then
mykeyright = False
mykeyup = True
mykeyleft = False
mykeydown = False
End If
If whichKey = 40 Then
mykeyright = False
mykeyup = False
mykeyleft = False
mykeydown = True
End If
End Sub
Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
whichKey = e.KeyCode
If whichKey = 39 Then
mykeyright = False
End If
If whichKey = 37 Then
mykeyleft = False
End If
If whichKey = 38 Then
End If
If whichKey = 40 Then
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim skitype As String
Randomize()
contact = "no"
For i = 0 To 30
ReDim Preserve picTreeTarget(i)
picTreeTarget(i) = New PictureBox
picTreeTarget(i).Text = CStr(i)
picTreeTarget(i).Size = picTree.Size
If i < 15 Then picTreeTarget(i).Image = picTree.Image
If i >= 15 Then picTreeTarget(i).Image = picTree2.Image
picTreeTarget(i).Visible = True
AddHandler picTreeTarget(i).Click, AddressOf PictureBox_Click
Me.Controls.Add(picTreeTarget(i))
x(i) = Int(Rnd() * 800) + 1
y(i) = Int(Rnd() * 600) + 1
picTreeTarget(i).Location = New Point(x(i), y(i))
Next i
For i = 0 To 7
ReDim Preserve picMogulTarget(i)
picMogulTarget(i) = New PictureBox
picMogulTarget(i).Text = CStr(i)
picMogulTarget(i).Size = picMogul.Size
picMogulTarget(i).Image = picMogul.Image
picMogulTarget(i).Visible = True
picMogulTarget(i).BackColor = Color.Transparent
AddHandler picMogulTarget(i).Click, AddressOf PictureBox_Click
Me.Controls.Add(picMogulTarget(i))
picMogulTarget(i).Location = New Point(mogulx(i), moguly(i))
Next i
For i = 0 To 5
ReDim Preserve picRockTarget(i)
picRockTarget(i) = New PictureBox
picRockTarget(i).Text = CStr(i)
picRockTarget(i).Size = picRock.Size
picRockTarget(i).Image = picRock.Image
picRockTarget(i).Visible = True
picRockTarget(i).BackColor = Color.Transparent
AddHandler picRockTarget(i).Click, AddressOf PictureBox_Click
Me.Controls.Add(picRockTarget(i))
rockx(i) = Int(Rnd() * 800) + 1
rocky(i) = Int(Rnd() * 600) + 1
picRockTarget(i).Location = New Point(rockx(i), rocky(i))
Next i
speedy = 5
speedx = 0
santaDirection = chooseDirection()
skitype = "santa"
picSantaTarget.Image = chooseImage(santaDirection, skitype)
picSantaTarget.Location = New Point(Int(Rnd() * 800) + 1, Int(Rnd() * 600) + 1)
pinkSkierDirection = chooseDirection()
skitype = "pink"
picPinkSkierTarget.Location = New Point(Int(Rnd() * 800) + 1, Int(Rnd() * 600) + 1)
picPinkSkierTarget.Image = chooseImage(pinkSkierDirection, skitype)
greenSkierDirection = chooseDirection()
skitype = "green"
picGreenSkierTarget.Location = New Point(Int(Rnd() * 800) + 1, Int(Rnd() * 600) + 1)
picGreenSkierTarget.Image = chooseImage(greenSkierDirection, skitype)
End Sub
Function chooseImage(ByVal d As String, ByVal skitype As String) As Image
Dim myimage As Image
If d = "SE" Then
If skitype = "pink" Then
myimage = picPinkSkierLeft.Image
End If
If skitype = "green" Then
myimage = picGreenSkierLeft.Image
End If
If skitype = "santa" Then
myimage = picSantaLeft.Image
End If
ElseIf d = "SW" Then
If skitype = "pink" Then
myimage = picPinkSkierRight.Image
End If
If skitype = "green" Then
myimage = picGreenSkierRight.Image
End If
If skitype = "santa" Then
myimage = picSantaRight.Image
End If
Else
If skitype = "pink" Then
myimage = picPinkSkierStraight.Image
End If
If skitype = "green" Then
myimage = picGreenSkierStraight.Image
End If
If skitype = "santa" Then
myimage = picSanta0.Image
End If
End If
Return myimage
End Function
Function chooseDirection() As String
Dim directChoose As Integer
Dim direct As String
directChoose = Int((Rnd() * 3) + 1)
If directChoose = 1 Then
direct = "SE"
ElseIf directChoose = 2 Then
direct = "SW"
Else
direct = "S"
End If
Return direct
End Function
Private Sub PictureBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim pic As PictureBox = DirectCast(sender, PictureBox)
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim i As Integer
Static h As Integer
Static jump As Integer
Dim skitype As String
If contact = "hit" Then h = h + 1
If h = 15 Then
imgOuch.Visible = False
End If
If h > 14 Then
contact = ""
h = 0
imgOuch.Visible = False
End If
If SantaCrash = False Then
If santaDirection = "SW" Then
picSantaTarget.Location() = New Point(picSantaTarget.Left - speedx + 25, picSantaTarget.Top - speedy + 23) 'moves santa independently
End If
If santaDirection = "SE" Then
picSantaTarget.Location() = New Point(picSantaTarget.Left - speedx - 25, picSantaTarget.Top - speedy + 23) 'moves santa independently
End If
If santaDirection = "S" Then
picSantaTarget.Location() = New Point(picSantaTarget.Left - speedx, picSantaTarget.Top - speedy + 23) 'moves santa independently
End If
End If
If SantaCrash = True And contact <> "hit" Then
picSantaTarget.Location() = New Point(picSantaTarget.Left - speedx, picSantaTarget.Top - speedy) 'moves santa independently
End If
If greenSkierCrash = False Then
If greenSkierDirection = "SW" Then
picGreenSkierTarget.Location() = _
New Point(picGreenSkierTarget.Left - speedx + 25, picGreenSkierTarget.Top - speedy + 23) 'moves santa independently
End If
If greenSkierDirection = "SE" Then
picGreenSkierTarget.Location() = _
New Point(picGreenSkierTarget.Left - speedx - 25, picGreenSkierTarget.Top - speedy + 23) 'moves santa independently
End If
If greenSkierDirection = "S" Then
picGreenSkierTarget.Location() = _
New Point(picGreenSkierTarget.Left - speedx, picGreenSkierTarget.Top - speedy + 23) 'moves santa independently
End If
End If
If pinkSkiercrash = False Then
picPinkSkierTarget.Image = picPinkSkierTarget.Image
If pinkSkierDirection = "SW" Then
picPinkSkierTarget.Location() = _
New Point(picPinkSkierTarget.Left - speedx + 25, _
picPinkSkierTarget.Top - speedy + 23)
End If
If pinkSkierDirection = "SE" Then
picPinkSkierTarget.Location() = _
New Point(picPinkSkierTarget.Left - speedx - 25, _
picPinkSkierTarget.Top - speedy + 23)
End If
If pinkSkierDirection = "S" Then
picPinkSkierTarget.Location() = _
New Point(picPinkSkierTarget.Left - speedx, _
picPinkSkierTarget.Top - speedy + 23)
End If
End If
If picSantaTarget.Top > 632 Then
santaDirection = chooseDirection()
skitype = "santa"
picSantaTarget.Image = chooseImage(santaDirection, skitype)
picSantaTarget.Left = Int(Rnd() * 800) + 1
picSantaTarget.Top = -32
SantaCrash = False
End If
If picSantaTarget.Top < -32 Then
santaDirection = chooseDirection()
skitype = "santa"
picSantaTarget.Image = chooseImage(santaDirection, skitype)
picSantaTarget.Left = Int(Rnd() * 800) + 1
picSantaTarget.Top = -32
SantaCrash = False
End If
If picPinkSkierTarget.Left < -32 Then
santaDirection = chooseDirection()
skitype = "santa"
picSantaTarget.Image = chooseImage(santaDirection, skitype)
picSantaTarget.Left = Int(Rnd() * 800) + 1
picSantaTarget.Top = -32
SantaCrash = False
End If
If picPinkSkierTarget.Left > 800 Then
santaDirection = chooseDirection()
skitype = "santa"
picSantaTarget.Image = chooseImage(santaDirection, skitype)
picSantaTarget.Left = Int(Rnd() * 800) + 1
picSantaTarget.Top = -32
SantaCrash = False
End If
If picPinkSkierTarget.Top > 632 Then
pinkSkierDirection = chooseDirection()
skitype = "pink"
picPinkSkierTarget.Image = chooseImage(pinkSkierDirection, skitype)
picPinkSkierTarget.Left = Int(Rnd() * 800) + 1
picPinkSkierTarget.Top = -32
pinkSkiercrash = False
End If
If picPinkSkierTarget.Top < -32 Then
pinkSkierDirection = chooseDirection()
skitype = "pink"
picPinkSkierTarget.Image = chooseImage(pinkSkierDirection, skitype)
picPinkSkierTarget.Left = Int(Rnd() * 800) + 1
picPinkSkierTarget.Top = -32
pinkSkiercrash = False
End If
If picPinkSkierTarget.Left < -32 Then
pinkSkierDirection = chooseDirection()
skitype = "pink"
picPinkSkierTarget.Image = chooseImage(pinkSkierDirection, skitype)
picPinkSkierTarget.Left = Int(Rnd() * 800) + 1
picPinkSkierTarget.Top = -32
pinkSkiercrash = False
End If
If picPinkSkierTarget.Left > 800 Then
pinkSkierDirection = chooseDirection()
skitype = "pink"
picPinkSkierTarget.Image = chooseImage(pinkSkierDirection, skitype)
picPinkSkierTarget.Left = Int(Rnd() * 800) + 1
picPinkSkierTarget.Top = -32
pinkSkiercrash = False
End If
If picGreenSkierTarget.Top > 632 Then
greenSkierDirection = chooseDirection()
skitype = "green"
picGreenSkierTarget.Image = chooseImage(greenSkierDirection, skitype)
picGreenSkierTarget.Left = Int(Rnd() * 800) + 1
picGreenSkierTarget.Top = -32
greenSkierCrash = False
End If
If picGreenSkierTarget.Top < -32 Then
greenSkierDirection = chooseDirection()
skitype = "green"
picGreenSkierTarget.Image = chooseImage(greenSkierDirection, skitype)
picGreenSkierTarget.Left = Int(Rnd() * 800) + 1
picGreenSkierTarget.Top = -32
greenSkierCrash = False
End If
If picGreenSkierTarget.Left < -32 Then
greenSkierDirection = chooseDirection()
skitype = "green"
picGreenSkierTarget.Image = chooseImage(greenSkierDirection, skitype)
picGreenSkierTarget.Left = Int(Rnd() * 800) + 1
picGreenSkierTarget.Top = -32
greenSkierCrash = False
End If
If picGreenSkierTarget.Left > 800 Then
greenSkierDirection = chooseDirection()
skitype = "green"
picGreenSkierTarget.Image = chooseImage(greenSkierDirection, skitype)
picGreenSkierTarget.Left = Int(Rnd() * 800) + 1
picGreenSkierTarget.Top = -32
greenSkierCrash = False
End If
If contact = "jump" Then jump = jump + 1
If jump = 6 Then
contact = ""
jump = 0
If contact <> "hit" Then
picSnowBoardTarget.Image = picSnowBoard0Right.Image
End If
If contact = "hit" Then
picSnowBoardTarget.Image = picSnowBoardCrash1.Image
End If
End If
Call kdown()
Call kright()
Call kleft()
For i = 0 To 30
If contact <> "hit" Then
x(i) = x(i) - speedx
y(i) = y(i) - speedy
picTreeTarget(i).Location = New Point(x(i), y(i))
If i < 8 And contact <> "hit" Then
mogulx(i) = mogulx(i) - speedx
moguly(i) = moguly(i) - speedy
picMogulTarget(i).Location = New Point(mogulx(i), moguly(i))
End If
If i < 6 And contact <> "hit" Then
rockx(i) = rockx(i) - speedx
rocky(i) = rocky(i) - speedy
picRockTarget(i).Location = New Point(rockx(i), rocky(i))
End If
End If
If picSnowBoardTarget.Left < picTreeTarget(i).Left + 8 _
And picSnowBoardTarget.Left > picTreeTarget(i).Left - 16 Then
If picSnowBoardTarget.Top < picTreeTarget(i).Top + 0.25 * speedy _
And picSnowBoardTarget.Top > picTreeTarget(i).Top - 0.75 * speedy + 1 _
Then
picSnowBoardTarget.Image = picSnowBoardCrash1.Image
speedx = 0
rightcount = 0
leftcount = 0
imgOuch.Location = New Point(picSnowBoardTarget.Left + 8, _
picSnowBoardTarget.Top - 32)
imgOuch.Visible = -1
contact = "hit"
speedy = 5
End If
End If
If i < 6 Then
If picSnowBoardTarget.Left < picRockTarget(i).Left + 24 _
And picSnowBoardTarget.Left > picRockTarget(i).Left - 32 Then
If picSnowBoardTarget.Top < picRockTarget(i).Top + 0.25 * speedy _
And picSnowBoardTarget.Top > _
picRockTarget(i).Top - 0.75 * speedy + 1 Then
speedx = 0
picSnowBoardTarget.Image = picSnowBoardCrash1.Image
imgOuch.Location = New Point(picSnowBoardTarget.Left + 8, _
picSnowBoardTarget.Top - 32)
imgOuch.Visible = True
contact = "hit"
speedy = 5
End If
End If
End If
If i < 8 Then
If picSnowBoardTarget.Left < picMogulTarget(i).Left + 32 _
And picSnowBoardTarget.Left > picMogulTarget(i).Left - 32 Then
If picSnowBoardTarget.Top < picMogulTarget(i).Top + 0.25 * speedy _
And picSnowBoardTarget.Top > picMogulTarget(i).Top - 0.75 * _
speedy + 1 Then
picSnowBoardTarget.Image = picSnowBoardJump.Image
contact = "jump"
End If
End If
End If
If y(i) < -32 Then
x(i) = Int(Rnd() * 800) + 1
y(i) = 600
picTreeTarget(i).Left = x(i)
End If
If y(i) > 600 Then
x(i) = Int(Rnd() * 800) + 1
y(i) = -32
picTreeTarget(i).Left = x(i)
End If
If x(i) < -32 Then
y(i) = Int(Rnd() * 600) + 1
x(i) = 800
picTreeTarget(i).Top = y(i)
End If
If x(i) > 800 Then
y(i) = Int(Rnd() * 600) + 1
x(i) = -32
picTreeTarget(i).Top = y(i)
End If
If i < 8 Then
If moguly(i) < -32 Then
mogulx(i) = Int(Rnd() * 800) + 1
moguly(i) = 600
picMogulTarget(i).Left = mogulx(i)
End If
If moguly(i) > 600 Then
mogulx(i) = Int(Rnd() * 800) + 1
moguly(i) = -32
picMogulTarget(i).Left = mogulx(i)
End If
If mogulx(i) < -32 Then
moguly(i) = Int(Rnd() * 600) + 1
mogulx(i) = 800
picMogulTarget(i).Top = y(i)
End If
If mogulx(i) > 800 Then
moguly(i) = Int(Rnd() * 600) + 1
mogulx(i) = -32
picMogulTarget(i).Top = y(i)
End If
End If
If i < 6 Then
If rocky(i) < -32 Then
rocky(i) = 600
rockx(i) = Int(Rnd() * 800) + 1
picRockTarget(i).Left = rockx(i)
End If
If rocky(i) > 600 Then
rockx(i) = Int(Rnd() * 800) + 1
rocky(i) = -32
picRockTarget(i).Left = rockx(i)
End If
If rockx(i) < -32 Then
rocky(i) = Int(Rnd() * 600) + 1
rockx(i) = 800
picRockTarget(i).Top = y(i)
End If
If rockx(i) > 800 Then
rocky(i) = Int(Rnd() * 600) + 1
rockx(i) = -32
picRockTarget(i).Top = y(i)
End If
End If
If picSantaTarget.Left < picTreeTarget(i).Left + 8 _
And picSantaTarget.Left > picTreeTarget(i).Left - 16 Then
If picSantaTarget.Top < picTreeTarget(i).Top + 8 _
And picSantaTarget.Top > picTreeTarget(i).Top - 16 Then
picSantaTarget.Image = picSantaCrashAlone.Image
SantaCrash = True
End If
End If
If i < 6 Then
If picSantaTarget.Left < picRockTarget(i).Left + 8 _
And picSantaTarget.Left > picRockTarget(i).Left - 16 Then
If picSantaTarget.Top < picRockTarget(i).Top + 8 _
And picSantaTarget.Top > picRockTarget(i).Top - 16 Then
picSantaTarget.Image = picSantaCrashAlone.Image
SantaCrash = True
End If
End If
If i < 1 Then
If picSantaTarget.Left < picSnowBoardTarget.Left + 32 _
And picSantaTarget.Left > picSnowBoardTarget.Left - 32 Then
If picSantaTarget.Top < picSnowBoardTarget.Top + 32 _
And picSantaTarget.Top > picSnowBoardTarget.Top - 16 Then
picSantaTarget.Image = picSantaCrashAlone.Image
picSantaTarget.Location = _
New Point(picSantaTarget.Left + 32, picSantaTarget.Top)
picSnowBoardTarget.Location = _
New Point(picSnowBoardTarget.Left, picSnowBoardTarget.Top)
picSnowBoardTarget.Image = picSnowBoardCrash1.Image
contact = "hit"
imgOuch.Location = _
New Point(picSnowBoardTarget.Left + 8, picSnowBoardTarget.Top - 32)
imgOuch.Visible = True
SantaCrash = True
End If
End If
End If
If i < 1 Then
If picGreenSkierTarget.Left < picSnowBoardTarget.Left + 32 _
And picGreenSkierTarget.Left > picSnowBoardTarget.Left - 32 Then
If picGreenSkierTarget.Top < picSnowBoardTarget.Top + 32 _
And picGreenSkierTarget.Top > picSnowBoardTarget.Top - 16 Then
picGreenSkierTarget.Image = picGreenSkierCrash.Image
picGreenSkierTarget.Location = _
New Point(picGreenSkierTarget.Left + 32, _
picGreenSkierTarget.Top)
picSnowBoardTarget.Location = _
New Point(picSnowBoardTarget.Left, picSnowBoardTarget.Top)
picSnowBoardTarget.Image = picSnowBoardCrash1.Image
contact = "hit"
imgOuch.Location = New Point(picSnowBoardTarget.Left + 8, _
picSnowBoardTarget.Top - 32)
imgOuch.Visible = True
greenSkierCrash = True
End If
End If
End If
If i < 1 Then
If picPinkSkierTarget.Left < picSnowBoardTarget.Left + 32 _
And picPinkSkierTarget.Left > picSnowBoardTarget.Left - 32 Then
If picPinkSkierTarget.Top < picSnowBoardTarget.Top + 32 _
And picPinkSkierTarget.Top > picSnowBoardTarget.Top - 16 Then
picPinkSkierTarget.Image = picPinkSkierCrash.Image
picPinkSkierTarget.Location = _
New Point(picPinkSkierTarget.Left + 32, _
picPinkSkierTarget.Top)
picSnowBoardTarget.Location = _
New Point(picSnowBoardTarget.Left, picSnowBoardTarget.Top)
picSnowBoardTarget.Image = picSnowBoardCrash1.Image
contact = "hit"
imgOuch.Location = _
New Point(picSnowBoardTarget.Left + 8, _
picSnowBoardTarget.Top - 32)
imgOuch.Visible = True
pinkSkiercrash = True
End If
End If
End If
If SantaCrash = True Then
santaCrashCount = santaCrashCount + 1
If santaCrashCount > 80 Then
SantaCrash = False
santaCrashCount = 0
picSantaTarget.Image = picSanta0.Image
contact = ""
imgOuch.Visible = False
End If
End If
If greenSkierCrash = True Then
greenSkierCrashCount = greenSkierCrashCount + 1
If greenSkierCrashCount > 80 Then
greenSkierCrash = False
greenSkierDirection = chooseDirection()
skitype = "green"
picGreenSkierTarget.Image = _
chooseImage(greenSkierDirection, skitype)
greenSkierCrash = False
greenSkierCrashCount = 0
contact = ""
imgOuch.Visible = False
End If
End If
If pinkSkiercrash = True Then
pinkSkierCrashCount = pinkSkierCrashCount + 1
If pinkSkierCrashCount > 80 Then
pinkSkiercrash = False
pinkSkierDirection = chooseDirection()
skitype = "pink"
picPinkSkierTarget.Image = _
chooseImage(pinkSkierDirection, skitype)
pinkSkiercrash = False
pinkSkierCrashCount = 0
contact = ""
imgOuch.Visible = False
End If
End If
End If
Next i
End Sub
Sub kdown()
If mykeydown And contact <> "hit" Then
rightcount = 0
leftcount = 0
direction = "S"
speedy = speedy + 2
If speedy > 5 Then speedy = 25
End If
If direction = "S" Then
If contact <> "jump" And contact <> "hit" And (mykeyleft = False Or mykeyright = False) Then
switcharm = switcharm + 1
If switcharm < 11 Then picSnowBoardTarget.Image = _
picSnowBoard0Right.Image
If switcharm > 10 Then picSnowBoardTarget.Image = _
picSnowBoard0Left.Image
If switcharm > 20 Then switcharm = 0
End If
End If
End Sub
Sub kright()
If mykeyright And contact <> "hit" Then
leftcount = 0
rightcount = rightcount + 1
If rightcount = 1 Then
direction = "SE"
picSnowBoardTarget.Image = picSnowBoard60Right.Image
speedx = 10
speedy = 22
End If
If rightcount = 2 Then
direction = "SE"
picSnowBoardTarget.Image = picSnowBoard60Right.Image
speedx = 15
speedy = 19
End If
If rightcount = 3 Then
direction = "SE"
picSnowBoardTarget.Image = picSnowBoard45Right.Image
speedx = 20
speedy = 16
End If
If rightcount = 4 Then
direction = "SE"
picSnowBoardTarget.Image = picSnowBoard45Right.Image
speedx = 10
speedy = 8
End If
If rightcount = 5 Then
direction = "SE"
picSnowBoardTarget.Image = picSnowBoard90.Image
Keine Kommentare:
Kommentar veröffentlichen
Hinweis: Nur ein Mitglied dieses Blogs kann Kommentare posten.