Blitz: 2 Player Shooter
Posted: Tue Jun 15, 2004 12:32 pm
Here is a somewhat advanced version of the 2 player shooter game. Notice how I made functions that could handle both players, instead of making a function for each player...
See if that helps any.
Code: Select all
;Advanced 2 Player Shooter By JS Lemming
;Set graphics mode
Graphics 640,480
;Set buffer to backbuffer
SetBuffer BackBuffer()
;Declare Player Type
Type Player
Field x, y, life
End Type
;Declare Bullet Type
Type Bullet
Field x, y, direction
End Type
;Create Player 1 and 2
Global Player1.Player = New Player
Global Player2.Player = New Player
;Set Player 1 variables
Player1\x = 0
Player1\y = 200
Player1\life = 50
;Set Player 2 variables
Player2\x = 590
Player2\y = 200
Player2\life = 50
;Display game controls for a few seconds
;Clear the screen
Cls
;Change color to white
Color 255,255,255
Text 200,160,"Press Enter To Start"
Text 200,200,"Game controls:"
Text 220,224,"- Player 1"
Text 220,236," Move up - 'Up Key'"
Text 220,248," Move down - 'Down Key'"
Text 220,260," Shoot - 'Right Enter Key'"
Text 220,272,"- Player 2"
Text 220,284," Move up - 'A Key'"
Text 220,296," Move down - 'Z Key'"
Text 220,308," Shoot - 'Space Bar'"
;Flip screen into view
Flip
;Wait till space bar pressed
While Not KeyHit(28)
Wend
;While the user doesn't press the ESC key...
While Not KeyDown(1)
;Clear the screen
Cls
UserInput()
UpdateBullets()
CheckHit()
CheckLife()
DrawBullets()
DrawLifeBar()
;Keep the players on the screen
If Player1\y < 0 Then Player1\y = 0
If Player1\y > 430 Then Player1\y = 430
If Player2\y < 0 Then Player2\y = 0
If Player2\y > 430 Then Player2\y = 430
;Draw Player 1 as red
Color 255,0,0
Rect Player1\x,Player1\y,50,50,1
;Draw Player 2 as blue
Color 0,0,255
Rect Player2\x,Player2\y,50,50,1
;Flip screen into view
Flip
Wend
;<< Game Functions >>;
;Creates a bullet when called, possible arguments: 1 or 2
Function CreateBullet(Player)
;Create the type
Bullet.Bullet = New Bullet
;If bullet for Player 1 then
If Player = 1
;Set Bullet x to 50
Bullet\x = 50
;Set Bullet y to equal player one's current y position
Bullet\y = Player1\y + 20
;Set direction to 1 (will move right)
Bullet\direction = 1
EndIf
;If bullet for Player 2 then
If Player = 2
;Set Bullet x to 50
Bullet\x = 590
;Set Bullet y to equal player two's current y position
Bullet\y = Player2\y + 20
;Set direction to -1 (will move left)
Bullet\direction = -1
EndIf
End Function
;Updates all bullets in game
Function UpdateBullets()
;Loop through all bullets
For Current.Bullet = Each Bullet
;If bullet direction is 1, move right
If Current\direction = 1
;Add 8 to each bullet's x
Current\x = Current\x + 8
;if not, move left
Else
;Subtract 8 from each bullet's x
Current\x = Current\x - 8
EndIf
Next
End Function
;Draw all bullets in game
Function DrawBullets()
;Loop through all bullets
For Current.Bullet = Each Bullet
;Draw a green rectangle for each bullet
Color 0,255,0
Rect Current\x, Current\y, 20, 5, 1
Next
End Function
;Gets user input and acts accordingly
Function UserInput()
;--- Input from player 1 ---
;If up key pressed...
If KeyDown(200) Then Player1\y = Player1\y - 3
;If down key pressed...
If KeyDown(208) Then Player1\y = Player1\y + 3
;If right enter key pressed...
If KeyHit(156) Then CreateBullet(1)
;--- Input from player 2 ---
;If A key pressed...
If KeyDown(30) Then Player2\y = Player2\y - 3
;If Z key pressed...
If KeyDown(44) Then Player2\y = Player2\y + 3
;If space bar pressed...
If KeyHit(57) Then CreateBullet(2)
End Function
;Checks if a bullet hits a player
Function CheckHit()
;Loop through all bullets
For Current.Bullet = Each Bullet
;If bullet direction is 1, check if it hit player 2
If Current\direction = 1
;If bullet x is within the rectangle of player 2
If (Current\x + 20) > Player2\x And (Current\x + 20) < (Player2\x + 50)
;If bullet y is within the rectangle of player 2
If Current\y > Player2\y And Current\y < (Player2\y + 50)
;Subtract 1 from player 2's life
Player2\life = Player2\life - 1
;Draw yellow ball at bullets x and y
Color 255,255,0
Oval Current\x,(Current\y - 10),20,20,1
;Delete bullet from existance
Delete Current
EndIf
EndIf
;If bullet direction is -1, check if it hit player 1
ElseIf Current\direction = -1
;If bullet x is within the rectangle of player 1
If Current\x > Player1\x And Current\x < (Player1\x + 50)
;If bullet y is within the rectangle of player 1
If Current\y > Player1\y And Current\y < (Player1\y + 50)
;Subtract 1 from player 2's life
Player1\life = Player1\life - 1
;Draw yellow ball at bullets x and y
Color 255,255,0
Oval (Current\x - 5),(Current\y - 10),20,20,1
;Delete bullet from existance
Delete Current
EndIf
EndIf
EndIf
Next
End Function
;Draws a fancy life bar for each player
Function DrawLifeBar()
;Loop and draw for the amount of life of player 1
For i = 1 To (Player1\life * 3) Step 3
;Draw a redish rect. for each life point
Color 255,i,0
Rect 80 + i,10,2,15
Next
;Loop and draw for the amount of life of player 2
For i = 1 To (Player2\life * 3) Step 3
;Draw a blueish rect. for each life point
Color 0,i,255
Rect 410 + i,10,2,15
Next
End Function
;See if anyone is dead yet
Function CheckLife()
;If player 1 dead, make player 2 the winner
If Player1\life <= 0 Then Winner(2)
;If player 2 dead, make player 1 the winner
If Player2\life <= 0 Then Winner(1)
End Function
;Displays the winner screen, possible arguments: 1 or 2
Function Winner(Player)
;While the user doesn't press the ESC key...
While Not KeyDown(1)
;Clear the screen
Cls
;Make color randomly flash
Color Rand(0,255),Rand(0,255),Rand(0,255)
;If Player 1 won
If Player = 1
;Display text
Text 220,230,"PLAYER 1 IS THE VICTOR!!!"
EndIf
;If Player 2 won
If Player = 2
;Display text
Text 220,230,"PLAYER 2 IS THE VICTOR!!!"
EndIf
;Say Press ESC to quit
Color 200,200,200
Text 220,440,"Press the ESC key to quit."
;Flip screen into view
Flip
Wend
;Shutdown the game
End
End Function