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

Username:   Password: 
 RegisterRegister   
 Start Of Game - Help With Controls,Movement
Index -> Programming, Turing -> Turing Help
Goto page 1, 2, 3  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Rasta Fella




PostPosted: Sat Jan 07, 2006 3:05 pm   Post subject: Start Of Game - Help With Controls,Movement

Ok, I am taking Computer Science at school and it is my first year taking it. I am in grade 10 learning Turing. At school they teach us the basics loops, for, end, exit when, etc. Now summatives are approaching or for some have started. My teacher wants each student to create a game using 5 inputs; up, down, right, centre and button. I decieded to do a game in which you are a small dot (filled circle) and you must reach a certain goal (coloured block). But there are obstacles you must face to get through to reach the "goal". I started and have a good startup page with GUI.

Here is the problem:

I want to make the dot as like those "snake games". The dot starts going in a "up" direction until another direction is pressed. When you push right it turn right and goes at a steady pace until another direction is put in. The problem with my code right now is that you have to hold the up button for it to continuously go up. Now any help is "good help" but remember, I just started turing so not to complex. And if you do help and it is complex just briefly explain so I can understand it.

Here is the part of a code which I started.

code:
var x,y: int
x:=20
y:=20
var chars: array char of boolean

%Draw Backgorund Colour
    drawfillbox(0,0,560,350,black)

        loop
       
            Input.KeyDown (chars)
            if chars (KEY_UP_ARROW) then
            if y <= 348 then
                y:=y+1
                end if
            end if
            if chars (KEY_RIGHT_ARROW) then
            if x <=559 then
                x:=x+1
                end if
            end if
            if chars (KEY_LEFT_ARROW) then
            if x >= 1 then
                x:=x-1
                end if
            end if
            if chars (KEY_DOWN_ARROW) then
            if y >= 1 then
                y:=y-1
                end if
            end if
           
            drawfilloval(x, y, 1, 1,white)
            delay(10)
            drawfilloval(x, y, 1, 1,black)
           
        end loop
Sponsor
Sponsor
Sponsor
sponsor
cool dude




PostPosted: Sat Jan 07, 2006 3:41 pm   Post subject: (No subject)

just make a for loop where u are drawing your snake and in the coordinates stick the variable u used for the for loop!

i actually made snake it turned out really good but i had a lot of problems so beware. i strongly suggest to make 4 procedure called move_up, move_down, move_left, and move_right. then have a procedure move snake. then in the procedure move snake have something like this

code:

if key = KEY_UP_ARROW and move not= "DOWN" then
        snake_up    %calls procedure
elsif key = KEY_UP_ARROW and move = "DOWN" then
        snake_down    %calls procedure


continue the if statements for all the procedures. since u will have a procedure for each movement it will make your life much easier! Smile
RedRogueXIII




PostPosted: Sun Jan 08, 2006 10:45 am   Post subject: (No subject)

boolean flags anyone?
4 boolean switchs for 4 directions
when a key is pressed it turns all of the other ones off and turns the flag corresponding to the direction on.
then an if statement inside the main loop that checks to see which flag is on and then moves accordingly. ( Then this could also probably be accomplished by a variable that stores the last arrow key pressed....)
eg
if goUpFlag = true then
y += 1
end if
cool dude




PostPosted: Sun Jan 08, 2006 10:48 am   Post subject: (No subject)

The reason i didn't suggest boolean flags is that when it comes around to programming the whole game it will be much easier to have a procedure for each movement so u can just call it. this will save him a lot of code and headachs.
RedRogueXIII




PostPosted: Sun Jan 08, 2006 11:00 am   Post subject: (No subject)

well you can do it like that but when using the flags the procedures become unneccesary, its also pretty simple, just call it after a key is pressed and an moving condition is fufilled. if moving = false and keydown arrow then moving = true.
if moving = true then
move
end if


Also i know it is easier to understand like that but how would you keep the guy moving if you attached the movement to the buttons, because anytime you let go the snake stops moving then.
Cervantes




PostPosted: Sun Jan 08, 2006 11:02 am   Post subject: (No subject)

RedRogueXIII wrote:
boolean flags anyone?
4 boolean switchs for 4 directions
when a key is pressed it turns all of the other ones off and turns the flag corresponding to the direction on.
then an if statement inside the main loop that checks to see which flag is on and then moves accordingly. ( Then this could also probably be accomplished by a variable that stores the last arrow key pressed....)
eg
if goUpFlag = true then
y += 1
end if


Why would you create four variables when you could do it with one? Sure, applying the movement to the object with only one variable might be a little tougher, but it will save you a lot of lines, and a lot of
code:

up := true
down := false
right := false
left := false
cool dude




PostPosted: Sun Jan 08, 2006 11:16 am   Post subject: (No subject)

RedRogueXIII wrote:


Also i know it is easier to understand like that but how would you keep the guy moving if you attached the movement to the buttons, because anytime you let go the snake stops moving then.


actually if u use a for loop and make the snake move in there it will be moving at all times. and thats wat u want! so no matter wat the user will press the snake will still move. now wat cervenas says with the one variable obviously if u want to have less lines of code go for it but if your new to turing i suggest making it as easy as u can with having a bit more variables than one!
Rasta Fella




PostPosted: Sun Jan 08, 2006 12:36 pm   Post subject: (No subject)

Thanks 'cool dude' for the reply but I don't understand. Let me reconfirm, you want me to create a procedure called "move snake" in that procedure put your code (you only made one for move up, so I will make the rest). And thats all there is to it? And I am not that fond of the procedure command...it's almost like a stating the variable thing right? It's confusing for me so if i stated anthing wrong please correct me. Thanks for everyone who replied. I am testing your code right now.
Sponsor
Sponsor
Sponsor
sponsor
chrispminis




