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

Username:   Password: 
 RegisterRegister   
 Changing object's movement speed
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Aange10




PostPosted: Sun Sep 04, 2011 12:52 pm   Post subject: Changing object's movement speed

What is it you are trying to achieve?
I'm trying to set a speed for my oval. (I'm making frogger!Very Happy) And I'm wondering if there is some way to use a decimal in the equation... I've been reading up and I can't seem to find out how to use a decimal for the speed at which my object will travel. Right now i'm just increasing the delay after the key is pressed [and it makes the game lag]. But is there a way to make my oval travel less than 1 pixel (It's quite fast!) per key press?

What is the problem you are having?
I can't put a real number in the integer syntax (?)


Describe what you have tried to solve this problem
I've gone around and tried to read about movement, and I've tried altering my coding.


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

Turing:


%Frogger!
View.Set("Offscreenonly, graphics:maxx;maxxy, position:center;center, title: Frogger")
var frog1_x, frog1_y, frog1_xr, frog1_yr : int %My frog's cordinates
var chars : array char of boolean % Has to be here...
var level : int % Defines what level we are on
var continue : string  %Allows the user to continue
var mainmenu : boolean % Variable to help me tell wether or not to bring up a mian menu
var mainmenu_continue : string % Varriable to toggle main menu
var fontTNR : int % My font Times New Roman
frog1_x := 25  %Frog X
frog1_y := 25  % Frog Y
frog1_xr := 20 % Frog X radius
frog1_yr := 10 % Frog y radius
level := 1  % level
mainmenu := true
continue := "a"  %Var to continue or not
loop % Main Loop
    loop % Main menu bug proof loop
        if mainmenu = true then % Toggles main menu
            fontTNR := Font.New ("TimeNewRoman:18")
            Font.Draw ("Welcome to Frogger!", maxx div 2, maxy div 2 + 35, fontTNR, green)
            fontTNR := Font.New ("TimeNewRoman:12")
            delay (1000)
            Font.Draw ("To continue, type 'r' !", maxx div 2 + 15, maxy div 2, fontTNR, red)
            locatexy (maxx div 2 + 20, maxy div 2 - 15)
            get mainmenu_continue  %Asks if they'd like to continue (types r)
        else %If main menu is false it exits the mainmenu loop
        exit
        end if
        if mainmenu_continue = "r" then
            mainmenu := false  %If you typed 'r' it changes mainmenu to false
            cls
            exit %exits the loop
        else %If you typed anything else...
            cls
            put "That is an invalid answer!"
            delay (2000)
            cls
        end if  %Repeats loop after telling you that that is an invalid answer.
    end loop
    Input.KeyDown (chars)
        if chars ('d') then  %Frog moving to the right
            frog1_x += + 1
            delay(2)
        end if
        if chars (KEY_RIGHT_ARROW) then %^
            frog1_x += + 1
            delay (2)
        end if
        if chars (KEY_LEFT_ARROW) then % Frog moving to the left
            frog1_x += - 1
            delay (2)
        end if
        if chars ('a') then % ^
            frog1_x += -1
            delay (2)
        end if
        if chars (KEY_ENTER) then % Resets the frogs position to the starting  position
            frog1_x := 25
            frog1_y := 25
        end if
        if chars (KEY_UP_ARROW) then % Moves the frog up
            frog1_y += +1
            delay (2)
        end if
        if chars ('w') then % ^
            frog1_y += +1
            delay (2)
        end if
        if chars ('s') then % Moves the frog down
            frog1_y += -1
            delay (2)
        end if
        if chars (KEY_DOWN_ARROW) then % ^
            frog1_y += -1
            delay (2)
        end if
    loop
        if frog1_y >= 390 then  %If you get to the top it tells you that you win
        cls
            put "You win! Type 'c' to conintue!"
            get continue
    View.Update
        else
        exit
        end if
        if continue = "c" then
            cls
            level += + 1 %if you won and typed c it changes your level
            exit
        else   
        end if
    end loop
    if frog1_x <= 30 then % Left border
        frog1_x += 1
    end if
    if frog1_y <= 25 then % bottom border
        frog1_y += 1
    end if
    if frog1_x >= 620 then % right border
        frog1_x += -1
    end if 
    cls
    put "Level:", level %Current level
    colorback(0) put "Frog1_x:", frog1_x, " Frog1_y:", frog1_y %Frog cordinates (Helps to see it while programming)
    drawfilloval (frog1_x, frog1_y, frog1_xr, frog1_yr, green) %Frog
    View.Update
end loop % end of main loop



Please specify what version of Turing you are using
4.1

------ALSO-------
As I develop the game I know i'll have a few more questions that either A: Weren't on the tutorials or B: I couldn't find them there..
So how would I:
1) Make the computer generate the cars (Rectangles!)?
2) Make the cars disappear once they cross to border? (Necessary?)
3) Make the generated rectangles colide with my frog. (I think this would have to do with an array generating the x1,y1,x2,y2 for the rectangles, and then using the Rectangle on Rectangle collision code except instead of the cars having varriables, i use the generated arrays.. Or something along those lines... Very Happy)
4) [Optional] Drawling a road/ adding collision sounds?
--------------------
Thanks so much for you guys' help! In three days I've almost programmed my two favorite games!!
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Sun Sep 04, 2011 1:09 pm   Post subject: RE:Changing object\'s movement speed

code:

var foo : real := 0.5
Draw.Dot (round (foo), round (foo), black)


This help?
Aange10




PostPosted: Sun Sep 04, 2011 3:43 pm   Post subject: RE:Changing object\'s movement speed

The syntax for that is Draw.Dot (x,y,color) ... It would only be a pixel in radius... That's too small for my frogger D:
Insectoid




PostPosted: Sun Sep 04, 2011 3:55 pm   Post subject: RE:Changing object\'s movement speed

That's just an example. You need to adapt it to your own program. The round() part is what's important.
Aange10




PostPosted: Sun Sep 04, 2011 4:49 pm   Post subject: RE:Changing object\'s movement speed

Ahh, you sir, are awesome. Thankyou!

Incase your curious, or would like to criticze, here is the "Testing Code" I used to know that your suggestion is awesome.

Turing:

View.Set ("Offscreenonly")
var o_x, o_y : real := 25
var chars : array char of boolean
var directionx, directiony, speed : real
directionx := 1.2
directiony := 1
speed := +1.86543
loop
cls
Input.KeyDown (chars)
if chars ('d') then
    o_x := o_x + (directionx * speed)
end if
put "o_x:", o_x
drawfilloval (round (o_x), round (o_y), 10, 10, blue)
View.Update
delay (10)
end loop


See the decimals at the top left!! xD!!
Raknarg




PostPosted: Mon Sep 05, 2011 10:37 am   Post subject: RE:Changing object\'s movement speed

For your car generation and collision, you definitlely need to look into arrays first. Otherwise you're going to end up with way more code than you need.
Aange10




PostPosted: Mon Sep 05, 2011 11:04 am   Post subject: RE:Changing object\'s movement speed

I keep reading the tutorial over and over, but from what I understand an array is just a varriable of varriables...

code:

var my_array : array 1 .. 10 of string


Is all it is used for is having a bunch of varriables (my_array(1..10)) in one line?

^ I'm sure thats not, but I'm having trouble putting it into practice.
DemonWasp




PostPosted: Mon Sep 05, 2011 11:13 am   Post subject: RE:Changing object\'s movement speed

The more important change is that you can refer to those elements by their index, or number. That lets you use for loops with more than one variable:

Turing:

var my_array : array 1 .. 5 of string := init ( "Hello", "World", "This", "is", "array" )

for i : 1 .. 5 % could also be lower(my_array) .. upper(my_array) if your bounds are different
    put my_array(i)
end for


versus

Turing:

put "Hello"
put "World"
put "This"
put "is"
put "verbose"


That's with 5 elements, because I hate typing. Imagine the difference with 100 elements, or 1000 -- the first example doesn't get any longer, but the second sure does. Imagine if you didn't know the number of things beforehand -- the first example still works, but it's nearly impossible to write the second correctly.

In your case, you probably want an array of all the cars, or else an array of all the car X coordinates, all the car Y coordinates, etc. Then you can determine whether car-i and car-j collide by looking at car_x (i), car_x (j), car_y (i), car_y (j) (or whatever name you choose). You can write your collision code once, not for every pair of cars in the game.
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: