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

Username:   Password: 
 RegisterRegister   
 Paddle Side Collision
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
bubb4h0t3p




PostPosted: Wed Apr 08, 2015 6:19 pm   Post subject: Paddle Side Collision

What is it you are trying to achieve?
Have the balls properly bounce off of the sides of the paddle


What is the problem you are having?
The balls stick and glitch into the sides of the paddle


Describe what you have tried to solve this problem
Readjusting the margins at which the balls collide on the sides and changing values, nothing has seemed to work unfortunately


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
CONTROLS:
Spacebar = release ball from paddle
r = generate a new ball, use it with space.
Escape = stop the game from running (exit)
t = reset game
a or Left Arrow Key move paddle left
d or Right Arrow Key move paddle right
Turing:

% Turing BrickBreaker
% coded by: James Williamson, started: Tuesday April 7th, 2015

View.Set ("graphics:max;max, offscreenonly, nocursor, nobuttonbar, title: Brick Breaker")

var paddleX, paddleY : int
var keys : array char of boolean
var paddleSpeed : int
var paddleWidth : int
var paddleHeight : int
var playerScore : int
var playFieldMaxx : int
var HUDfont : int := Font.New ("palatino:30")
var lives : int
var lineThickness : int := 6

type ballType :
    record
        x, y, radius : int
        velocityX, velocityY : real
        launched : boolean
    end record

var balls : flexible array 1 .. 1 of ballType

class Brick
    import playFieldMaxx
    export generate, draw
    var x, y, brickLength, brickHeight : int
    brickLength := 200
    brickHeight := 50
    var generated : boolean := false
    procedure draw
        Draw.FillBox (x, y, x + brickHeight, y + brickLength, white)
    end draw
    procedure generate

        x := Rand.Int (1, playFieldMaxx - brickLength)
        y := Rand.Int (1, maxy - brickHeight)

        generated := true
    end generate
end Brick

var b : flexible array 1 .. 1 of ^Brick
for i : 1 .. upper (b)
    new Brick, b (i)
end for

% procedure that resets all of the values to their defaults
procedure resetGame

    lives := 3
    playFieldMaxx := maxx - round (maxx / 5) - (lineThickness * 3)
    paddleWidth := 175
    paddleHeight := 45
    paddleX := round (playFieldMaxx / 2) - paddleWidth
    paddleY := 10
    for i : 1 .. upper (balls)
        balls (i).radius := 12
        balls (i).x := round ((paddleX + (paddleWidth + paddleX)) / 2)
        balls (i).y := paddleY + paddleHeight + balls (i).radius
        balls (i).velocityX := 0
        balls (i).velocityY := 0
        balls (i).launched := false
    end for
    playerScore := 0
    paddleSpeed := 6
end resetGame

% procedure generates a new ball and sets the default values
procedure generateBall
    new balls, upper (balls) + 1
    balls (upper (balls)).radius := 12
    balls (upper (balls)).x := round ((paddleX + (paddleWidth + paddleX)) / 2)
    balls (upper (balls)).y := paddleY + paddleHeight + balls (upper (balls)).radius
    balls (upper (balls)).velocityX := 0
    balls (upper (balls)).velocityY := 0
    balls (upper (balls)).launched := false
end generateBall

% procedure to draw and control the paddle
procedure paddleMovement
    import keys, paddleX, paddleY, paddleSpeed, paddleWidth, paddleHeight, playFieldMaxx, lineThickness

    if keys ('a') and paddleX > 0 or keys (KEY_LEFT_ARROW) and paddleX > 0 then
        paddleX := paddleX - paddleSpeed
    end if

    if keys ('d') and paddleX < playFieldMaxx - paddleWidth or keys (KEY_RIGHT_ARROW) and paddleX < playFieldMaxx - paddleWidth then
        paddleX := paddleX + paddleSpeed
    end if

    Draw.FillBox (paddleX, paddleY, paddleX + paddleWidth, paddleY + paddleHeight, white)
    Draw.FillBox (paddleX + lineThickness, paddleY + lineThickness, paddleX + paddleWidth - lineThickness, paddleY + paddleHeight - lineThickness, black)
end paddleMovement

procedure HUD
    import HUDfont, playFieldMaxx, playerScore, lives, lineThickness
    Draw.FillBox (playFieldMaxx, 0, maxx, maxy, white)
    Draw.FillBox (playFieldMaxx + lineThickness * 3, lineThickness * 3, maxx - lineThickness * 3, maxy - lineThickness * 3, black)
    Font.Draw ("Score: " + intstr (playerScore), (playFieldMaxx + length ("Score: " + intstr (playerScore))) + lineThickness * 3, maxy - 50 - lineThickness * 3, HUDfont, white)
    Font.Draw ("Lives: " + intstr (lives), (playFieldMaxx + length ("Lives: " + intstr (lives))) + lineThickness * 3, maxy - 150 - lineThickness * 3, HUDfont, white)
end HUD

procedure wallCollision (var ball : ballType)
    import playFieldMaxx
    if ball.x > playFieldMaxx - ball.radius then
        ball.x := playFieldMaxx - ball.radius
        ball.velocityX := ball.velocityX * -1
    end if
    if ball.x < ball.radius then
        ball.x := ball.radius
        ball.velocityX := ball.velocityX * -1
    end if
    if ball.y > maxy - ball.radius then
        ball.y := maxy - ball.radius
        ball.velocityY := ball.velocityY * -1
    end if
    if ball.y < ball.radius then
        lives := lives - 1
        ball.launched := false
    end if
end wallCollision

procedure ballMovement (var ball : ballType)
    import keys, paddleX, paddleY, paddleHeight, paddleWidth, lineThickness
    if ball.launched = true then
        ball.x := ball.x + round (ball.velocityX)
        ball.y := ball.y + round (ball.velocityY)
        ball.velocityY := ball.velocityY + (ball.velocityY * 0.0005)
        ball.velocityX := ball.velocityX + (ball.velocityX * 0.0005)
    else
        ball.velocityY := 0
        ball.velocityX := 0
        ball.x := round ((paddleX + (paddleWidth + paddleX)) / 2)
        ball.y := paddleY + paddleHeight + ball.radius
        % note: blank actually represents the spacebar
        if keys (' ') then
            ball.velocityY := 5
            ball.velocityX := 4
            ball.launched := true
        end if
    end if
    Draw.FillOval (ball.x, ball.y, ball.radius, ball.radius, white)
    Draw.FillOval (ball.x, ball.y, ball.radius - lineThickness, ball.radius - lineThickness, black)
end ballMovement

process bounceSound
Music.Sound (1, 100)
end bounceSound
% procedure to determine if a ball has hit the paddle and bounce it off if it has.
procedure ballPaddleCollision (var ball : ballType)
    import paddleX, paddleY, paddleWidth, paddleHeight, bounceSound
    % if the checks if the ball is within the paddle
    if ball.x > paddleX - ball.radius and ball.x < paddleX + paddleWidth + ball.radius and ball.y > paddleY - ball.radius and ball.y < paddleY + paddleHeight + ball.radius then
        %fork bounceSound
        % checks if the ball is hitting the left side of the paddle
        if ball.y < paddleY + paddleHeight - 5 and ball.x > paddleX - ball.radius and ball.x < paddleX + paddleWidth + ball.radius - round (paddleWidth / 2) then
            ball.x := paddleX - (ball.radius * 2)

            % checks if the ball is hitting the right side of the paddle
        elsif ball.y < paddleY + paddleHeight - 5 and ball.x < paddleX + paddleWidth + ball.radius then
            ball.x := paddleX + paddleWidth + (ball.radius * 2)

            % if the ball isn't hitting the right or left side it is assumed the top of the paddle was hit
        else
            ball.y := paddleY + paddleHeight + ball.radius
        end if

        % bounces the ball off of the paddle
        ball.velocityY := ball.velocityY * -1
    end if
end ballPaddleCollision

% calls the resetGame procedure before running to set all values to default
resetGame
% generates all of the bricks
for i : 1 .. upper (b)
    b (i) -> generate
end for
loop
    Input.KeyDown (keys)
    Draw.FillBox (0, 0, maxx + 1, maxy + 1, black)

    for i : 1 .. upper (balls)
        wallCollision (balls (i))
        ballMovement (balls (i))
        ballPaddleCollision (balls (i))
    end for
    paddleMovement
    HUD
    /*
     for i : 1 .. upper (b)
     b (i) -> draw
     end for
     */

    if keys (KEY_ESC) then
        return
    end if
    if keys ('r') then
    generateBall
    end if
    if keys ('t') then
    resetGame
    end if
    View.Update
    delay (10)
    cls
end loop




Please specify what version of Turing you are using
Open Turing 1.0.1
Sponsor
Sponsor
Sponsor
sponsor
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  [ 1 Posts ]
Jump to:   


Style:  
Search: