Main Menu

YA Pong Game

Started by ken tankerous, October 09, 2005, 05:08:10 PM

Previous topic - Next topic

ken tankerous

Another Pong game :P

Hopefully it will help someone. It's my first real attempt at programming
anything in PB and it was quite fun  :D

PlayBASIC Code: [Select]
; PROJECT : PbPong
; AUTHOR : Ken Tankerous
; CREATED : 10/4/2005
; EDITED : 10/9/2005
;
; ---------------------------------------------------------------------
; My version of Pong using PlayBasic - the main twist is the ball
; stays in play. Your opponent gains points if you allow the ball
; to hit the wall behind your paddle. You will also give your opponent
; more points if you do not get out of the way quick enough when the ball
; bounces off the wall behind you (the AI *tries* it's best to get out of
; the way). If the player or AI hits the ball with a moving paddle it will
; impart a bit of additional up/down movement rate to the ball, set with
; the Constant MOVEMULTIPLIER#, up to a point, at which the up/down
; movement rate is restored to the original rate. Starting movement rates
; (X and Y) are random and set by the min/max of Constants SPEEDX# and SPEEDY#.
; You can vary the match length with the Constant SCORELIMT.
;
; You can change the first 9 Constants to make the game easy or hard
; and the AI player better or useless.
;
; I purposely used sprites and sprite collisions so I could learn something
; about sprite functions in PB.
;
; I've tried to heavily comment the code with the intent of using my
; learning experience as a tutorial for others. This is my first try at
; doing something with PB.

; I'm open to suggestions/comments on things that could have been done
; better or if you would like me to clarify something.
; ---------------------------------------------------------------------

; =========================================================
; Set Global Variables
; =========================================================
Global Height = GetScreenHeight()
Global Width = GetScreenWidth()
Global MovX#, MovY#, MovYOrg#
Global LastCollision
Global LeftPaddlePos# = Width*0.10
Global RightPaddlePos# = Width*0.90

; =========================================================
; Set Game Constants
; =========================================================
; This first group of Constants effect how hard the game is
; by changing the ball speed with SPEEDX#, SPEEDY# and MOVEMULTIPLIER#
; and how good the AI player is by changing AIMOVESCALE#,
; R_REACTZONE# and L_REACTZONE#
Constant AIMOVESCALE# = 4;how fast AI moves paddle
Constant PLAYERMOVESCALE# = 8; how fast player moves paddle
Constant SPEEDX# = 7;max X movement rate for ball
Constant SPEEDY# = 4;max Y movement rate for ball & Y multiplier for paddle hit
Constant SCALEBALL = 5;ball size (small # - big ball)
Constant R_REACTZONE# = 0.60; % board length where AI player start to react
Constant L_REACTZONE# = 0.40; % board length where AI player start to react
Constant MOVEMULTIPLIER# = 1.25; multiplier for ball Y movement rate
Constant SCORELIMT = 5; game ends when a player reaches the number
;----------------------------------------------------------
Constant MANUAL = 1
Constant AUTO = 0
Constant RED = RGB(255,0,0)
Constant WHITE = RGB(255,255,255)
;----------------------------------------------------------
; Makes reading the sprite index easy
Constant BALL = 1
Constant LSIDE = 10
Constant RSIDE = 11
Constant TOP = 12
Constant BOTTOM = 13
Constant R_PADDLE = 14
Constant L_PADDLE = 15
Constant SCOREBOX = 16
;----------------------------------------------------------


; =========================================================
; Main Program Loop (can leave at anytime with Esc Key)
; =========================================================
While EscKey() = 0

; =========================================================
; Display the Main Menu
; =========================================================
Match = MainMenu()
; Match variable is returned with AI vs. AI or Human vs. AI
; if Human vs. AI then set the ReactZoneL to 100 which allows
; the human player to move paddle freely up and down any time
If Match = MANUAL
ReactZoneL# = 100
ReactZoneR# = R_REACTZONE#
Else
ReactZoneL# = L_REACTZONE#
ReactZoneR# = R_REACTZONE#
EndIf

; =========================================================
; Create Array to hold paddle Y positions used in VaryBallY()
; =========================================================
Dim PaddleMove#(3)
; =========================================================
; Create Array to hold each players score in PlayerScore(0)
; & PlayerScore(0)
; =========================================================
Dim PlayerScore(1)

; =========================================================
; Create Ball (send data to MakeSprite Function)
; =========================================================
;scale the ball size To the screen
Size=(Width-Height)/SCALEBALL
MakeSprite(Size/2,Size/2,Size/2,0,Size,Size,BALL,1,Width/2,Height/2)

; =========================================================
; Create Sides (send data to MakeSprite Function)
; =========================================================
MakeSprite(0,0,10,Height,10,Height,LSIDE,2,1,Height/2)
MakeSprite(0,0,10,Height,10,Height,RSIDE,2,Width-1,Height/2)
MakeSprite(0,0,Width,10,Width,10,TOP,3,Width/2,1)
MakeSprite(0,0,Width,10,Width,10,BOTTOM,3,Width/2,Height-5)

; =========================================================
; Create Paddles (send data to MakeSprite Function)
; =========================================================
MakeSprite(0,0,15,70,15,70,R_PADDLE,4,RightPaddlePos#,Height/2)
MakeSprite(0,0,15,70,15,70,L_PADDLE,4,LeftPaddlePos#,Height/2)

; =========================================================
; Create Score Box (send data to MakeSprite Function)
; =========================================================
MakeSprite(0,0,Width,50,200,50,SCOREBOX,0,Width/2,50)

; =========================================================
; Limit speed of the program To 60 Frames per second
; =========================================================
SetFPS 60

; =========================================================
; Get a random start movement For ball between + and - speed constants
; =========================================================
While ok = 0
MovX#= RndRange(Neg(SPEEDX#),SPEEDX#)
MovY#= RndRange(Neg(SPEEDY#),SPEEDY#)
; Speed can't be 0 For x or y direction
If MovX# = 0 Or MovY# = 0
ok = 0
Else
ok = 1
EndIf
Login required to view complete source code