2 PLayer Pong
This is a quick port of an old DB mini game example, for those interesting in having a go at Clever Coders challenge 15 (http://www.underwaredesign.com/forums/index.php?topic=2767.0).
[pbcode]
` *=---------------------------------------------------------------------=*
`
` >> 2 Player Simple Pong V0.01 <<
`
` By Kevin Picone
`
` 28th, May, 2002
`
` (c) Copyright 2002 Kevin Picone, All Rights Reserved.
`
` *=---------------------------------------------------------------------=*
`
`
` So WHAT does this do ?:
` =======================
`
`
` This mini game re-creates a basic versions of one the first arcade games
` "pong". A good learning exercise for new programmers to dig through
` it and see how a full mini game works. This one includes a basic intro
` the game and game over screen. Their pretty ugly to look at, so see
` if you can improve them..
`
` Well, good luck !
`
`
`
` Known bugs:
` ===========
`
` The ball rebound and get stuck on the player bats.. Can you fix it :)
`
` *=---------------------------------------------------------------------=*
setFps 60
` Create Some GAME STATE Variables to make our code more readible
` These are used to allow us to easily determine what part of the
` games code to run, The INTRO, THE GAME, or the GAME OVER screen
`
acset =1
Constant GameState_Intro_FadeIn =ac(1)
Constant GameState_Intro_HitKey =ac(1)
Constant GameState_Intro_faDeOut =ac(1)
Constant GameState_Game_in_play =ac(1)
Constant GameState_GameOver =ac(1)
` Now Set The GAMESTATE variable to INTRO mode
GameState=GameState_Intro_FadeIn
Intro_Text_colour =Rgb(250,25,150)
Intro_Fade_Percent# =0
` *=---------------------------------------------------------------------=*
` *=---------------------------------------------------------------------=*
` *=---------------------------------------------------------------------=*
do
cls 0
` -------------------
` fade the intro IN
` -------------------
if GameState=GameState_intro_fadeIn
ink RgbFade(Intro_Text_colour,Intro_Fade_Percent#)
Display_intro_text()
Intro_Fade_Percent#=Intro_Fade_Percent#+2
if Intro_Fade_Percent#>100
` When the fade is complete set game mode to HIT KEY
GameState=GameState_intro_hitKey
endif
Endif
` ----------------------------
` fade the intro OUT to Black
` -----------------------------
if GameState=GameState_intro_fadeout
ink RgbFade(Intro_Text_colour,Intro_Fade_Percent#)
Display_intro_text()
Intro_Fade_Percent#=Intro_Fade_Percent#-2
if Intro_Fade_Percent#<0
`once the fade out is done now lets - Start the game
GameState=GameState_Game_in_play
endif
Endif
` ---------------------------------------
` IF Game State = Game State Intro then Were displaying the intro titles
` ---------------------------------------
if GameState=GameState_intro_hitKey
ink Intro_Text_colour
Display_intro_text()
if inkey$()<>""
` Fade out the intro
GameState=GameState_intro_fadeout
` Init the players positions
Gosub Init_player_positions
` Init The ball Speed
BallXspeed#=Get_Random_Ball_Speed(-5,5)
BallYspeed#=Get_Random_Ball_Speed(-4,4)
` int the Balls starting position
BallX#=320
BallY#=240
endif
endif
` ---------------------------------------
` IF Game State = Game State GAME OVER then Were displaying game over
` screen
` ---------------------------------------
If GameState=GameState_Game_Over
ink GameOver_Colour
centerText GetScreenWidth()/2,GetScreenHeight()*0.4,"Game Over"
dec GameOver_ShowFrames
if GameOver_ShowFrames<0
` Set Game back to intro mode
GameState=GameState_Intro_FadeIn
` Reste The gameover_frame counter for next time
Gosub Set_Display_game_over_Frames
endif
Endif
` -----------------------------------------------------
` IF Game STATE = GAME IN PLAY then the game is in PLAY
` ----------------------------------------------------------
if GameState=GameState_Game_in_play
CenterX=GetSCreenWidth()/2
;ScorePos=
` Player #1
BallxSpeed#=controls(0,30,44,10,10,50,CenterX/2,Ballx#,Bally#,BallXspeed#)
` Player #2
Ballxspeed#=controls(1,200,208,GetScreenWidth()-20,10,50,CenterX+(CenterX/2),Ballx#,Bally#,BallXspeed#)
` Check if the Ball the top or bottom of screen, if so, rebound it
` -----------------------------------------------------------------
if bally#<10 or Bally#>(GetScreenHeight()-10) then BallySpeed#=BallySpeed#*-1
ballx#=ballx#+BallXspeed#
bally#=bally#+Ballyspeed#
` Check if Ball Passed Either player
` -----------------------------------
if ballx#<-20 or BallX#>(GetScreenWidth()+20)
GameState=GameState_GAME_OVER
Gosub Set_Display_game_over_Frames
endif
` ----------------
` draw the Ball
` ---------------
CircleC ballx#,Bally#,3,true,rgb(0,255,0)
Endif
sync
loop
` *=---------------------------------------------------------------------=*
` Display The Title Screen Information
` *=---------------------------------------------------------------------=*
Function Display_intro_text()
Y=100
x=getscreenwidth()/2
centerText x,y,"2 P l a y e r P o n g "
y=y+20
centerText x,y,"By: Kevin Picone"
y=y+40
centerText x,y,"Controls:"
y=y+30
centerText x,y," Player 1 Keys: A=up ,Z=Down)"
y=y+20
centerText x,y," Player 2 Keys: UpArrow=up ,DownArrow=Down)"
y=y+30
centerText x,y,"(c) Copyright 2002 Kevin Picone, Underware design"
y=y+20
centerText x,y+50,"Press Any Key To PLay"
EndFunction
` *=---------------------------------------------------------------------=*
` Set up The Game Over Variables
` *=---------------------------------------------------------------------=*
Set_Display_game_over_Frames:
GameOver_Colour=Rgb(100,255,200)
GameOver_ShowFrames=100
return
` *=---------------------------------------------------------------------=*
` Init Players
` *=---------------------------------------------------------------------=*
Init_player_positions:
` Init Both Players
Dim Players_Ypos(2)
Dim Players_Score(2)
Dim Players_Colour(2)
Ypos=GetScreenHeight()/2
players_ypos(0)=Ypos
Players_Colour(0)=rgb(255,0,0)
players_ypos(1)=Ypos
Players_Colour(1)=rgb(255,0,255)
return
` *=---------------------------------------------------------------------=*
` Control Players
` *=---------------------------------------------------------------------=*
`
` The Same routine is used to control both players.
`
` This routine also changes the direction of the Ball when a collision
` occurs.
`
`
` *=---------------------------------------------------------------------=*
Function controls(player,up_key,Down_key,Xpos,width,height,Scorexpos,Ballx#,Bally#,BallXspeed#)
` Control The player
if keystate(up_key)=1 and players_ypos(player)>0 then players_ypos(player)=players_ypos(player)-5
if keystate(Down_key)=1 and players_ypos(player)<(GetScreenHeight()-height) then players_ypos(player)=players_ypos(player)+5
` draw the players Bat
` ----------------------
BoxC xpos,players_ypos(player),xpos+width,players_ypos(player)+height,true,Players_Colour(player)
` ----------------------------------------
` Handle Collision With the Ball and bat
` ----------------------------------------
if (players_ypos(player)+height)>BallY# and players_ypos(player)<(BallY#+5)
if (xpos+width)>BallX# and xpos<(BallX#+5)
` If the bat hit the Ball then reflex the ballas Direction
BallXspeed#=BallXspeed#*-1
` Add soem score to the player
players_score(player)=players_score(player)+5
endif
Endif
ink $ffffff
Centertext scorexpos,0,"Player "+str$(player+1)+" Score:"+Digits$(players_score(player),8)
Endfunction BallxSpeed#
Function Get_Random_Ball_Speed(low,top)
d=top-low
repeat
speed=low+rnd(d)
until abs(speed)>2
endFunction Speed
[/pbcode]