forums

TNT Basic Forums > Programming Q&A
Can't get modified PONG to run (Warning;Long, source included)
< Last Thread     Next Thread >
Author
Thread        Post A Reply

12-17-2001 07:05

Posted by:
Anode

Click Here to Email Anode   Find more posts by Anode

Hello everyone,

I am having some problems getting my modified PONG to run. I tried modification tip number one (let players play multiple games and keep score,) First I tried running the game as a procedure and running it again if the players pressed Enter at the end, but that wouldn't run so I modified it to a GoTo statement. I added 2 input sources 1 for Enter Key and 4 for Esc key (making players 1 input 2 and player 2 input 3) as to avoid deactivating input.

All of the other modifications I put in (higher res, "Ready" screen) work. Once I did this and corrrected syntax errors, it tries to run (opens TNT Basic etc.) but doesn't do anything. I'm very new to programming so I'm hoping somebody here can help me out by taking a look at the source. Sorry if it's messy ;>


'readies input devices
load input 128
edit input
activate input

'self explanatory
graphics mode 800,600
hide mouse


'declare variables
int input ' used for value of input from keyboard/mouse etc
float ballx=400,bally=300 ' the x and y position of the ball
float ballxvel,ballyvel=random(-4,4) ' the x and y velocities of the ball
int winner=0 ' the winner of the game, 0 means no winner
float player1y=100,player2y=300 ' the y coords of the bats, 320 is middle of y screen
int enter=0 'is enter key pressed
int Player1score=0, Player2score=0
int esc=0 'is esc pressed
int downkey=0
'set ball's initial x speed
if random (0,1) = 1
ballxvel=4
else
ballxvel=-4
end if
'make a ready screen

set pen color white
draw text 200,100,"Press Enter When Ready!"
draw frame

while enter=0
enter=poll input(1)
wend

Game:
'draw blue paddle
set pen color blue
fill rect 5,0 to 10,50
set pen color white
fill rect 0,0 to 5,50
'copy image into image bank as image 0
copy image 0,0,10,50, -1, true to 0

' draw red paddle
set pen colour red
fill rect 0,0 to 5,50
set pen colour white
fill rect 5,0 to 10,50
copy image 0,0,10,50,-1,true to 1

' draw ball
set pen colour green
fill oval 50,50 to 60,60
copy image 50,50,60,60,black,true to 2
'hotspot in center
set image offsets 2,5,5
set pen colour black
paint canvas
DrawStarfield()
set pen colour white
frame rect 0,0 to 800,600

'the actual loop

repeat
'draw the paddle and ball sprites
sprite 0,790,player2y,0
sprite 1,0,player1y,1
sprite 2,ballx,bally,2
draw frame

' bounce the ball off the floor and ceiling
if bally<=5 or bally>=595
' move the ball away from what it just hit
bally=bally-ballyvel
' then reverse the direction
ballyvel=-ballyvel
end if

'if sprite 2 has collided with sprite 1 or sprite 0, pit the value of the collided sprite in variable hitbat
int hitbat=sprite col (2,0 to 1)

'if no collision hitbat=-1
if hitbat>-1
' move the ball away from the bat it just hit
ballx=ballx-ballxvel
' reverse the balls direction
ballxvel=-ballxvel

' change the yvelocity of the ball according to where on the bat the ball hit
' if it hit near the top of the bat then make the ball move upwards faster, if
' it hit near the bottom of the bat then make the ball move downwards faster,
' and if it hits in the middle of the bat simply reflect it without changing the
' yvelocity
float batcentre
if hitbat=0 then batcentre=player2y+25 ' bat height is 50 so + 25 gives the middle of the bat
if hitbat=1 then batcentre=player1y+25

ballyvel=ballyvel+(bally-batcentre)*0.1
end if

if ballx<=5 then winner=2
if ballx>=795 then winner=1

' handle player movement
input=poll input (2) ' get player 1 input
player1y=player1y+input/20
if player1y<0 then player1y=0
if player1y>590 then player1y=590

input=poll input (3) ' get player 2 input
player2y=player2y+input/20
if player2y<0 then player2y=0
if player2y>590 then player2y=590


' move the ball
ballx=ballx+ballxvel
bally=bally+ballyvel


until winner > 0

' print who won on the screen
if winner = 1
player1score=player1score+1
set pen colour red
draw text 0,100,"PLAYER 1 WINS!SCORE: Player 1= " + As String(player1score) + " Player 2= " + As String(player2score) + ". Press Enter to play again, press Esc to exit."
else
set pen colour blue
draw text 400,100,"PLAYER 2 WINS!SCORE: Player 1= " + As String(player1score) + " Player 2= " + As String(player2score) + ". Press Enter to play again, press Esc to exit."
draw frame
while downkey=0
enter=poll input(1)
esc=poll input(4)
if enter> 0 then downkey=1
if esc>0 then downkey=2
wend

if downkey=1 then goto Game
if downkey=2 then end

'PROCEDURES
procedure DrawStarfield()
int star

for star=1 to 100
set pixel colour random(0,800),random(0,640),random(black,white)
next star
end proc

12-18-2001 22:55

Posted by:
Mark Tully

Find more posts by Mark Tully

Missing "end if"

You're missing an "end if" on line 134, that's around:

set pen colour blue
draw text 400,100,"PLAYER 2 WINS!SCORE: Player 1= " + As String(player1score) + " Player 2= " + As String(player2score) + ". Press Enter to play again, press Esc to exit."
<< insert end if here >>
draw frame

Unfortunately, there's a bug in TNT Basic where if you miss an "end if" out of your program it doesn't run and it doesn't report an error. This also applies to loops, such as while, repeat and for. Sorry about that, we'll have it fixed soon!

Mark

12-19-2001 01:25

Posted by:
Anode

Click Here to Email Anode   Find more posts by Anode

Thanks so much for your reply Mark. Now that that problem's out of the way, I can work on the rest of the inefficient coding.

Through my couple days of reading the board, I have noticed that undocumented bugs are the reason many people have questions. Perhaps incuding a list of known bugs in the download would kill a lot of duplicate forum traffic. Just a suggestion of course ;>

Anode

All times are GMT        Post A Reply

Forum Jump:
< Last Thread     Next Thread >

< Contact Us - TNT Basic >

Powered by: vBulletin Lite Version 1.0.1 Lite
Copyright © Jelsoft Enterprises Limited 2000.