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 Previous  1, 2, 3, 4, 5  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Insectoid




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




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




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




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




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




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




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




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




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




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




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




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




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




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




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


Style:  
Search: