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

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




PostPosted: Fri Oct 20, 2006 11:16 am   Post subject: Cheap Air Hockey

This is my 2nd program. Its a very cheap air hockey kinda game. Moving the ball left is very hard unless your right on it. Opinions and suggestions are appreciated.

code:

%********************************
%name block    ICS    ICS     ICS     ICS     ICS
%name - James Nguyen
%date - September 18, 2006
%program description - air hockey game using input keydown
%*********************************
%
%
%*********************************
%Algorithm
%A set of instructions in order, used to pre-plan a program
%1. Tells the players the controls and what the scores up to [using put]
%2. Game begins while players try to score on each other [using ifs to check if anything touches]
%3. Game ends when either player hits 5 points [checks with ifs]
%*********************************
%
%
%*********************************
%Variables Dictionary
%declare variable by name and type
var chars : array char of boolean %....used for the input keydown
var x1 : int := 325 %..................x location of gamepiece
var y1 : int := 150 %..................y location of gamepiece
var compscore : int := 0 %.............comp score
var playerscore : int := 0 %...........playa score
var x2 : int := 325 %..................x location of ball
var y2 : int := 250 %..................y location of ball
var r : int := 25 %....................radius of the paddle
var r2 : int := 25 %...................radius of the ball
var xchange, ychange : int := 2 %......changes position of puck
var xchange2, ychange2 : int := 15 %...changes position of puck
var enemyx : int := 325 %..............player 2 x coordinates
var enemyy : int := 380 %..............player 2 y coordinates
var enemyr : int := 25 %...............player 2 radius
var font1 : int
font1 := Font.New ("serif:30")
assert font1 > 0
%*********************************
%
%
%*********************************
%Procedures/Subprograms/Subroutines
%do each seperatly in a block

%<><><><><><><><><><><><><><><><><><>THE SCREEN BACKROUND
proc backround
    drawfillbox (0, 0, 639, 479 - 30, red)     %........border
    drawfillbox (10, 10, 639 - 10, 479 - 10, brightblue) %......backround
    drawfillbox (270, 0, 400, 20, black)     %..........net
    drawfillbox (270, 450, 400, 430, black)     %.......net
    drawfillbox (0, 445, 639, 450, red) %...............line
    drawfillbox (0, 245, 639, 255, red) %...............border
    drawfilloval (639, 250, 25, 25, red)     %......,...trap1
    drawfilloval (639, 250, 15, 15, brightblue)    %....trap1
    drawfilloval (0, 250, 25, 25, red)     %............trap2
    drawfilloval (0, 250, 15, 15, brightblue)     %.....trap2
    drawfilloval (325, 250, 55, 55, red) %..............circle in the middle
    drawfilloval (325, 250, 45, 45, brightblue) %.......middle
    drawfillbox (0, 110, 639, 120, red) %...............draws lines around border
    drawfillbox (0, 390, 639, 380, red) %...............draws lines around border
    drawfilloval (120, 0, 50, 50, red) %................draws half circles around border
    drawfilloval (120, 0, 35, 35, brightblue) %.........draws half circles around border
    drawfilloval (525, 0, 50, 50, red) %................draws half circles around border
    drawfilloval (525, 0, 35, 35, brightblue) %.........draws half circles around border
    drawfilloval (120, 449, 50, 50, red) %..............draws half circles around border
    drawfilloval (120, 449, 35, 35, brightblue) %.......draws half circles around border
    drawfilloval (525, 449, 50, 50, red) %..............draws half circles around border
    drawfilloval (525, 449, 35, 35, brightblue) %.......draws half circles around border
    drawfillbox (0, 450, 639, 479, white)         %.....top
    locate (1, 30)     %................................locates the put
    put "Player 2 score: ", compscore ..     %..........shows the score
    locate (1, 1)     %.................................locates the put
    put "Player 1 score: ", playerscore ..     %........shows the score
end backround

%<><><><><><><><><><><><><><><><><><>GAMEPIECE OF THE PADDLE
proc gamepiece
    drawfilloval (x1, y1, r, r, 71) %...............draws the main part of game piece
    drawfilloval (x1, y1, 20, 20, yellow) %.........draws the handle of the paddle
    drawfilloval (x1, y1, 18, 18, brightblue) %.....draws the handle of the paddle
    drawfilloval (x1, y1, 15, 15, black)    %.......draws the handle of the paddle
    drawfilloval (x1, y1, 12, 12, grey)        %....draws the handle of the paddle
    drawfilloval (x1, y1, 10, 10, 6)        %.......draws the handle of the paddle
    drawfilloval (x1, y1, 8, 8, 10)            %....draws the handle of the paddle
    drawfilloval (x1, y1, 6, 6, 13)             %...draws the handle of the paddle
end gamepiece

%<><><><><><><><><><><><><><><><><><>THE BALL
proc puck
    drawfilloval (x2, y2, r2, r2, black) %..........draws the ball
end puck

%<><><><><><><><><><><><><><><><><><>COMPUTER PADDLE
proc comp
    drawfilloval (enemyx, enemyy, enemyr, enemyr, 13) %...parts of the computer paddle
    drawfilloval (enemyx, enemyy, 20, 20, 14) %...........parts of the computer paddle
    drawfilloval (enemyx, enemyy, 18, 18, 46) %...........parts of the computer paddle
    drawfilloval (enemyx, enemyy, 15, 15, 54) %...........parts of the computer paddle
    drawfilloval (enemyx, enemyy, 12, 12, 64) %...........parts of the computer paddle
    drawfilloval (enemyx, enemyy, 10, 10, 66) %...........parts of the computer paddle
    drawfilloval (enemyx, enemyy, 8, 8, 71) %.............parts of the computer paddle
    drawfilloval (enemyx, enemyy, 6, 6, 55) %.............parts of the computer paddle
end comp

%<><><><><><><><><><><><><><><><><><>CONTROLS TO THE GAME
proc controls
    loop %.......................................repeats forever
        colourback (white)
        cls % ...................................clears screen
        locate (10, 17) %........................locates put
        put "Welcome to Air Hockey! Use the keydown keys to move." %intro
        locate (11, 12) %........................locates put
        put "Controls are the keydown for player 1 and w,a,s,d for player 2." %intro
        View.Update %............................puts things into memory
        locate (12, 15) %........................locates the put
        View.Update %............................puts things into memory
        put "First to 5 points wins! Press any keydown keys to continue." %intro
        View.Update %............................displays the intro
        exit when hasch % .......................exits the loop when any key is pressed
    end loop
end controls

%<><><><><><><><><><><><><><><><><><>INTRO SCREEN
proc intro
    loop %...............................................repeats forever
        for c : 1 .. 255 by 2 %..........................makes fancy border
            drawbox (0 + c, 0 + c, 639 - c, 479 - c, c) %draws border
            delay (10) %.................................delays how fast border is drawn
            View.Update %................................shows the border
        end for
        locate (20, 29) % ...............................locates put
        colourback (yellow) % ...........................changes backround colour
        Font.Draw ("Air Hockey", 230, 270, font1, brightred) %draws font
        put "Press any key to continue" .. % ............tells playa to hit any key
        exit when hasch % ...............................exits the loop when any key is pressed
    end loop
end intro

%*********************************
%
%
%*********************************
%Mainline
%step by step enter the program
setscreen ("offscreenonly")     %............puts things into memory
intro % .....................................shows intro
controls % ..................................shows controls
backround %..................................draws backround
gamepiece %..................................draws gamepiece
puck %.......................................draws ball
comp %.......................................draws computer
loop %.......................................repeats forever
    backround     %..........................puts backround up again
    gamepiece     %..........................puts gamepiece on screen
    puck     %...............................puts ball on screen
    comp %...................................draws computer
    % locate (5, 1)     %....................locates the put
    % put x1 : 5, y1 : 5 ..     %............shows where x and y coordinates are
    % locate (6, 1)     %....................locates the put
    % put x2 : 5, y2 : 5 ..     %............shows where x and y coordinates are
    % locate (7, 1)
    % put enemyx : 5, enemyy : 5 ..
    Input.KeyDown (chars)     %..................checks for what characters are touched

    if chars (KEY_UP_ARROW) then     %...........checks if the player touches up key
        delay (2)     %..........................slows the game
        View.Update     %........................puts things into memory
        y1 := y1 + 2     %.......................moves the gamepiece
    end if

    if chars (KEY_RIGHT_ARROW) then     %........checks if player touches right arrow
        delay (2)     %..........................slows the game
        View.Update     %........................puts things into memory
        x1 := x1 + 2    %........................moves the gamepiece
    end if

    if chars (KEY_LEFT_ARROW) then     %.........checks if player touches left key
        delay (2)     %..........................slows the game
        View.Update     %........................puts things into memory
        x1 := x1 - 2     %.......................moves the gamepiece
    end if

    if chars (KEY_DOWN_ARROW) then     %.........checks if player touches down key
        delay (2)     %..........................slows the game
        View.Update     %........................puts things into memory
        y1 := y1 - 2     %.......................moves the gamepiece
    end if

    %............................................checks if gamepiece touches the puck
    if Math.Distance (x1, y1, x2, y2) < r * 2 and x1 > x2 then
        x1 += xchange     %......................changes position of puck
        x2 -= xchange2     %.....................changes position of puck
    end if

    %............................................checks if gamepiece touches the puck
    if Math.Distance (x1, y1, x2, y2) < r * 2 and y1 < y2 then
        y1 -= ychange     %......................changes position of puck
        y2 += ychange2     %.....................changes position of puck
    end if

    %............................................checks if gamepiece touches the puck
    if Math.Distance (x1, y1, x2, y2) < r * 2 and y1 > y2 then
        y1 += ychange     %......................changes position of puck
        y2 -= ychange2     %.....................changes position of puck
    end if

    %............................................checks if gamepiece touches the puck
    if Math.Distance (x1, y1, x2, y2) < r * 2 and x1 < x2 then
        x1 -= xchange     %......................changes position of puck
        x2 += xchange2     %.....................changes position of puck
    end if

    % drawfillbox (270, 0, 400, 20, black)     %..........net
    % drawfillbox (270, 450, 400, 430, black)     %.......net

    %bottom net
    if x2 > 245 and x2 < 425 and y2 > 0 and y2 < 45 then   %checks if the puck touches net
        compscore := compscore + 1     %....................adds points
        for c : 1 .. 150 %..................................makes colours
            drawfillbox (270, 0, 400, 20, c) %..............flashes net
            locate (1, 30) %................................locates the put
            colour (c) %....................................changes text colour
            put "Player 2 score: ", compscore .. %..........shows the score
            View.Update %...................................text will show
            delay (10) %....................................slows how fast colours go on
        end for
        x2 := 325 %.........................................reset values for the puck
        y2 := 250 %.........................................reset values for the puck
        x1 := 325 %.........................................reset values for the playa 1
        y1 := 150 %.........................................reset values for the playa 1
        enemyx := 325 %.....................................reset values for the playa 2
        enemyy := 380 %.....................................reset values for the playa 2
    end if
    %top net
    if x2 > 245 and x2 < 425 and y2 > 410 and y2 < 421 then %checks if the puck touches net
        playerscore := playerscore + 1     %.................adds points
        for c : 1 .. 150 %...................................adds fancy colours
            drawfillbox (270, 450, 400, 430, c) %............flashes net
            locate (1, 1) %..................................locates the put
            colour (c) %.....................................changes colour of text
            put "Player 1 score: ", playerscore .. %.........shows the score
            View.Update %....................................shows the text
            delay (10) %.....................................you can see the colour
        end for
        x2 := 325 %..........................................reset values for the puck
        y2 := 250 %..........................................reset values for the puck
        x1 := 325         %..................................reset values for the playa 1
        y1 := 150 %..........................................reset values for the playa 1
        enemyx := 325 %......................................reset values for the playa 2
        enemyy := 380 %......................................reset values for the playa 2
    end if

    %............................................checks if gamepiece touches the puck
    if Math.Distance (enemyx, enemyy, x2, y2) < enemyr * 2 and enemyx > x2 then
        enemyx += xchange     %..................changes position of puck
        x2 -= xchange2     %.....................changes position of puck
    end if

    %............................................checks if gamepiece touches the puck
    if Math.Distance (enemyx, enemyy, x2, y2) < enemyr * 2 and enemyy < y2 then
        enemyy -= ychange    %...................changes position of puck
        y2 += ychange2     %.....................changes position of puck
    end if

    %............................................checks if gamepiece touches the puck
    if Math.Distance (enemyx, enemyy, x2, y2) < enemyr * 2 and enemyy > y2 then
        enemyy += ychange     %..................changes position of puck
        y2 -= ychange2     %.....................changes position of puck
    end if

    %............................................checks if gamepiece touches the puck
    if Math.Distance (enemyx, enemyy, x2, y2) < enemyr * 2 and enemyx < x2 then
        enemyx -= xchange     %..................changes position of puck
        x2 += xchange2     %.....................changes position of puck
    end if

    %........................................prevents game piece from goin left off screen
    if x1 < 30 then     %....................checks if gamepiece goes too far left
        x1 := x1 + 2     %...................makes sure gamepiece doesn't go off screen
    end if

    %........................................prevents game piece from going off screen
    if y1 > 420 then     %...................checks if game piece goes too far off screen
        y1 := y1 - 2     %...................makes sure gamepiece doesn't go off screen
    end if

    %........................................prevents game piece from going off screen
    if x1 > 610 then     %...................checks if game piece goes too far off screen
        x1 := x1 - 2     %...................makes sure gamepiece doesn't go off screen
    end if

    %........................................prevents game piece from going off the screen
    if y1 < 30 then     %....................checks if game piece goes too far off screen
        y1 := y1 + 3     %...................makes sure gamepiece doesn't go off screen
    end if

    %........................................prevents game piece from goin left off screen
    if x2 < 30 then     %....................checks if gamepiece goes too far left
        x2 := x2 + 75     %..................makes the ball bounce back
    end if

    %........................................prevents game piece from going off screen
    if y2 > 420 then     %...................checks if game piece goes too far off screen
        y2 := y2 - 75    %...................makes the ball bounce back
    end if

    %........................................prevents game piece from going off screen
    if x2 > 610 then     %...................checks if game piece goes too far off screen
        x2 := x2 - 75    %...................makes the ball bounce back
    end if

    %........................................prevents game piece from going off the screen
    if y2 < 30 then     %....................checks if game piece goes too far off screen
        y2 := y2 + 75    %...................makes the ball bounce back
    end if

    %........................................makes sure enemys paddle doesn't move off screen

    %........................................prevents game piece from goin left off screen
    if enemyx < 30 then     %................checks if gamepiece goes too far left
        enemyx := enemyx + 2    %............makes sure gamepiece doesn't go off screen
    end if

    %........................................prevents game piece from going off screen
    if enemyy > 420 then     %...............checks if game piece goes too far off screen
        enemyy := enemyy - 2    %............makes sure gamepiece doesn't go off screen
    end if

    %........................................prevents game piece from going off screen
    if enemyx > 610 then     %...............checks if game piece goes too far off screen
        enemyx := enemyx - 2     %...........makes sure gamepiece doesn't go off screen
    end if

    %........................................prevents game piece from going off the screen
    if enemyy < 30 then     %................checks if game piece goes too far off screen
        enemyy := enemyy + 2    %............makes sure gamepiece doesn't go off screen
    end if

    if chars ('w') then         %...........checks if the player touches up key
        delay (2)     %.....................slows the game
        View.Update     %...................puts things into memory
        enemyy := enemyy + 2     %..........moves the gamepiece
    end if

    if chars ('d') then     %...............checks if player touches right arrow
        delay (2)     %.....................slows the game
        View.Update     %...................puts things into memory
        enemyx := enemyx + 2    %...........moves the gamepiece
    end if

    if chars ('a') then     %...............checks if player touches left key
        delay (2)     %.....................slows the game
        View.Update     %...................puts things into memory
        enemyx := enemyx - 2     %..........moves the gamepiece
    end if

    if chars ('s') then     %...............checks if player touches down key
        delay (2)     %.....................slows the game
        View.Update     %...................puts things into memory
        enemyy := enemyy - 2    %...........moves the gamepiece
    end if

    %............................................checks if player 2 touches player 1
    if Math.Distance (enemyx, enemyy, x1, y1) < enemyr * 2 and enemyx > x1 then
        enemyx := enemyx + 2 %...................changes position of player
        x1 := x1 - 2 %...........................changes position of player
    end if

    %............................................checks if player 2 touches player 1
    if Math.Distance (enemyx, enemyy, x1, y1) < enemyr * 2 and enemyy < y1 then
        enemyy := enemyy - 2 %...................changes position of player
        y1 := y1 + 2 %...........................changes position of player
    end if

    %............................................checks if player 2 touches player 1
    if Math.Distance (enemyx, enemyy, x1, y1) < enemyr * 2 and enemyy > y1 then
        enemyy := enemyy + 2 %...................changes position of player
        y1 := y1 - 2 %...........................changes position of player
    end if

    %............................................checks if player 2 touches player 1
    if Math.Distance (enemyx, enemyy, x1, y1) < enemyr * 2 and enemyx < x1 then
        enemyx := enemyx - 2 %...................changes position of player
        x1 := x1 + 2 %...........................changes position of player
    end if
    %............................................exits after 5 points
    exit when playerscore = 5 or compscore = 5

end loop
%................................................checks if either player hits 5 points
if playerscore = 5 then
    cls %........................................clears screen
    locate (11, 35) %............................locates put
    put "Player 1 has won." %....................tells player who wins
    View.Update %................................makes sure everything is shown
    locate (12, 40) %............................locates put
    put "Credits" %..............................displays credits
    locate (13, 33) %............................locates put
    put "Made by James Nguyen" %.................displays credits
    locate (14, 26) %............................locates put
    put "With help from Cody Beckett Patterson" %displays credits
end if
%................................................checks if either player hits 5 points
if compscore = 5 then
    cls %........................................clears screen
    put "Player 2 has won." %....................tells player who wins
    View.Update %................................makes sure everything is shown
    locate (12, 40) %............................locates put
    put "Credits" %..............................displays credits
    locate (13, 33) %............................locates put
    put "Made by James Nguyen" %.................displays credits
    locate (14, 26) %............................locates put
    put "With help from Cody Beckett Patterson" %displays credits
end if
%*********************************
Sponsor
Sponsor
Sponsor
sponsor
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  [ 1 Posts ]
Jump to:   


Style:  
Search: