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

Username:   Password: 
 RegisterRegister   
 How to make the ball bounce, without using anything too complicated (no whatdotcolor or maxy,maxx) I want to find a simp
Index -> Programming, Turing -> Turing Help
Goto page 1, 2, 3, 4, 5  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
chandrapanta




PostPosted: 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
Sponsor
Sponsor
Sponsor
sponsor
Nathan4102




PostPosted: 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.
chandrapanta




PostPosted: 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
Insectoid




PostPosted: 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?
chandrapanta




PostPosted: 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.
Insectoid




PostPosted: 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?
chandrapanta




PostPosted: 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!
Nathan4102




PostPosted: 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
Sponsor
Sponsor
Sponsor
sponsor
chandrapanta




PostPosted: 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...
chandrapanta




PostPosted: 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
Insectoid




PostPosted: 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.
chandrapanta




PostPosted: 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.
Nathan4102




PostPosted: 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
Insectoid




PostPosted: 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?
chandrapanta




PostPosted: 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
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 5  [ 67 Posts ]
Goto page 1, 2, 3, 4, 5  Next
Jump to:   


Style:  
Search: