Computer Science Canada

How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a simp

Author:  chandrapanta [ Thu Dec 24, 2015 3:55 pm ]
Post subject:  How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a simp

Here is my code, I am new to Turing so i need to use simple things. How can i make the ball bounce from wall to wall. Basically I am trying to make a ping pong game so yeah!

var x, y : int := 10
var score, score2 : int := 0
setscreen ("graphics:900;600")
loop
x := x + 2
y := y + 2
delay (50)
cls
drawfilloval (x, y, 5, 5, red)
if y >= 590 then
x := 600-50
y := 590
end if
if y <= 0 then
x := x + 5
y := y + 10
end if
if x <= 0 then
score := score + 1
elsif x >= 900 then

score2 := score2 + 1
end if
exit when score = 10 or score2 = 10
end loop

Author:  Nathan4102 [ Thu Dec 24, 2015 4:42 pm ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

For something like this, you don't want to be working in only position. Instead, keep track of velocity and adjust position based on velocity. Start with a constant velocity and have it switch when you hit a wall. When that works, you can try to factor in gravity.

Author:  chandrapanta [ Thu Dec 24, 2015 8:04 pm ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

I meant for real like what codes can add to this to make the ball bounce off the all the walls except the left and the right

Author:  Insectoid [ Thu Dec 24, 2015 10:13 pm ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

We aren't going to just hand you the answers. You've got to meet us halfway. What is a bounce? It's really just an object changing direction when it hits something. How do you change the direction of an object? Well, to move right you increase the x value and to move left you decrease it, so just switch from increasing to decreasing.

In the code you posted it kind of looks like you have some sort of idea of what to do. Your code detects the collisions between the ball and the wall already, so now you just need to make the ball do the correct thing (change direction) when it hits the wall. Why, in your code, does hitting the top wall affect the x coordinate? Does that seem like correct behaviour to you?

Author:  chandrapanta [ Fri Dec 25, 2015 9:34 am ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

Oh u mean the x:=600-50 so it should be y:=590-50 then. I tried making it bounce off the top and bottom but I can't seem to get it. Turing is very new to me! But for the left and right wall it should go over and award a pint to the right of it goes left and via.

Author:  Insectoid [ Fri Dec 25, 2015 11:09 am ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

Quote:
Oh u mean the x:=600-50 so it should be y:=590-50 then


No, I didn't say that. I only asked why you're changing the x value.

How do you make the ball move down? By decreasing the y coordinate. Does the y coordinate ever decrease in your code?

Author:  chandrapanta [ Fri Dec 25, 2015 11:20 am ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

No good point. I decreased the x cuz I want it to go to the left and then go down. And imagine there is two paddles on each side so when it hits those paddles, the ball should bounce off unless they miss. Basically ping pong with two players. Can u please help me!

Author:  Nathan4102 [ Fri Dec 25, 2015 12:03 pm ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

Your approach won't work with position, you have no way of knowing what direction the ball is moving in. Here's some pseudo-code, I'll leave it to you to put it all together.

If we hit the top or bottom border, flip y velocity.
If we hit the left or right border, flip x velocity.
Add x velocity to the x position, add y velocity to the y position.
Draw the ball

Author:  chandrapanta [ Fri Dec 25, 2015 12:28 pm ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

Why does flip x and flip y velocity mean in terms of code. So basically I create two more car for x and y velocity and basically add x to x velocity and y to y velocity? Right...

Author:  chandrapanta [ Fri Dec 25, 2015 12:41 pm ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

This is what i have so far after you told me that...

var x5, y5 : int := 10
var yvel, xvel : int := 50
var score, score2 : int := 0
setscreen ("graphics:900;600")
loop
x5 := x5 + 2
y5 := y5 + 2
drawfilloval (x5, y5, 5, 5, black)
delay (50)
if y5 >= 590 or y5 <= 1 then
y5 := y5 - yvel
drawfilloval (x5, y5, 5, 5, black)
end if
if x5 >= 890 or x5 <= 1 then
x5 := x5 - 0
drawfilloval (x5, y5, 5, 5, black)
score := score + 1
score2 := score2 + 1
exit when score = 10 or score2 = 10
end if
end loop

Author:  Insectoid [ Fri Dec 25, 2015 1:38 pm ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

Your ball will always move up at a diagonal because every loop, you add 2 to both x and y.

Forget programing for a minute. What is velocity? Speed and direction.

What's going on in these two lines?
code:
x5 := x5 + 2
y5 := y5 + 2


You are adding two to both x and y. This means that every frame, your ball moves right two pixels and up two pixels. This means you x velocity is 2 to the right, and your y velocity is 2 to the left. If you change the number 2 to a variable, you can change the velocity of the ball.

x5 := x5 + velocity.

now, if velocity is a negative number, your ball will move to the left, instead of to the right (because you are subtracting from the x coordinate.

What is the math to change a positive number to a negative number?

Here's some pseudocode:

code:

int x
int y
int x_velocity
int y_velocity

loop
if ball hits right wall then
    switch x velocity from positive to negative
if ball hits left wall then
    switch x velocity from negative to positive
if ball hits roof then
   switch y velocity from postitive to negative
if ball hits floor then
   switch y velocity from negative to positive

x := x + x_velocity
y := y + y_velocity
end loop


See if you can figure it out from there.

Author:  chandrapanta [ Fri Dec 25, 2015 1:50 pm ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

thank you so much i fixed the problem and all i need to complete my game (ping pong) is to make the ball bounce off only when it hits the two paddles. So if it hits the left paddle it should bounce off instead of going through and increase the opponent's point and via. Other than that it does bounce off but it doesnt bounce off when the paddle touch the ball.

Author:  Nathan4102 [ Fri Dec 25, 2015 2:29 pm ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

You'll have to adjust your code for left and right code collisions. Two things need to be true for the ball to bounce off a paddle.
First, the ball has to be at the correct x position to collide with the paddle. This is the easy part because the x value won't change.
Second, you need to check that the y value of the ball is colliding with the paddle. You check this using the y value of your ball and the y value (and height) of your paddle.
If and only if both of these conditions are met, we should flip sign on the x velocity.
Me or Insectoid can give you more hints, but try to figure it out on your own. If its not your work, its not much of an accomplishment. Good luck Smile

Author:  Insectoid [ Fri Dec 25, 2015 3:03 pm ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

your paddle is a rectangle. Your ball is (more or less) a point. You need to apply point-rectangle collision. Grab some graph paper, draw a rectangle and a point inside it. Try to figure out a relationship between the point and the points of the rectangle (remember, in Turing, a rectangle has two points; bottom left and top right). What is the relationship when the point is inside the rectangle? What about outside?

Author:  chandrapanta [ Fri Dec 25, 2015 6:57 pm ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

kk this is what i have so far...

var font1, font2, font3, cnum, cnum2 : int
font1 := Font.New ("Courier:50:Bold italic")
font2 := Font.New ("Times New Roman:14:Bold")
font3 := Font.New ("Impact:14:italic")

var y, y2, y3, y4, xx, yy, bgc, paco, paco2 : int
var choice : string := " "
y := 100
y2 := 40
y3 := 30
y4 := 100
xx := 1
yy := 1
var chars : array char of boolean
var x5, y5 : int := 10
var xin, yin : int := 3
var score, score2 : int := 0




%How to play screen
setscreen ("Graphics:900;600")
colorback (black)
cls
color (white)
put "What color do you want the background to be?"
get bgc
put "What color do you want the first paddel to be?"
get paco
put "What color do you want the second paddel to be?"
get paco2
colorback (bgc)
cls
Font.Draw ("Controls", 300, 520, font1, black)
color (black)
locate (12, 30)
put "Player 1: up arrow to move up and down arrow to move down."
locate (14, 30)
put "Player 2: ctrl key to move up and alt key to move down."
locate (16, 50)
put "Enjoy!!!!!!"
delay (5000)
cls


loop
Input.KeyDown (chars)
%Player 1 Controls
if chars (KEY_UP_ARROW) then
y := y + 5
y2 := y2 + 5
end if
if chars (KEY_DOWN_ARROW) then
y2 := y2 - 5
y := y - 5
end if

%Player 2 Controls
if chars (KEY_CTRL) then
y3 := y3 + 5
y4 := y4 + 5
end if
if chars (KEY_ALT) then
y4 := y4 - 5
y3 := y3 - 5
end if
delay (15)
cls
drawfillbox (885, y3, 910, y4, paco)
drawfillbox (0, y, 10, y2, paco2)
drawfilloval (x5, y5, 5, 5, purple)
delay (20)
cls

%Moving ball and score calculation
x5 := x5 + xin
y5 := y5 + yin

if x5 <= 1 then
score2 := score2 + 1
xin := 0 - xin
elsif x5 > 890 then
score := score + 1
xin := 0 - xin
end if

if x5 = 10 then
xin := xin - 1
end if

if x5 = 885 then
xin := xin - 1
end if
if y5 < 10 or y5 > 590 then
yin := 0 - yin
end if
locate (1, 10)
put "Player 1 score:", score
locate (1, 85)
put "Player 2 score:", score2
exit when score >= 10 or score2 >= 10
end loop

delay (300)
cls
colorback (bgc)
cls
if score >= 10 then
Font.Draw ("Good job, the first player won", 350, 350, font2, white)
end if
if score2 >= 10 then
Font.Draw ("Good job, the second player won", 350, 350, font2, white)
end if
delay (1000)
cls
put "Do you want to play again (y/n)?"
loop
get choice
exit when choice = "n"
end loop

so how can i make it so that when the ball collides with the paddle it bounces back

Author:  Insectoid [ Fri Dec 25, 2015 7:48 pm ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

code:
if x5 = 10 then
xin := xin - 1
end if


What are you trying to do with this code?

Author:  chandrapanta [ Fri Dec 25, 2015 9:34 pm ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

I want to make the ball bounce off when it touches the paddle on the left and I need to do the same for the other paddle on the right side.

Author:  Insectoid [ Sat Dec 26, 2015 7:58 am ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

Well, right now that code checks if the ball is exactly at x = 10, and if it is, accelerates it by one pixel per frame. What if the ball moves past x = 10 without ever hitting it? For example, in one frame it could be at x = 9, and then the next at x = 11. It never actually hit 10, so that condition will never trip. You need to check if it's anywhere on the paddle, not just the edge of it.

What about the y axis? What if the paddle is at the top and the ball is at the bottom? Your code doesn't check for that at all. You need to compare the ball's y value to the paddle's y value(s).

It looks like you've already figured out how to make the ball bounce off the top and bottom, so you just need to apply that to bouncing off the paddle. It's the same thing, just in the x direction instead of the y direction.

Author:  chandrapanta [ Sat Dec 26, 2015 8:12 am ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

So I should just set an y condition instead of an X. For example, y5:=the height of the first paddle then x5:=0-Xin. And the same for the other side as well.??? I'm really not getting how to make the ball bounce off the paddle. Please I really need help!

Author:  Insectoid [ Sat Dec 26, 2015 8:21 am ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

Quote:
y5:=the height of the first paddle


What is the height of the first paddle? The y value of it? That would only be the bottom corner. Your paddle is probably 10 or 20 pixels tall. You need to check if it's between the top and bottom of the paddle. How does the y value of the ball relate to the y values of the paddle when it's between?

You will also need to compare the x values of the ball and paddle. In order for a collision to occur, the ball's x value has to be at or past the paddle's x value, and the ball's y value has to be in between the paddle's y values. Then, and only then, can you run the line x5 :=0 - Xin.

By the way, you should rename your variables to something more descriptive. What is x5? Why did you name it x5? Why not call it ballX or something, so if someone else reads your code, they don't have to figure out what it is? If they see 'ballX' it's obvious that it represents the ball's x value.

Author:  chandrapanta [ Sat Dec 26, 2015 1:19 pm ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

I took your advice and changed the code to:

if ballx >= 10 then
ballx := 0 - xin
elsif ballx >= 910 then
ballx := 0 - xin
end if

if bally >= y and bally <= y2 then
ballx := 0 - xin

elsif bally >= y3 and bally <= y4 then
ballx := 0 - xin
end if

but it still doesn't work because on the output screen the ball sticks with the paddle and then clears the screen and says that the second player won? I don't get this i made the ballx and the x value of the paddle the same as well as the y value in between the paddle's y value and it doesn't work.

Author:  chandrapanta [ Sat Dec 26, 2015 1:21 pm ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

Oh and here is all the variables:

var y, y2, y3, y4, xx, yy, bgc, paco, paco2 : int
var choice : string := " "
y := 100
y2 := 40
y3 := 30
y4 := 100
xx := 1
yy := 1
var chars : array char of boolean
var ballx, bally : int := 10
var xin, yin : int := 3
var score, score2 : int := 0

drawfillbox (885, y3, 910, y4, paco)
drawfillbox (0, y, 10, y2, paco2)
drawfilloval (ballx, bally, 5, 5, purple)
delay (20)
cls

%Moving ball and score calculation
ballx := ballx + xin
bally := bally + yin

this comes before the codes that i changed in the last post.

Author:  Insectoid [ Sat Dec 26, 2015 3:21 pm ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

You're getting closer. You need to combine your x coordinate checks with your y coordinate checks. At the moment, your code checks the x values, and then the y values, and if either of them collide, then it switches direction of the ball. If both of them collide, then it switches the direction of the ball twice (so the ball doesn't really change direction at all). You need to only bounce the ball if both the x values and the y values collide. You also need to look at some of your greater than/less than signs. Some of them are wrong.

Author:  chandrapanta [ Sat Dec 26, 2015 4:59 pm ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

Like you mean:

If ballx>=10 and bally<=y2 then
ballx:=0-xin
End if

If ballx<=910 and bally>=y4then
ballx:=0-xin
End if

Yes is that the what you mean? Can you specifically tell me what signs are facing the wrong way.

Author:  Insectoid [ Sat Dec 26, 2015 7:55 pm ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

code:
If ballx>=10


This means 'if ballX is greater than or equal to 10'. When is ballX greater than 10? almost always. Are you sure you don't want less than 10?

Every time you add something to your code, you're taking something away. One step forward, two steps back. You're so close.
code:
If ballx>=10 and bally<=y2 then


What happened to this part?
code:
bally >= y


You need to put all of the pieces together. Don't just glue some bits together and throw the others away. All the pieces are there. Just stick them together.

Author:  chandrapanta [ Sun Dec 27, 2015 10:11 am ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

Yo I did it. I found out how to make it bounce it when it collides with the paddle. But i only found that out for the paddle on the left. The one on the right doesn't work

this one works but when it touches the paddle, it reflects off too fast;like a cannon

if ballx <= 10 and bally >= y2 and bally <= y then
xin := xin + ballx
delay (20)
end if

this one doesn't work for some reason
if ballx >= 910 and bally >= y4 and bally >= y3 then
xin := xin + ballx
delay (50)
end if

Author:  Insectoid [ Sun Dec 27, 2015 11:51 am ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

Take a close look at this line:

code:
if ballx >= 910 and bally >= y4 and bally >= y3 then


This is an obvious mistake that you've made before earlier in this thread. Don't just post here right away when something doesn't work. You need to look for the bug on your own first. 95% of the time, when my code doesn't work, it's something dumb like this. These mistakes are the very first thing I check for.

And take a look at this line:
code:
xin := xin + ballx


Now I'm confused. What is xin? I thought it was your velocity. Why are you adding the ball's x coordinate to your velocity? Maybe this line actually makes sense, but I don't know, because I don't know what your variables mean. When I said x5 was a bad variable name, I didn't mean just x5 was a bad variable name. X1/2/3 and Y1/2/3 are all terrible names. xin is a terrible name. You need to change all of these to something useful. If you can't tell what a variable is just by looking at its name, it's a bad variable name. What the hell is paco? Remember, variable names don't have to be super-short abbreviations and acronyms. Paddle1X1 is a totally fine name, and tells me a whole lot more than just x or x2 or xx.

Author:  chandrapanta [ Sun Dec 27, 2015 12:28 pm ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

Okay I'll rename it but why doesn't that line work? It is because some of the signs are facing the wrong way? And don't worry about paco and paco2 and bgc those are just background colours and paddle colours. The other line works fine but this next one doesn't. Like for the paddle on the left.

Author:  Insectoid [ Sun Dec 27, 2015 2:04 pm ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

Quote:
don't worry about paco and paco2 and bgc


I do worry about it. Those are bad variable names. What's wrong with 'backgroundcolor'? I'm not just trying to help you with this assignment, I'm trying to help you become a better programmer in general. Good variable names are a critical part of good programming.

Quote:
why doesn't that line work? It is because some of the signs are facing the wrong way


It's up to you to figure that out. Why are you checking if bally is bigger than y3? You're already checking if it's bigger than y4, and isn't y4 always bigger than y3? Therefore, if bally is bigger than y4, then it's also bigger than y3, so there's no reason to check for that. Unless maybe you were trying to check something else?

Author:  chandrapanta [ Sun Dec 27, 2015 5:13 pm ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

Hey Insectoid thank you for all your help, but i just do not understand this last part on how to make the ball bounce off the other paddle. so can you do me a favor and delete your post so that i can delete the one before that and so on until i can erase my codes because i do not want others to use my work. Please and thank you! Or at least let me delete the post with most of the code on it.This is because it is not letting me delete post that have been replied to. Sorry for the inconvenience and once again i would be glad if you did this last task to not let others use my work. Thank you. Unless you can tell me an even easier way to delete this topic or a specific post.

Author:  Insectoid [ Sun Dec 27, 2015 6:41 pm ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

code:
i just do not understand this last part on how to make the ball bounce off the other paddle.


Well, what's the difference between the code for the paddle that works, and for the paddle that doesn't?

No, I won't delete your posts for you. This thread will serve as a learning resource for future students. There are hundreds of working Pong submissions on this website. There is no reason for anyone to steal incorrect code. If you're worried about being accused of plagiarism, you can prove to your teacher that this is your account by logging into it.

Author:  chandrapanta [ Sun Dec 27, 2015 6:49 pm ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

The variables I guess and the value of the variables as well as the direction of the signs.

And I changed the second part of the code to
If ballx>=885 and bally<=y4 then
Xin:=xin+ballx
End if

Cuz you said that 100 or y4 is already greater than 30 or y3 and is not needed.
but it doesn't work the ball bounces so fast and disappears after and says that the first player won?

Author:  Insectoid [ Sun Dec 27, 2015 7:24 pm ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

Quote:
you said that 100 or y4 is already greater than 30 or y3 and is not needed


Based on the checks you were doing it was not needed. More likely it was needed, but you were doing the wrong checks. Instead of just changing things because I told you they were wrong, try to figure out why it's wrong. You're just throwing things at the wall to see what sticks. That does not work when programming.

That line of code should look almost identical to the line for the paddle that works.

Now, you tell me the ball is shooting off the screen. Maybe the ball is accelerating? What would make the ball accelerate? Maybe changing the velocity? Are you changing the velocity anywhere in your code? Is there anywhere in the code that the velocity increases?

Author:  chandrapanta [ Mon Dec 28, 2015 8:28 am ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

It is hard to make the codes identical because this paddle is located on the opposite side of the one that is working so that might mean everything is backwards. And I tried to make everything the opposite but it doesn't work. When the ball touches the paddle the ball dissaperas and then repeats in a random spot.??????????????????????????????????

Author:  Insectoid [ Mon Dec 28, 2015 1:06 pm ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

Quote:
I tried to make everything the opposite


Instead of just making everything the opposite, figure out what parts need to be the opposite, what parts need to change in other ways, and what parts can stay the same. It can help to draw the ball and paddle on graph paper and label the coordinates so you can actually see the relationship between all the variables at the time of collision.

Author:  chandrapanta [ Mon Dec 28, 2015 6:54 pm ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

Good news, i managed to get the ball to bounce off of both of the paddles, but for some reason the score board is not working. The scores only work properly for the second player but not for the first? And how can i make the paddles and the ball blink less and become more clear. Cuz right now all the paddles and the ball blinks very fast, which i do not want.

Here is my codes so far:

var paddle1y, paddle1y2, paddle2y, paddle2y2, xx, yy, backgroundcolor, paddle1color, paddle2color : int
var choice : string := " "
paddle1y := 100
paddle1y2 := 40
paddle2y := 30
paddle2y2 := 100
var chars : array char of boolean
var ballx, bally : int := 10
var xvelocity, yvelocity : int := 3
var score, score2 : int := 0


ballx := ballx + xvelocity
bally := bally + yvelocity

% Collsion with the ball, if the y and x of the ball are the same as the paddle then it will bounce off
if ballx <= 10 and bally >= paddle1y2 and bally <= paddle1y then
xvelocity := 0 - xvelocity
delay (20)
end if

if ballx >= 885 or bally >= paddle2y2 and bally <= paddle2y then
xvelocity := 0 - xvelocity
end if

%Making the bounce ball when it is not in contact with the paddles and calculating the scores
if ballx <= 1 then
score2 := score2 + 1
xvelocity := 0 - xvelocity

elsif ballx >= 890 then
score := score + 1
xvelocity := 0 - xvelocity
end if

if bally < 10 or bally > 590 then
yvelocity := 0 - yvelocity
end if

%Displaying the score
locate (1, 10)
put "Player 1 score: ", score
locate (1, 85)
put "Player 2 score: ", score2
exit when score >= 10 or score2 >= 10

please help???

Author:  Insectoid [ Tue Dec 29, 2015 5:54 am ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

When you run your code, it looks like the paddles are working properly, but they actually aren't. Your scores actually do work. It's the paddles that don't. Paddle one never works. Look at this line:
code:
if ballx <= 10 and bally >= paddle1y2 and bally <= paddle1y then


Will that line ever return true? Substitute the Can bally ever be bigger than y2 but smaller than y1? Is that even possible?

Now look at this line:
code:
if ballx >= 885 or bally >= paddle2y2 and bally <= paddle2y then


This line actually will return true, but not at the right time. Nothing after the 'or' will ever return true, for the same reason that the line for paddle1 will never return true. However, because you used an 'or', if the first part is true, then the whole thing is true. Which means that any time ballx is bigger than 885, the paddle will bounce, whether the ball hits the paddle or not.

So why don't the scores work (and neither of them do)? On the left side (player 1), the ball never, ever bounces off the paddle. It goes through the paddle and bounces off the wall, which gives player 2 a point, even if he shouldn't have. On the right side, the pall always 'hits' the paddle and bounces off, even when it shouldn't, so the ball never hits the wall, so player 1 never gets a point, even when he should.

Author:  chandrapanta [ Tue Dec 29, 2015 12:28 pm ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

I solved the problem, the ball bounces off the paddles nicely, even though the ball and the paddle flicker so fast. And the point thing works greatly. I just have one question, how can i make it flicker less but run fast at the same time. Like i dont mean increase the delay time.

like this is my code:

drawfilloval (ballx, bally, 5, 5, brightred)
delay (20)
cls

drawfillbox (885, paddle2y, 910, paddle2y2, paddle2color)
drawfillbox (0, paddle1y, 10, paddle1y2, paddle1color)
delay (25)
cls

but when i play the game, the shapes blink so fast, i want it to not blink as fast but still run at the same speed.

Author:  Nathan4102 [ Tue Dec 29, 2015 12:42 pm ]
Post subject:  Re: How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a

What you're looking for is called double buffering.

Author:  chandrapanta [ Tue Dec 29, 2015 3:47 pm ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

How do I do that? What is this double buffering?

Author:  Insectoid [ Tue Dec 29, 2015 3:55 pm ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

Did you read the link Nathan provided?

Author:  chandrapanta [ Tue Dec 29, 2015 5:04 pm ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

Forget it, it's too complicated for me. Well thanks for all the help. Anyways do u have any ideas on how to make my ping pong game better and really fun? Like maybe I can let the user chose whether thy want multiplayer or single player. I don't know... But I'm basically done everything.

Author:  Nathan4102 [ Wed Dec 30, 2015 12:27 am ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

It's not complicated at all, give it a read. Giving up is a bad habit you should try to break, especially if you're looking to pursue computer science. Try reading it and if you have any specific questions, let us know.

Author:  chandrapanta [ Wed Dec 30, 2015 10:21 am ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

Do you know of any retro games such as snake or tetris? Basically like a game that is simple but really fun to play because i was thinking of making something else. And thanks for all your help

Author:  Insectoid [ Wed Dec 30, 2015 2:09 pm ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

I'd recommend fixing Pong up so it's nice and polished- remove the flicker, add multiplayer, etc. Pong really is the most simple game to program. Snake and Tetris are orders of magnitude more difficult and if you're struggling with Pong, you don't stand a chance with those. If you finish your Pong game, you'll learn a lot more of the fundamentals that you need to know to build anything more complicated.

Author:  chandrapanta [ Wed Dec 30, 2015 4:11 pm ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

ping pong already has multiplayer so why would i add it again? Wow thanks for crushing my hopes of making snakes or tetris or anything for that matter. Do you mean single player? If i add single player then i need to have blocks for it to hit. Or is that breakout? Whatever in single player should i should keep everything the same but remove the second player?

Author:  Insectoid [ Wed Dec 30, 2015 4:44 pm ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

You could. Or you could write an AI to control the opposite paddle, which would be more interesting.

I don't mean to crush your dreams. I mean that you should finish up your current project before moving on to something bigger. We gave you a link to a tutorial to fix the flickering in your game, and you told us it was too hard for you (it's not). Do you think there won't be flickering in Snake or Tetris? It will be worse, unless you learn how to solve it.

Do you think calculating collisions between complicated shapes like in Tetris is any easier than between a paddle and a ball? The code for those collisions alone would be longer than the entire code you wrote for Pong.

You gave up on Pong, but you want to move on to a bigger, more difficult project? Don't limit yourself, but don't set unrealistic goals either. Take things in steps. You will be able to make Snake or Tetris very soon, but you need to learn the basics. Otherwise, you'll be wasting both your time and ours asking huge, broad questions like 'how do I make tetris shapes not fall through each other' when you haven't even discovered how to detect a collision between simple rectangles.

Author:  chandrapanta [ Thu Dec 31, 2015 8:51 am ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

What do u mean write an AI and control the opposite paddle? What is an AI and explain what is supposed to be meant from controlling the opposite paddle???

Author:  Nathan4102 [ Thu Dec 31, 2015 9:39 am ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

After you hit it from one side, make the program try to hit it back to you.

Author:  chandrapanta [ Thu Dec 31, 2015 5:18 pm ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

So basically one player? Or the computer is controlling a paddle of its one? If I have one player then the person has to hit the other side and when it bounces off the wall and goes behind the paddle then they lose? Is that what u mean? Can u eleobrate more?

Author:  Nathan4102 [ Thu Dec 31, 2015 9:53 pm ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

Your player controls one paddle, the program controls the second paddle. Based on what side misses the ball, give a point to the appropriate side.

Author:  chandrapanta [ Fri Jan 01, 2016 9:56 am ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

How do i make the computer control one of them? like what do i use, i dont think i have learned how to make the computer control itself. And do i just copy my multiplayer codes but delete the part about the other player? Please explain!

Author:  Insectoid [ Fri Jan 01, 2016 4:39 pm ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

Quote:
i dont think i have learned how to make the computer control itself.


You made the computer control the ball, didn't you? This is no different.

The paddle only does two things, move up and move down. Look at the code for the player controlled paddles:

code:
if up arrow is pressed then
    move paddle up
if down arrow is pressed then
    move paddle down
end if


The code for the AI (artificial intelligence, ie computer player) paddle will be almost the same. Obviously you can't use the keyboard to decide what the paddles will do, so you need to find some other way to decide. Think about it, when would you want the paddle to move up? When do you want it to move down?

Author:  chandrapanta [ Fri Jan 01, 2016 4:54 pm ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

when the ball comes near it?

Author:  Insectoid [ Fri Jan 01, 2016 5:51 pm ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

When YOU play the game, when do you push the up key? When do you push the down key?

Author:  chandrapanta [ Sat Jan 02, 2016 6:10 am ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

I push the key randomly. Or if the ball is heading up then I push key up and if it's heading down I push down arrow.

Author:  Insectoid [ Sat Jan 02, 2016 2:38 pm ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

Quote:
if the ball is heading up then I push key up and if it's heading down I push down arrow.


So make the computer do this. If the ball is going up, move the paddle up. If the ball is going down, move the paddle down.

Author:  chandrapanta [ Sat Jan 02, 2016 2:56 pm ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

But then how can the other player score a point? If the paddle follows exactly the ball how is the ball gonna go behind?Or is that the point?

Author:  Nathan4102 [ Sat Jan 02, 2016 5:48 pm ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

You could make the paddle move at a certain vertical speed, a speed less than the max vertical speed of the ball. That way you'll be able to score ocasional points against the cpu

Author:  chandrapanta [ Sat Jan 02, 2016 8:07 pm ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

Yo mean make the delay for the paddle that the computer is control longer than the ball and my paddle?

Author:  Insectoid [ Sat Jan 02, 2016 8:39 pm ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

Why not try it and see if it works?

Author:  chandrapanta [ Sat Jan 02, 2016 9:21 pm ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

This is my code but the computer always manages to reach and make the ball bounce on time before it reaches the wall.

var paddle1y, paddle1y2, paddle2y, paddle2y2, xx, yy : int
var backgroundcolor, paddle1color, paddle2color, paddlecolor3, answer : int
var choice, name, name2, name3 : string := " "
paddle1y := 100
paddle1y2 := 40
paddle2y := 30
paddle2y2 := 100
var chars : array char of boolean
var ballx, bally : int := 10
var xvelocity, yvelocity : int := 3
var score, score2 : int := 0

loop
Input.KeyDown (chars)

%Player 1 Controls
if chars (KEY_CTRL) then
paddle1y := paddle1y + 5
paddle1y2 := paddle1y2 + 5
end if
if chars (KEY_ALT) then
paddle1y2 := paddle1y2 - 5
paddle1y := paddle1y - 5
end if

%Drawing the shapes which are to be used in the game
drawline (470, 900, 470, 0, black)

drawfilloval (ballx, bally, 5, 5, brightred)
delay (35)
cls

%Moving the position of the ball and the paddles
ballx := ballx + xvelocity
bally := bally + yvelocity

drawfillbox (0, paddle1y, 10, paddle1y2, paddle1color)
delay (35)
cls

drawfillbox (885, paddle2y, 910, paddle2y2, paddlecolor3)
delay (50)
cls

if bally >= paddle2y2 and ballx >= 470 then
paddle2y := paddle2y + 2
paddle2y2 := paddle2y2 + 2
end if
if bally <= paddle2y and ballx >= 470 then
paddle2y2 := paddle2y2 - 2
paddle2y := paddle2y - 2
end if

Author:  Insectoid [ Sat Jan 02, 2016 9:44 pm ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

code:
if bally >= paddle2y2 and ballx >= 470 then


By the looks of this line here, your paddle won't do anything until it's already too late. I'll leave it to you to figure out why.

And holy smokes, that runs extremely slowly and flickers like mad! All of your delays add up to 120ms per frame! That gives you an absolute maximum possible speed of 8.3 frames per second! And you're clearing the screen every time you draw something! Why? I mean, flickering is a common problem in Turing, but not for this reason. This game will run just fine with zero flickering without any special code, and I'll tell you how.

What you're doing is drawing one thing, then waiting, and then erasing it before you draw the next thing. Why do you have to erase it? You're allowed to have two things on the screen at a time! You only need to erase things when things move. When do things move? Between frames! So just draw everything at once inside the loop, and then wait once, and then erase everything once. This way, you kill two birds with one stone. You speed up the game by literally over one thousand percent (I'm not exaggerating, it will speed up that much), and on top of that, it won't flicker anymore!

Author:  chandrapanta [ Sun Jan 03, 2016 8:55 am ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

Really? So there should not be a delay and a cold after I draw something?

This is my code but the computer always manages to reach and make the ball bounce on time before it reaches the wall.

var paddle1y, paddle1y2, paddle2y, paddle2y2, xx, yy : int
var backgroundcolor, paddle1color, paddle2color, paddlecolor3, answer : int
var choice, name, name2, name3 : string := " "
paddle1y := 100
paddle1y2 := 40
paddle2y := 30
paddle2y2 := 100
var chars : array char of boolean
var ballx, bally : int := 10
var xvelocity, yvelocity : int := 3
var score, score2 : int := 0

loop
Input.KeyDown (chars)

%Player 1 Controls
if chars (KEY_CTRL) then
paddle1y := paddle1y + 5
paddle1y2 := paddle1y2 + 5
end if
if chars (KEY_ALT) then
paddle1y2 := paddle1y2 - 5
paddle1y := paddle1y - 5
end if

%Drawing the shapes which are to be used in the game
drawline (470, 900, 470, 0, black)

drawfilloval (ballx, bally, 5, 5, brightred)

%Moving the position of the ball and the paddles
ballx := ballx + xvelocity
bally := bally + yvelocity

drawfillbox (0, paddle1y, 10, paddle1y2, paddle1color)

drawfillbox (885, paddle2y, 910, paddle2y2, paddlecolor3)
delay (30)
cls

Is that what you mean? And I only did the line of codes that u put in quotes because I was checking to see if i made it onto move when it came near, the ball wouldn't bounce off the computer controlled paddle all the time. And I even slowed down the delay an decreased the interval from 5 to 2. But it doesn't work. How can I make it so that it misses sometimes and sometimes it doesn't because then it would be impossible to score a point?

Author:  Insectoid [ Sun Jan 03, 2016 11:00 am ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

Look, you're not linking your entire code. All of the bits pertaining to your problem are missing. I can't help you if I can't see what you're doing.

That said, changing the delay won't work. It only controls the speed of the animation, not the relative speed between two objects. You can change something else though.

Author:  chandrapanta [ Sun Jan 03, 2016 12:40 pm ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

I fixed it, by that i mean the flickering. And for the computer controlled paddle i decreased the interval by which it increases or decreases and it seems to work. One other question: my paddles can go off screen if you hold down the controls. So basically i only want them to be able to go within the screen not off the screen. How can i fix that?
All the paddles can do that except the computer controlled one. So yeah.

here is my code:

var paddle1y, paddle1y2, paddle2y, paddle2y2, xx, yy : int
var backgroundcolor, paddle1color, paddle2color, paddlecolor3, answer : int
var choice, name, name2, name3 : string := " "
paddle1y := 100
paddle1y2 := 40
paddle2y := 30
paddle2y2 := 100
var chars : array char of boolean
var ballx, bally : int := 10
var xvelocity, yvelocity : int := 3
var score, score2 : int := 0


loop
Input.KeyDown (chars)

%Player 1 Controls
if chars (KEY_CTRL) then
paddle1y := paddle1y + 5
paddle1y2 := paddle1y2 + 5
end if
if paddle1y <= 900 and paddle1y2 = 540 then
paddle1y := paddle1y + 0
paddle1y2 := paddle1y2 + 0
end if


if chars (KEY_ALT) then
paddle1y2 := paddle1y2 - 5
paddle1y := paddle1y - 5
end if

if paddle1y2 >= 0 then
paddle1y := paddle1y - 0
paddle1y2 := paddle1y2 - 0
end if

Basically if the paddle reaches to the top of the output screen then i dont want it to increase anymore. Therefore not being off the screen and if it reached the bottom then it shouldnt decrease any more.

Author:  Insectoid [ Sun Jan 03, 2016 2:07 pm ]
Post subject:  RE:How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a s

I'll let you figure this one out on your own.


: