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
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Insectoid




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




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




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




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




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




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




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


Style:  
Search: