@Big man, beat you to it :-P!
I went through your code, which, btw is very nice and organized, good job! I put in code for the stuff you had trouble with and tried to comment everything very well so you would understand. I also added a point counter. Here's what I got:
REM Title : Pong Basic Version 0.1
REM Author : Reality Studio
REM Date : 26/3/2006
REM VARIABLES
Bat1Y1 = 50
Bat2Y1 = 335
Bat1Speed1 = 3
Bat2Speed1 = 5 `added a computer speed, it's faster to give computer an advantage
BallX = 320
BallY = 240
BallSize = 15
BallSpeedX = 5
BallSpeedY = 5
dim points(2) `create an array to hold points. points(1) is player, points(2) is computer
REM MAIN PROGRAM
RANDOMIZE TIMER() `Don't know what the timer is for...
main_scrn()
BOX 40,Bat1Y1,55,Bat1Y1+70
BOX 585,Bat2Y1,600,Bat2Y2
REM MAIN LOOP
DO
CLS
main_scrn()
rem Got rid of the Bat1Y2 variable, because we can just add 70 to Bat1Y2 to get the same value
BOX 40,Bat1Y1,55,Bat1Y1+70
BOX 585,Bat2Y1,600,Bat2Y1+70
CIRCLE BallX,BallY,BallSize
BallX = BallX - BallSpeedX
BallY = BallY - BallSpeedY
IF BallY >= 405 THEN BallSpeedY = BallSpeedY * -1
IF BallX >= 595
rem so if the ball is past the paddle on right, add point to player
points(1) = points(1)+1
rem put ball back in middle
BallX = 320
BallY = 240
endif
IF BallY <= 45 THEN BallSpeedY = BallSpeedY * -1
IF BallX <= 45
rem so if the ball is past the paddle on right, add point to computer
points(2) = points(2)+1
rem put ball back in middle
BallX = 320
BallY = 240
endif
rem Check to see if ball hit the player's paddle
if BallY > Bat1Y1 and Bally < Bat1Y1+70 `is it in the y range of the paddle?
if BallX < 55+BallSize and BallX > 45 +BallSize `check if the ball hit the FRONT of the paddle. We added ballsize so that the edge of the ball would be checked, not the middle
rem reverse the ball speed
BallSpeedX = BallSpeedX * -1
endif
endif
rem Check if the ball hit the computer's paddle
if BallY > Bat2Y1 and Bally < Bat2Y1+70 `is it in the y range of the paddle?
if BallX > 585-BallSize and BallX < 595-BallSize `check if the ball hit the FRONT of the paddle. We added ballsize so that the edge of the ball would be checked, not the middle
rem reverse the ball speed
BallSpeedX = BallSpeedX * -1
endif
endif
rem instead of changing the speed when the paddle is out of range,
rem just check if it's in range before moving it
IF upkey() = 1 and Bat1Y1 > 40
Bat1Y1 = Bat1Y1 - Bat1Speed1
endif
rem same as above, we have to take into account that the paddle is 70 pixels tall,
rem and subtract that from 420 to get where the Bat1Y1 should stop at
IF downkey() = 1 and Bat1Y1 < 350
Bat1Y1 = Bat1Y1 + Bat1Speed1
endif
rem Here is the code for the computer moving the bat, very simple
rem if the ball is above the paddle, move up, otherwise move down
rem also, we add 35 to the Bat2Y1 to get the CENTER of the paddle
if BallY < Bat2Y1+35
rem make sure to check that the paddle is in bounds first, then subtract speed from paddle
if Bat2Y1 > 40 then Bat2Y1 = Bat2Y1-Bat2Speed1
else
rem make sure to check that the paddle is in bounds first, then add speed to paddle
if Bat2Y1 < 350 then Bat2Y1 = Bat2Y1+Bat2Speed1
endif
LOOP
REM FUNCTIONS
FUNCTION main_scrn()
SET TEXT FONT "MS Dialog"
SET TEXT SIZE 16
CENTER TEXT 320,440,"Reality Studio Presents"
LINE 30,30,610,30
LINE 30,420,610,420
LINE 30,30,30,420
LINE 610,30,610,420
SET TEXT SIZE 20
CENTER TEXT 320,450,"PONG BASIC VERSION 0.1"
rem print the player scores to the screen above their paddle
rem use str$() to convert the number of points into a string to print it
center text 20,10,str$(points(1))
center text 620,10,str$(points(2))
ENDFUNCTION
If you have any questions, just post them here. Hope that helped!
-H4ck1d
There are 10 types of people, those who understand binary and those that don't.