Computer Science Canada Hasch question, cheking to see if key pressed |
Author: | lordroba [ Fri Aug 12, 2005 9:26 am ] |
Post subject: | Hasch question, cheking to see if key pressed |
I'm making a racing game and I want the car speed up while the UP key is being pressed(i know how to do this), and I want the speed to slowly go down while no key is pressed. How do I check if no key is being pressed and put this into an 'IF' condition so that it lowers the value of the speed variable when this is true? |
Author: | MihaiG [ Fri Aug 12, 2005 10:59 am ] | ||
Post subject: | |||
heres a small example
|
Author: | lordroba [ Fri Aug 12, 2005 11:07 am ] |
Post subject: | |
thank you very much |
Author: | Cervantes [ Fri Aug 12, 2005 11:32 am ] | ||||
Post subject: | |||||
Use Input.KeyDown, not hasch and getch. It will allow you to get multiple keystrokes at once. You probably don't want to slow your car down when no key is pressed. Think about it: the user could press "h" and he would go at a constant speed and not use any fuel or anything. Instead, you want the car to slow down when the up arrow is not pressed.
car.deceleration should be slightly less than 1. 0.99 is a good value to test. Of course, you could use some physics to determine accelerations with regard to mass, applied force, and frictional forces.
I think this is all correct, except for the fact that the coefficient of friction changes depending upon your speed. This also does not take into account air resistance, which is greater than the friction between the tires and the road. |
Author: | lordroba [ Sat Aug 13, 2005 9:00 am ] |
Post subject: | |
Alright now i'm really pissed, I spent all day yesterday trying to figure this out but I'm having no success. ![]() can someone plz help me. I not only want the car to move a fair bit and eventually stop after you let go of the gas, but I also want it so that the user still has ability to steer the car thru the corner when its moving. Any help greatly appreciated! |
Author: | Cervantes [ Sat Aug 13, 2005 12:11 pm ] | ||||
Post subject: | |||||
There are two tutorials you should look at: First, check out zylum's spaceship movement tutorial. Second, check out Asian's rotation and movement tutorial. It explains how to store the rotated pictures in an array, so you don't have to Pic.Rotate each time through you're loop. That will speed your program up a lot. The reason it stops immediately after letting go of the up arrow is because you've got this part inside your if keys (KEY_UP_ARROW) statement:
You want that to happen all the time. He won't move if his speed is 0, remember. Here's the modified code.
I also rotated the picture by 90°, so you don't have to bother with the Pic.Rotate in the initialization stages. |
Author: | [Gandalf] [ Sat Aug 13, 2005 3:52 pm ] |
Post subject: | |
I was wondering how would you check if the user hit any key at all, not everything (including having no key pressed at all) except some certain keys. Can't think of anything right now ![]() |
Author: | Cervantes [ Sat Aug 13, 2005 4:17 pm ] |
Post subject: | |
Could you restate that? |
Author: | [Gandalf] [ Sat Aug 13, 2005 9:07 pm ] |
Post subject: | |
Yeah, I knew it was a bit unclear... When you have an Input.Keydown statment, like if keys ('a') then ... and then you add an else, that else will include when no keys are pressed at all. I only want to do something when an actual key is pressed (when a key is pressed that is not 'b' for example). Basically, I want to do an 'else' like statement but without a null key - I can't think of how to detect that. Probably a pretty simple solution, but I've been wondering for a while and decided to ask. |
Author: | Cervantes [ Sun Aug 14, 2005 7:11 am ] | ||
Post subject: | |||
[Gandalf] wrote: When you have an Input.Keydown statment, like if keys ('a') then ... and then you add an else, that else will include when no keys are pressed at all.
The else would be entered only if 'a' is not pressed. Other conditions, such as if 'b' is pressed, are irrelevent. |
Author: | [Gandalf] [ Sun Aug 14, 2005 5:56 pm ] |
Post subject: | |
Ok, thanks. I wasn't thinking about hasch/getch, trying to avoid it. Is there a way of doing it without them? Still, it's a pretty good use of hasch (as opposed to using it as a replacement of Input.Keydown). I know about the else statment, I was just trying to explain the problem that I can't detect if keys(nothingatall). |
Author: | Cervantes [ Sun Aug 14, 2005 7:04 pm ] | ||
Post subject: | |||
I agree: I try to avoid using hasch & getch with Input.KeyDown, because of problems with one catching keystrokes that I want the other to catch. Here's another way to do it. There's some need tricks in here, particularly the range of the for loop.
|
Author: | [Gandalf] [ Sun Aug 14, 2005 11:18 pm ] |
Post subject: | |
Nice. I'll try it out later on, thanks again ![]() Interesting way of doing it, I was wondering about different for loops, now I have something to think about. |