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

Username:   Password: 
 RegisterRegister   
 Air Hockey Game
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
richcash




PostPosted: Fri Jun 23, 2006 10:25 pm   Post subject: Air Hockey Game

This is a simple air hockey game that my friend and I did for our final project.
My friend's name that I worked with is PhOtO!
You may have to adjust it a bit depending on your computer speed. I hope it's not too inconvenient to adjust. The MathDistance and MathDistancePointLine are not predefined on Turing 4.0.1 so they were supplied to us by Gandalf.
code:

import GUI
setscreen ("graphics:700;max,position:center;center,nobuttonbar,offscreenonly")
var x, y, button, paddlex, paddley := 100
var cpux, cpuy := maxx div 2 % x and y value of paddle
var ballx, bally : real := 100 % x and y value of ball
var dx, dy : real := 0 % difference in x and y value for ball
var cpuxd, cpuyd := 1 % difference in x and y value for comp paddle
var maxspeed := 3 % maximum speed for comp paddle
var numofhits : int := 0
var maxballspeed : int := 3
var a, b : int := 0
var paddler, cpupaddler : int := 35
var ballr : int := 15
var px, py : int := 0
var hit : boolean := false
var ex := 0
ballx := maxx div 2
bally := maxy div 2
proc help
    locate (1, 1)
    put "You are trying to score in the black box (net) on the computer's side (top) and"
    put "trying to prevent the computer from scoring in you net at the bottom."
    put "Move the paddle around your side of the table with the mouse and hit the ball when"
    put "it comes to you. You are playing a game against the computer up to 15. To make it"
    put "more challenging, your paddle will get smaller each time you score and you will"
    put "find that you are hitting the ball with less power."
end help
proc start
    ex := 1
end start
proc Exit
    ex := 2
end Exit
proc beginning
    loop
        var instructions := GUI.CreateButton (maxx div 2, maxy div 7 * 3, 0, "Help", help)
        var start := GUI.CreateButton (maxx div 2, maxy div 3, 0, "Start", start)
        var exitbut := GUI.CreateButton (maxx div 2, maxy div 4, 0, "Exit", Exit)
        exit when GUI.ProcessEvent or ex > 0
        View.Update
    end loop
end beginning

function MathDistance (x1, y1, x2, y2 : real) : real
    var dx : real := x1 - x2
    var dy : real := y1 - y2
    result sqrt (dx * dx + dy * dy)
end MathDistance

function MathDistancePointLine (px, py, x1, y1, x2, y2 : real) : real
    var lineSize : real := MathDistance (x1, y1, x2, y2)
    if lineSize = 0 then
        result MathDistance (px, py, x1, y1)
    end if

    var u : real := ((px - x1) * (x2 - x1) +
        (py - y1) * (y2 - y1)) / (lineSize * lineSize)

    if u < 0.0 then
        result MathDistance (px, py, x1, y1)
    elsif u > 1.0 then
        result MathDistance (px, py, x2, y2)
    else
        var ix : real := x1 + u * (x2 - x1)
        var iy : real := y1 + u * (y2 - y1)
        result MathDistance (px, py, ix, iy)
    end if
end MathDistancePointLine
proc drawscreen
    drawfillbox (0, 0, maxx, maxy, red)     % Border
    drawfillbox (20, 20, maxx - 20, maxy - 20, 1)     %Background
    drawfillbox (20, maxy div 2 - 150, maxx - 20, maxy div 2 - 155, red)     % bottom line from center
    drawfillbox (20, maxy div 2 + 150, maxx - 20, maxy div 2 + 155, red)     % top line from center
    drawfillbox (20, 20, maxx - 20, 30, grey)     % bottom
    drawfillbox (maxx div 2 - 70, 20, maxx div 2 + 70, 40, black)     % net
    drawfillbox (20, maxy - 20, maxx - 20, maxy - 30, grey)     % top
    drawfillbox (maxx div 2 - 70, maxy - 20, maxx div 2 + 70, maxy - 40, black)     % net
    drawfillbox (20, maxy div 2, maxx - 20, maxy div 2 + 5, grey)     %center line
    %Center Circle
    drawfilloval (maxx div 2, maxy div 2, 80, 80, grey)
    drawfilloval (maxx div 2, maxy div 2, 70, 70, 1)
    drawfilloval (maxx div 2, maxy div 2, 10, 10, grey)
    % Left top circle
    drawfilloval (maxx div 2 - 200, maxy div 2 + 250, 70, 70, grey)
    drawfilloval (maxx div 2 - 200, maxy div 2 + 250, 60, 60, 1)
    % Right top circle
    drawfilloval (maxx div 2 + 200, maxy div 2 + 250, 70, 70, grey)
    drawfilloval (maxx div 2 + 200, maxy div 2 + 250, 60, 60, 1)
    % Right bottom
    drawfilloval (maxx div 2 + 200, maxy div 2 - 250, 70, 70, grey)
    drawfilloval (maxx div 2 + 200, maxy div 2 - 250, 60, 60, 1)
    % Left bottom
    drawfilloval (maxx div 2 - 200, maxy div 2 - 250, 70, 70, grey)
    drawfilloval (maxx div 2 - 200, maxy div 2 - 250, 60, 60, 1)
    %ball
    if ballx > 35 and ballx < maxx - 35 and bally > 35 and bally < maxy - 35 then
        drawfilloval (round (ballx), round (bally), ballr, ballr, black)
    end if
    %paddle
    drawfilloval (paddlex, paddley, paddler, paddler, 12)
    drawfilloval (cpux, cpuy, cpupaddler, cpupaddler, 12)
    locate (1, 1)
    put "Computer score:", a
    locate (1, 30)
    put "Player score:", b
    View.Update
end drawscreen

proc detection
    %if the ball hits either end then make it go in the opposite direction
    if ballx > maxx - 35 then
        dx := abs (dx) * -1
    elsif ballx < 35 then
        dx := abs (dx)
    end if
    %if the ball hits either side then make it go in the opposite direction
    if bally > maxy - 35 then
        dy := abs (dy) * -1
    elsif bally < 35 then
        dy := abs (dy)
    end if

    %if the distance between the ball and the paddle is less than 50 (touching) then change direction
    if MathDistancePointLine (ballx, bally, paddlex, paddley, px, py) < paddler + ballr then
        if not hit then
            dx := (ballx - px) / 5
            dy := (bally - py) / 5
            ballx := paddlex
            bally := paddley
        end if
        hit := true
    end if

    if (sqrt ((ballx - cpux) ** 2 + (bally - cpuy) ** 2)) < cpupaddler + ballr then
        dx := (ballx - cpux) / 5
        dy := (bally - cpuy) / 5
        numofhits := numofhits + 1
    end if
end detection

beginning

loop
    exit when ex = 2
    paddler := 35 - b * 2
    cpuy := maxy - 100
    if bally < maxy div 2 then
        numofhits := 0
    end if
    if numofhits > 3 then
        cpux := maxx div 2
        cpuy := maxy - 110
    end if
    if MathDistancePointLine (ballx, bally, paddlex, paddley, px, py) > paddler + ballr then
        hit := false
    end if
    %USER PADDLE
    mousewhere (x, y, button)
    %if not hit then
    if y + paddler > maxy div 2 then     %if the mouse is above the center line, the paddle stays at center line
        paddley := maxy div 2 - paddler
    elsif y < paddler + 20 then
        paddley := paddler + 20
    else
        paddley := y
    end if
    if x > maxx - paddler + 20 then     %if the mouse is off the screen to the right then make the paddle be at the
        paddlex := maxx - (paddler + 20)     %furthest point horizontally on the screen (maxx)
    elsif x < paddler + 20 then     %**same as above but for the left side**
        paddlex := paddler + 20
    else
        paddlex := x
    end if
    %end if

    %BALL MOVEMENT
    ballx += dx     %make the ball move
    bally += dy     %""

    %COMPUTER PADDLE
    cpux := cpux + cpuxd     % makes comp paddle move

    if cpux > ballx then     % checks if cpu x coordinate is greater than ball x coordinate
        cpuxd := cpuxd - 1     % comp paddle x value moves negative direction
    elsif
            cpux < ballx then     % checks if cpu x coordinate is less than ball x coordinate
        cpuxd := cpuxd + 1     % comp paddle x value moves positive direction
    end if



    % Computer paddle speed doesn't go past assigned maximum speed
    if cpuxd > maxspeed then     %
        cpuxd := maxspeed
    elsif cpuxd < maxspeed * -1 then
        cpuxd := maxspeed * -1
    end if

    % Keeps computer paddle inside the table
    if cpux > maxx - 100 then
        cpux := maxx - 100
    elsif
            cpux < 100 then
        cpux := 100
    end if


    detection

    if ballx > maxx div 2 - 70 and ballx < maxx div 2 + 70 and bally < 35 then
        a := a + 1
        ballx := maxx div 2
        bally := maxy div 2
        delay (500)
    elsif ballx > maxx div 2 - 70 and ballx < maxx div 2 + 70 and bally > maxy - 35 then
        b := b + 1
        ballx := maxx div 2
        bally := maxy div 2
        delay (500)
    end if
    drawscreen
    if a = 15 then
        cls
        put "Computer Wins. Please play again."
        exit
    elsif b = 15 then
        cls
        put "YOU WIN!"
        exit
    end if
    if not hit then
        px := paddlex
        py := paddley
    end if
end loop
Sponsor
Sponsor
Sponsor
sponsor
upthescale




PostPosted: Sat Jun 24, 2006 11:16 am   Post subject: (No subject)

awsome grphics man, good game, cept at the top put import GUI because you had some gui graphics

*Edit, wops u did put import GUI just it wasn't in the code lol
PhOtO!




PostPosted: Sat Jun 24, 2006 6:43 pm   Post subject: (No subject)

I'm just here if anyone decide's to give bits... Wink
Try changing the ball radius (ballr) to 30, i enjoy it more like that.
Tony




PostPosted: Sat Jun 24, 2006 7:01 pm   Post subject: (No subject)

it's pretty cool, though the game bugged out on me, and as the puck spawened, it would bounce of the wall and go directly into the computer's net every time. I scored 13 points in less than 30 seconds without moving my mouse
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Slaivis




PostPosted: Sat Jun 24, 2006 7:52 pm   Post subject: (No subject)

Tony wrote:
it's pretty cool, though the game bugged out on me, and as the puck spawened, it would bounce of the wall and go directly into the computer's net every time. I scored 13 points in less than 30 seconds without moving my mouse


Yeah, a similar thing happened to me.

The computer scored a few points on me, then at some point the puck simply generated itself in a position in which it would bounce into the computer's net before the computer could block it.

Very nice game, though!
MysticVegeta




PostPosted: Sun Jun 25, 2006 11:11 am   Post subject: (No subject)

Its a pretty nice game. I enjoyed playing it. but you know a body in motion has to come to rest by friction. when I made the ball go up and down without both computer or me touching, it never came to rest.
upthescale




PostPosted: Sun Jun 25, 2006 12:25 pm   Post subject: (No subject)

in this case, makes the ablls varialbes "real"
xAXISx




PostPosted: Thu Jun 29, 2006 6:09 pm   Post subject: (No subject)

I like it a lot, and other than that little bug, good job. I like how fast it is, you suck at it when you start, but after 15 minutes of playing you own. It would make a good First Person Shooter training game, since it requires you to be active all the time.
Sponsor
Sponsor
Sponsor
sponsor
xtremehacker




PostPosted: Thu Sep 07, 2006 6:08 pm   Post subject: (No subject)

This is a really cool game the graphics are awesome and it runs at a good speed good work.
TokenHerbz




PostPosted: Thu Sep 07, 2006 9:55 pm   Post subject: (No subject)

Xtremehacker......

If you dont stop posting in posts that are inactive, your going to get in alot of trouble...


one month, maybe... if you comment is good, which it wasn't,

then i see you did it in a 2month dead post, again, you comment wasn't worth bumping the topic up.

This one hits the 3month mark, which is just retarded.

Stop this at once, its forbidden on compsci.ca.
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  [ 10 Posts ]
Jump to:   


Style:  
Search: