Author |
Message |
Velocity

|
Posted: Wed Dec 21, 2011 7:28 pm Post subject: Make the A.I Paddle follow the ball |
|
|
What is it you are trying to achieve?
I aim to make the A.I Paddle follow the ball so that it never goes below it.
What is the problem you are having?
I am unable to make the paddle move first of all but i think this is just the same step as making it follow the ball so i want to make it follow the ball.
Describe what you have tried to solve this problem
making if statements for if the paddle is greater than this location paddle -= 10 and if it is less than this location it goes += 10, it doesnt move, it stays still
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Turing: |
if ball_y2 >= maxy - 50 then
ball_directiony2 * = - 1
end if
if ball_x2 >= maxx div 2 + 40 then
ball_directionx2 * = - 1
end if
if ball_x2 <= maxx - 40 then
ball_directionx2 * = - 1
end if
var direction1 : int
ball_x2 + = (ball_directionx2 * ball_speed )
ball_y2 + = (ball_directiony2 * ball_speed )
if ball_x2 <= x2_ 2 and ball_y2 <= y2_ 2 + 15 and ball_x2 >= x1_ 1 and ball_y2 >= y1_ 1 + 15 then
ball_directiony2 * = - 1
end if
%%% above is my syntax to make my ball move
% I want to know how to make the paddle :
x1_ 1 := 750
y1_ 1 := 25
x2_ 2 := 850
y2_ 2 := 50
drawfillbox (x1_ 1, y1_ 1, x2_ 2, y2_ 2, 131)
% follow the ball and the collision part i got covered.
|
Please specify what version of Turing you are using
4.1.1a |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Velocity

|
Posted: Wed Dec 21, 2011 7:50 pm Post subject: RE:Make the A.I Paddle follow the ball |
|
|
+ karma for help, need to make it in the next 10mins |
|
|
|
|
 |
RandomLetters
|
Posted: Wed Dec 21, 2011 9:05 pm Post subject: RE:Make the A.I Paddle follow the ball |
|
|
If you want it to follow the ball, then you will need to use the ball's coordinates somehow.
A simple way is to just draw the paddle where the ball is
x1 = 10
x2 = 20
y1 = by + 10
y2 = by - 10
better (beatable way) is to check the location of the paddle, compare it to the location of the ball, and move appropriately
for example,
y = 0
if deltay > max then //check if the paddle is above the ball and set it to move down
v = -1
end if
y += v
y1= y + 10
y2 = y - 10 |
|
|
|
|
 |
Zren

|
Posted: Wed Dec 21, 2011 10:36 pm Post subject: RE:Make the A.I Paddle follow the ball |
|
|
So much hardcoding... If you use a number more than once for a specific value (Eg: ball radius, ball speed, paddle speed) then you should make a variable or constant for it. Actually, you should always use a variable to describe 'magic numbers' even if you only use the variable once. It makes the code more readable.
Building on what Rand was talking about:
You also need to take into account when the object's destination is within the movement speed. Otherwise you'll get a jittery effect as one frame it's on the left, next on the right, etc.
Turing: |
var delta := ball.pos.x - paddle.pos.x % d = p2 - p1
if abs (delta ) <= paddle.speed then
% Will reach destination this tick.
paddle.nextPos.x := ball.pos.x
elsif delta > 0 then
% Destination is to the right
paddle.nextPos.x := paddle.pos.x + paddle.speed
elsif delta < 0 then
% Destination is to the left
paddle.nextPos.x := paddle.pos.x - paddle.speed
end if
|
|
|
|
|
|
 |
Velocity

|
Posted: Thu Dec 22, 2011 12:17 pm Post subject: RE:Make the A.I Paddle follow the ball |
|
|
zren i implemented your code into my program and changed the co-ords around a little, and the a.i paddle moves where the ball is but it only twitches like + 5 on the co-ord that way... so it doesnt exactly follow it only + 5 out of 100, this is how i put your code in :
var paddle_speed : int := 5
var shadower := ball_x2 - x2_2
if abs (shadower) <= paddle_speed then
x2_2 := ball_x2
x1_1 := ball_x2
elsif shadower > 100 then
x1_1 := x1_1 + paddle_speed + 100
x1_1 := x2_2 - paddle_speed - 100
elsif shadower < 100 then
x2_2 := x1_1 - paddle_speed - 100
x2_2 := x1_1 + paddle_speed + 100
end if |
|
|
|
|
 |
Velocity

|
Posted: Thu Dec 22, 2011 12:21 pm Post subject: RE:Make the A.I Paddle follow the ball |
|
|
This changed the paddle speed but ithen the paddle moves and it gets to the maxx and disappears
var paddle_speed : int := 50
var shadower := ball_x2 - x2_2
if abs (shadower) <= paddle_speed then
x2_2 := ball_x2
x1_1 := ball_x2
elsif shadower > 100 then
x1_1 := x1_1 + paddle_speed + 200
elsif shadower < 100 then
x2_2 := x1_1 - paddle_speed - 100
end if |
|
|
|
|
 |
Insectoid

|
Posted: Thu Dec 22, 2011 1:01 pm Post subject: RE:Make the A.I Paddle follow the ball |
|
|
You're severely over-complicating this.
code: |
if ball_y < paddle_y then
move_paddle_down
else if ball_y > paddle_y then
move_paddle_up
end if
|
It's that simple. |
|
|
|
|
 |
Velocity

|
Posted: Thu Dec 22, 2011 6:12 pm Post subject: RE:Make the A.I Paddle follow the ball |
|
|
insectoid thank you so much that helped alot! +karma |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Velocity

|
Posted: Thu Dec 22, 2011 6:46 pm Post subject: RE:Make the A.I Paddle follow the ball |
|
|
if ball_y2 > x1_1 then
x2_2 += 10
x1_1 += 10
elsif ball_y2 < x1_1 then
x2_2 -= 10
x1_1 -= 10
end if
insectoid i used your code but for some reason with these boundaries of my paddle
if x2_2 > maxx - 31 and x1_1 < maxx - 131 then
x1_1 -= 10
x2_2 -= 10
elsif x2_2 < maxx div 2 + 131 and x1_1 > maxx div 2
+ 31 then
x1_1 += 10
x2_2 += 10
end if
the paddle goes to the left and then even when the ball moves right the paddle trys to move right but stays on the left. |
|
|
|
|
 |
Dreadnought
|
Posted: Thu Dec 22, 2011 7:06 pm Post subject: Re: Make the A.I Paddle follow the ball |
|
|
Is it intended that you're comparing the y-coordinate of the ball with the x coordinate of the paddle? |
|
|
|
|
 |
Insectoid

|
Posted: Thu Dec 22, 2011 8:11 pm Post subject: RE:Make the A.I Paddle follow the ball |
|
|
The paddle doesn't even need an X variable, since it only goes up and down. |
|
|
|
|
 |
Velocity

|
Posted: Thu Dec 22, 2011 10:35 pm Post subject: RE:Make the A.I Paddle follow the ball |
|
|
mine goes left and right |
|
|
|
|
 |
Velocity

|
Posted: Thu Dec 22, 2011 10:35 pm Post subject: RE:Make the A.I Paddle follow the ball |
|
|
its not pong, its brick breaker. Please help me to know where i went wrong  |
|
|
|
|
 |
RandomLetters
|
Posted: Thu Dec 22, 2011 11:04 pm Post subject: RE:Make the A.I Paddle follow the ball |
|
|
Then why are you comparing the y values of the paddle? |
|
|
|
|
 |
Velocity

|
Posted: Fri Dec 23, 2011 1:11 am Post subject: RE:Make the A.I Paddle follow the ball |
|
|
X1-1 is the first x co'ord of the paddle
X2-2 is the second x co'ord of the paddle
Ball-y2 is the y co'ord of the a.i ball
Nowhere in the code does it say the y co'ord of the paddle
X1-1 is where the drawfill box starts
X2-2 is where it ends
How do i get my paddle to follow the ball properly, the code i prompted it with worked but when my code starts the a.i paddle goes to the left and trys to go right but a force blocks it and it doesnt. Would you feel more comfortable with my question if i provided the code for my actual game? |
|
|
|
|
 |
|