Simple Pong-type Game (Sample Programs)

by Jeremiah Stoddard ⌂ @, Rosarito, Mexico, Sunday, February 06, 2011, 05:15 (3026 days ago)

I've been looking at software for teaching programming to a grade-school aged relative, and BASIC-256 seems to fit the bill nicely (would've gone with FreeBASIC, but I needed an IDE consistent between Windows, Mac & Linux -- he can "graduate" to it later, I guess). Anyway, I installed BASIC-256 today and spend an hour or two playing around to make sure I could make use of it. In doing so I came up with the following, which I'm posting in case someone else finds it useful.

This is sort of a bounce the ball against the wall and hit it when it comes back type of game. Moves with the arrow keys. Quit with "q". Adjust "speed" variable to get it to something nice for your computer.

rem simple pong
rem by Jeremiah Stoddard
rem ----------------------------

rem initialize screen
start:
clg
color white
rect 0,0,300,300
fastgraphics

rem initialize variables
ball_x = 10
ball_y = 10
ball_old_x = 10
ball_old_y = 10
ball_vel_x = int(rand*3)+1
ball_vel_y = int(rand*3)+1
paddle_pos = 130
paddle_old_pos = 130
score = 0
speed = 500000 rem higher numbers mean go slower

rem draw screen
drawscreen:
color white
circle ball_old_x, ball_old_y, 10
rect paddle_old_pos, 295, 40, 5
color red
circle ball_x, ball_y, 10
color darkorange
rect paddle_pos, 295, 40, 5
refresh

rem move paddle
movepaddle:
paddle_old_pos = paddle_pos
keypress = key
if keypress = 16777234 then paddle_pos = paddle_pos - 20 rem move left
if keypress = 16777236 then paddle_pos = paddle_pos + 20 rem move right
if keypress = 81 then goto quit
if paddle_pos > 260 then paddle_pos = 260
if paddle_pos < 0 then paddle_pos = 0

rem move ball
random = int(rand*3)
ball_old_x = ball_x
ball_old_y = ball_y
rem move along x-axis
if ball_x + ball_vel_x > 290 then ball_vel_x = (-1*ball_vel_x) - random
if ball_x + ball_vel_x < 10 then ball_vel_x = (-1*ball_vel_x) + random
if ball_vel_x > 5 then ball_vel_x = ball_vel_x - 5
if ball_vel_x < -5 then ball_vel_x = ball_vel_x + 5
ball_x = ball_x + ball_vel_x
rem move along y-axis
if ball_y + ball_vel_y > 285 then goto collisioncheck
aftercollisioncheck:
if ball_y + ball_vel_y < 10 then ball_vel_y = (-1*ball_vel_y) + random
if ball_vel_y > 5 then ball_vel_y = ball_vel_y - 5
if ball_vel_y < -5 then ball_vel_y = ball_vel_y + 5
ball_y = ball_y + ball_vel_y

rem delay loop, so game doesn't go too fast
for i = 1 to speed
next i
rem loop back to draw screen
goto drawscreen

rem check if player hit or missed ball
collisioncheck:
if ball_x < paddle_pos or ball_x > paddle_pos + 40 then goto missed
ball_vel_y = (-1*ball_vel_y) - random
score = score + 1
cls
print "Score: "+score
goto aftercollisioncheck

rem player missed ball
missed:
print "You missed!"

rem end of game
quit:
print "Your score was "+score+". Thanks for playing."
end


Complete thread:

 RSS Feed of thread

powered by my little forum