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

Username:   Password: 
 RegisterRegister   
 A very weird problem that delays movement of my red square!
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
DanceMacabre




PostPosted: Sat May 06, 2006 8:19 pm   Post subject: A very weird problem that delays movement of my red square!

ok! This is the weirdest thing thats ever happened and I'll try my best to explain it. I want this red square to move fluidly and not all screwed up like. as the square moves, it leave red behind it and thats ok, I want that to happen. but sometimes the red behind the square disappears and the squares movement lags like mad. Please look at the code to see if you can figure it out, cause I can't, let alone explain my problem properly. Thank you so much Sad
code:

var keys : array char of boolean
View.Set ("graphics,offscreenonly")
var x1, y1 : int := 225
const size : int := 3
y1 := maxy - size
cls
loop
drawback
delay(300)
    Input.KeyDown (keys)
    if keys (KEY_DOWN_ARROW) then
        loop
            y1 -= 1
            Draw.FillBox (x1, y1, x1 + size, y1 + size, brightred)
            delay(10)
            View.Update
            exit when hasch
        end loop
    end if
    if keys (KEY_RIGHT_ARROW) then
        loop
            x1 += 1
            Draw.FillBox (x1, y1, x1 + size, y1 + size, brightred)
            delay(10)
            View.Update
            exit when hasch
        end loop
    end if
    if keys (KEY_LEFT_ARROW) then     %%%
        loop
            x1 -= 1
            Draw.FillBox (x1, y1, x1 + size, y1 + size, brightred)
            delay(10)
            View.Update
            exit when hasch
        end loop
    end if
    if keys (KEY_UP_ARROW) then     %%%
        loop
            y1 += 1
            Draw.FillBox (x1, y1, x1 + size, y1 + size, brightred)
            delay(10)
            View.Update
            exit when hasch
        end loop
    end if
end loop

much appreciated
Sponsor
Sponsor
Sponsor
sponsor
Cervantes




PostPosted: Sat May 06, 2006 9:10 pm   Post subject: (No subject)

Loops nested within if statements within loops is a pretty hefty structure. What's more, it's entirely unecessary.

Instead, use one variable to store the current direction of the box.

code:
var x, y := 100
var keys : array char of boolean
var direction := 'u'
const speed := 1

loop
    Input.KeyDown (keys)
    if keys (KEY_DOWN_ARROW) then
        direction := 'd'
    elsif keys (KEY_UP_ARROW) then
        direction := 'u'
    elsif keys (KEY_LEFT_ARROW) then
        direction := 'l'
    elsif keys (KEY_RIGHT_ARROW) then
        direction := 'r'
    end if
    % I'd use a case construct here, but I don't think I remember the syntax quite right
    if direction = 'u' then
        y += speed
    elsif direction = 'd' then
        y -= speed
    elsif direction = 'l' then
        x -= speed
    elsif direction = 'r' then
        x += speed
    end if
    Draw.FillOval (x, y, 5, 5, black)
    delay (10)
end loop

You could even get rid of that second if structure if you used some trig:
code:
var x, y := 100
var keys : array char of boolean
var direction := 90
const speed := 1

loop
    Input.KeyDown (keys)
    if keys (KEY_DOWN_ARROW) then
        direction := 270
    elsif keys (KEY_UP_ARROW) then
        direction := 90
    elsif keys (KEY_LEFT_ARROW) then
        direction := 180
    elsif keys (KEY_RIGHT_ARROW) then
        direction := 0
    end if
    y += sind (direction) * speed
    x += cosd (direction) * speed
    Draw.FillOval (x, y, 5, 5, black)
    delay (10)
end loop
DanceMacabre




PostPosted: Sun May 07, 2006 9:22 pm   Post subject: (No subject)

Thanke me matey. Makes sense.
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  [ 3 Posts ]
Jump to:   


Style:  
Search: