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

Username:   Password: 
 RegisterRegister   
 Shifting array spectrum
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
ExquisitePotato




PostPosted: Thu Jun 14, 2012 5:46 pm   Post subject: Shifting array spectrum

What is it you are trying to achieve?
I'm trying to get the past value of a variable without re-assigning the variable to another one, so for example, like a snake game, where the last circle in the array get's deleted, but it's replaced with a new one. Essentially, I'm trying to make a line of circles follow the mouse. How can I do this? Please help, thanks Smile


What is the problem you are having?
I don't know how to access the past value of the variable efficiently.


Describe what you have tried to solve this problem
I found a small loophole, but it only works in one direction, where I just place a put statement so that the circles created get shifted over... how do i avoid this, and make it follow the mouse?


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)

Turing:


var x, y, btn, posx, posy, fillposx, fillposy, nx, ny, fillposorx, fillposory, dra, lol : int := 0
var esc : array char of boolean
setscreen ("graphics:max;max,offscreenonly")
x := 100
fillposx := 300
fillposorx := fillposx
fillposy := 600
fillposory := fillposy
y := 300
const circlesize := 5
const speed := 20
loop
    mousewhere (x, y, btn)
    randint (posx, -14, 14)
    randint (posy, -14, 14)
    y := y + circlesize

    nx := x
    ny := y
    drawfilloval (nx + posx, ny + posy, circlesize, circlesize, brightred)

    View.Update

    put " "

    Input.KeyDown (esc)
    if esc ('a') then
        cls
    elsif esc ('s') then
        exit
    end if
    exit when btn = 1

end loop



Please specify what version of Turing you are using
4.1



dad.t
 Description:
The first part of the code is where I need the circles to follow the mouse.

Download
 Filename:  dad.t
 Filesize:  1.81 KB
 Downloaded:  75 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
copthesaint




PostPosted: Sun Jun 17, 2012 9:38 am   Post subject: Re: Shifting array spectrum

Normally you wont get this much help, but I think you'll still have alot to do if you try implement this into your own program idea, you will definatly understand flexible arrays by the end if you do it correctly.

Turing:
var chars : flexible array 0 .. -1 of char % array of characters
var charsBound : int := -1 %array upper bound
var counter : int := 0 %cycle counter
var timer : int := Time.ElapsedCPU %timer
var delayConst : int := 50 %cycle dealy

procedure New_Char (amount : int) %increases the flexible arrays bound size to amount
    if amount > 0 then % if any values are created
        var temp : int := charsBound + 1 %the first increase to get the first value after the last new value
        charsBound := charsBound + amount %The new bound size
        if charsBound > upper (chars) then % if bound size is > then the upper of chars bound
            new chars, charsBound % new characters
        end if
        for i : temp .. charsBound % for all the new characters
            chars (i) := '0' % default character value
        end for
    end if
end New_Char

function To_String (c : array 0 .. * of char, up : int) : string %adds all the values to a string to be easily put
    var resultValue : string := "" % resulting string value
    for i : 0 .. up % for all values in the array that are used
        resultValue := resultValue + c (i) % adds to resulting value
    end for
    result resultValue % returns value
end To_String

New_Char (16) % adds 16 characters

View.Set ("graphics;offscreenonly,nobuttonbar,title:Cycle.test")
loop
    if Time.ElapsedCPU - delayConst > timer then  % if time is greater then  last timer plus delay amount in ms
        chars (counter) := '0' % last character is equal to '0'
        counter := counter + 1  %counter is increased
        if counter > charsBound then % if counter is greater then greatest value
            counter := 0 % restarts cycle
            New_Char (1) % new character
        end if
        chars (counter) := '-'  % current value to shift
        timer := Time.ElapsedCPU % resets timer
    end if
    put To_String (chars, charsBound)
    put "Time to complete one cycle: ", ((charsBound * delayConst) / 1000), "s" % displays cycle info
    View.Update
    cls
end loop
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  [ 2 Posts ]
Jump to:   


Style:  
Search: