
-----------------------------------
PhOtO!
Thu Jun 02, 2005 7:02 pm

Tabletennis program again, I need help with the angles
-----------------------------------
I posted a new topic cause everyone just stopped going to my previous one.

I fixed most of the problems, but i still have a problem with the angles ( i want the ball to go in the direction depending on what angle it hits my paddle with)


ok this is my program so far..


setscreen ("graphics:max;max")

var x, y, button : int %x and y for paddle
var x2, y2 : int  %x and y for ball
var x3, y3 : int
var nx2, ny2 : int := 2 %change of x and y for ball
var nx3 : int  %x and y for computer paddle
var r : int := 9 %radius for ball 
var r1 : int := 15 %x radius for paddle
var r2 : int := 20 %y radius for paddle
var t : int     %counter for computer score
var u : int     %counter for player score
var n : int     %final score

x := Rand.Int (10, 100) %set x for paddle 
y := Rand.Int (10, 100) %set y for paddle
x2 := Rand.Int (10, 100) %set x for ball
y2 := Rand.Int (10, 100) %set y for ball
y3 := 600 %sets y axis for computer paddle
x3 := Rand.Int (10,100) %sets x axis for computer paddle

t := 0  %computer counter
u := 0 %player counter


drawfill (1,1,1,1)      %sets background
colourback (1)
colour (0)              %font colour
put "How much do you want the score to go up till?"
get n
cls


setscreen ("offscreenonly")
%Tennis court lines
loop


drawfill (500,360,9,9)                  
drawfillbox (400,450,405,85, 0)

drawfillbox (250,450,540,455,0)
drawfillbox (220,85,580,90,0)

for i : 1..5
drawline (220+i,85,250+i,450,0) 
end for

for a : 1..5
drawline (580-a,90,540-a,455,0) 
end for

drawline (200,0,200,maxy,0)
drawline (600,0,600,maxy,0)


%Net

for b : 0..30 by 10
drawline (240,262,560,262,0) %Horizontal lines
drawline (240,261+b,555,261+b,0)
end for

for c : 0..310 by 10         %Vertical lines
drawline (244+c,261,244+c,291,0)
end for




    Draw.FillOval (x, y, r1, r2, red) %player paddle
    Draw.FillOval (x2, y2, r, r, white) %ball
    Draw.FillOval (x3,y3,r1,r2,red) %computer paddle
    View.Update
    Draw.FillOval (x3,y3,r1,r2,9) %player paddle  
    Draw.FillOval (x2, y2, r, r, 9) %ball
    Draw.FillOval (x, y, r1, r2, 9) %computer paddle                                                                                                                                         
      
          
    Mouse.Where (x, y, button) %locates x and y for paddle 
 

    
    if x2 = 650 then %check to see if ball goes off to the top 
        ny2 := - 3 %chages the direction
        u := u + 1
    end if  

 
    %checks if paddle hit ball
    if x + r1 > x2 - r and x - r1 < x2 + r and y + r2 > y2 - r and y - r2 < y2 + r          
             then
             
        %chages the direction of the ball
        nx2 := nx2 * - 1
        ny2 := ny2 * - 1
    end if    
    %checks if computer paddle hit the ball
    if x3 + r1 > x2 - r and x3 - r1 < x2 + r and y3 + r2 > y2 - r and y3 - r2 < y2 + r      
    then 
    %changes the direction of the ball
        nx2 := nx2 * - 1 
        ny2 := ny2 * - 1
    
    end if
    
    %makes ball move
    x2 += nx2 
    y2 += ny2
    x3 += nx2


%locates player score and prints    
locatexy (5,60)
colourback (9)
put "Player score : "..
put u
%locates computer score and prints
locatexy (600,700)
colourback (9)
put "Computer score : "..
put t
exit when t = n or u = n

end loop

if t = n then
put " YOU LOSE "
put " GAME OVER "
elsif u = n then
put " YOU WIN "
put " GAME OVER "

end if

-----------------------------------
Delos
Thu Jun 02, 2005 9:16 pm


-----------------------------------
1)
Please learn to use italics tags, so [code] or [syntax] shouldn't be a stretch.

2)
Your code isn't too bad so far.  Your commenting is rigorous, so always appreciated.

3)
What you're looking for is circular collision detection.  Check the Tutorials for any references to this (if you need a starting point, Cervantes has posted a walkthrough of all tutorials).
If you're still in need of help, search out some Slime Volleyball programmes posted up.  They'll help you with the idea.

The concept is that you'll be determining the angle of collision with respect to the centre of your paddle.  This will then allow you to assign your components to your resultant velocity of your ball.  In your code, your components appear to be represented by 'nx2' and 'ny2'.
If you're not sure how components work, ask a physics teacher or consult a suitable text book.
