Computer Science Canada Pong game Paddle hit detection issues |
Author: | TripleC [ Thu Nov 05, 2015 11:02 am ] |
Post subject: | Pong game Paddle hit detection issues |
I'm making a pong game and cant for the life of me figure out why my hit detection is not working (I'm in grade ten and new to turing, and ignore all the other terrible broken stuff its heavily work in progress. Just focusing on the important stuff first) %---------------------------------------------------------% % File name: pong_game_template.t % % Date: 2012/11/05 % % Programmers: % % Description: This is a pong game for two players; % % the first to score 7 goals is the winner. % %---------------------------------------------------------% setscreen ("graphics:800;600") setscreen ("offscreenonly") var border : int % var paddle1 : int %Player one's paddle var paddle2 : int %Player two's paddle var score1 : int := 1%Determines the score of player one var score2 : int := 1%Determines the score of player two var ballX1 : int := 400 % var ballX2 : int % var ballY1 : int := 300 % var ballY2 : int % var shiftY : int := 5 var shiftX : int := 5 var ballPoint : int % var pong : int var y, y2, P2y, P2y2 : int y := 200 y2 := 100 P2y := 200 P2y2 := 100 var scorefont : int scorefont := Font.New ("Arial:50:bold") var chars : array char of boolean randint (shiftX, 4, 8) randint (shiftY, 4, 8) %-------------------------------------------------------------------------- procedure redrawGameField % pong := Window.Open ("position:top;right,graphics:800;600,nobuttonbar") % View.Set ("title: Pong") %draws game field drawfillbox (0, 0, 800, 600, black) drawfillbox (395, 0, 405, 600, white) %draw Paddle1 drawfillbox (760, y, 780, y2, red) %Draw Paddle2 drawfillbox (20, P2y, 40, P2y2, red) %Draws the ball drawfilloval (ballX1, ballY1, 20, 20, red) %Draw score Font.Draw ("0", 325, 500, scorefont, white) View.Update if ballX1 <= 0 then ballX1 := 250 ballY1 := 250 ballX2 := 1 score2 := score2 + 1 delay (1000) elsif ballY1 <= 0 then ballY2 := 1 end if if ballX1 >= maxx then ballX1 := Rand.Int (50, 150) ballY1 := Rand.Int (50, 150) ballX2 := 2 score1 := score1 + 1 delay (1000) elsif ballY1 >= maxy - 110 then ballY2 := -1 end if end redrawGameField %-------------------------------------------------------------------------- procedure bounceBall if ballX1 >= 800 or ballX1 <= 0 then shiftX := -shiftX end if if ballY1 >= 600 or ballY1 <= 0 then shiftY := -shiftY end if if y <= 100 or y2 >= 200 then shiftX := -shiftX end if if P2y <= 100 or P2y2 >= 200 then shiftX := -shiftX end if end bounceBall %-------------------------------------------------------------------------- procedure movePaddles Input.KeyDown (chars) if chars (KEY_UP_ARROW) then y := y + 5 end if if chars (KEY_DOWN_ARROW) then y := y - 5 end if if chars (KEY_UP_ARROW) then y2 := y2 + 5 end if if chars (KEY_DOWN_ARROW) then y2 := y2 - 5 end if Input.KeyDown (chars) if chars (KEY_SHIFT) then P2y := P2y + 5 end if if chars (KEY_CTRL) then P2y := P2y - 5 end if if chars (KEY_SHIFT) then P2y2 := P2y2 + 5 end if if chars (KEY_CTRL) then P2y2 := P2y2 - 5 end if end movePaddles %-------------------------------------------------------------------------- procedure moveBall ballX1 := ballX1 + shiftX ballY1 := ballY1 + shiftY end moveBall %-------------------------------------------------------------------------- procedure castBall % casting a new ball simply means to assign new coordinates to THE ball % first ball in the game should be randomly cast from left or right side % every next new ball should be cast from the side of the player who lost % a point, but the vertical direction (up or down) should still be random end castBall %-------------------------------------------------------------------------- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Main Program %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % output instructions about the game % pause for couple of seconds, so players can read the instructions % draw the game field % ... and here we go: loop redrawGameField delay (10) cls bounceBall movePaddles moveBall % the program should repeatedly do the following: % - cast a ball at the begiining of each set % - constantly redraw the game field - animation % - constantly update (move) the paddles (if key action is detected) % - constantly update (move) the ball in the current direction % - bounce the ball when it hits an obstacle % - detect when the ball goes off the screen, and score a point % - exit when a player scores 7 points end loop % display final score |