Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Lapsus's Pong
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Lapsus Antepedis




PostPosted: Wed Nov 09, 2005 11:53 am   Post subject: Lapsus's Pong

It's pong time for my friend's compsci class, so I decided to try and write one myself without using whatdotcolour collision detection...

Here's the code!

code:
%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)


Questions? Comments? Complete faliures of code to run?
Leave a comment!

You can uncomment the AI and comment the controls to have a computer player as well... and it's not infallible!

EDIT: Changed amount of bounced befor ball speeds up to ten.
Sponsor
Sponsor
Sponsor
sponsor
do_pete




PostPosted: Wed Nov 09, 2005 2:07 pm   Post subject: (No subject)

nice start
Lapsus Antepedis




PostPosted: Wed Nov 09, 2005 2:45 pm   Post subject: (No subject)

do_pete wrote:
nice start


Well, if you have any ideas for things to add, then please tell me!
I need some semi challenging things to do in turing! Razz
[Gandalf]




PostPosted: Wed Nov 09, 2005 4:11 pm   Post subject: (No subject)

Yes, don't make 2 word posts...

The AI should be improved to be more realistic instead of just making it go whereever the ball is. When you score, the delay is a bit long. Your title isn't showing up, the proper line should be:
code:
var window : int := Window.Open ("graphics:600;400, offscreenonly, title: Lapsus' Pong!")


Have you made a snake game? That is more challenging than pong. There are a lot of other ideas, I am only mentioning an obvious one. Try making a simulation of something too, that gets as hard as you want it to be.
ZeroPaladn




PostPosted: Thu Nov 10, 2005 11:14 am   Post subject: (No subject)

great pong game! every time i try to make one it ends up being left alone because i get fusturated with the collision detection. heres a challenge for ya, make it so you can play over IP addresses Shocked . now THAT would be neat. make it so you can set up nicknames for p1 and p2, and maybe make the ball a little less... whats the word im looking for... readable. i hope that challenge enough for ya, looking forward to seeing newer versions of it.
sylvester-27




PostPosted: Fri Nov 25, 2005 2:47 pm   Post subject: (No subject)

its a pretty good game but i suggest to make it look better add a backround and some other neat features like make a title screen where you can choose 1 or 2 players. Then have another option to have 2 sided or 4 sided pong. A really cool option would be multiplayer or single player where you could play against friends or a computer and have different levels and difficulties
Albrecd




PostPosted: Fri Dec 16, 2005 1:21 pm   Post subject: (No subject)

ZeroPaladn Said:
Quote:
make it so you can play over IP addresses . now THAT would be neat. make it so you can set up nicknames for p1 and p2


That would be quite dificult, and considering that it's pong, probably overkill.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: