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

Username:   Password: 
 RegisterRegister   
 Moving the ball in Brick Breaker.
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
miller6




PostPosted: Tue Jan 19, 2010 11:20 am   Post subject: Moving the ball in Brick Breaker.

What is it you are trying to achieve?
I am trying to move the ball (in this case a snowflake) just like the ball in breakbreaker. Where it bounces off the side of the wall when it hits it.

What is the problem you are having?
Moving the ball all around the screen and showing the movement of it.


Describe what you have tried to solve this problem
Alot. I have tried different ways of moving it, but still have problems.


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
This is the main part of moving the ball. Not entire program posted.
Turing:


var x_s, y_s : int := 1  % sflake coordinates
var width_s : int := 59 % sflake width
var sflake : int := Pic.FileNew ("sflake.gif")

loop

x_s := Rand.Int (1, maxx)
y_s := Rand.Int (1, maxy)

Pic.Draw (sflake, x_s, y_s, picMerge)
if (x_s + width_s) >= (maxx - 1) then
        x_s := x_s * -1

    elsif (y_s + width_s) >= (maxy - 1) then
        y_s := y_s * -1
    elsif (x_s - width_s) <= 1 then
        x_s := 2

    elsif (y_s - width_s) <= 1 then
        y_s := 2 
    end if
delay (500)
end loop




Please specify what version of Turing you are using
4.1.1



sflake.gif
 Description:
 Filesize:  1.7 KB
 Viewed:  4062 Time(s)

sflake.gif



sflake.gif
 Description:
 Filesize:  1.7 KB
 Viewed:  4062 Time(s)

sflake.gif


Sponsor
Sponsor
Sponsor
sponsor
miller6




PostPosted: Tue Jan 19, 2010 11:20 am   Post subject: RE:Moving the ball in Brick Breaker.

sorry for the double attachment.
Turing_Gamer




PostPosted: Tue Jan 19, 2010 5:34 pm   Post subject: Re: Moving the ball in Brick Breaker.

Have 2 separate variables: speed and direction. Then do the following...
Turing:
loop
    x := x + xdir
    y := y + ydir
    if x >= maxx then
        xdir := xdir * 1
    elsif x <= 0 then
        xdir := xdir * 1
    end if
    % Repeat for y coordinate
end loop

Puting how fast and what direction is easy. Use either boolean or if statements.
TheGuardian001




PostPosted: Tue Jan 19, 2010 6:22 pm   Post subject: Re: Moving the ball in Brick Breaker.

Turing_Gamer @ Tue Jan 19, 2010 5:34 pm wrote:
Have 2 separate variables: speed and direction. Then do the following...
Turing:
loop
    x := x + xdir
    y := y + ydir
    if x >= maxx then
        xdir := xdir * 1
    elsif x <= 0 then
        xdir := xdir * 1
    end if
    % Repeat for y coordinate
end loop

Puting how fast and what direction is easy. Use either boolean or if statements.


I think you probably meant
code:

xdir = -xdir %invert the x velocity


since multiplying by 1 won't do anything...
andrew.




PostPosted: Tue Jan 19, 2010 8:53 pm   Post subject: RE:Moving the ball in Brick Breaker.

Or you can multiply by -1 (which was probably his intention).

Turing:
xdir *= -1 % invert the x component
miller6




PostPosted: Wed Jan 20, 2010 11:17 am   Post subject: Re: Moving the ball in Brick Breaker.

okay, so it is still doing the same thing, its just popping up in random places. It is not travelling from each position to each position. This is the changed code:


Turing:


var x_s, y_s : int := 1  % sflake coordinates
var xdir_s, ydir_s : int := 1
var width_s : int := 59 % sflake width
var sflake : int := Pic.FileNew ("sflake.gif")

loop
    x_s := Rand.Int (1, maxx)
    y_s := Rand.Int (1, maxy)
    x_s := x_s + xdir_s
    y_s := y_s + ydir_s

    Pic.Draw (sflake, x_s, y_s, picMerge)
   
    if (x_s + width_s) >= (maxx - 1) then
        xdir_s := xdir_s * -1
    elsif (y_s + width_s) >= (maxy - 1) then
        ydir_s := ydir_s * -1
    elsif (x_s - width_s) <= 1 then
        xdir_s := xdir_s * -1
    elsif (y_s - width_s) <= 1 then
        ydir_s := ydir_s * -1
    end if
    delay (500)
end loop

miller6




PostPosted: Wed Jan 20, 2010 12:13 pm   Post subject: Re: Moving the ball in Brick Breaker.

Okay, so now i changed it for it to bounce off the side at a different angle each time but still am having trouble getting it to move in one continious motion.

Here is the new coding:
Turing:


var WinId : int
var x_s, y_s : int := 1  % sflake coordinates
var width_s : int := 59 % sflake width
var x_angle, y_angle, x_angle2, y_angle2, x_angle3, y_angle3 : int := 1
var sflake : int := Pic.FileNew ("sflake.gif")

WinId := Window.Open ("position:centre;centre,graphics:1010;695")

loop
    x_s := Rand.Int (1, maxx)
    y_s := Rand.Int (1, maxy)

    x_angle := Rand.Int (-3, -1)
    y_angle := Rand.Int (-3, 3)
    x_angle2 := Rand.Int (-3, 3)
    y_angle2 := Rand.Int (-3, -1)
    x_angle3 := Rand.Int (1, 3)
    y_angle3 := Rand.Int (1, 3)

    if (x_s + width_s) >= (maxx - 1) then 
        y_s := y_s + y_angle

    elsif (y_s + width_s) >= (maxy - 1) then
        y_s := y_s + y_angle2
        x_s := x_s + x_angle2

   
    elsif (x_s + width_s) <= 1 then
        x_s := x_s + x_angle3
        y_s := y_s + y_angle

    elsif (y_s + width_s) <= 1 then
        y_s := y_s + y_angle3
        x_s := x_s + x_angle
    end if
   
    Pic.Draw (sflake, x_s, y_s, picMerge)
    delay (500)
end loop

miller6




PostPosted: Wed Jan 20, 2010 12:16 pm   Post subject: Re: Moving the ball in Brick Breaker.

sorry, i missed part of it.

Turing:


var WinId : int
var x_s, y_s : int := 1  % sflake coordinates
var width_s : int := 59 % sflake width
var x_angle, y_angle, x_angle2, y_angle2, x_angle3, y_angle3 : int := 1
var sflake : int := Pic.FileNew ("sflake.gif")

WinId := Window.Open ("position:centre;centre,graphics:1010;695")

loop
    x_s := Rand.Int (1, maxx)
    y_s := Rand.Int (1, maxy)

    x_angle := Rand.Int (-3, -1)
    y_angle := Rand.Int (-3, 3)
    x_angle2 := Rand.Int (-3, 3)
    y_angle2 := Rand.Int (-3, -1)
    x_angle3 := Rand.Int (1, 3)
    y_angle3 := Rand.Int (1, 3)

    if (x_s + width_s) >= (maxx - 1) then 
        x_s := x_s + x_angle
        y_s := y_s + y_angle

    elsif (y_s + width_s) >= (maxy - 1) then
        y_s := y_s + y_angle2
        x_s := x_s + x_angle2

    elsif (x_s + width_s) <= 1 then
        x_s := x_s + x_angle3
        y_s := y_s + y_angle

    elsif (y_s + width_s) <= 1 then
        y_s := y_s + y_angle3
        x_s := x_s + x_angle
    end if

    Pic.Draw (sflake, x_s, y_s, picMerge)
    delay (500)
end loop


Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Wed Jan 20, 2010 1:33 pm   Post subject: RE:Moving the ball in Brick Breaker.

code:
x_angle := Rand.Int (-3, -1)
    y_angle := Rand.Int (-3, 3)
    x_angle2 := Rand.Int (-3, 3)
    y_angle2 := Rand.Int (-3, -1)
    x_angle3 := Rand.Int (1, 3)
    y_angle3 := Rand.Int (1, 3)

This part should not be in the loop. You are selecting a random direction every time the loop iterates, which makes your ball fuffle around aimlessly.
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 1  [ 9 Posts ]
Jump to:   


Style:  
Search: