10. User Input.

Slides:



Advertisements
Similar presentations
Games in Python – the easy way
Advertisements

 Variables  What are they?  Declaring and initializing variables  Common uses for variables  Variables you get “for free” in Processing ▪ Aka: Built-in.
Cosc 5/4730 Input Keyboard, touch, and Accelerometer.
1 Computer Graphics Chapter 2 Input Devices. RM[2]-2 Input Devices Logical Input Devices  Categorized based on functional characteristics.  Each device.
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,
Week 10 Writing Games with Pygame Special thanks to Scott Shawcroft, Ryan Tucker, and Paul Beck for their work on these slides. Except where otherwise.
2 What is pyGame? A set of Python modules to make it easier to write games. –home page: –documentation:
Pygame Dick Steflik.
Escape A sticky man adventure using TKINTER python module By: Channing Burgess.
Unit 9 pyGame Special thanks to Roy McElmurry, John Kurkowski, Scott Shawcroft, Ryan Tucker, Paul Beck for their work. Except where otherwise noted, this.
Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Technology Education Copyright © 2006 by The McGraw-Hill Companies,
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,
Continuous February 16, Test Review What expression represents the zip car eligibility rules of at least 18 years old and no incidents?
KeyListener and Keyboard Events Another type of listener listens for keyboard entry – the KeyListener which generates KeyEvents –to implement KeyListener,
Reference: The Game Loop Animation / Game loop 1. Update variables 2. [Get input from the user] (GameLoop only) 3. Draw (using variables)
Lesson Two: Everything You Need to Know
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.
Scratch for Interactivity Dr. Ben Schafer Department of Computer Science University of Northern Iowa.
PyGame - Unit 1 PyGame Unit – – Introduction to PyGame.
Implement User Input Windows Development Fundamentals LESSON 2.4A.
Graphics in Python On entry: Run Python 2.78 from N: drive/computing and ICT VLE: Computing home page - check your feedback Success criteria: ●Understands.
Intro to Pygame Lecture 05. What is Pygame? It is a set of Python modules designed for writing games. It makes writing games possible for beginners. import.
(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.
11. Skier Let’s Learn Saengthong School, June – August 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
3. Drawing Let’s Learn Saengthong School, June – August 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
9. Media (sound effects, music, video) Let’s Learn Saengthong School, June – August 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
5. Animation Let’s Learn Saengthong School, June – August 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
Section06: Sequences Chapter 4 and 5. General Description "Normal" variables x = 19 – The name "x" is associated with a single value Sequence variables:
13. Sprites. Outline 1.Game Things in Pygame (again) 2.The Pygame sprite Module 3.The Sprite Class 4.Groups of Sprites 5.Types of Collision Detection.
Building a rocks and spaceship game with Pygame Zero
Sprites (Images) and Sounds
Section06: Sequences Chapter 4 and 5.
Sound and more Animations
CS 134 Alternate Inputs.
MOM! Phineas and Ferb are … Aims:
Pixels, Colors and Shapes
15. Media (sound effects, music, video)
Scratch for Interactivity
Catapult 2016.
PYGAME.
Let’s Learn 2. Installing Pygame
8. Installing Pygame
Or just artificialworlds.net/blog
p5.js mouse, keyboard and if’s
Keyboard Input.
9. Drawing.
Game Loops + Part II Reference:
11. Animation Let's Learn Python and Pygame
Lesson Two: Everything You Need to Know

9. Drawing Let's Learn Python and Pygame
Let’s Learn 10. Invaders Saengthong School, June – August 2016
Scratch for Interactivity
Let’s make a shape…. Move!
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.
More programming with "Processing"
Let’s Learn 7. Sprites Saengthong School, June – August 2016
14. Pong Let's Learn Python and Pygame
11. Animation.
Directions are in slide notes. You can view them in two ways
Creating a Simple Game in Scratch
L L Line CSE 420 Computer Games Lecture #6 Game Foundations.
CoE Software Lab II , Semester 2, Pong.
CoE Software Lab II 1. Pygame Intro , Semester 2,
CoE Software Lab II , Semester 2, Sprites.
Presentation transcript:

10. User Input

Outline Types of User Input Keyboard Events Mouse Events Mouse Demo Gamepad (Joystick) Moving a Stick Man

1. Types of User Input Pygame can process events from: the keyboard http://www.pygame.org/docs/ref/key.html the mouse http://www.pygame.org/docs/ref/mouse.html gamepads (pygame calls them joysticks) http://www.pygame.org/docs/ref/joystick.html Pygame must initialize the gamepads first

Printing all Events (printEvents.py)

Code import pygame from pygame.locals import * pygame.init() screen = pygame.display.set_mode((400, 300)) screen.fill((255,255,255)) # white background pygame.display.set_caption("Print Events") clock = pygame.time.Clock() # initialize joysticks for i in range(pygame.joystick.get_count()): j = pygame.joystick.Joystick(i) j.init() running = True while running: clock.tick(30) for event in pygame.event.get(): if event.type == QUIT: running = False print(event) pygame.display.update() pygame.quit()

2. Keyboard Events The unicode value is a character (e.g. 'd'), while key is a Pygame key code (e.g. K_d). mod is a modifier key, which is 0 if no modifier key is being pressed modifiers include <ctrl>, <shift>, <alt>

Some Pygame Key Codes See http://www.pygame.org/ docs/ref/key.html for full details.

Example This sets running to False if the escape key is pressed down. while running: clock.tick(30) for event in pygame.event.get(): if event.type == QUIT: running = False if event.type == KEYDOWN: if event.key == K_ESCAPE: This sets running to False if the escape key is pressed down. could also look at event.mod and event.unicode

3. Mouse Events pos is the mouse's (x,y) position inside the Pygame window rel is a relative move (xChange, yChange), which will be negative if up or to the left buttons is a tuple of 3 button states button is the ID of the button that sent the event IDs start at 1; the middle button is ID 2, 4, and 5!

Using Mouse Functions The 3 most useful mouse functions are: pygame.mouse.get_pressed() returns the mouse buttons pressed as a tuple of three booleans, one for the left, middle, and right mouse buttons e.g. [ True, False, False ] pygame.mouse.get_pos() returns the mouse coordinates inside the window as a tuple of x and y values e.g. [ 240, 320 ] pygame.mouse.get_rel() returns the relative mouse movement as a tuple of x and y value e.g. [-10, 24]

Example running stops when the middle mouse button is pressed while running: clock.tick(30) for event in pygame.event.get(): if event.type == QUIT: running = False if event.type == MOUSEBUTTONDOWN and \ pygame.mouse.get_pressed()[1]: # middle button pos = pygame.mouse.get_pos() x, y = pos[0], pos[1] running stops when the middle mouse button is pressed

4. Mouse Demo (mouseDemo.py) Moves a black dot around the window Prints relative position, pressed/released info.

import pygame from pygame. locals import import pygame from pygame.locals import * BLACK = ( 0, 0, 0) WHITE = ( 255, 255, 255) pygame.init() screen = pygame.display.set_mode([340,240]) pygame.display.set_caption("Mouse Demo") clock = pygame.time.Clock() # Hide the mouse cursor pygame.mouse.set_visible(0) running = True while running: clock.tick(30) for event in pygame.event.get(): if event.type == QUIT: running = False : Code

if event. type == MOUSEBUTTONDOWN: print(' Pressed:', pygame. mouse if event.type == MOUSEBUTTONDOWN: print(' Pressed:', pygame.mouse.get_pressed()) elif event.type == MOUSEBUTTONUP: print(' Released:', if event.type == MOUSEMOTION: print('Move:', pygame.mouse.get_rel()) # redraw screen.fill(WHITE) # draw a circle around the mouse pointer pos = ( pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]) pygame.draw.circle(screen, BLACK, pos, 5, 0) pygame.display.update() pygame.quit()

5. Gamepad (Joystick) a typical gamepad has 4 kinds of input buttons D Pad (hat) left, right, up, down buttons axes (analog sticks) each stick has 2 axes + track balls

Joystick(s) Initialization Pygame will not receive joystick events unless the joysticks are initialized before the game loop: for i in range(pygame.joystick.get_count()): j = pygame.joystick.Joystick(i) # initialize all joysticks j.init() running = True while running: : # get events from any/all of the joysticks

Joystick Events joy is the ID numbers for the joystick that sent the event ID start at 0 axis, ball, hat, and button are ID numbers ID for each kind start at 0

Example running stops when the button 2 of the joystick is pressed while running: clock.tick(30) for event in pygame.event.get(): if event.type == QUIT: running = False if event.type == JOYBUTTONDOWN and \ event.button == 2: # usually the 'X' button running = False # as shown on slide 15 running stops when the button 2 of the joystick is pressed

Joystick Functions When using D-Pads (hats), trackballs, or analog sticks (axes), it is easier to use joystick functions. see http://www.pygame.org/docs/ref/joystick.html There are 2 types of "get" function: pygame.joystick.Joystick.get_numXXX() gets the number of XXX's pygame.joystick.Joystick.get_XXX(id) gets info on XXX which has id number

Examples // values are 1,0,-1 for i in range(joystick.get_numbuttons()): if joystick.get_button(i): print("pressed", i) else: print("not pressed", i) for i in range(joystick.get_numhats()): xAxis, yAxis = joystick.get_hat(i) // values are 1,0,-1

for i in range(joystick.get_numballs()): xRel, yRel = joystick.get_ball(i) // values are positive/negative integers for i in range(joystick.get_numaxes()): print( joystick.get_axis(i) ) // value is float between -1 and 1 The axis data is the hardest to use since one "stick" uses two axes: one for the x-axis, one for the y-axis one stick; two axes

joyDemo.py GUI 2 analog sticks ( 4 axes) D Pad (1 hat) right stick is being pushed left and up using 2 axes D Pad (1 hat) left (-1), right (1), up (1), down (-1) 12 buttons (green = pressed)

6. Moving a Stick Man move_keyboard.py, move_mouse.py, and move_joy.py all 'do' the same thing: they move a little "stick man" around the Pygame window but they use different input techniques: arrow keys in move_keyboard.py mouse movement in move_mouse.py the left stick in move_joy.py Keep redrawing the stick man inside the Pygame game loop at a new (x,y).

Stick Man Drawing

(x, y) 6.1 Drawing a Stick Man (x+5, y+17) All 3 programs contain the same draw_stick_figure() function: def draw_stick_figure(screen, x, y): # Head pygame.draw.ellipse(screen, BLACK, [1 + x, y, 10, 10], 0) # Legs pygame.draw.line(screen, BLACK, [5 + x, 17 + y], [10 + x, 27 + y], 2) pygame.draw.line(screen, BLACK, [5 + x, 17 + y], [x, 27 + y], 2) # Body pygame.draw.line(screen, RED, [5 + x, 17 + y], [5 + x, 7 + y], 2) # Arms pygame.draw.line(screen, RED, [5 + x, 7 + y], [9 + x, 17 + y], 2) pygame.draw.line(screen, RED, [5 + x, 7 + y], [1 + x, 17 + y], 2) (x+10, y+27)

6.2. move_mouse.py pygame.init() screen = pygame.display.set_mode([700, 500]) pygame.display.set_caption("Move Mouse") clock = pygame.time.Clock() # Hide the mouse cursor pygame.mouse.set_visible(0) running = True while running: clock.tick(30) for event in pygame.event.get(): if event.type == QUIT: running = False :

if event. type == MOUSEBUTTONDOWN and \ pygame. mouse if event.type == MOUSEBUTTONDOWN and \ pygame.mouse.get_pressed()[1]: # middle button pressed running = False pos = pygame.mouse.get_pos() # pos is [x, y] of mouse x, y = limitPos(screen, pos[0], pos[1]) # redraw screen.fill(WHITE) draw_stick_figure(screen, x, y) pygame.display.update() pygame.quit()

6.3. Disappearing Stick Man The (x,y) position of the stick man can be changed to anything, including values that are off the edges of the window! e.g. (-100, 20), (1000, 56), (14, -50), (100, 700)

Limiting the Stick man's Position I do not want the stick man to disappear off the sides of the window. There are 4 sides with x and y values: x-axis (0,0) (700,0) y-axis (0,500) (700,500)

Min and Max x's and y's Left side: minimum x == 0 Top side: minimum y == 0 Right side: maximum x = 700-1 but want all of stick man to be seen, so max x = 700 – 1 – 10 Bottom side: maximum y = 500-1 but want all of stick man to be seen, so max y = 500 – 1 – 27 right, bottom of window

This position limiting of (x, y) is implemented in all 3 programs by the same function, limitPos(): def limitPos(screen, x, y): width, height = screen.get_size() if (x < 0): x = 0 elif (x > width-1 - 10): # add in stick figure max width x = width-1 - 10 if (y < 0): y = 0 elif (y > height-1 - 27): #add in stick figure max height y = height-1 - 27 return (x, y)

Changes to main Inside the game loop, replace: by: x += xStep y += yStep by: x, y = limitPos(screen, x+xStep, y+yStep)

6.4. move_keyboard.py pygame.init() screen = pygame.display.set_mode([700, 500]) pygame.display.set_caption("Move Keyboard") clock = pygame.time.Clock() # move step in pixels done by a key xStep = 0; yStep = 0 # Current position of stick man x = 10; y = 10 running = True while running: clock.tick(30) for event in pygame.event.get(): if event.type == QUIT: running = False : More complicated code since need for x, y, xStep, and yStep

if event. type == KEYDOWN: if event if event.type == KEYDOWN: if event.key == K_ESCAPE: running = False # if an arrow key, adjust step elif event.key == K_LEFT: xStep =- 3 elif event.key == K_RIGHT: xStep = 3 elif event.key == K_UP: yStep =- 3 elif event.key == K_DOWN: yStep = 3 elif event.type == KEYUP: # if an arrow key, reset step to zero if event.key == K_LEFT: xStep = 0 yStep = 0 # Move according to the step values x += xStep y += yStep screen.fill(WHITE) draw_stick_figure(screen, x, y) pygame.display.update() pygame.quit()

6.5. move_joy.py pygame.init() screen = pygame.display.set_mode([700, 500]) pygame.display.set_caption("Move Joy") clock = pygame.time.Clock() # Current position of stick man x = 10; y = 10 joys = initJoys() joy = joys[0] running = True while running: clock.tick(30) for event in pygame.event.get(): if event.type == QUIT: running = False : Must initialize joysticks in Pygame and set stick man's start position.

Pressing any joystick button will stop the program running. if event.type == JOYBUTTONDOWN: running = False # move using left analog stick (uses axes 0 and 1) # multiply by 10 to increase step to -10 -- 10 range x += int(joy.get_axis(0) * 10) y += int(joy.get_axis(1) * 10) x, y = limitPos(screen, x, y) # redraw screen.fill(WHITE) draw_stick_figure(screen, x, y) pygame.display.update() pygame.quit() This approach "feels" the best. The stick man can move in more directions because the axes can range over more values.

Initializing the Joysticks def initJoys(): # create a list of joysticks joys = [] for i in range(pygame.joystick.get_count()): j = pygame.joystick.Joystick(i) j.init() joys.append(j) # report joystick charateristics print(i, "Joystick: \"", j.get_name(), "\"") print(" buttons :", j.get_numbuttons()) print(" balls : ", j.get_numballs()) print(" axes : ", j.get_numaxes()) print(" hats : ", j.get_numhats()) print("") if not joys: print("No joysticks found") pygame.quit() sys.exit() return joys