Keyboard Movement

Goals

  • Detect keyboard events

  • The bad way to do keyboard movement

  • The good way to do keyboard movement

  • Moving with inertia and friction

Starter code

Pygame Template

Video Tutorial

What you should do

  • See if you can make the ball move faster if the user is holding the B key.

    • In the event loop, under KEYDOWN, print the event to check what value the B key is.

    • Where the other key press code is, create an if-statement that checks if that key is pressed and modify either the speed or the acceleration.

Final code (basic movement)

# Basic Movement
import pygame
from pygame.locals import K_ESCAPE, KEYDOWN, QUIT

pygame.init()

WIDTH = 640
HEIGHT = 480
SIZE = (WIDTH, HEIGHT)

screen = pygame.display.set_mode(SIZE)
clock = pygame.time.Clock()

# ---------------------------
# Initialize global variables

circle_x = 200
circle_y = 200

# ---------------------------

running = True
while running:
    # EVENT HANDLING
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                running = False
        elif event.type == QUIT:
            running = False

    # GAME STATE UPDATES
    # All game math and comparisons happen here
    keys = pygame.key.get_pressed()
    if keys[119] == True:  # w
        circle_y -= 10

    if keys[97] == True:  # a
        circle_x -= 10

    if keys[115] == True:  # s
        circle_y += 10

    if keys[100] == True:  # d
        circle_x += 10


    # DRAWING
    screen.fill((255, 255, 255))  # always the first drawing command

    pygame.draw.circle(screen, (0, 0, 255), (circle_x, circle_y), 30)

    # Must be the last two lines
    # of the game loop
    pygame.display.flip()
    clock.tick(30)
    #---------------------------


pygame.quit()

Final code (acceleration and friction)

# Moving with acceleration and friction
import pygame
from pygame.locals import K_ESCAPE, KEYDOWN, QUIT

pygame.init()

WIDTH = 640
HEIGHT = 480
SIZE = (WIDTH, HEIGHT)

screen = pygame.display.set_mode(SIZE)
clock = pygame.time.Clock()

# ---------------------------
# Initialize global variables

FRICTION = 0.1

circle_x = 200
circle_y = 200
speed_x = 0
speed_y = 0

# ---------------------------

running = True
while running:
    # EVENT HANDLING
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            print(event)
            if event.key == K_ESCAPE:
                running = False
        elif event.type == QUIT:
            running = False

    # GAME STATE UPDATES
    # All game math and comparisons happen here
    keys = pygame.key.get_pressed()
    acceleration = 1

    if keys[119] == True:  # w
        speed_y -= acceleration

    if keys[97] == True:  # a
        speed_x -= acceleration

    if keys[115] == True:  # s
        speed_y += acceleration

    if keys[100] == True:  # d
        speed_x += acceleration

    speed_x *= 1 - FRICTION
    speed_y *= 1 - FRICTION

    circle_x += speed_x
    circle_y += speed_y


    # DRAWING
    screen.fill((255, 255, 255))  # always the first drawing command

    pygame.draw.circle(screen, (0, 0, 255), (circle_x, circle_y), 30)

    # Must be the last two lines
    # of the game loop
    pygame.display.flip()
    clock.tick(30)
    #---------------------------


pygame.quit()