bap

Blitz3D Forums/Blitz3D Beginners Area/bap

Beginners All Purpose Pong.

Here is a retro clasic remade by me
 
; ============================================================================
; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
; Game Title	:	Beginners All-Purpose Pong
;
; Author		:	Stefan Lulham
; Email			:	Stefanlulham@...
;
; Things to try;
;	1. Make a mode selector
;	2. Make a simple ball follow ai system
;	3. Make it save high scores to be viewed later
;	4. Make it Record how long a set whent on for
;	5. Make a hi-score based upon the above
;
; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
; ============================================================================

; Setup
Graphics 800, 600, 32, 1 ; Set the GFX Mode 1 = Full Screen, 2 = Windowed, 3 = Sizeable Windowed
SeedRnd(MilliSecs()) ; start the randomize timer
AutoMidHandle 1 ; make it draw everything from the center

; Consts
Const SPEEDUP% = 12 ; after this period of time in secs the ball will speed up
Const PADDLEMOVESPEED% = 5 ; This is how fast you can move your paddles
Const PLMOVESPEED% = 8
Const MakeBalls = 5
Type ball
Field  ballx#
Field bally#
Field ballangle#
Field balldirection
Field ballspeed
End Type
; Globals
Global Player1Score%, Player2Score% ; Varibles to hold the scores
Global Player1X# = 50 ; Player 1 X Pos
Global Player2X# = 750 ; Player 2 X Pos
Global Player1Y# = 300 ; Player 1 Y Pos
Global Player2Y# = 300 ; Player 2 Y Pos
Global ball.ball
Global ball2.ball
ball.ball = New ball
ball\ballx# = 400 
ball\bally#= 300
ball\ballangle# = 0
ball\balldirection = 0 
ball\ballspeed = 8

Global SecsPassed% = 0 ; Seconds passed (for the start timer ... "ready - Steady - Go!"
Global GameSecsPassed% = 0 ; Seconds passed (for in game time control - When this reaches the speedup value then the ball will speed up
Global StartTime% = MilliSecs() / 1000 ; Used as seed for all time calculations (/     0 to make it seconds)
;Global BallSpeed% = 8 ; How fast the ball will move in pixels
Global GameState = 1 ; The Current game status

; Create and load the Images
;Global TitleImage = LoadImage("Images\Logo-Title.bmp")
Global PaddleImage = CreateImage(30, 100) ; Creates a blank 30 by 100 image
Global BallImage = CreateImage(20, 20) ; Creates a blank 20 by 20 image

; Draw to the images
SetBuffer(ImageBuffer(PaddleImage)) ; Makes it so that instead of drawing to the back buffer, draw to this image - every useful if you want to make a two player game
	; set the color and fill both images
	Color 255, 255, 255
	Rect 0, 0, 30, 100
SetBuffer(ImageBuffer(BallImage))
	Color 255, 255, 255
	Rect 0, 0, 20, 20
SetBuffer(BackBuffer()) ; set the drawing buffer back to the back buffer

; Mask the images
;MaskImage TitleImage, 255, 0, 255

; Load the sounds
;Global Beep1 = LoadSound("Sounds\Beep1.mp3")
;Global Beep2 = LoadSound("Sounds\Beep2.mp3")

; Setup thy font
Global GMFont = LoadFont("Arial", 64, True)
Global SmallFont = LoadFont("Arial", 24, True)
SetFont GMFont

; Main Loop
While Not GameState = 0
	Select GameState
		
		Case 1:
			; Rendering
						
			; Draw Title
			;DrawImage TitleImage, 400, 250, 0
			
			; Message
			SetFont GMFont
			Text 400, 200, "B.A.P", True
			Text 400, 500, "PRESS SPACE TO START", True
			SetFont SmallFont
			Text 400, 250, "Created by Stefan J Lulham",True
			SetFont GMFont
			
			; Processing
			
			; if ESC was hit then goto next screen
			If KeyHit(57) Then GameState = 2
			
		Case 2:
			; Rendering
			
			; Draw paddles (Set the color)
			Color 255, 255, 255
			
			; Player 1
			DrawImage PaddleImage, Player1X, Player1Y
			; Player 2
			DrawImage PaddleImage, Player2X, Player2Y
			
			; Draw the ball
			For ball.ball = Each ball 
			DrawImage BallImage, ball\BallX, ball\BallY
			Next
			
			; Draw scores
			Text 200, 10, Player1Score, True, False
			Text 600, 10, Player2Score, True, False
			
			; Processing
			If SecsPassed < 4 Then
				; Update the timer
				Select SecsPassed
					; Display some text upon how long has passed
					Case 1:
						Text 400, 10, "Ready", True, False
					Case 2:
						Text 400, 10, "Steady", True, False
					Case 3:
						Text 400, 10, "Go!", True, False
				End Select
				SecsPassed = (MilliSecs() / 1000) - StartTime
				FlushKeys() ; resets intput so anything that was pressed during this time it won't be carried out latter ( Get Rid of this line then press ESC while it is displaying Ready Steady go and you'll see what i mean )
			Else
				; Controlls
				
				; Player 1

				If KeyDown(200) Then ; UP
					Player1Y = Player1Y - PLMOVESPEED
					If Player1Y - 50 < 0 Then Player1Y = Player1Y + PLMOVESPEED ; Go back if out of bounds
				ElseIf KeyDown(208) ; DOWN
					Player1Y = Player1Y + PLMOVESPEED
					If Player1Y + 50 > 600 Then Player1Y = Player1Y - PLMOVESPEED
				EndIf
				
				; Player 2
				If KeyDown(17) Then ; UP (W)
					Player2Y = Player2Y - PLMOVESPEED
					If Player2Y - 50 < 0 Then Player2Y = Player2Y + PLMOVESPEED
				ElseIf KeyDown(31) ; DOWN (S)
					Player2Y = Player2Y + PLMOVESPEED
					If Player2Y + 50 > 600 Then Player2Y = Player2Y - PLMOVESPEED
				EndIf
				
				; Update the ball
				k = 0
				For ball.ball = Each ball
				If ball <> Null Then k = k +1
				Select ball\BallDirection
				
					Case 0:
						; Move the ball forweds
						ball\BallX = ball\BallX - (Cos(ball\BallAngle) * ball\ballspeed)
						ball\BallY = ball\BallY + (Sin(ball\BallAngle) * ball\ballspeed)
					
					Case 1:
						; Move the ball backwards
						ball\BallX = ball\BallX - (Cos(ball\BallAngle) * ball\BallSpeed)
						ball\BallY = ball\BallY - (Sin(ball\BallAngle) * ball\BallSpeed)
						
				End Select
				
				; Cball\heck out of bounds
				If ball\BallX - 15 < 0 Then
					Delete ball 
					
					; Player 2 Scored
					Player2Score = Player2Score + 1
					Goto g5
				EndIf
				
				If ball\BallX + 15 > 800 Then
					Delete ball 
					
					; Player 1 Scored
					player1score = Player1Score + 1
					Goto g5
				EndIf
				
				If ball\BallY# - 15 < 0 Then
				;ball\bally# = 1
					Select ball\BallDirection
						Case 0: 
							ball\BallDirection = 1 ; Back
						Case 1:
							ball\BallDirection = 0 ; Forweds
					End Select
					;PlaySound Beep1
				EndIf
				
				If ball\BallY# + 15 > 600 Then
					Select ball\BallDirection
						Case 0: 
							ball\BallDirection = 1
						Case 1:
							ball\BallDirection = 0
					End Select
					;PlaySound Beep1
				EndIf
				
				; Check Player Collision
				If ImagesCollide(BallImage, ball\BallX, ball\BallY, 0, PaddleImage, Player1X, Player1Y, 0) Then
					ball\BallAngle = Rand(150, 210)
					ball\ballx# = 80
					;PlaySound Beep2
					For i = 0 To MakeBalls
					ball2.ball = New ball
					ball2\ballx# = ball\ballx#
					ball2\bally# = ball\bally#
					ball2\balldirection = ball\balldirection
					ball2\ballangle# = Rand(150,210)
					ball2\ballspeed = Rand(2,14)
					Next
				ElseIf ImagesCollide(BallImage, ball\BallX, ball\BallY, 0, PaddleImage, Player2X, Player2Y, 0) Then
					ball\BallAngle = Rand(-30, 30)
					ball\ballx# = 720
					PlaySound Beep2
					For i = 0 To MakeBalls
					ball2.ball = New ball
					ball2\ballx# = ball\ballx#
					ball2\bally# = ball\bally#
					ball2\balldirection = ball\balldirection
					ball2\ballangle# = Rand(-30,30)
					ball2\ballspeed = Rand(2,14)
					Next
				EndIf
				
				.g5
				Next
				If k = 0 Then 
				
				setup()
				End If
				
				; Update the ingame timer
				GameSecsPassed = (MilliSecs() / 1000) - StartTime
				If GameSecsPassed = SPEEDUP Then
					; increase ball speed
					For ball.ball = Each ball
					ball\BallSpeed = ball\BallSpeed + 1
					Next
					GameSecsPassed = 0
					StartTime = MilliSecs() / 1000
				EndIf
				
				If KeyHit(1) Then
					; If ESC Key was pressed go to a different state and stop rendering and processing in game
					StartTime = MilliSecs() / 1000
					SecsPassed = 0
					GameState = 3
				EndIf
			EndIf
		
		Case 3:
			SecsPassed = (MilliSecs() / 1000) - StartTime
			If SecsPassed > 2 Then GameState = 0
			
			; Rendering
			
			; Draw paddles (Set the color)
			Color 255, 255, 255
			
			; Player 1
			DrawImage PaddleImage, Player1X, Player1Y
			; Player 2
			DrawImage PaddleImage, Player2X, Player2Y
			
			; Draw the ball
			DrawImage BallImage, BallX, BallY
			
			; Draw scores
			Text 200, 10, Player1Score, True, False
			Text 600, 10, Player2Score, True, False
			
			; Display who won
			If Player1Score > Player2Score Then
				Winner% = 1
			ElseIf Player2Score > Player1Score Then
				Winner% = 2
			Else
				Winner% = 3
			EndIf
			
			Color 155, 155, 155
			If Not Winner = 3 Then
				Text 400, 300, "Player " + Winner + " Wins", True, True
			Else
				Text 400, 300, "DRAW!", True, True
			EndIf

	End Select
	
	Flip:Cls
Wend: End

; Function
Function Setup()
	; Reset some startup values for another set
	ball.ball = New ball
ball\ballx# = 400 
ball\bally#= 300
ball\ballangle# = 0
ball\balldirection = 0 
	ball\ballspeed = 8
	ball\ballDirection = Rand(0, 1)
	
	StartDelay = 100
	GameSecsPassed = 0
	SecsPassed = 0
	StartTime = MilliSecs() / 1000
	Player1Y = 300
	Player2Y = 300
	Player1X = 50
	Player2X = 750
End Function


comments please.

I sould be addeding AI Soon so any ideas on how todo this would be apprecited...

thx in advance

Pong, eh? I started with Pong. Been so long, though. How are you learning Blitz? Through a book?

No i just lernt it all through the net tutorials and the help files

i get an infinite number of balls rebounding.. >_<

Change makes balls to lower number

it changes how manys balls are made when you hit one.