TGC Codebase Backup



Advanced A.I. P.O.N.G! by NanoBrain

12th Jul 2004 12:15
Summary

A P.O.N.G! game with advanced a.i. The a.i. mimics the way a person plays, and becomes more skilled as the game progresses. Try making it to level 10.



Description

/////////////////////////////////////
/ Code created by Chris Martindal /
/ Share the code with others /
/ Credit to me is unecessary /
/////////////////////////////////////

The display mode is set to 800,600,32.
Do not change the disply mode, for it
will corrupt the gameplay. Everything
in the code is set for this display
mode. I will have an updated version
soon, which allows for different display
modes, in which the player can choose
from.

I created this game as a test and a teacher
on a.i. Feel free to edit the code and
redistribute it. Email me at techi@easy.com
with newer versions of the source code.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    randomize 10
set display mode 800,600,32
sync on:sync rate 60

hide mouse

REM load sounds:
load sound "start.wav",1
load sound "count.wav",2
load sound "cheers.wav",3
load sound "bullet.wav",4
load sound "laugh.wav",5
load sound "levelup.wav",6
load sound "looser.wav",7
load sound "ponghit1.wav",8
load sound "ponghit2.wav",9


tryagain:
sync
cls

REM main start screen:
start$ = "press s to start"
pong$ = "P.O.N.G.!"
fade = 0
repeat
	set text size 80
	set text to bold
	ink rgb(255,255,255),0
	center text 400,200,pong$
	set text size 15
	set text to normal
	if fade = 0 then fadedir = 1
	if fade = 255 then fadedir = 0
	if fadedir = 1 then fade = fade + 5
	if fadedir = 0 then fade = fade - 5	
	ink rgb(fade,fade,fade),0
	center text 400,350,start$
	sync
	cls
until scancode() = 31

REM fade out main start screen:
play sound 1
fade = 255
repeat
	ink rgb(fade,fade,fade),0
	set text size 80
	set text to bold
	center text 400,200,pong$
	set text size 15
	set text to normal
	center text 400,350,start$
	fade = fade - 5
	sync
	cls
until fade = 0
REM end of main start screen:

fade = 0
fadedir = 0

wait 2000

REM create player sprite:
ink rgb(255,255,255),0
box 0,0,5,40
get image 1,0,0,40,40
cls
sprite 1,0,0,1

REM create computer sprite:
box 0,0,5,40
get image 2,0,0,40,40
cls
sprite 2,0,0,2

REM create ball sprite:
load image "ball.bmp",3

REM show sprites:
show sprite 1
show sprite 2

REM reset game variables:
level = 1
comspeed = 4
ballspeed = 6
compoint = 0
playpoint = 0

REM start of game screen:
start:
sync

REM print level number to screen:
set cursor 375,50
print "Level ",level

REM print game scores:
set cursor 50,50
print "Player = ",playpoint
set cursor 700,50
print "Computer = ",compoint

REM set screen boundaries:
line 0,100,800,100:line 0,500,800,500

REM position player sprite:
xplayer = 40:yplayer = 290
sprite 1,xplayer,yplayer,1
REM position computer sprite:
xcomputer = 755:ycomputer = 290
sprite 2,xcomputer,ycomputer,2

wait 1000

REM game start countdown:
3$ = "3":2$ = "2":1$ = "1"
play sound 2
count = 0
repeat 
	ink rgb(255,255,255),0
	set cursor 375,50
	print "Level ",level
	set cursor 50,50
	print "Player = ",playpoint
	set cursor 700,50
	print "Computer = ",compoint
	ink rgb(255,255,255),0
	line 0,100,800,100:line 0,500,800,500
	count = count + 1
	ink rgb(0,0,200),0
	set text size 50
	set text to bold
	center text 400,300,3$
	ink rgb(255,255,255),0
	set text to normal
	set text size 35
	center text 400,308,3$
	sync
	cls
until count = 20
count = 0
play sound 2
repeat
	ink rgb(255,255,255),0
	set cursor 375,50
	print "Level ",level
	set cursor 50,50
	print "Player = ",playpoint
	set cursor 700,50
	print "Computer = ",compoint
	ink rgb(255,255,255),0
	line 0,100,800,100:line 0,500,800,500
	count = count + 1
	ink rgb(0,0,200),0
	set text size 50
	set text to bold
	center text 400,300,2$
	ink rgb(255,255,255),0
	set text to normal
	set text size 35
	center text 400,308,2$
	sync
	cls
until count = 20
count = 0
play sound 2
repeat 
	ink rgb(255,255,255),0
	set cursor 375,50
	print "Level ",level
	set cursor 50,50
	print "Player = ",playpoint
	set cursor 700,50
	print "Computer = ",compoint
	ink rgb(255,255,255),0
	line 0,100,800,100:line 0,500,800,500
	count = count + 1
	ink rgb(0,0,200),0
	set text size 50
	set text to bold
	center text 400,300,1$
	ink rgb(255,255,255),0
	set text to normal
	set text size 35
	center text 400,308,1$
	sync
	cls
until count = 20
count = 0

REM position and direct ball sprite:
spot = rnd(3)
if spot = 0
	xball = 50
	yball = 100
	ballydir = 1
	ballxdir = 1
	change = 1
endif
if spot = 1
	xball = 750
	yball = 100
	ballydir = 1
	ballxdir = 0
	change = 0
endif
if spot = 2
	xball = 50
	yball = 500
	ballydir = 0
	ballxdir = 1
	change = 1
endif
if spot = 3
	xball = 750
	yball = 500
	ballydir = 0
	ballxdir = 0
	change = 0
endif

REM to catch player and computer sprites up to ball sprite speed:
if level => 9 then sight = sight + 10
if level => 11 then pspeed = pspeed + 1

randompos = rnd(4)

REM --------Main game loop--------:
do

REM update sprites:
sprite 1,xplayer,yplayer,1
sprite 2,xcomputer,ycomputer,2
sprite 3,xball,yball,3

REM counter for computer random positioning:
randomcount = randomcount + 1
if randomcount = 100
	randomcount = 0
	randompos = rnd(4)
	rspeed = rnd(6)
endif

REM print player scores and level number to screen:
ink rgb(255,255,255),0
set cursor 375,50
print "Level ",level
set cursor 50,50
print "Player = ",playpoint
set cursor 700,50
print "Computer = ",compoint

REM random timer counter:
rancount = rancount + 1
if rancount = 75
	random = rnd(8)
	rancount = 0
endif

REM top and bottom lines:
ink rgb(255,255,255),0
line 0,100,800,100:line 0,500,800,500

REM ball sprite coordinates:
xball = sprite x(3):yball = sprite y(3)
xxball = sprite x(3) + 10:yyball = sprite y(3) + 10

REM move player sprite with arrow keys:
xplayer = 40
if upkey() = 1 then dec yplayer,8 + pspeed
if downkey() = 1 then inc yplayer,8 + pspeed
REM reposition player if out of bounds:
if yplayer < 100 then yplayer = 100
if yplayer + 40 > 500 then yplayer = 460

REM reposition computer sprite if out of bounds:
if ycomputer < 100 then ycomputer = 100
if ycomputer + 40 > 500 then ycomputer = 460
REM settle computer sprite if ball on other side of court:
if change = 0
	if randompos = 0
		if ycomputer < 290 then inc ycomputer,rspeed
		if ycomputer > 290 then dec ycomputer,rspeed
   endif
	if randompos = 1
		if ycomputer < 220 then inc ycomputer,rspeed
		if ycomputer > 220 then dec ycomputer,rspeed
	endif
	if randompos = 2
		if ycomputer < 150 then inc ycomputer,rspeed
		if ycomputer > 150 then dec ycomputer,rspeed
	endif
	if randompos = 3
		if ycomputer < 350 then inc ycomputer,rspeed
		if ycomputer > 350 then dec ycomputer,rspeed
	endif
	if randompos = 4 
		if ycomputer < 430 then inc ycomputer,rspeed
		if ycomputer > 430 then dec ycomputer,rspeed
	endif
endif
REM move computer sprite to hit ball:
if change = 1 and xball + 5 > 475 - sight
	if sprite y(3) < ycomputer then dec ycomputer,comspeed + random + helper
	if sprite y(3) + 10 > ycomputer + 40 then inc ycomputer,comspeed + random + helper
endif

REM ball sprite direction shift:
REM if ball sprite goes out of bounds:
if xball < 0 
	play sound 5
	compoint = compoint + 1
	delete sprite 3
	if compoint = 3
		cls
		hide sprite 1
		hide sprite 2
		hide sprite 3
		wait 2000
		looser(level)
		goto tryagain
	endif
	sync
	cls
	goto start
endif
if xxball > 800
	play sound 3
	playpoint = playpoint + 1
	delete sprite 3
	if playpoint = 3
		delete sprite 3
		level = level + 1
		compoint = 0
		play sound 6
		playpoint = 0
		comspeed = comspeed + 1
		ballspeed = ballspeed + 1
	endif
	sync
	cls
	goto start
endif
REM if ball sprite hits top or bottom walls:
if yball < 100 
	ballydir = 1
	play sound 8
endif
if yyball > 500
	ballydir = 0
	play sound 8
endif
REM if ball sprite hits player sprite:
if xball < 45 and xball > 25 and yball => sprite y(1) and yball =< sprite y(1) + 40
	if sprite y(1) > 100 and sprite y(1) < 500
		if upkey() = 1 and sprite y(1) > 100 
			change = 1
			force = 4
			ballydir = 0
			ballxdir = 1
			play sound 4
		endif
		if downkey() = 1 and sprite y(1) + 40 < 500
			change = 1
			force = 4
			ballydir = 1
			ballxdir = 1
			play sound 4
		endif
	endif
	if sprite y(1) => 100 and sprite y(1) =< 500 and upkey() = 0 and downkey() = 0
		change = 1
		ballxdir = 1
		play sound 9
	endif
	if sprite y(1) => 100 and sprite y(1) =< 500 and upkey() = 1 or downkey() = 1
		change = 1
		ballxdir = 1
		play sound 9
	endif
endif
if xball < 45 and xball > 25 and yyball => sprite y(1) and yyball =< sprite y(1) + 40
	if sprite y(1) > 100 and sprite y(1) < 500
		if upkey() = 1 and sprite y(1) > 100
			change = 1
			force = 4
			ballydir = 0
			ballxdir = 1
			play sound 4
		endif
		if downkey() = 1 and sprite y(1) + 40 < 500
			change = 1
			force = 4
			ballydir = 1
			ballxdir = 1
			play sound 4
		endif
	endif
	if sprite y(1) => 100 and sprite y(1) =< 500 and upkey() = 0 and downkey() = 0
		change = 1
		ballxdir = 1
		play sound 9
	endif
	if sprite y(1) => 100 and sprite y(1) =< 500 and upkey() = 1 or downkey() = 1
		change = 1
		ballxdir = 1
		play sound 9
	endif
endif

REM calculate computer's current direction:
if dircount = 0
	dirrecord1 = sprite y(2)
endif
if dircount = 1
	dirrecord2 = sprite y(2)
	dircount = 0
endif
dircount = dircount + 1
if dirrecord2 < dirrecord1 then currecord = 0
if dirrecord2 > dirrecord1 then currecord = 1
if dirrecord2 = dirrecord1 then currecord = 2
REM if ball sprite hits computer sprite:
if xxball > 755 and xxball < 775 and yball => sprite y(2) and yball =< sprite y(2) + 40
	if sprite y(2) > 100 and sprite y(2) < 500
		if currecord = 0 and sprite y(2) > 100
			change = 0
			force = 4
			ballydir = 0
			ballxdir = 0
			play sound 4
		endif
		if currecord = 1 and sprite y(2) + 40 < 500
			change = 0
			force = 4
			ballydir = 1
			ballxdir = 0
			play sound 4
		endif
	endif
	if sprite y(2) => 100 and sprite y(2) =< 500 and currecord = 2
		change = 0
		ballxdir = 0
		play sound 9
	endif
	if sprite y(2) => 100 and sprite y(2) =< 500 and currecord = 0 or currecord = 1 
		change = 0
		ballxdir = 0
		play sound 9
	endif
endif
if xxball > 755 and xxball < 775 and yyball => sprite y(2) and yyball =< sprite y(2) + 40
	change = 0
	ballxdir = 0
	play sound 9
endif

REM move the ball sprite:
if ballxdir = 1 then inc xball,ballspeed + force
if ballxdir = 0 then dec xball,ballspeed + force
if ballydir = 1 then inc yball,ballspeed + force
if ballydir = 0 then dec yball,ballspeed + force

REM add the force to the ball sprite:
if force = 4
	forcecount = forcecount + 1
	helper = 1
endif
if forcecount = 60
	force = force - 1
	if force = 0
		forcecount = 0
		helper = 0
	endif
endif


sync
cls
loop

REM looser's end of game sequence:
function looser(level)
	repeat
		fadeend = fadeend + 5
		set text size 50
		set cursor 295,270
		ink rgb(fadeend,fadeend,fadeend),0
		print "You made it to level ",level
		sync
		cls
	until fadeend = 255
	if level =< 8
		play sound 7
		repeat
			counter = counter + 1
			set cursor 295,270
			ink rgb(255,255,255),0
			print "You made it to level ",level
			set cursor 331,300
			print "LOOSER!"
			sync
			cls
		until counter = 300
	else
		play sound 3
		repeat
			counter = counter + 1
			set cursor 295,270
			ink rgb(255,255,255),0
			print "You made it to level ",level
			set cursor 331,300
			print "GOOD JOB!"
			sync 
			cls
		until counter = 300
	endif
	counter = 0
endfunction