
-----------------------------------
TheCatsMeow
Thu Jan 06, 2011 3:46 pm

Creating a zone on a sprite?
-----------------------------------
What is it you are trying to achieve?
What i'm trying to achieve here is, making a ball bounce off a paddle.. I am trying to recreate pong


What is the problem you are having?
I used a sprite to create my paddles in my game of pong, they work fine they don't go past the barriers i set for them, they stay within the limits. The only problem i am having, is actually making the ball bounce off the paddles


Describe what you have tried to solve this problem
I tried putting the ball to not go within the x or y of the sprite, but then i realised that, that didn't work because my x and y variables are made to set the position of the sprite.


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
%Programmer: Trevor Khan
%Date: Jan 5 2010
%Program: Pong main game

%**********************************************************************************************************

%What i'm trying to create here is the left paddle to move up and down
%I'm also going to test making barriors for it

%**********************************************************************************************************

var win : int           %declaring window variable
var lpaddle, lpaddlex, lpaddley : int           %This is the variable that holds the left "paddle" sprite
var rpaddle, rpaddlex, rpaddley : int           %This is the variable that holds the right "paddle" sprite
var chars : array char of boolean       %This holds the characters variable
var key : boolean           %This will be my Flag variable
var clr1, clr2, clr3, clr4, clr5, clr6, clr7, clr8 : int    %These are the barrior variables for the left side
var clr9, clr10, clr11, clr12, clr13, clr14, clr15, clr16 : int  %These are the barrior variables for the right side
var ball1, ball2, ball3, ball4, ball5, ball6, ball7, ball8 : int %These will be the barriers for the balls
var ballx, bally : int      %The balls x and y variables
var changex, changey : int  %The balls placement variables
var dirx, diry : int        %The balls direction variables

%**********************************************************************************************************
%Creating the window
%   -graphics 1268;956 (Comp. Sci. labs regular computer monitor size) NOTE: This is only for my test program

win := Window.Open ("graphics:1268;956")

%**********************************************************************************************************
%Initializing section
lpaddlex := 50      %left paddle x
lpaddley := 375     %left paddle y

rpaddlex := 1185    %right paddle x
rpaddley := 375     %right paddle y

ballx := 650    %Ball coordinants
bally := 450

changex := 3    %This controls how the ball changes its place on the coordinants grid
changey := 2

dirx := 1       %This controls the balls direction, wheter it's left or right, or up or down
diry := 1

%**********************************************************************************************************

Pic.ScreenLoad ("PongGame.bmp", 0, 0, picMerge)     %This is the background image

%**********************************************************************************************************


%Now i'm gonna make the sprite move when its pressed by the keys W and S
%The w key will make the sprite move up and the s key will make it move down



lpaddle := Sprite.New (Pic.FileNew ("Paddle.bmp"))  %The width is 17, and the length is 140
rpaddle := Sprite.New (Pic.FileNew ("Paddle.bmp"))  %The width is 17, and the length is 140

%=========================================================================================================

process leftpaddle %This is going to be the process for the player one, or the left player


    loop
        clr1 := whatdotcolour (lpaddlex - 2, lpaddley - 2)      %bottom left
        clr2 := whatdotcolour (lpaddlex - 2, lpaddley + 70)     %middle left
        clr3 := whatdotcolour (lpaddlex - 2, lpaddley + 142)    %top lef
        clr4 := whatdotcolour (lpaddlex + 10, lpaddley - 2)     %bottom middle
        clr5 := whatdotcolour (lpaddlex + 10, lpaddley + 142)   %top middle
        clr6 := whatdotcolour (lpaddlex + 19, lpaddley - 2)     %bottom right
        clr7 := whatdotcolour (lpaddlex + 19, lpaddley + 70)    %middle right
        clr8 := whatdotcolour (lpaddlex + 19, lpaddley + 142)   %top right
        Input.KeyDown (chars)
        key := false  %reset key to false when loop restarts so it only moves if a key is pressed
        if chars ('w') and (clr3 not= 10) and (clr5 not= 10) and (clr8 not= 10) then
            %up
            key := true
            lpaddley := lpaddley + 1
            Sprite.Show (lpaddle)
            Sprite.SetPosition (lpaddle, lpaddlex, lpaddley, false)
            delay (5)
        elsif chars ('s') and (clr1 not= 10) and (clr4 not= 10) and (clr6 not= 10) then
            %down
            key := true
            lpaddley := lpaddley - 1
            Sprite.Show (lpaddle)
            Sprite.SetPosition (lpaddle, lpaddlex, lpaddley, false)
            delay (5)
        else
            Sprite.SetPosition (lpaddle, lpaddlex, lpaddley, false)
            Sprite.Show (lpaddle)
            delay (5)
        end if
    end loop

end leftpaddle

%=========================================================================================================

process rightpaddle %This is going to be the process for the player 2, or the right player

    loop
        clr9 := whatdotcolour (rpaddlex - 2, rpaddley - 2)      %bottom left
        clr10 := whatdotcolour (rpaddlex - 2, rpaddley + 70)     %middle left
        clr11 := whatdotcolour (rpaddlex - 2, rpaddley + 142)    %top lef
        clr12 := whatdotcolour (rpaddlex + 10, rpaddley - 2)     %bottom middle
        clr13 := whatdotcolour (rpaddlex + 10, rpaddley + 142)   %top middle
        clr14 := whatdotcolour (rpaddlex + 19, rpaddley - 2)     %bottom right
        clr15 := whatdotcolour (rpaddlex + 19, rpaddley + 70)    %middle right
        clr16 := whatdotcolour (rpaddlex + 19, rpaddley + 142)   %top right
        Input.KeyDown (chars)
        key := false %reset key to false when loop restarts so it only moves if a key is pressed
        if chars (KEY_UP_ARROW) and (clr11 not= 10) and (clr13 not= 10) and (clr16 not= 10) then
            rpaddley := rpaddley + 1
            Sprite.Show (rpaddle)
            Sprite.SetPosition (rpaddle, rpaddlex, rpaddley, false)
            delay (5)
        elsif chars (KEY_DOWN_ARROW) and (clr9 not= 10) and (clr12 not= 10) and (clr14 not= 10) then
            %down
            rpaddley := rpaddley - 1
            Sprite.Show (rpaddle)
            Sprite.SetPosition (rpaddle, rpaddlex, rpaddley, false)
            delay (5)
        else
            Sprite.Show (rpaddle)
            Sprite.SetPosition (rpaddle, rpaddlex, rpaddley, false)
            delay (5)
        end if
    end loop

end rightpaddle

%**********************************************************************************************************


process ball

    loop

        Draw.Oval (ballx, bally, 5, 5, red)
        delay (20)
        ballx := ballx + changex * dirx
        bally := bally + changey * diry
        if (ballx >= maxx - 10) or (ballx = maxy - 310) or (bally  maxX

For colliding with paddles, you would need to check that the coordinates of the ball is within the coordinates of the rectangle.  Then there is collision.

if ballX is between minX and maxX and ballY is between minY and maxY

To inverse direction you can use vectors.  But for something like pong where all the surfaces are vertical or horizontal, you simply need to invert the horizontal/vertical direction, as Tony said.

-----------------------------------
TheCatsMeow
Mon Jan 10, 2011 2:18 pm

RE:Creating a zone on a sprite?
-----------------------------------
Alright. i get it now, the only problem i'm haing a problem is is making the ball actually work. i need to re write the ball part of the code, thats probably my problem. I'll try changing that part of the code..
