Computer Science Canada help with pong |
Author: | lufthansa747 [ Sat Dec 05, 2009 10:43 pm ] |
Post subject: | help with pong |
here is the pong game. it works fin until it restarts. some one help View.Set ("graphics:max;max,nobuttonbar") var player1Score : int := 0 var player2Score : int := 0 var playerWinFont : int := Font.New ("sans serif:100:bold") var continue : char var fontWidth : int var gamePlay : string := "" var scoreToReach : int var player1Dif : char var player2Dif : char class player export start, player2, move, x2, y, y2, x, AI, changeColour, changeColourAt, changeColourNow, changeCol var sizex : int := 20 var sizey : int := 200 var x : int := 0 var x2 : int := x + sizex var y : int := maxy div 2 - sizey div 2 var y2 : int := maxy div 2 + sizey div 2 var yint : int := 5 var col : int := white var changeColour : int := 0 var changeColourAt : int := 100 procedure start Draw.FillBox (x, y, x2, y2, white) end start procedure move (userAction : char) if changeColour > 0 then col := brightred changeColour -= 1 elsif changeColour = 0 then col := white end if if userAction = 'w' and y2 + yint <= maxy then Draw.FillBox (x, y, x2, y2, black) y += yint y2 += yint Draw.FillBox (x, y, x2, y2, col) elsif userAction = 's' and y - yint >= 0 then Draw.FillBox (x, y, x2, y2, black) y -= yint y2 -= yint Draw.FillBox (x, y, x2, y2, col) elsif userAction = '1' then Draw.FillBox (x, y, x2, y2, black) Draw.FillBox (x, y, x2, y2, col) end if if userAction = KEY_UP_ARROW and y2 + yint <= maxy then Draw.FillBox (x, y, x2, y2, black) y += yint y2 += yint Draw.FillBox (x, y, x2, y2, col) elsif userAction = KEY_DOWN_ARROW and y - yint >= 0 then Draw.FillBox (x, y, x2, y2, black) y -= yint y2 -= yint Draw.FillBox (x, y, x2, y2, col) elsif userAction = '2' then Draw.FillBox (x, y, x2, y2, black) Draw.FillBox (x, y, x2, y2, col) end if end move procedure player2 x := maxx - sizex x2 := maxx end player2 procedure AI (upState, bally : int, level : char) if level = 'i' then if upState > 0 and bally > y + yint and bally < y2 + yint then move (KEY_UP_ARROW) elsif upState < 0 and bally > y - yint and bally < y2 - yint then move (KEY_DOWN_ARROW) end if else if upState > 0 and bally > y - yint and bally < y2 - yint then move (KEY_UP_ARROW) elsif upState < 0 and bally > y + yint and bally < y2 + yint then move (KEY_DOWN_ARROW) end if end if end AI procedure changeColourNow (add : boolean) if add = false then changeColour := 0 else changeColour := changeColourAt col := brightred end if end changeColourNow procedure changeCol if changeColour > 0 then Draw.FillBox (x, y, x2, y2, col) changeColour -= 1 else col := white Draw.FillBox (x, y, x2, y2, col) end if end changeCol end player var p : pointer to player var p2 : pointer to player class ball import p, p2 export start, draw, upState, y var x : int := maxx div 2 var y : int := maxy div 2 var radius : int := 20 var xint, yint : int := 3 var upState, sideState : int loop upState := Rand.Int (-1, 1) exit when upState not= 0 end loop loop sideState := Rand.Int (-1, 1) exit when sideState not= 0 end loop procedure start Draw.FillBox (0, 0, maxx, maxy, black) Draw.FillOval (x, y, radius, radius, white) end start procedure draw (x1, x3, y1, y2, y3, y4 : int) if x - xint <= 0 and sideState = -1 then %Check left wall sideState := 1 player2Score += 1 p2 -> changeColourNow (true) p2 -> changeCol end if if x + xint >= maxx and sideState = 1 then % check right wall sideState := -1 player1Score += 1 p -> changeColourNow (true) p -> changeCol end if if y + yint >= maxy and upState = 1 then %check top wall upState := -1 end if if y - yint <= 0 and upState = -1 then % check bottom wall upState := 1 end if if x - xint - radius <= x1 and sideState = -1 and y > y1 and y < y2 then % check left guy hits ball sideState := 1 elsif x + xint + radius >= x3 and sideState = 1 and y > y3 and y < y4 then % check left guy hits ball sideState := -1 end if Draw.FillOval (x, y, radius, radius, black) x += xint * sideState y += yint * upState Draw.FillOval (x, y, radius, radius, white) end draw end ball procedure updateScore () locate (1, 1) put "Player 1: ", player1Score, " Player 2: ", player2Score end updateScore loop View.Set ("graphics:max;max,nobuttonbar") put "do you want to play pvp(player vs player), pvc (player vs computure) or cvc (computure vs computure)" put "Note you can click Esc to end a cvc mach: " .. loop get gamePlay exit when gamePlay = "pvp" or gamePlay = "pvc" or gamePlay = "cvc" end loop if gamePlay = "cvc" then put "what dfficulty do you want player 1 to be set to. Easy or imposible(e/i): " .. loop player1Dif := getchar exit when player1Dif = 'e' or player1Dif = 'i' end loop put player1Dif end if if gamePlay = "pvc" or gamePlay = "cvc" then put "what dfficulty do you want player 2 to be set to. Easy or imposible(e/i): " .. loop player2Dif := getchar exit when player2Dif = 'e' or player2Dif = 'i' end loop put player2Dif end if put "How many points will the game be up to: " .. get scoreToReach put "Press any Key when you are ready to play: " .. continue := getchar View.Set ("graphics:max;max,offscreenonly,nobuttonbar,nocursor") player1Score := 0 player2Score := 0 var userAction : array char of boolean var ball1 : pointer to ball new ball, ball1 ball1 -> start new player, p p -> start new player, p2 p2 -> player2 p2 -> start for decreasing count : 5 .. 1 var countDown : string := intstr (count) fontWidth := Font.Width (countDown, playerWinFont) Font.Draw (countDown, maxx div 2 - fontWidth div 2, maxy div 2, playerWinFont, brightblue) View.Update delay (1000) ball1 -> start p -> start p2 -> start View.Update end for loop Input.KeyDown (userAction) if gamePlay not= "cvc" then if userAction ('w') then p -> move ('w') elsif userAction ('s') then p -> move ('s') else p -> changeCol end if elsif gamePlay = "cvc" then p -> AI (ball1 -> upState, ball1 -> y, player1Dif) end if if gamePlay = "pvp" then if userAction (KEY_UP_ARROW) then p2 -> move (KEY_UP_ARROW) elsif userAction (KEY_DOWN_ARROW) then p2 -> move (KEY_DOWN_ARROW) else p2 -> changeCol end if elsif gamePlay not= "pvp" then p2 -> AI (ball1 -> upState, ball1 -> y, player2Dif) end if ball1 -> draw (p -> x2, p2 -> x, p -> y, p -> y2, p2 -> y, p2 -> y2) updateScore exit when player1Score = scoreToReach or player2Score = scoreToReach or userAction (KEY_ESC) View.Update end loop if player1Score = scoreToReach then fontWidth := Font.Width ("Player 1 Wins !!!!!", playerWinFont) Draw.Text ("Player 1 Wins!!!!!", maxx div 2 - fontWidth div 2, maxy div 2, playerWinFont, brightred) elsif player2Score = scoreToReach then fontWidth := Font.Width ("Player 2 Wins !!!!!", playerWinFont) Draw.Text ("Player 2 Wins!!!!!", maxx div 2 - fontWidth div 2, maxy div 2, playerWinFont, brightred) else fontWidth := Font.Width ("It is a Tie", playerWinFont) Draw.Text ("It is a Tie", maxx div 2 - fontWidth div 2, maxy div 2, playerWinFont, brightred) end if View.Update continue := getchar cls View.Update end loop |
Author: | Zren [ Sun Dec 06, 2009 1:46 pm ] |
Post subject: | RE:help with pong |
View.Set on line 175. You go from OffScreenonly and don't change back. Try putting the opposite of offscreenonly in View.Set. |