Skip to content

Instantly share code, notes, and snippets.

@JeffersGlass
Created September 27, 2020 19:52
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 JeffersGlass/430d103dab90f5242a67dac7f8a6affb to your computer and use it in GitHub Desktop.
Save JeffersGlass/430d103dab90f5242a67dac7f8a6affb to your computer and use it in GitHub Desktop.
import pygame
import sys #will use the exit function
#Loads all the main pygame modules and initializes them
pygame.init()
screen_width=640
screen_height=480
blue = (0,0,255)
white = (255,255,255)
#Create a new Surface with the dimensions listed above
screen=pygame.display.set_mode((screen_width, screen_height))
#Use this code to find all the fonts on your system, if necessary:
#for f in pygame.font.get_fonts():
# print(f)
#Rect takes (left, top, width, height)
box = pygame.Rect(100,100,25,25)
boxColor = (255,0,0)
#Loop forever:
while True:
screen.fill((0,0,0))
#The following makes the window close when clicking the "X" or exit button
#Without this, the window will not close, and will need to be killed
#with the task manager
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
#get the state of all keyboard keys
#This is a list of booleans; use the built-in indexes in Pygame to get
#The value you want
keys = pygame.key.get_pressed()
#Check the arrow keys to move the box around
if keys[pygame.K_LEFT]: box.x -= 1
if keys[pygame.K_RIGHT]: box.x += 1
if keys[pygame.K_UP]: box.y -= 1
if keys[pygame.K_DOWN]: box.y += 1
#render takes a string, Anti-alias? (T/F), color)
pygame.draw.rect(screen, boxColor, box)
#Output new changes to the screen
pygame.display.flip()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment