%Lapsus' Pong!
%Last revised: Nov 9, 2005
var window : int := Window.Open ("graphics:600;400, offscreenonly, title Lapsus' Pong!") %Opens a new window
type object : %Custom type
record
x, y, clr, siz, xs, ys, score : int
end record
var p1, p2, ball : object %Players and ball
var keys : array char of boolean %Used for Input.KeyDown
var endstring : string := ""
var bouncecount : int := 0 %Used to make game more interesting
%Initalizing variables for p1
p1.x := 10
p1.y := maxy div 2
p1.clr := black
p1.siz := maxy div 10
p1.ys := 2
p1.score := 0
%Initializing variables for p2
p2.x := maxx - 10
p2.y := maxy div 2
p2.clr := black
p2.siz := maxy div 10
p2.ys := 2
p2.score := 0
%Initializing variables for ball
ball.x := maxx div 2
ball.y := maxy div 2
ball.clr := brightblue
ball.siz := maxy div 100
ball.xs := 1
ball.ys := 1
loop %Main loop (duh...)
Input.KeyDown (keys) %Get input from keyboard
%Put score in top corner
locate (1, 1)
put "Player 1 score: ", p1.score : 3
put "Player 2 score: ", p2.score : 3
%Draw Players and ball
drawbox (p1.x - 3, p1.y + p1.siz div 2, p1.x + 2, p1.y - p1.siz div 2, p1.clr)
drawbox (p2.x - 2, p2.y + p2.siz div 2, p2.x + 3, p2.y - p2.siz div 2, p2.clr)
drawoval (ball.x, ball.y, ball.siz, ball.siz, ball.clr)
View.Update %Update window
delay (10) %Keep program at a reasonable speed
cls %Clear window
%Move Ball
ball.x += ball.xs
ball.y += ball.ys
% Player 1 controls
if keys ('a') then
p1.y += p1.ys
elsif keys ('z') then
p1.y -= p1.ys
end if
% Player 2 controls
if keys (KEY_UP_ARROW) then
p2.y += p2.ys
elsif keys (KEY_DOWN_ARROW) then
p2.y -= p2.ys
end if
% %Player 1 AI
% if ball.xs < 0 then
% if p1.y + p1.siz div 2 < ball.y then
% p1.y += p1.ys
% elsif p1.y - p1.siz div 2 > ball.y then
% p1.y -= p1.ys
% end if
% end if
% %Player 2 AI
% if ball.xs > 0 then
% if p2.y + p2.siz div 3 < ball.y then
% p2.y += p2.ys
% elsif p2.y - p2.siz div 3 > ball.y then
% p2.y -= p2.ys
% end if
% end if
%Keep player 1 onscreen
if p1.y > maxy - p1.siz div 3 then
p1.y := maxy - p1.siz div 3
elsif p1.y < p1.siz div 2 then
p1.y := p1.siz div 2
end if
%Keep player 2 onscreen
if p2.y > maxy - p2.siz div 2 then
p2.y := maxy - p2.siz div 2
elsif p2.y < p2.siz div 2 then
p2.y := p2.siz div 2
end if
%Bounce ball off top and bottom of screen
if ball.y > maxy - ball.siz then
bouncecount += 1
ball.ys *= -1
ball.y := maxy - ball.siz - 1
elsif ball.y < ball.siz then
bouncecount += 1
ball.ys *= -1
ball.y := ball.siz + 1
end if
%Bounce off paddles
if ball.y > p1.y - p1.siz div 2 and ball.y < p1.y + p1.siz div 2 and ball.x < p1.x + 2 + 5 and ball.x > 0 then
bouncecount += 1
ball.xs *= -1
ball.x := p1.x + 2 + 5
elsif ball.y > p2.y - p2.siz div 2 and ball.y < p2.y + p2.siz div 2 and ball.x > p2.x - 2 - 5 and ball.x < maxx then
bouncecount += 1
ball.xs *= -1
ball.x := p2.x - 2 - 5
end if
%Speed up after 10 bounces
if bouncecount >= 10 then
bouncecount := 0
if ball.xs > 0 then
ball.xs += 1
else
ball.xs -= 1
end if
end if
%Scoring
if ball.x < 0 then
colour (0) %White text
colourback (black) %Black background
cls
ball.x := maxx div 2 %Move ball to center of screen
ball.xs := 1 %Reset speed
ball.ys := 1
p2.score += 1 %Add a point to score
%Draw score screen
put "Player 1 score: ", p1.score : 3
put "Player 2 score: ", p2.score : 3
put "Player 2 scores!"
drawbox (p1.x - 3, p1.y + p1.siz div 2, p1.x + 2, p1.y - p1.siz div 2, white)
drawbox (p2.x - 2, p2.y + p2.siz div 2, p2.x + 3, p2.y - p2.siz div 2, white)
drawoval (ball.x, ball.y, ball.siz, ball.siz, white)
View.Update
delay (2000) %Wait for 2 seconds
elsif ball.x > maxx then
colour (0)
colourback (black)
cls
ball.x := maxx div 2
ball.xs := -1
ball.ys := -1
p1.score += 1
put "Player 1 score: ", p1.score : 3
put "Player 2 score: ", p2.score : 3
put "Player 1 scores!"
drawbox (p1.x - 3, p1.y + p1.siz div 2, p1.x + 2, p1.y - p1.siz div 2, p1.clr)
drawbox (p2.x - 2, p2.y + p2.siz div 2, p2.x + 3, p2.y - p2.siz div 2, p2.clr)
drawoval (ball.x, ball.y, ball.siz, ball.siz, ball.clr)
View.Update
delay (2000)
end if
colour (black) %Black text
colourback (white) %White background
exit when p1.score >= 21 or p2.score >= 21 %End game when someone wins
end loop
cls
%Tells who won by how much
if p1.score > p2.score then
endstring += "Player 1"
else
endstring += "Player 2"
end if
endstring += " won by " + intstr (abs (p1.score - p2.score)) + " point"
if abs (p1.score - p2.score) > 1 then
endstring += "s"
end if
endstring += "!"
put endstring
put "Press any key to quit."
View.Update
loop
exit when hasch
end loop
Window.Close (window) |