Computer Science Canada turing pong |
Author: | gardnega [ Mon Jun 08, 2009 7:41 am ] |
Post subject: | turing pong |
View.Set ("graphics: 526;640, offscreenonly, position: center;center") var x1, x2 : int % paddle at bottom var xx1, xx2 : int % paddle at top var chars : array char of boolean % movement of paddles var circlex, circley, circleradius : int % first circle variables var circle1x, circle1y, circle1radius : int % second circle variables var xdir, ydir : int % for movement of 1st circle var xdir1, ydir1 : int % for movement of 2nd circle var scoreint1, scoreint2 : int % scores var scorestr1, scorestr2 : string % change the scores into a string var colourcount : int := 32 % make ball flash colours var score : int % for the score font var scoreposition1, scoreposition2 : int % To move the score when > 9 var winner : int % Winner font var playagain : int % playagain font var key : string (1) % for getch var title : int % title font % Give the variables values circlex := 50 circley := 60 circleradius := 8 circle1x := 50 circle1y := 60 circle1radius := 8 x1 := 20 x2 := 140 xx1 := 300 xx2 := 420 xdir := 5 ydir := 5 xdir1 := 4 ydir1 := 4 scoreint1 := 0 scoreint2 := 0 scoreposition1 := 203 scoreposition2 := 203 % Font Declarations score := Font.New ("Swis721 Ex BT:160:bold") assert score > 0 winner := Font.New ("Arial:43:bold") assert winner > 0 playagain := Font.New ("Arial:27:bold") assert playagain > 0 title := Font.New ("Swis721 Ex BT:110:bold") assert title > 0 loop cls %background drawfill (maxx, maxy, 255, 255) %lines across middle drawfillbox (0, 314, 1, 326, 40) drawfillbox (11, 314, 40, 326, 40) drawfillbox (50, 314, 80, 326, 40) drawfillbox (90, 314, 120, 326, 40) drawfillbox (130, 314, 160, 326, 40) drawfillbox (170, 314, 200, 326, 40) drawfillbox (210, 314, 240, 326, 40) drawfillbox (250, 314, 280, 326, 40) drawfillbox (290, 314, 320, 326, 40) drawfillbox (330, 314, 360, 326, 40) drawfillbox (370, 314, 400, 326, 40) drawfillbox (410, 314, 440, 326, 40) drawfillbox (450, 314, 480, 326, 40) drawfillbox (490, 314, 520, 326, 40) % title Font.Draw ("PONG!", 0, 400, title, 40) Font.Draw ("Press any key to continue.", 34, 200, playagain, 40) locatexy (1, 1) getch (key) View.Update cls %clear %background drawfill (maxx, maxy, 255, 255) %lines across middle drawfillbox (0, 314, 1, 326, 40) drawfillbox (11, 314, 40, 326, 40) drawfillbox (50, 314, 80, 326, 40) drawfillbox (90, 314, 120, 326, 40) drawfillbox (130, 314, 160, 326, 40) drawfillbox (170, 314, 200, 326, 40) drawfillbox (210, 314, 240, 326, 40) drawfillbox (250, 314, 280, 326, 40) drawfillbox (290, 314, 320, 326, 40) drawfillbox (330, 314, 360, 326, 40) drawfillbox (370, 314, 400, 326, 40) drawfillbox (410, 314, 440, 326, 40) drawfillbox (450, 314, 480, 326, 40) drawfillbox (490, 314, 520, 326, 40) % How to play screen Font.Draw ("How to play", 90, 550, winner, 40) color (40) colorback (255) locatexy (0, 475) put "Player 1- You are the one on the bottom. You control your paddle with the left and right arrow keys." locatexy (0, 410) put "Player 2- You are the one on the top. You control your paddle" put "with the key 'a' for left and 'd' for right." locatexy (0, 250) put "The objective is to keep the ball from touching your side." locatexy (0, 200) put "If the ball touches your opponents side, then you receive a point" locatexy (0, 150) put "The first person to 25 points wins. After both players are at 10 points another ball will appear and the first will slow down." View.Update getch (key) % wait for key loop % to make ball flash colourcount := colourcount + 1 if colourcount > 55 then colourcount := 32 end if cls % clear %background drawfill (maxx, maxy, 255, 255) %lines across middle drawfillbox (0, 314, 1, 326, 40) drawfillbox (11, 314, 40, 326, 40) drawfillbox (50, 314, 80, 326, 40) drawfillbox (90, 314, 120, 326, 40) drawfillbox (130, 314, 160, 326, 40) drawfillbox (170, 314, 200, 326, 40) drawfillbox (210, 314, 240, 326, 40) drawfillbox (250, 314, 280, 326, 40) drawfillbox (290, 314, 320, 326, 40) drawfillbox (330, 314, 360, 326, 40) drawfillbox (370, 314, 400, 326, 40) drawfillbox (410, 314, 440, 326, 40) drawfillbox (450, 314, 480, 326, 40) drawfillbox (490, 314, 520, 326, 40) % make score integer a string scorestr1 := intstr (scoreint1) scorestr2 := intstr (scoreint2) % Show the score Font.Draw (scorestr1, scoreposition1, 400, score, 40) Font.Draw (scorestr2, scoreposition2, 80, score, 40) % paddles and circle drawfilloval (circlex, circley, circleradius, circleradius, colourcount) % ball drawfillbox (x1, 1, x2, 10, 40) % paddle at bottom drawfillbox (xx1, 640, xx2, 631, 40) % paddle at top %Drawing second ball if scoreint1 >= 10 and scoreint2 >= 10 then drawfilloval (circle1x, circle1y, circle1radius, circle1radius, colourcount) % ball end if delay (2) %User presses a key and paddle moves appropriately Input.KeyDown (chars) %Bottom player's keys if chars (KEY_LEFT_ARROW) and x2 > 113 then x1 := x1 - 4 x2 := x2 - 4 end if if chars (KEY_RIGHT_ARROW) and x1 < 403 then x1 := x1 + 4 x2 := x2 + 4 end if %Top player's keys if chars ('a') and xx2 > 113 then xx1 := xx1 - 4 xx2 := xx2 - 4 end if if chars ('d') and xx1 < 403 then xx1 := xx1 + 4 xx2 := xx2 + 4 end if View.Update %Circle movement for first ball if scoreint1 >= 10 and scoreint2 >= 10 then if circlex > 516 then xdir := -4 elsif circlex < 10 then xdir := 4 end if if circley > 632 then ydir := -4 elsif circley < 8 then ydir := 4 end if %Circle movement for first ball before 2 balls else if circlex > 516 then xdir := -5 elsif circlex < 10 then xdir := 5 end if if circley > 632 then ydir := -5 elsif circley < 8 then ydir := 5 end if end if %End of circle movement for first ball %Circle movement for second ball if scoreint1 >= 10 and scoreint2 >= 10 then if circle1x > 516 then xdir1 := -4 elsif circle1x < 10 then xdir1 := 4 end if if circle1y > 632 then ydir1 := -4 elsif circle1y < 8 then ydir1 := 4 end if end if %End of circle movement for second ball %First ball changing direction on paddle %First ball changes direction after it hits bottom player's paddle if circley <= 23 and circley > 1 and circlex >= x1 and circlex <= x2 then ydir := 4 end if %First ball changes direction after it hits top player's paddle if circley >= 617 and circley < 639 and circlex >= xx1 and circlex <= xx2 then ydir := -4 end if % speed before 2nd ball if scoreint1 <= 10 and scoreint2 <= 10 then %First ball changes direction after it hits bottom player's paddle if circley <= 23 and circley > 1 and circlex >= x1 and circlex <= x2 then ydir := 5 end if %First ball changes direction after it hits top player's paddle if circley >= 617 and circley < 639 and circlex >= xx1 and circlex <= xx2 then ydir := -5 end if end if %Second ball changing direction if scoreint1 >= 10 and scoreint2 >= 10 then %Second ball changes direction after it hits bottom player's paddle if circle1y <= 23 and circle1y > 1 and circle1x >= x1 and circle1x <= x2 then ydir1 := 4 end if %Second ball changes direction after it hits top player's paddle if circle1y >= 617 and circle1y < 639 and circle1x >= xx1 and circle1x <= xx2 then ydir1 := -4 end if end if %Movement of the ball based on the direction it's traveling circley := circley + ydir circlex := circlex + xdir %Movement of second ball if scoreint1 >= 10 and scoreint2 >= 10 then circle1y := circle1y + ydir1 circle1x := circle1x + xdir1 end if %Increases score for both players if circley > 632 then scoreint2 := scoreint2 + 1 elsif circley < 8 then scoreint1 := scoreint1 + 1 end if %Increases score for both players on second ball if scoreint1 >= 10 and scoreint2 >= 10 then if circle1y > 632 then scoreint2 := scoreint2 + 1 elsif circle1y < 8 then scoreint1 := scoreint1 + 1 end if end if View.Update %Changes location of the score when the score is > 10 if scoreint1 >= 10 then scoreposition1 := 100 end if if scoreint2 >= 10 then scoreposition2 := 100 end if exit when scoreint1 = 25 or scoreint2 = 25 end loop % Winner of the game (first to 25) cls if scoreint1 = 25 then drawfill (maxx, maxy, green, green) Font.Draw ("Player 2 wins,", 5, 400, winner, 40) Font.Draw ("the one on top!", 5, 300, winner, 40) elsif scoreint2 = 25 then drawfill (maxx, maxy, green, green) Font.Draw ("Player 1 wins,", 5, 400, winner, 40) Font.Draw ("the one on bottom!", 5, 300, winner, 40) end if % play again? Font.Draw ("Play again? (y/n)", 90, 150, playagain, 40) View.Update locatexy (50, 120) getch (key) if key = "n" then exit elsif key = "y" then scoreint1 := 0 scoreint2 := 0 scoreposition1 := 203 scoreposition2 := 203 end if end loop |
Author: | BigBear [ Mon Jun 08, 2009 2:01 pm ] |
Post subject: | RE:turing pong |
1 suggestion setscreen("noecho") for the first getch |
Author: | Kharybdis [ Mon Jun 08, 2009 4:45 pm ] |
Post subject: | RE:turing pong |
i find it a bit slow... but good job. |