PostPosted: Sun Jan 08, 2006 10:49 pm   Post subject: (No subject)

Personally, I prefer Red Rogue XIII's and Cervantes way, mostly Cervantes. Why would you use booleans? Could you not use just one variable say called direction where direction := 1 is up etc. So just set some if's that decide which keys give direction which values, preferably the right ones but there are mirror games and such. So instead of

code:

if goUpFlag = true then
y += 1
end if


You could have :
[code]
if direction = 1 then %1 meaning up, perhaps 2 for right, 3 for left, etc.

So for you Rasta Fella
y += 1
end if

I think that all works, and i tihnk its what Cervantes meant. Instead of:
[code]
Input.KeyDown (chars)
if chars (KEY_UP_ARROW) then
if y <= 348 then
y:=y+1
end if
end if
if chars (KEY_RIGHT_ARROW) then
if x <=559 then
x:=x+1
end if
end if
if chars (KEY_LEFT_ARROW) then
if x >= 1 then
x:=x-1
end if
end if
if chars (KEY_DOWN_ARROW) then
if y >= 1 then
y:=y-1
end if
end if
[/code]

You could do this:
[code]
Input.KeyDown (chars)
if chars (KEY_UP_ARROW) then
direction := 1
end if
if chars (KEY_RIGHT_ARROW) then
direction := 2
end if
if chars (KEY_LEFT_ARROW) then
direction := 3
end if
if chars (KEY_DOWN_ARROW) then
direction := 4
end if
[/code]

And add this as well into the loop:

[code]
if direction := 1 then
y += 1 %Or wutever, depending on speed, applies to all that follow
elsif direction := 2 then
x += 1
elsif direction := 3 then
x -= 1
elsif direction := 4 then
y-= 1
end if
[/code]

This could be put into a procedure say called move or something if you want. I hope you understand now, plus i've basically given you the source, although you'll have to improve it. Don't forget boundaries. We don't want him to run off the screen.
chrispminis




PostPosted: Sun Jan 08, 2006 10:51 pm   Post subject: (No subject)

Whoa did I do do something wrong or are code tags not working? And why does the edit button disappear sometimes? Is it not in the mood for working?
Cervantes




PostPosted: Sun Jan 08, 2006 11:33 pm   Post subject: (No subject)

chrispminis wrote:

code:

if direction := 1 then
y += 1 %Or wutever, depending on speed, applies to all that follow
elsif direction := 2 then
x += 1
elsif direction := 3 then
x -= 1
elsif direction := 4 then
y-= 1
end if


Just to throw this out there: Instead of making that big if statement, you can store the angle of the snake's movement, then use some trig and rounding to generalize his movement. You have to be sure to incriment the angle by +/- 90 (or pi / 4).

chrispminis: I think you missed a "[/code]" at the end of your if direction = 1 then %1 meaning up, perhaps 2 for right, 3 for left, etc., which threw everything off. As for the edit button, it's disabled in Turing Help, since users were posting their question then removing them after it was answered.
chrispminis




PostPosted: Sun Jan 08, 2006 11:47 pm   Post subject: (No subject)

Oh, Ok, that explains everything. But moderators can edit it? If so, wanna add that little end code tab for me? Smile Also it seems in my code there were a few typos. Correct me if im wrong it shouldn't be if direction := 1 but if direction = 1. Also thats not such a large if statement IMO, i've seen and used MUCH larger, but then again im probably not nearly as advanced as you are in Turing, it seems that your trig function would take longer than an if statement would it not? Since he is using a grid, and not like movement where he could move in angles other than right angles or multiples of 90 degrees.
Rasta Fella




PostPosted: Mon Jan 09, 2006 4:36 pm   Post subject: (No subject)

Thanks chrispminis and crevantes for your reply but when i do the code it still doesnt work. You still have to hold the up arrow key to make it move.

but I was wondering if a code something like this will work...
This code does not work...but is it possible to do something along the lines of this like loop the input until another direction is pressed?
code:


        loop
       
            Input.KeyDown (chars)
            if chars (KEY_UP_ARROW) then
            if y <= 348 then

                loop
               
                y:=y+1
                exit when %like exit when a different direction is pressed
               
                loop
           
            drawfilloval(x, y, 1, 1,white)
            delay(10)
            drawfilloval(x, y, 1, 1,black)
 
        end loop
chrispminis




PostPosted: Mon Jan 09, 2006 4:47 pm   Post subject: (No subject)

"y += 1" = "y:= y +1"

So your basically talking about my code but adding in the exit. I meant for you to loop it anyways, I didn't know it wasn't clear. In order for you to continue moving you must loop it.
RedRogueXIII




PostPosted: Tue Jan 10, 2006 8:49 pm   Post subject: (No subject)

To keep moving all you have to do is keep calling in the part that moves over. Like i said in my 2nd post just make moveing flag that checks if the game (or a direction has been started) Once that codition has been met then you just keep calling in this part of code
Quote:
You could do this:
code:

Input.KeyDown (chars)
if chars (KEY_UP_ARROW) then
direction := 1
end if
if chars (KEY_RIGHT_ARROW) then
direction := 2
end if
if chars (KEY_LEFT_ARROW) then
direction := 3
end if
if chars (KEY_DOWN_ARROW) then
direction := 4
end if



Which will keep on going on from the last direction.
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 3  [ 31 Posts ]
Goto page 1, 2, 3  Next
Jump to:   


Style:  
Search: