Clock ( ) while True : for event in pygame. The following program offfers this functionality:Ĭlock = pygame. It would be better if the movement was continuous as the key was held down. The program works otherwise quite well, but the key needs to be pressed again each time we want to move again. The x value, however, increases by 10 whenever the user presses the arrow key to the right, and decreases by 10 whenever the left arrow key is pressed. The y value does not change throughout the execution of the program. The variable y is set so that the sprite appears at the bottom of the window. In the code above we have the variables x and y which contain the coordinate location for the sprite. QUIT :ĭepending on how you use your arrow keys, running the program could look like this: get_height ( ) while True : for event in pygame. The program prints out which key was pressed. This program can process events where the user presses the arrow key either to the right or to the left on their keyboard. You can look for event descriptions in the pygame documentation, but it can sometimes be easier to print out events with the code above, and look for the event that occurs when something you want to react to happens. Each event has at least a type, but they may also offer some other identifying info, such as the location of the mouse cursor or the key that was pressed. The first few events concern mouse usage, ten there are some events from the keyboard, and finally the last event closes the program.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |