Skip to content

Instantly share code, notes, and snippets.

@pinglunliao
Created June 3, 2019 03:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pinglunliao/d83335207f99ebe6c83b40526cd029e1 to your computer and use it in GitHub Desktop.
Save pinglunliao/d83335207f99ebe6c83b40526cd029e1 to your computer and use it in GitHub Desktop.
Moving a sprite with keyboard
# -*- coding: utf-8 -*-
import pygame
pygame.init()
#
# Step 1: Create the screen window
#
# Screen width, height
S_W = 500
S_H = 500
winScreen = pygame.display.set_mode((S_W, S_H))
pygame.display.set_caption("Keyboard Demo")
#
# Step 2: Set the Ball's velocity, position, size
#
R = 20
x = int((S_W - R) / 2)
y = int((S_H - R) / 2)
vel = 5
run = True
while run:
pygame.time.delay(10)
#
# Step 3: Detect which key is pressed.
#
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
x -= vel
if keys[pygame.K_RIGHT]:
x += vel
if keys[pygame.K_UP]:
y -= vel
if keys[pygame.K_DOWN]:
y += vel
#
# Step 4: Draw the ball.
#
winScreen.fill((0, 0, 0)) # Fills the screen with black
pygame.draw.circle(winScreen, (255, 255, 0), (x, y), R)
pygame.display.update()
pygame.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment