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

Username:   Password: 
 RegisterRegister   
 Collision in Air Hockey Game
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Master Programmer Dude




PostPosted: Fri Apr 22, 2005 10:34 am   Post subject: Collision in Air Hockey Game

theres a prob i wanna change the colour in the maple leafs but the puck thinks that its a paddle whats wrong?



code:
% Air Hockey V.3



View.Set ("graphics:800;600,title:Air Hockey, offscreenonly, nocursor")

var puckX, puckY : int := 0
var pad1X, pad1Y, button : int := 0
var pad2X, pad2Y : int := 0
var changeX, changeY := 10
var playerScore, compuScore : int := 0
var sign1X : int := 0
var sign2X : int := 480
var font1 : int := Font.New ("Courier New:40:bold")
var font2 : int := Font.New ("Courier New:20:bold")
var font3 : int := Font.New ("Courier New:10:bold")
var timeLeft : int := 6000
var keyPressed : string (1)
var signSwitch : boolean := false
var overTime : boolean := false

procedure Reset (restart : boolean)
    changeX := -10
    changeY := -10
    puckX := 650
    puckY := 300
    pad2X := 740
    pad2Y := 250
    if restart then
        playerScore := 0
        compuScore := 0
        timeLeft := 6000
        overTime := false
    end if
end Reset

procedure Intro
    loop
        Text.Cls
        Font.Draw ("Press any key to continue", 260, 25, font3, blue)
        Font.Draw ("Air Hockey", sign1X, 500, font1, blue)
        Font.Draw ("Air Hockey", sign2X, 100, font1, blue)
        Font.Draw ("Instructions", 350, 450, font3, blue)
        Font.Draw ("Move the mouse to block the puck", 250, 400, font3, blue)
        Font.Draw ("from entering your net", 250, 380, font3, blue)
        Font.Draw ("Keyboard:", 250, 325, font3, blue)
        Font.Draw ("p - Pauses the game", 250, 305, font3, blue)
        Font.Draw ("x - Exits current game and", 250, 275, font3, blue)
        Font.Draw ("    returns to this screen", 250, 255, font3, blue)
        Font.Draw ("r - Refreshs the screen", 250, 235, font3, blue)
        Font.Draw ("Each game lasts one minute", 250, 170, font3, blue)
        if sign1X <= 0 then
            signSwitch := true
        elsif sign1X >= 460 then
            signSwitch := false
        end if
        if signSwitch then
            sign1X += 1
            sign2X -= 1
        elsif signSwitch = false then
            sign1X -= 1
            sign2X += 1
        end if
        View.Update
        exit when hasch
        Time.Delay (10)
    end loop
    Reset (true)
end Intro

loop
    Intro
    loop
        % Time
        timeLeft -= 2
        if timeLeft <= 0 then
            if playerScore > compuScore then
                Font.Draw ("Player Wins", 300, 240, font2, black)
                View.Update
                getch (keyPressed)
                Intro
            elsif playerScore < compuScore then
                Font.Draw ("Computer Wins", 285, 240, font2, black)
                View.Update
                getch (keyPressed)
                Intro
            elsif playerScore = compuScore then
                timeLeft := 0
                overTime := true
            end if
        end if
        % Controls
        Mouse.Where (pad1X, pad1Y, button)
        if pad1X >= 365 then
            pad1X := 365
        elsif pad1X <= 35 then
            pad1X := 35
        end if
        if pad1Y >= 465 then
            pad1Y := 465
        elsif pad1Y <= 35 then
            pad1Y := 35
        end if
        if hasch then
            getch (keyPressed)
            if keyPressed = "p" then
                getch (keyPressed)
            elsif keyPressed = "r" then
                puckX := 600
                puckY := 250
                pad1X := 50
                pad1Y := 250
                pad2X := 700
                pad2Y := 250
            elsif keyPressed = "x" then
                exit
            end if
        end if
        % Computer
        if changeX >= 0 then
            pad2X += 3
        elsif changeX <= 0 then
            pad2X -= 3
        end if
        if changeY >= 0 then
            pad2Y += 3
        elsif changeY <= 0 then
            pad2Y -= 3
        end if
        if pad2X >= 765 then
            pad2X := 765
        elsif pad2X <= 35 then
            pad2X := 35
        end if
        if pad2Y >= 465 then
            pad2Y := 465
        elsif pad2Y <= 35 then
            pad2Y := 35
        end if
        % Puck
        puckX += changeX
        puckY += changeY
        if whatdotcolor (puckX + 11, puckY) = red then
            changeX := -changeX
            puckX += changeX
            puckY += changeY
        end if
        if whatdotcolor (puckX - 11, puckY) = red then
            changeX := -changeX
            puckX += changeX
            puckY += changeY
        end if
        if whatdotcolor (puckX, puckY + 11) = red then
            changeY := -changeY
            puckX += changeX
            puckY += changeY
        end if
        if whatdotcolor (puckX, puckY - 11) = red then
            changeY := -changeY
            puckX += changeX
            puckY += changeY
        end if
        if whatdotcolor (puckX + 13, puckY + 13) = red then
            changeX := -changeX
            changeY := -changeY
            puckX += changeX
            puckY += changeY
        end if
        if whatdotcolor (puckX + 13, puckY - 13) = red then
            changeX := -changeX
            changeY := -changeY
            puckX += changeX
            puckY += changeY
        end if
        if whatdotcolor (puckX - 13, puckY + 13) = red then
            changeX := -changeX
            changeY := -changeY
            puckX += changeX
            puckY += changeY
        end if
        if whatdotcolor (puckX - 13, puckY - 13) = red then
            changeX := -changeX
            changeY := -changeY
            puckX += changeX
            puckY += changeY
        end if
        if puckX <= 10 or puckX >= 790 then
            changeX := -changeX
            sound (500, 10)
        end if
        if puckY <= 10 or puckY >= 490 then
            changeY := -changeY
            sound (500, 10)
        end if
        % Graphics
        Text.Cls
        locate (29, 80)
        Font.Draw ("Player", maxx div 2 - 300, maxy - 50, font2, black)
        Font.Draw (intstr (playerScore), maxx div 2 - 260, maxy - 85, font2, black)
        Font.Draw ("Computer", maxx div 2 + 175, maxy - 50, font2, black)
        Font.Draw (intstr (compuScore), maxx div 2 + 235, maxy - 85, font2, black)
        if overTime then
            Font.Draw ("Over Time", maxx div 2 - 170, maxy - 60, font1, black)
        else
            Font.Draw (realstr (timeLeft / 100, 0), maxx div 2 - 80, maxy - 60, font1, black)
        end if
        Draw.FillMapleLeaf (300, 20, 480, 200, red)
        Draw.FillMapleLeaf (300, 480, 480, 300, red)
        Draw.Box (10, 10, 790, 490, black)
        Draw.FillBox (0, 175, 10, 325, purple)
        Draw.FillBox (790, 175, 800, 325, purple)
        Draw.FillOval (pad1X, pad1Y, 25, 25, red)
        Draw.FillOval (pad2X, pad2Y, 25, 25, red)
        Draw.FillOval (puckX, puckY, 10, 10, black)
        % Scoring
        if puckX + 10 >= maxx - 10 and puckY > 185 and puckY < 315 then
            Reset (false)
            playerScore += 1
            Font.Draw ("Goal", 355, 240, font2, black)
            View.Update
            getch (keyPressed)
        end if
        if puckX - 10 <= 10 and puckY > 185 and puckY < 315 then
            Reset (false)
            compuScore += 1
            Font.Draw ("Goal", 355, 240, font2, black)
            View.Update
            getch (keyPressed)
        end if
        % Others
        View.Update
        Time.Delay (10)
    end loop
end loop


EDIT [Martin]: Fixed it up for you. Everybody gets one.
Sponsor
Sponsor
Sponsor
sponsor
Martin




PostPosted: Fri Apr 22, 2005 10:37 am   Post subject: (No subject)

Edit your post and give it a more descriptive title please. Also use [code] tags.
Mazer




PostPosted: Fri Apr 22, 2005 10:42 am   Post subject: (No subject)

And posting in the right section never hurts.
StarGateSG-1




PostPosted: Fri Apr 22, 2005 10:46 am   Post subject: (No subject)

What the H*** do you mean, I don't understand that questions at all.
Tony




PostPosted: Fri Apr 22, 2005 11:49 am   Post subject: (No subject)

StarGateSG-1 wrote:
What the H*** do you mean, I don't understand that questions at all.

Laughing

Back to the question
air hockey code wrote:

if whatdotcolor (puckX + 11, puckY) = red then
changeX := -changeX

oh hey, guess what else on your screen is (or trying to be) red!

If you insist on using whatdotcolor(), then you have to use unique colours for everything.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: