-
Notifications
You must be signed in to change notification settings - Fork 0
/
pygameTest.py
56 lines (44 loc) · 1.81 KB
/
pygameTest.py
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
53
54
55
56
import pygame
successes, failures = pygame.init()
print("Initializing pygame: {0} successes and {1} failures.".format(successes, failures))
screen = pygame.display.set_mode((720, 480))
clock = pygame.time.Clock()
FPS = 60
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface((32, 32))
self.image.fill(WHITE)
self.rect = self.image.get_rect() # Get rect of some size as 'image'.
self.velocity = [0, 0]
def update(self):
self.rect.move_ip(*self.velocity)
player = Player()
running = True
while running:
dt = clock.tick(FPS) / 1000 # Returns milliseconds between each call to 'tick'. The convert time to seconds.
screen.fill(BLACK) # Fill the screen with background color.
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
player.velocity[1] = -200 * dt # 200 pixels per second
elif event.key == pygame.K_s:
player.velocity[1] = 200 * dt
elif event.key == pygame.K_a:
player.velocity[0] = -200 * dt
elif event.key == pygame.K_d:
player.velocity[0] = 200 * dt
elif event.type == pygame.KEYUP:
if event.key == pygame.K_w or event.key == pygame.K_s:
player.velocity[1] = 0
elif event.key == pygame.K_a or event.key == pygame.K_d:
player.velocity[0] = 0
player.update()
screen.blit(player.image, player.rect)
pygame.display.update() # Or pygame.display.flip()
print("Exited the game loop. Game will quit...")
quit() # Not actually necessary since the script will exit anyway.