Keyboard Input.

Slides:



Advertisements
Similar presentations
Games in Python – the easy way
Advertisements

Introduction to Macromedia Director 8.5 – Lingo
Mrs. Chapman. Tabs (Block Categories) Commands Available to use Script Area where you type your code Sprite Stage All sprites in this project.
Week 9 Writing Games with Pygame Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise noted,
2 What is pyGame? A set of Python modules to make it easier to write games. –home page: –documentation:
Pygame Dick Steflik.
Guide to Programming with Python
Unit 9 pyGame Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except where otherwise noted, this.
Week 10 Writing Games with Pygame, continued Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where.
Introduction to TouchDevelop
Meet Pygame Noah Kantrowitz June 8, The Basics Cross-platform Based on SDL (don’t quote me on that) Handle input (keyboard, mouse) and output (graphics,
KeyListener and Keyboard Events Another type of listener listens for keyboard entry – the KeyListener which generates KeyEvents –to implement KeyListener,
User Input and Collisions COSC 315 Fall 2014 Bridget M. Blodgett.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 7 The Game Loop and Animation Starting Out with Games & Graphics.
11 Adding Tomato Targets Session Session Overview  We now have a game which lets a player bounce a piece of cheese on a bread bat  Now we have.
Game Maker – Getting Started What is Game Maker?.
Creating a Simple Game in Scratch Barb Ericson Georgia Tech June 2008.
Video Games Writing Games with Pygame Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Events Programming with Alice and Java First Edition by John Lewis.
Today we are learning to: Understand how actions and events control our game. Completing the catch the clown game – making a room – adding music Gather.
Scratch for Interactivity Dr. Ben Schafer Department of Computer Science University of Northern Iowa.
PyGame - Unit 1 PyGame Unit – – Introduction to PyGame.
(More) Event handling. Input and Windowed-OS’s Windowed OS’s (Windows, OSX, GUI-linux’s) send messages (events) when the user does something “on” the.
Creating a Simple Game in Scratch Barb Ericson Georgia Tech May 2009.
11. Skier Let’s Learn Saengthong School, June – August 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
Game Maker Tutorials Introduction Clickball IntroductionClickball Where is it? Shooting Where is it?Shooting.
5. Animation Let’s Learn Saengthong School, June – August 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
Intro CS – Keyboard and mouse input Lesson Plan 7.
Building a rocks and spaceship game with Pygame Zero
Sprites (Images) and Sounds
Sound and more Animations
Create a Halloween Computer Game in Scratch
MOM! Phineas and Ferb are … Aims:
Pixels, Colors and Shapes
User-Written Functions
Scratch for Interactivity
Catapult 2016.
Animations.
Event Loops and GUI Intro2CS – weeks
PYGAME.
Let’s Learn 2. Installing Pygame
8. Installing Pygame
Starting Out with Alice: A Visual Introduction to Programming
Intro CS – Keyboard and mouse input
10. User Input.

Let's Race! Typing on the Home Row
Introduction to Events
Miscellaneous Topics #6: Polygons GUI Components and Event Handlers
Scratch for Interactivity
Programming – Touch Sensors
13. Sprites Let's Learn Python and Pygame
Lesson 17: Building an App: Canvas Painter
8. Starting Pygame Let's Learn Python and Pygame
10. User Input Let's Learn Python and Pygame
14. Pong.
16. Invaders.
Introduction to TouchDevelop
Arrays
Let’s Learn 7. Sprites Saengthong School, June – August 2016
14. Pong Let's Learn Python and Pygame
11. Animation.
ICT Gaming Lesson 3.
Creating a Simple Game in Scratch
CoE Software Lab II , Semester 2, Pong.
So you want to be a Game Designer
PACMAN OO Style.
CoE Software Lab II 1. Pygame Intro , Semester 2,
Chapter 7 The Game Loop and Animation
Presentation transcript:

Keyboard Input

KeyboardInput.py This program looks identical to the collision detection program earlier in this chapter. But in this program, the bouncer only moves around when we hold down keys on the keyboard.

Keyboard Input The "W" key moves the bouncer up. The "A" key moves the bouncer to the left The "D" key moves the bouncer to the right. The "S" key moves the bouncer down. The user can also use the keyboard's arrow keys. We can also click anywhere in the GUI window and create new food objects at the coordinates where we clicked. In addition, the ESC key will quit the program and the "X“ key will teleport the bouncer to a random place on the screen.

Same as Collision Detection

Same as Collision Detection

Setting up Window and data structures We are going to use 4 different boolean variables to keep track of which of the arrow keys are being held down. For example, the left arrow key will set the moveLeft variable to True. When the key is released, we will set the moveLeft variable back to False. The "W" key (moveUp variable), the "S" key (moveDown), and the "D" key (moveRight) are set in a similar way.

Same as Collision Detection Lines 34 to 43 are identical to code in the previous Pygame programs. These lines handle the start of the game loop and handling what to do when the user wants to quit the program.

Events We have seen the QUIT event being handled in our code. Below is a brief list of other events that could be returned by pygame.event.get()

Events

Events If the event type is KEYDOWN, then the event object will have a key attribute that will tell us which key was pressed down.

Events On line 46, we can compare this value to K_LEFT, which represents the left arrow key on the keyboard. We will do this for each of the arrow keys: K_LEFT, K_RIGHT, K_UP, K_DOWN. When one of these keys is pressed down, we will set the corresponding movement variable to True, and opposite to False.

Events You may notice that on line 46, event.key can either be equal to K_LEFT or ord('a'). The value in event.key is set to the integer ASCII value of the key that was pressed on the keyboard. (There is no ASCII value for the arrow keys, which is why we use the constant K_LEFT.) You can use the ord() function to get the ASCII value of any single character to compare it with event.key.

Handling the KEYUP Event When the user releases the key that they are holding down, a KEYUP event is generated. If the key that the user released was the Esc key, then we want to terminate the program. Remember, in Pygame you must call the pygame.quit() function before calling the sys.exit() function. We want to do this when the user releases the Esc key, not when they first Esc key down.

Handling the KEYUP Event Lines 62 to 69 will set a movement variable to False if that direction's key was let go.

Teleporting the Player If the user released one of the keys that moves the player, then we want to set the movement variable that corresponds with the key to False. This will tell the later parts of our program to no longer move the player's square on the screen. We will also add teleportation to our game. If the user presses the "X" key, then we will set the position of the user's square to a random place on the window. This will give the user the ability to teleport around the window by pushing the "X" key (though they can't control where they will teleport: it's completely random).

Handling the MOUSEBUTTONUP Event Mouse input is handled by events just like keyboard input is. The MOUSEBUTTONUP event occurs when the user clicks a mouse button somewhere in our window, and releases the mouse button. The pos attribute in the Event object is set to a tuple of two integers for the XY coordinates. On line 75, the X-coordinate is stored in event.pos[0] and the Ycoordinate is stored in event.pos[1]. We will create a new Rect object to represent a new food and place it where the MOUSEBUTTONUP event occurred. By adding a new Rect object to the foods list, a new food square will be displayed on the screen.

Moving the Bouncer Around the Screen We have set the movement variables (moveDown, moveUp, moveLeft, and moveRight) to True or False depending on what keys the user has pressed. Now we will actually move the player's square (which is represented by the pygame.Rect object stored in player) around by adjusting the XY coordinates of player. If moveDown is set to True (and the bottom of the player's square is not below the bottom edge of the window), then we move the player's square down by adding MOVESPEED to the player's current top attribute. We do the same thing for the other three directions as well.

The Colliderect() Method In our previous Collision Detection program, we had our own function to check if one rectangle had collided with another. That function was included in this so that we could understand how the code behind collision detection works. In this program, we can use the collision detection function that comes with Pygame. The colliderect() method for pygame.Rect objects is passed another pygame.Rect object as an argument and returns True if the 2 rectangles collide and False if they do not. This is the exact same behavior as the doRectsOverlap() function in our previous Collision Detection program.

The Rest of the Program The rest of the code is similar to the code in the Input program is similar to the Collision Detection program: Draw the food squares and the player squares to the windowSurface surface. Occasionally add a new food square at a random location to the foods list. Check if the player square has collided with any of the food squares. Call mainClock.tick(40) to make the program run at an appropriate speed.

What is Next? Sprites and Sound

KeyboardInput.py Lab Type in and test the animation. Experiment and make changes in the code so that you can understand what is going on and why. You will be working in groups to explain the code to the rest of the class.