Computer Science Canada Start Of Game - Help With Controls,Movement |
Author: | Rasta Fella [ 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.
|
Author: | cool dude [ Sat Jan 07, 2006 3:41 pm ] | ||
Post 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
continue the if statements for all the procedures. since u will have a procedure for each movement it will make your life much easier! ![]() |
Author: | RedRogueXIII [ Sun Jan 08, 2006 10:45 am ] |
Post 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 |
Author: | cool dude [ Sun Jan 08, 2006 10:48 am ] |
Post 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. |
Author: | RedRogueXIII [ Sun Jan 08, 2006 11:00 am ] |
Post 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. |
Author: | Cervantes [ Sun Jan 08, 2006 11:02 am ] | ||
Post 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
|
Author: | cool dude [ Sun Jan 08, 2006 11:16 am ] |
Post 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! |
Author: | Rasta Fella [ Sun Jan 08, 2006 12:36 pm ] |
Post 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. |
Author: | chrispminis [ Sun Jan 08, 2006 10:49 pm ] | ||
Post 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
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. |
Author: | chrispminis [ Sun Jan 08, 2006 10:51 pm ] |
Post 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? |
Author: | Cervantes [ Sun Jan 08, 2006 11:33 pm ] | ||
Post subject: | |||
chrispminis wrote:
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. |
Author: | chrispminis [ Sun Jan 08, 2006 11:47 pm ] |
Post subject: | |
Oh, Ok, that explains everything. But moderators can edit it? If so, wanna add that little end code tab for me? ![]() |
Author: | Rasta Fella [ Mon Jan 09, 2006 4:36 pm ] | ||
Post 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?
|
Author: | chrispminis [ Mon Jan 09, 2006 4:47 pm ] |
Post 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. |
Author: | RedRogueXIII [ Tue Jan 10, 2006 8:49 pm ] | ||
Post 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:
Which will keep on going on from the last direction. |
Author: | Rasta Fella [ Wed Jan 11, 2006 6:08 pm ] |
Post subject: | |
RedRogueXIII I understand what you mean but does this mean I can add your previous code to the one I currently have? And the variable direction..what do I declare it as?? integer?? Thanks for your help so far[/code] |
Author: | RedRogueXIII [ Wed Jan 11, 2006 7:49 pm ] | ||
Post subject: | |||
my mistake i quoted the wrong code the part lemme just make it up
that part can be added on anywhere in the main loop as long as you check for the moving flag, it will move the character as long as the game is in that loop. |
Author: | Rasta Fella [ Tue Jan 17, 2006 4:47 pm ] | ||
Post subject: | |||
I didn't want to bring an old topic back up but I also didn't want to create a brand new help topic regarding the same thing. The "help question" I posted before stated I needed help with a movement issue. Well, i was troubleshooting and figured it out but now i have a problem exiting the loop. All the variables are stated in my real document but the problem is I do not know how to exit the loop where y+1 is. I want to make it so if any other direction is pressed then exit loop.The 'error' I have written in the code below in programmers comments. So I am thinking something along the lines of this...
If its hard to understand what I mean post and I will explain more thoroughly(spelling?) Although i do appreciate other alternatives to moving the dot like getch etc. I want to keep it this way because I understand it but if its not possible then I will change it. |
Author: | Clayton [ Tue Jan 17, 2006 11:49 pm ] | ||||||
Post subject: | |||||||
Rasta Fella wrote: I didn't want to bring an old topic back up but I also didn't want to create a brand new help topic regarding the same thing. The "help question" I posted before stated I needed help with a movement issue. Well, i was troubleshooting and figured it out but now i have a problem exiting the loop. All the variables are stated in my real document but the problem is I do not know how to exit the loop where y+1 is.
I want to make it so if any other direction is pressed then exit loop.The 'error' I have written in the code below in programmers comments. So I am thinking something along the lines of this...
If its hard to understand what I mean post and I will explain more thoroughly(spelling?) Although i do appreciate other alternatives to moving the dot like getch etc. I want to keep it this way because I understand it but if its not possible then I will change it. okay when you are trying to get out of a loop with many possible exit conditions ie
u have to restate what you are comparing to so that you can exit, you cant just go
this wont work because for the 2 and 3 there is nothing to compare it to, try that and see if that is your problem |
Author: | Rasta Fella [ Wed Jan 18, 2006 6:58 pm ] |
Post subject: | |
Thanks SuperFreak82 I got It too work. I appreciate your help. |
Author: | Rasta Fella [ Wed Jan 18, 2006 7:08 pm ] | ||
Post subject: | |||
Ok, I found out how to move, now when you press the arrow keys I want it to constantly check the x,y. So know when I press right arrow key it will remember THAT x,y cordinate, while the dot is STILL moving to the right and goes through objects because it only remember the LAST x,y cordinate. Now the problem is how can I fix it so the x,y values keep checking even while the dot is moving toward the right. Part of the code is below, I need to find a way how to constantly check the x,y value even though you are not pressing a key. Code Below:
Any help is appreciated considering the due date for this project is coming closer. |
Author: | Rasta Fella [ Thu Jan 19, 2006 4:32 pm ] |
Post subject: | |
Is it possible to constantly check the x,y value even though you are not pressing a button and the object is moving? I need it for the code above. If so how? Explanation with some source would be helpful. I need this today or tommorow. I will award bits for my procrastination (last minute question). |
Author: | Drakain Zeil [ Thu Jan 19, 2006 5:57 pm ] |
Post subject: | |
Store an ascending numerated list in an array, and a second variable (since, creating a true/false for every value on the list isn't worth it) which stores the number of movements; length of snake. From there, you can simply tell the entire list to jump back one, and add on a new guy to the list when you hit the apple. The list would store the x/y position, as a list of 1 to length. |
Author: | Rasta Fella [ Thu Jan 19, 2006 6:30 pm ] |
Post subject: | |
Thanks, for explaining(+Bits) but the thing is I don't fully understand. Can you post a small source for me. If you can't thats fine but thanks for helping. |
Author: | Drakain Zeil [ Thu Jan 19, 2006 7:49 pm ] |
Post subject: | |
Okay, so do something like... type snakelist record: length:int %Length of snake x:array 1..999 of int %x position for this segment y:array 1..999 of int %y position for this segment end record Then for your snake, you assign each value in the array a number, this represents which part of the snake it is. So, you can draw the snake by going for i:1..snake.length Draw.Dot (snake.x,snake.y,blue) %(or watever your draw code is) end for When the snake moves, give each next section on the snake the previous section's value, so part 1 gains a new value, part 2 get's part 1's, part 3 get's what part 2 had and so on till we reach snake.length, where we just drop off the old value. (you might get problems here, watch out the order you're doing these things in! If you assign everything the same value then... yeah) Also, for gaining new segments, we simply give it the first position, add one to the length, and move all of the exisiting segments back. Beyond this, I don't think I can get into much more detail, I'm just not that good at answering specific questions, but sometimes I have my moments, so who knows. |
Author: | Drakain Zeil [ Thu Jan 19, 2006 8:23 pm ] | ||
Post subject: | |||
Okay, I have a decent example of what I'm talking about after you requested it via PM, I'm sure there's likely to be an error or two, but it works.
|
Author: | Rasta Fella [ Thu Jan 19, 2006 8:33 pm ] |
Post subject: | |
Thanks, Drakain Zeil...but doesn't seem to work I'm sure it is correct but I'm having a problem with it. Was wondering any other simpler way to accomplish this. Oh well... Thanks Drakain Zeil (+ Bits) For typing that code out. Hopefully it will help other users. |
Author: | Drakain Zeil [ Thu Jan 19, 2006 10:11 pm ] |
Post subject: | |
What problems are you having? |
Author: | Rasta Fella [ Thu Jan 19, 2006 10:13 pm ] |
Post subject: | |
I don't want to use arrays... I need a easier way that just keeps checking the x,y cordinates. |
Author: | Drakain Zeil [ Thu Jan 19, 2006 10:28 pm ] |
Post subject: | |
Well, the only quick thing I can think of is to fork a process, which I don't suggest you do. Anyway, sorry I can't help anymore, I have to goto bed. Good luck with that program. |
Author: | RedRogueXIII [ Mon Jan 23, 2006 8:51 pm ] | ||
Post subject: | |||
Well i can say this, your posts made me want to do snake, so here an example of what i did,
basically you need an x,y, and direction variable which can all be integers. then a flexible array or just a huge array thats predetermined to store the body x and y. Also the food x and y. So using a main loop your code should be like this. loop keyboardInput % not actually moves but changes the direction variable move%takes the direction and then moves 1 to that direction if snake x, y = food x,y then + 1 to the flexible snake x and y array. for decreasing loop *for decreasing i:upper(snakex)..1 snakex(i):=snakex(i-1) %Y-Coordinates can be in the same for loop end for* then check to see if the snake hit a wall or if it hit itself *for i:1..upper(snakex) if x(the front dot) = snakex(i) (one of the snake body) end for end loop Remeber your not trying to make the whole game execute in only one run of the loop but to make it repeat small continous actions over and over. learn from my snake example, - [you cant die because i was to lazy to keep on restarting to playtest. ![]() |