Blitz: 2 Player Shooter

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

Blitz: 2 Player Shooter

Post by JS Lemming »

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...

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
See if that helps any.
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
User avatar
Don Pwnious
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 833
Joined: Tue Jun 15, 2004 5:32 pm
Location: on the streets wit my j23
Contact:

Post by Don Pwnious »

:evil: :(|):
whrn I tried it the first player wont fire. :shock:
1/8th time- 14secs
1/8th speed - 110mph
Guest

Post by Guest »

Thank you JS Lemming! (Don't listen to phantom, it all works!) That was the most useful post on this whole forum. The labels helped a whole lot. After studing that for a while, I think I can make one similar to yours, WITHOUT looking at yours. But the bullets, life guages, charactors, ect would be differnt. I'll post when, and IF I complete mine. Yesterday I got most of it to work correctly, but I still want to add a few things. Thanks again.

P.S. I loved the life guages
User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

Post by JS Lemming »

Thanks Arce, I too thought the life guages were a nice touch. :spin:

Hmmm... Phantom, are you using a labtop? I just realized that some people, especialy labtop users, don't have a right enter key. It's relativly easy to change the controls, if you want to try, find this section of the code:

Code: Select all

;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 
Now change 156 above to whatever scan code you wish it to be.
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
Guest

Post by Guest »

I made a shooter that worked! Or, at least I though. But when i created an executable, it flipped past the credits and instructions, then just closed. The game wouldn't work. Anyone know why? If you need to see the code, just ask.
User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

Post by JS Lemming »

Yes, please show the code, it makes it easier to debug. :wink:
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
Guest

Post by Guest »

LOL! Good point JS Lemming. You kinda need to see the code, don't you. :D

It was two in the morning when I made that program, and I was tired as h**l. So shoot me, I didn't put the executable in the same directory as the images! :wink:

JS Lemming, do you mind if I use your life guages in my shooter game?

Does anyone here know where I can get some good sound affects for my game? (Gun sounds)

Sorry if I sound rude, I'm just a mean person! :twisted:
User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

Post by JS Lemming »

Sure, you can use my life guages. About the sound effects, I would try a google search.

When it comes to organizing media for a game, I usually have a folder called "Data" in the game folder that holds the sound and graphics.
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
Guest

Post by Guest »

Thanks JS Lemming for letting me use your life guages.

GIVE ME GOOD SOUND EFFECTS NOW OR I'LL REAP YOUR SOUL!
:reaper:

Just kidding! :guffaw:

I will try I google search, if I get anything good, I'll post.

Thanks for all the help you've given me. Your my blitz idol :spin: :worship: :worship: :worship: :worship:
:ninjagun:
:sniper: :mike:

P.S. I'll be screwing around with these new emotions for a long time
Guest

Post by Guest »

Der.....I have yet another question...

This may sound gay, but how would I make it so if you hit key 153, and a variable is a certain number, a differnt thing happens?

If keyhit(153) and currentgun$="burst" then burst()
If keyhit(153) and currentgun$="normal" then createbullet()
If keydown(153) and currentgun$="rapid" then createbullet()

The above doesn't work

Code: Select all

If keyhit(153) then checkvariable()

Function checkvariable()
If currentgun$="burst" then 
burst()
Elseif currentgun$="rapid" then
rapid()
Else
createbullet()
end if
end function

Function burst()
For I = 1 to 9 step 3
updatebullet()
delay 1
createbullet()
next
end function
The above code ISN'T in my program, I just barely typed it, thats why there are no labels, missing functions, ect. And yes, the above doesn't work

So what can I do? I tried many other things, but NONE worked.

AND

How would I make it if the variable was "rapid", and you held that key that is supposed to do three things, it fires rapidly? (Using 'keydown' while all the other things the button can do is using 'keyhit')

I am sorry if I worded this stuff like human body waste, but I tried my best.

Please help, if I'm not already too hopeless....
User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

Post by JS Lemming »

Hmmm... I would just use "And" in the if statement, like so:

Code: Select all

If keyhit(153) and currentgun$="burst" then burst()
If keydown(153) and currentgun$="rapid" then rapid()
That's what I would do, haven't tried it though.
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
Guest

Post by Guest »

If keyhit(153) and currentgun$="burst" then burst()
If keyhit(153) and currentgun$="normal" then createbullet()
If keydown(153) and currentgun$="rapid" then createbullet()

The above doesn't work
quoting myself...e.e

I guess I'll try it again.... :mrgreen:
Guest

Post by Guest »

Once again I mock my own stupidity. Plz forgive me for wasteing your time, AGAIN. My problem was......I forgot to make currentgun$ global! sheet, I'm stupid! :nono:
User avatar
Don Pwnious
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 833
Joined: Tue Jun 15, 2004 5:32 pm
Location: on the streets wit my j23
Contact:

Post by Don Pwnious »

Arce
Do you want me to give you those sounds that you wanted
if so
contact me in my MSN messenger name
either Skter01John@aol.com or Tammyle36@aol.com[/b
1/8th time- 14secs
1/8th speed - 110mph
User avatar
Don Pwnious
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 833
Joined: Tue Jun 15, 2004 5:32 pm
Location: on the streets wit my j23
Contact:

Post by Don Pwnious »

Arce
Do you want me to give you those sounds that you wanted
if so
contact me in my MSN messenger name
either Skter01John@aol.com or Tammyle36@aol.com
1/8th time- 14secs
1/8th speed - 110mph
Post Reply