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

Username:   Password: 
 RegisterRegister   
 1-D and 2-D bouncing letter
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
baby_bubblz




PostPosted: Thu Feb 17, 2005 7:15 pm   Post subject: 1-D and 2-D bouncing letter

hi if somebody could just help me with this it'd be great..thanks

i'm trying to get a letter to bounce across the screen, just back and forth, this is what i have so far...

var x,y : int := 0

loop
locate (x,y)
put "e"
delay(50)
cls
y:= y+1 <--here i'm trying to make the letter bounce to the right
if y = 80 then
y:= y-1 <--here i'm trying to make the letter bounce back the other way
end if
if y= 0 then
y:= y+1 <-- and here make it bounce to the right again
end if
end loop

i also need help with bouncing a letter anywhere on the screen, and i'm not sure how to make the letter go in a diagonal direction..
Sponsor
Sponsor
Sponsor
sponsor
TheZsterBunny




PostPosted: Thu Feb 17, 2005 7:41 pm   Post subject: (No subject)

hrm.

this reminds me of all the pong tutorials we have.

what you need to do is to establish an X and a Y displacement factor (lol. sounds high-tech)

you then need to increment your position by those values.

If one of the coordinates is out of bounds, multiply the displacement factor by -1 to reverse direction.

i.e.
code:

var xfact, yfact : int := 5
colorback (black)
cls
var x : int := maxx div 2
var y : int := maxy div 2
loop
    drawdot (x, y, Rand.Int (32, 64))
    x += xfact
    y += yfact
    if x > maxx or x < 0 then
        xfact := -xfact
        x += xfact
    end if
    if y > maxy or y < 0 then
        yfact := -yfact
        y += yfact
    end if
    delay (10)
end loop


hope this helps explain it.

-Z

--edit--
has just achieved rank 'hacker'
^_^ when can i learn?
-Z
Token




PostPosted: Thu Feb 17, 2005 7:48 pm   Post subject: (No subject)

This is what i did, its a little less confusing for beginners

what it does is it makes the letter move right, once it gets all the way to the right it exits the loop and enters another loop that makes it move left again, once it gets all the way back to the left it exits that loop and then goes back to the start and moves it to the right again and so on... FYI I changed the x and y variables to c and r because when you do the locate function, it goes by rows and columns not x and y... hope it helps



code:
var r, c, counter : int
r := 1
c := maxrow div 2


loop
loop%%makes it go to the right
    locate (c, r)
    put "e"
    delay (50)
    cls
    r := r + 1

    exit when r = 80%makes it stop moving right
end loop


loop%makes it move left
    locate (c, r)
    put "e"
    delay (50)
    cls
    r := r - 1

    exit when r = 0 %makes it stop moving left
end loop
end loop
TheZsterBunny




PostPosted: Fri Feb 18, 2005 7:34 am   Post subject: (No subject)

token, ours are a bit different

I use pixel location b/c character graphics belong to the 1980s.

yours doesn't bounce along a diagonal, and will crash after 1 repetition.

a good start, but why not use for loops instead of loops and use the counter as your location?

-Z
Token




PostPosted: Fri Feb 18, 2005 3:11 pm   Post subject: (No subject)

okay well first of all i used a letter because thats what his problem asked for, and seccond of all he didn't say it needed it to bounce diagonally, he just wanted it to bounce back and fourth... you were right about the crashing right away tho, it was because when it said exit when r = 0 it also exited the main loop... my bad so puttung r:= 1 just before ending the main loop ends that problem, i just find that if somone is having a problem to try and answer it as simple as possible and let them expand on it themselfs, although ur program was pretty sweet Wink well heres the updated code

code:

var r, c: int
r := 1
c := maxrow div 2


loop
loop%%makes it go to the right
    locate (c, r)
    put "e"
    delay (50)
    cls
    r := r + 1

    exit when r = 80%makes it stop moving right
end loop


loop%makes it move left
    locate (c, r)
    put "e"
    delay (50)
    cls
    r := r - 1

    exit when r = 0 %makes it stop moving left
end loop
r:= 1
end loop


Edit: this code was posted after Drakain Zeil's post, when i did it originally i copy and pasted the wrong version and i did it twice, this one works fine and does all of the things that he asked it to do.
Drakain Zeil




PostPosted: Fri Feb 18, 2005 3:56 pm   Post subject: Re: 1-D and 2-D bouncing letter

code:

loop
  locate (x,y)
  put "e"
  delay(50)
  cls
  y:= y+1
 
 if y = 80 then   
    y:= y-1
  end if

  if y= 0 then
    y:= y+1
  end if

end loop


Okay, let's see here.

Here's your porblem.

...
y:= y+1
if y = 80 then
y:= y-1
end if
...


The first is wrong, because after it begins to change direction, it begins to go right again, it stays still at 80.

Next, you need to change it to only increase in the direction if it has reached that side. Therefore, make a variable, lets call it "bob"

You want to add bob to "y"

And your if statements should look like this...

if y = 80 then
bob:= -1
end if

if y = 0 then
bob:= 1
end if

And now outside of the IFs, just do

y:=y+bob
baby_bubblz




PostPosted: Fri Feb 18, 2005 9:51 pm   Post subject: (No subject)

thanks for all your help!
but if i were to bounce a letter anywhere on the screen, how would i do that?

var x,y : int :=1
loop
locate (x,y)
put "e"
delay(50)
cls
y:= y+1 <- here the letter is moving downwards in a right direction as the number of rows and columns increase
x:= x+1

if x = 25 then
y:= y+1 <- here the letter is moving upwards to the right
x:=x-1
end if

if y= 80 then
y:= y-1 <- bouncing left to the top wall
x:= x-1
end if

if x= 0 then
y:= y-1 <- bouncing left to the left wall
x:= x+1
end if
end loop

but sometimes the letter reaches a different wall and the program stops. i'm not sure how to fix that.

and btw, i'm a girl... not a guy. lol
Flikerator




PostPosted: Fri Feb 18, 2005 10:08 pm   Post subject: (No subject)

code:

var c, r : int := 1
var rchange, cchange : int := 1
var word : string
var len : int
colourback (black)
cls
colour (brightgreen)
put "Enter a word/phrase"
get word : *
len := length (word)
loop
    locate (r, c)
    put word
    if r + rchange > 25 or r + rchange < 1 then
        rchange := -rchange
    end if
    if c + cchange + len > 79 or c + cchange < 1 then
        cchange := -cchange
    end if
    r := r + rchange
    c := c + cchange
    delay (40)
    View.Update
    cls
end loop


This does a word or a sentece. I only read the last post so... Im looking for things to do because I dont have acces to my game files. So thats why im helping out Very Happy

EDIT - I didn't add any rem statements, but if you need anything explained send me a pm =P I might not check back here is all.
Sponsor
Sponsor
Sponsor
sponsor
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  [ 8 Posts ]
Jump to:   


Style:  
Search: