TGC Codebase Backup



Pong by Rburke

3rd Oct 2006 2:09
Summary

Pong Classic v1.1 with computer AI.



Description

Added A title screen with the option to play against the computer. Fixed a bug where the ball would lock on the floor and ceiling randomly. Use the mouse to move Player 1 and the Up/Down arrow keys to move player two. This is the first time I've programmed artificial intelligence. It's simple but works as it should.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    `////////////////////////
`/ Pong v1.1/////////////
`/ Author: Ryan Burke ///
`/ October 2, 2006 //////
`////////////////////////

Set Display Mode 640,480,32

Sync On
Hide Mouse
Randomize 10

ink RGB(255,255,255),0

rem Initial Values

p2tY = 100 ; p2bY = 180
slope = RND(3)
if slope = 0 Then slope = 1
speed = 3
selectY = 307
game = 1

rem Initial Ball Location
bx1 = 640/2 ; bx2 = 640/2 + 10
by1 = 480/2 ; by2 = 480/2 + 10

rem Ball Reset Point
sbx1 = 640/2 ; sbx2 = 640/2 + 10
sby1 = 480/2 ; sby2 = 480/2 + 10

rem Load Bitmaps
load bitmap "select.bmp"
load bitmap "title.bmp",1
set current bitmap 0
get image 1,1,1,12,14
copy bitmap 1,0
paste image 1,225,307

rem selection 1 is 1 Player ; selection 2 is 2 Players
rem TITLE SCREEN
repeat
copy bitmap 1,0
	if UPKEY()
		game = 1
		selectY = 307
	endif
	if DOWNKEY()
		game = 2
		selectY = 333
	endif
	paste image 1,225,selectY
	sync
	cls
until RETURNKEY()

cls

rem MAIN LOOP
do

	rem Set Player 1 Movement Restriction and Location
	if MouseY() < 1 Then p1tY = 1
	if MouseY() < 1 Then p1bY = 61
	if MouseY() > 10 Then p1tY = MouseY()
	if MouseY() > 390 Then p1bY = 470
	if MouseY() > 390 Then p1tY = 395
	if MouseY() < 390 Then p1bY = MouseY() + 80 
	box 50, p1tY, 70, p1bY

	rem Set Player 2, Computer Movement Restriction and Location
	if p2tY < 15 Then p2tY = 15
	if p2tY = 15 Then p2bY = 95
	if p2tY > 380 Then p2bY = 460
	if p2tY > 380 Then p2tY = 380
	
	rem Artifical Intelligence [Game Option 1]
	if game = 1
		if p2tY < by1
			p2tY = p2tY + 10
			p2bY = p2bY + 10
		endif
		if p2bY > by2
			p2tY = p2tY - 10
			p2bY = p2bY - 10
		endif
	endif
	
	rem Player two keys work only if the second game option was chosen
	if game = 2
		if UPKEY() Then p2tY = p2tY - 15
		if UPKEY() Then p2bY = p2bY - 15
		if DOWNKEY() Then p2tY = p2tY + 15
		if DOWNKEY() Then p2bY = p2bY + 15
	endif
		box 569,p2tY,589,p2bY

	rem Randomize Slope and Direction at beginning of Round
	while chance = 1
		Direction = RND(1)
		speed = 3
		slope = RND(3)
		if slope = 0 then slope = slope + 1
		chance = 0
	endwhile

	if Direction = 1
		bx1 = bx1 + speed; bx2 = bx2 + speed; by1 = by1 + slope; by2 = by2 + slope
	endif	
	
	if Direction = 0
		bx1 = bx1 - speed; bx2 = bx2 - speed; by1 = by1 + slope; by2 = by2 + slope
	endif

	rem If Player 2 Scores Reset Ball
	if bx1 < 30
		bx1 = sbx1; bx2 = sbx2; by1 = sby1; by2 = sby2
		chance = 1
		p2score = p2score + 1
	endif

	rem If Player 1 Scores Reset Ball
	if bx1 > 640
		bx1 = sbx1; bx2 = sbx2; by1 = sby1; by2 = sby2
		chance = 1	
		p1score = p1score + 1	
	endif

	rem Ball Collision With Floor and Ceiling
	if by2 > 475
		by2 = 475 ; by1 = 465
		slope = slope - slope - slope
	endif
	if by1 < 5
		by1 = 5 ; by2 = 15
		slope = slope - slope - slope
	endif

	rem Ball Collision With Player 1
	if bx1 < 70
		if by1 > p1tY and by1 < p1bY
			Direction = 1
			slope = RND(4)
			chance2 = RND(1)
			if chance2 = 1
				slope = slope - slope - slope
			endif
			if chance2 = 0
				slope = slope + slope
			endif
			speed = speed + 1		
		endif
	endif

	rem Ball Collision With Player 2, Computer
	if bx1 > 555
		if by1 > p2tY and by1 < p2bY
			Direction = 0
			slope = RND(4)
			chance2 = RND(1)
			if chance2 = 1
				slope = slope - slope - slope
			endif
			if chance2 = 0
				slope = slope + slope
			endif	
			speed = speed + 1
		endif
	endif

	rem Ball Position : Direction 1 is Right, 0 is Left
	box bx1,by1,bx2,by2

	rem Display Score
	Set Cursor 10,10
	ink RGB(255,0,0),1
	Print "Player 1:  ";p1score
	Set Cursor 500,10
	ink RGB(0,0,255),1
	Print "Player 2:  ";p2score	
	ink RGB(255,255,255),1

	rem Update
	sync
	cls

loop