-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstep2.py
More file actions
52 lines (41 loc) · 1.57 KB
/
Copy pathstep2.py
File metadata and controls
52 lines (41 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import pygame
# init MUST be included before pygame can do anything
pygame.init()
# creates a window that's 1280 pixels wide and 720 pixels tall
# display.setmode takes in a parameter of a tuple.
screen = pygame.display.set_mode((1280, 720))
# creates a clock that keeps track of time for us
clock = pygame.time.Clock()
running = True
# we want these outside of the loop, since we don't want to reset them every frame
player = pygame.Rect(0,0,50,50)
block = pygame.Rect(200,200,400,25)
# all code within the while loop is run over and over as long as running is true
while running:
# iterates through every new event since the last time get() was called
for event in pygame.event.get():
# if one of those events is QUIT, set running to false
if event.type == pygame.QUIT:
running = False
# keys is a list of 1s and 0s
# if a key is pressed, it's position in the list will be a 1
keys = pygame.key.get_pressed()
# print(keys)
# update position based on pressed keys
if keys[pygame.K_w]:
player.y -= 5
if keys[pygame.K_s]:
player.y += 5
if keys[pygame.K_d]:
player.x += 5
if keys[pygame.K_a]:
player.x -= 5
# clears everything that was on screen
screen.fill("grey")
# draws each object according to the Rect objects repersenting them
pygame.draw.rect(screen, "red", player)
pygame.draw.rect(screen, "black", block)
# flip() updates the display to show everything that has been drawn
pygame.display.flip()
clock.tick(60) # limits FPS to 60
pygame.quit()