
-----------------------------------
chandrapanta
Thu Dec 24, 2015 3:55 pm

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 = 590 or y5 = 890 or x5 = 885 or bally >= paddle2y2 and bally = paddle2y2 and ballx >= 470 then
            paddle2y := paddle2y + 2
            paddle2y2 := paddle2y2 + 2
        end if
        if bally = 470 then
            paddle2y2 := paddle2y2 - 2
            paddle2y := paddle2y - 2
        end if

-----------------------------------
Insectoid
Sat Jan 02, 2016 9:44 pm

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 [/code]

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
Sun Jan 03, 2016 8:55 am

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
Sun Jan 03, 2016 11:00 am

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
Sun Jan 03, 2016 12:40 pm

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 = 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
Sun Jan 03, 2016 2:07 pm

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.
