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

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




PostPosted: Fri Oct 26, 2007 6:49 am   Post subject: Pong :D

Just my remake of the original Pong ^_^

Might be adding some new things into it like powerups soon Razz

-------------------------------------

code:

View.Set ("graphics:800;400,position:center,center, offscreenonly")
colorback (black)
color (brightgreen)
cls
type paddle :
    record
        x : int
        y : int
        h : int
    end record

type ball :
    record
        x : int
        y : int
        rad : int
        xv : int
        yv : int
    end record
   
var paddles : array 1 .. 2 of paddle
var balls : ball
var key : array char of boolean
var points : array 1 .. 2 of int
var reply : string (1)
var speed : int
var poweruprand, powerupspeed : int

loop
    cls

    speed := 1

    paddles (1).x := 50
    paddles (1).y := maxy div 2 - 25
    paddles (1).h := 50

    paddles (2).x := 750
    paddles (2).y := maxy div 2 - 25
    paddles (2).h := 50

    balls.x := 400
    balls.y := 300
    balls.rad := 5
    randint (balls.xv, 1, 2)
    if balls.xv = 2 then
        balls.xv := -1
    end if
    balls.yv := -1

    for i : 1 .. upper (points)
        points (i) := 0
    end for


    loop
        balls.x += balls.xv
        balls.y += balls.yv
        if Math.Distance (balls.x, balls.y, balls.x, maxy) < balls.rad * 2 or Math.Distance (balls.x, balls.y, balls.x, 0) < balls.rad * 2 then
            balls.yv *= -1
        end if

        if Math.DistancePointLine (balls.x, balls.y, paddles (1).x, paddles (1).y, paddles (1).x, paddles (1).y + paddles (1).h) < balls.rad * 2 then
            balls.xv *= -1
        end if

        if Math.DistancePointLine (balls.x, balls.y, paddles (2).x, paddles (2).y, paddles (2).x, paddles (2).y + paddles (2).h) < balls.rad * 2 then
            balls.xv *= -1
        end if

        if Math.Distance (balls.x, balls.y, 0, balls.y) < balls.rad * 2 then
            points (2) += 1
            if points (2) >= 10 then
                put "Player 2 Wins!"
                View.Update
                delay(1000)
                exit
            end if
            locatexy (maxx div 2 - 50, maxy div 4 + maxy div 2)
            put "Player 2 Scores!"
                            View.Update
                delay(1000)
            paddles (1).x := 50
            paddles (1).y := maxy div 2 - 25
            paddles (1).h := 50

            paddles (2).x := 750
            paddles (2).y := maxy div 2 - 25
            paddles (2).h := 50

            balls.x := 400
            balls.y := 300
            balls.rad := 5
            balls.xv := 1
            balls.yv := -1
        end if

        if Math.Distance (balls.x, balls.y, maxx, balls.y) < balls.rad * 2 then
            points (1) += 1
            locatexy (maxx div 2 - 50, maxy div 4 + maxy div 2)
            put "Player 1 Scores!"
            View.Update
            delay(1000)
            if points (1) >= 10 then
                put "Player 2 Wins!"
                                View.Update
                delay(1000)
                exit
            end if
            paddles (1).x := 50
            paddles (1).y := maxy div 2 - 25
            paddles (1).h := 50

            paddles (2).x := 750
            paddles (2).y := maxy div 2 - 25
            paddles (2).h := 50

            balls.x := 400
            balls.y := 300
            balls.rad := 5
            balls.xv := -1
            balls.yv := -1
        end if

        Input.KeyDown (key)

        if key ('w') and paddles (1).y <= 335 then
            paddles (1).y += 1
        elsif key ('s') and paddles (1).y >= 20 then
            paddles (1).y -= 1
        end if
        if key (KEY_UP_ARROW) and paddles (2).y <= 335 then
            paddles (2).y += 1
        elsif key (KEY_DOWN_ARROW) and paddles (2).y >= 20 then
            paddles (2).y -= 1
        end if
        locate (1, 2)
        put points (1)
        locate (1, maxcol - 2)
        put points (2)

        for i : 1 .. upper (paddles)
            drawfillbox (paddles (i).x, paddles (i).y, paddles (i).x + 10, paddles (i).y + paddles (i).h, brightgreen)
        end for
        drawfilloval (balls.x, balls.y, balls.rad, balls.rad, brightgreen)
        View.Update
Time.DelaySinceLast(2)

        cls
    end loop
end loop

Sponsor
Sponsor
Sponsor
sponsor
petree08




PostPosted: Fri Oct 26, 2007 7:50 am   Post subject: RE:Pong :D

yeah about pong, there are like a million posts on compsci that are "pong", i think this subject is done to death. Sure its a great program for people starting out graphics but we don't need to see every single person's version of a very basic game. Still its good to see you have an interest. Have a look at the turing tutorials there are a lot of neat stuff you can learn.
Tony




PostPosted: Fri Oct 26, 2007 2:05 pm   Post subject: RE:Pong :D

I see arrays of records in there. Very nice! Absolutely love this line
code:

for i : 1 .. upper (paddles)

but the control keys are defined arbitrary elsewhere in the program, so simply creating a new padding record wouldn't do that much good.

you might want to replace randint with Rand.Int

and instead of locatexy use Font.Draw
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Nick




PostPosted: Fri Oct 26, 2007 2:46 pm   Post subject: RE:Pong :D

not bad not bad... take a gander at mine Very Happy

Turing:
setscreen ("graphics:450;400,nobuttonbar")
colorback (black)
color (white)
cls

var ch : char
const chatPort : int := 5055
var token : string
var netStream : int
var netAddress : string
var server : string := ""
var connection : string := "Keyboard"
var players : string := ""
var gameType : string := ""
var difficulty : string := ""
var ballSpeed, ballAccel : int := 1
var ballDirx, ballDiry : int := 1
var move, move2 : array char of boolean

var score : array 1 .. 2 of int
score (1) := 0
score (2) := 0

var ballx, bally : int
ballx := 50
bally := 50

var playerx, playery : array 1 .. 4 of int
playerx (1) := 10
playery (1) := 10
playerx (2) := maxx - 20
playery (2) := maxy - 110
playerx (3) := 10
playery (3) := 10
playerx (4) := maxx - 60
playery (4) := maxy - 70

loop
    locate (1, 1)
    put "Please make sure you are matching the case"
    locate (2, 1)
    put "Game type?? (Classic or New)"
    get gameType
    cls
    if gameType = "Classic" then
        exit
    elsif gameType = "New" then
        exit
    end if
end loop

loop
    locate (1, 1)
    put "Difficulty? (1,2 or 3)"
    get difficulty
    cls
    if difficulty = "1" then
        ballAccel := 1
        exit
    elsif difficulty = "2" then
        ballAccel := 3
        exit
    elsif difficulty = "3" then
        ballAccel := 5
        exit
    end if
end loop

loop
    locate (1, 1)
    put "Players? (1 or 2)"
    get players
    cls
    if players = "1" then
        exit
    elsif players = "2" then
        exit
    end if
end loop

if players = "2" then
    loop
        locate (1, 1)
        put "Please make sure you are matching the case"
        locate (2, 1)
        put "Connect to another computer or use the keyboard"
        locate (3, 1)
        put "for player 2 ('Connection' or 'Keyboard')"
        get connection
        cls
        if connection = "Keyboard" then
            exit
        elsif connection = "Connection" then
            exit
        end if
    end loop
end if

if connection = "Connection" then
    loop
        locate (1, 1)
        put "Please make sure you are matching the case"
        put "Will you act as the server or connect to the server"
        put "('Server' or 'Connect')"
        get server
        cls
        if server = "Server" then
            exit
        elsif server = "Connect" then
            exit
        end if
    end loop

    if server = "Connect" then
        put "Enter the address to connect to: " ..
        get netAddress
        netStream := Net.OpenConnection (netAddress, chatPort)
        if netStream <= 0 then
            put "Unable to connect to ", netAddress
            return
        end if
    else
        netStream := Net.WaitForConnection (chatPort, netAddress)
    end if
end if

setscreen ("graphics:450;400,nobuttonbar,offscreenonly")

procedure moveBall

    ballx += ballDirx
    bally += ballDiry
    if bally <= 0 then
        bally := 0
        ballDiry := 1
        ballSpeed := 1
        if gameType = "New" then
            score (2) += 1
        end if
    elsif bally >= maxy - 50 then
        bally := maxy - 50
        ballDiry := -1
        ballSpeed := -1
        if gameType = "New" then
            score (1) += 1
        end if
    end if
    if ballx <= 0 then
        ballx := 0
        score (2) += 1
        ballSpeed := 1
        ballDirx := 1
    elsif ballx >= maxx then
        ballx := maxx
        ballDirx := -1
        ballSpeed := -1
        score (1) += 1
    end if
    if (ballx >= playerx (1) and ballx <= playerx (1) + 10)
            and (bally >= playery (1) and bally <= playery (1) + 50) then
        ballDirx := ballSpeed
        ballSpeed += ballAccel
    end if
    if (ballx >= playerx (2) and ballx <= playerx (2) + 10)
            and (bally >= playery (2) and bally <= playery (2) + 50) then
        ballDirx := -ballSpeed
        ballSpeed += ballAccel
    end if
    if gameType = "New" then
        if (ballx >= playerx (3) and ballx <= playerx (3) + 50)
                and (bally >= playery (3) and bally <= playery (3) + 10) then
            ballDiry := ballSpeed
            ballSpeed += ballAccel
        end if
        if (ballx >= playerx (4) and ballx <= playerx (4) + 50)
                and (bally >= playery (4) and bally <= playery (4) + 10) then
            ballDiry := -ballSpeed
            ballSpeed += ballAccel
        end if
    end if

end moveBall

procedure movePlayers

    Input.KeyDown (move)
    if hasch then
        if connection = "Connection" then
            put : netStream, ch
        end if
    end if
    if move ('w') then
        playery (1) += 2
    end if
    if move ('s') then
        playery (1) -= 2
    end if
    if move ('a') then
        playerx (3) -= 2
    end if
    if move ('d') then
        playerx (3) += 2
    end if
    if connection = "Keyboard" then
        if players = "2" then
            if move (KEY_UP_ARROW) then
                playery (2) += 2
            end if
            if move (KEY_DOWN_ARROW) then
                playery (2) -= 2
            end if
            if move (KEY_LEFT_ARROW) then
                playerx (4) -= 2
            end if
            if move (KEY_RIGHT_ARROW) then
                playerx (4) += 2
            end if
        end if
    elsif connection = "Connection" then
        if Net.CharAvailable (netStream) then
            get : netStream, ch
            if ch = "w" then
                playery (2) -= 2
            end if
            if ch = "s" then
                playery (2) += 2
            end if
            if ch = "a" then
                playerx (4) += 2
            end if
            if ch = "d" then
                playerx (4) -= 2
            end if
        end if
    end if
    if playery (1) <= 0 then
        playery (1) += 2
    elsif playery (1) >= maxy - 95 then
        playery (1) -= 2
    end if
    if playery (2) <= 0 then
        playery (2) += 2
    elsif playery (2) >= maxy - 95 then
        playery (2) -= 2
    end if
    if playerx (3) <= 0 then
        playerx (3) += 2
    elsif playerx (3) >= maxx - 50 then
        playerx (3) -= 2
    end if
    if playerx (4) <= 0 then
        playerx (4) += 2
    elsif playerx (4) >= maxx - 50 then
        playerx (4) -= 2
    end if

end movePlayers

procedure moveComp

    if bally > playery (2) and ballx > maxx div 2 then
        playery (2) += 2
    elsif bally < playery (2) and ballx > maxx div 2 then
        playery (2) -= 2
    end if
    if ballx > playerx (4) and bally > maxy div 2 - 22 then
        playerx (4) += 2
    elsif ballx < playerx (4) and bally > maxy div 2 - 22 then
        playerx (4) -= 2
    end if

end moveComp

loop
    cls
    Draw.ThickLine (0, maxy - 45, maxx, maxy - 45, 5, white)
    Draw.ThickLine (maxx div 2, 0, maxx div 2, maxy - 45, 5, white)
    Draw.ThickLine (0, maxy div 2 - 22, maxx, maxy div 2 - 22, 5, white)
    drawfillbox (playerx (1), playery (1), playerx (1) + 10, playery (1) + 50, blue)
    drawfillbox (playerx (2), playery (2), playerx (2) + 10, playery (2) + 50, red)
    if gameType = "New" then
        drawfillbox (playerx (3), playery (3), playerx (3) + 50, playery (3) + 10, blue)
        drawfillbox (playerx (4), playery (4), playerx (4) + 50, playery (4) + 10, red)
    end if
    drawfilloval (ballx, bally, 5, 5, white)
    locate (1, 1)
    put "Player 1's score :", score (1)
    locate (2, 1)
    put "Player 2's score :", score (2)
    View.Update
    View.Update
    movePlayers
    moveBall

    if players = "1" then
        moveComp
    end if

    delay (10)
    exit when score (1) = 10
    exit when score (2) = 10
end loop

cls
locate (1, 1)
if score (1) = 10 then
    put "congrats player 1"
else
    put "congrats player 2"
end if
locate (2, 1)
put "You have survived for ", Time.Elapsed div 1000, " seconds"


multiple modes including difficulty and optional second player

also the lan is nto yet finsished so if u try too play over lan... tough luck sorry (u could fix it tho)

controls player 1 wasd
controls player 2 arrow keys
Tony




PostPosted: Fri Oct 26, 2007 3:51 pm   Post subject: RE:Pong :D

hey, cool. Also, check this out:
Turing:

if difficulty = "1" then
        ballAccel := 1
        exit
    elsif difficulty = "2" then
        ballAccel := 3
        exit
    elsif difficulty = "3" then
        ballAccel := 5
        exit
    end if

into
Turing:

if difficulty = "1" or difficulty = "2" or difficulty = "3" then
   ballAccel := strint(difficulty)*2 - 1
   exit
end if

Wink
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
TripleBla




PostPosted: Sun Oct 28, 2007 9:38 am   Post subject: Re: Pong :D

Tony, your methods of converting countless If structure into flawless code is awesome.

I always see myself looking at a handful of elsif's .. and wondering, there's got to be something better.
Clayton




PostPosted: Sun Oct 28, 2007 10:49 pm   Post subject: RE:Pong :D

Perhaps you should read this, and you'll see how you can do so too!
Nick




PostPosted: Sun Oct 28, 2007 11:02 pm   Post subject: RE:Pong :D

a great blog and all but what's it got to do with pong and/or if structures?
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Sun Oct 28, 2007 11:06 pm   Post subject: RE:Pong :D

Yeah Clayton, I think you've got the wrong post. You might have meant this one.

Though really, that only brings the attention to the fact that you should be questioning yourself if you're doing something the way it should be done. TripleBla already does that, the rest comes with practise.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Clayton




PostPosted: Mon Oct 29, 2007 12:21 pm   Post subject: RE:Pong :D

I uh... fixed it.
r.m_spy




PostPosted: Mon Dec 10, 2007 7:32 am   Post subject: RE:Pong :D

just trying to help

setscreen ("graphics:450;400,nobuttonbar")
colorback (black)
color (white)
cls

var ch : char
var choice:string
var l:int:=0
const chatPort : int := 5055
var token : string
var netStream : int
var netAddress : string
var server : string := ""
var connection : string := "Keyboard"
var players : string := ""
var gameType : string := ""
var difficulty : string := ""
var ballSpeed, ballAccel : int := 1
var ballDirx, ballDiry : int := 1
var move, move2 : array char of boolean

var score : array 1 .. 2 of int
score (1) := 0
score (2) := 0

var ballx, bally : int
ballx := 50
bally := 50

var playerx, playery : array 1 .. 4 of int
playerx (1) := 10
playery (1) := 10
playerx (2) := maxx - 20
playery (2) := maxy - 110
playerx (3) := 10
playery (3) := 10
playerx (4) := maxx - 60
playery (4) := maxy - 70
loop
put"Enter 'I' for Instructions."
put"Enter 'G' for the game."
get choice
if choice="I" then
cls
put" point of the game "
put""
put" defend the ball from getting past you"
put" try to get the ball past the red blocks"
put" first one to 10 wins"
put""
put""
put""
put" controls"
put""
put" player 1 in classic mode uses 'w' and 's'"
put"if player 1 is in New mode, use 'w','s','a', and 'd'."
put" player 2 in classic mode uses up and down"
put"if player 2 is in New mode, use up,down,left, and right."
put""
Input.Pause
elsif choice="G" then
l:=l+1
end if
cls
exit when l=1
end loop
cls
loop
locate (1, 1)
put "Please make sure you are matching the case"
locate (2, 1)
put "Game type?? (Classic or New)"
get gameType
cls
if gameType = "Classic" then
exit
elsif gameType = "New" then
exit
end if
end loop

loop
locate (1, 1)
put "Difficulty? (1,2 or 3)"
get difficulty
cls
if difficulty = "1" then
ballAccel := 1
exit
elsif difficulty = "2" then
ballAccel := 3
exit
elsif difficulty = "3" then
ballAccel := 5
exit
end if
end loop

loop
locate (1, 1)
put "Players? (1 or 2)"
get players
cls
if players = "1" then
exit
elsif players = "2" then
exit
end if
end loop

if players = "2" then
loop
locate (1, 1)
put "Please make sure you are matching the case"
locate (2, 1)
put "Connect to another computer or use the keyboard"
locate (3, 1)
put "for player 2 ('Connection' or 'Keyboard')"
get connection
cls
if connection = "Keyboard" then
exit
elsif connection = "Connection" then
exit
end if
end loop
end if

if connection = "Connection" then
loop
locate (1, 1)
put "Please make sure you are matching the case"
put "Will you act as the server or connect to the server"
put "('Server' or 'Connect')"
get server
cls
if server = "Server" then
exit
elsif server = "Connect" then
exit
end if
end loop

if server = "Connect" then
put "Enter the address to connect to: " ..
get netAddress
netStream := Net.OpenConnection (netAddress, chatPort)
if netStream <= 0 then
put "Unable to connect to ", netAddress
return
end if
else
netStream := Net.WaitForConnection (chatPort, netAddress)
end if
end if

setscreen ("graphics:450;400,nobuttonbar,offscreenonly")

procedure moveBall

ballx += ballDirx
bally += ballDiry
if bally <= 0 then
bally := 0
ballDiry := 1
ballSpeed := 1
if gameType = "New" then
score (2) += 1
end if
elsif bally >= maxy - 50 then
bally := maxy - 50
ballDiry := -1
ballSpeed := -1
if gameType = "New" then
score (1) += 1
end if
end if
if ballx <= 0 then
ballx := 0
score (2) += 1
ballSpeed := 1
ballDirx := 1
elsif ballx >= maxx then
ballx := maxx
ballDirx := -1
ballSpeed := -1
score (1) += 1
end if
if (ballx >= playerx (1) and ballx <= playerx (1) + 10)
and (bally >= playery (1) and bally <= playery (1) + 50) then
ballDirx := ballSpeed
ballSpeed += ballAccel
end if
if (ballx >= playerx (2) and ballx <= playerx (2) + 10)
and (bally >= playery (2) and bally <= playery (2) + 50) then
ballDirx := -ballSpeed
ballSpeed += ballAccel
end if
if gameType = "New" then
if (ballx >= playerx (3) and ballx <= playerx (3) + 50)
and (bally >= playery (3) and bally <= playery (3) + 10) then
ballDiry := ballSpeed
ballSpeed += ballAccel
end if
if (ballx >= playerx (4) and ballx <= playerx (4) + 50)
and (bally >= playery (4) and bally <= playery (4) + 10) then
ballDiry := -ballSpeed
ballSpeed += ballAccel
end if
end if

end moveBall

procedure movePlayers

Input.KeyDown (move)
if hasch then
if connection = "Connection" then
put : netStream, ch
end if
end if
if move ('w') then
playery (1) += 2
end if
if move ('s') then
playery (1) -= 2
end if
if move ('a') then
playerx (3) -= 2
end if
if move ('d') then
playerx (3) += 2
end if
if connection = "Keyboard" then
if players = "2" then
if move (KEY_UP_ARROW) then
playery (2) += 2
end if
if move (KEY_DOWN_ARROW) then
playery (2) -= 2
end if
if move (KEY_LEFT_ARROW) then
playerx (4) -= 2
end if
if move (KEY_RIGHT_ARROW) then
playerx (4) += 2
end if
end if
elsif connection = "Connection" then
if Net.CharAvailable (netStream) then
get : netStream, ch
if ch = "w" then
playery (2) -= 2
end if
if ch = "s" then
playery (2) += 2
end if
if ch = "a" then
playerx (4) += 2
end if
if ch = "d" then
playerx (4) -= 2
end if
end if
end if
if playery (1) <= 0 then
playery (1) += 2
elsif playery (1) >= maxy - 95 then
playery (1) -= 2
end if
if playery (2) <= 0 then
playery (2) += 2
elsif playery (2) >= maxy - 95 then
playery (2) -= 2
end if
if playerx (3) <= 0 then
playerx (3) += 2
elsif playerx (3) >= maxx - 50 then
playerx (3) -= 2
end if
if playerx (4) <= 0 then
playerx (4) += 2
elsif playerx (4) >= maxx - 50 then
playerx (4) -= 2
end if

end movePlayers

procedure moveComp

if bally > playery (2) and ballx > maxx div 2 then
playery (2) += 2
elsif bally < playery (2) and ballx > maxx div 2 then
playery (2) -= 2
end if
if ballx > playerx (4) and bally > maxy div 2 - 22 then
playerx (4) += 2
elsif ballx < playerx (4) and bally > maxy div 2 - 22 then
playerx (4) -= 2
end if

end moveComp

loop
cls
Draw.ThickLine (0, maxy - 45, maxx, maxy - 45, 5, white)
Draw.ThickLine (maxx div 2, 0, maxx div 2, maxy - 45, 5, white)
Draw.ThickLine (0, maxy div 2 - 22, maxx, maxy div 2 - 22, 5, white)
drawfillbox (playerx (1), playery (1), playerx (1) + 10, playery (1) + 50, blue)
drawfillbox (playerx (2), playery (2), playerx (2) + 10, playery (2) + 50, red)
if gameType = "New" then
drawfillbox (playerx (3), playery (3), playerx (3) + 50, playery (3) + 10, blue)
drawfillbox (playerx (4), playery (4), playerx (4) + 50, playery (4) + 10, red)
end if
drawfilloval (ballx, bally, 5, 5, white)
locate (1, 1)
put "Player 1's score :", score (1)
locate (2, 1)
put "Player 2's score :", score (2)
View.Update
View.Update
movePlayers
moveBall

if players = "1" then
moveComp
end if

delay (10)
exit when score (1) = 10
exit when score (2) = 10
end loop

cls
locate (1, 1)
if score (1) = 10 then
put "congrats player 1"
else
put "congrats player 2"
end if
locate (2, 1)
put "You have survived for ", Time.Elapsed div 1000, " seconds"
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  [ 11 Posts ]
Jump to:   


Style:  
Search: