-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyboard.py
77 lines (57 loc) · 1.77 KB
/
keyboard.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# Load modules
from pygame.locals import *
import pygame
# Define player class
class Player:
x = 0
y = 0
# Game class
class Game:
def __init__(self):
self._running = True
# Create player object
self.player = Player()
def on_init(self):
# Init pygame, set window and title
pygame.init()
self._display_surf = pygame.display.set_mode((640,480), pygame.HWSURFACE)
pygame.display.set_caption('Pygame example')
# Load image
self._cat_image = pygame.image.load("cat.jpg").convert()
def on_render(self):
# Clear screen
self._display_surf.fill((0,0,0))
# Show image
self._display_surf.blit(self._cat_image, (self.player.x,self.player.y))
# Update screen
pygame.display.flip()
def on_cleanup(self):
pygame.quit()
def on_execute(self):
if self.on_init() == False:
self._running = False
# Game loop
while self._running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
self._running = False
# Handle keyboard events
keys = pygame.key.get_pressed()
if keys[K_RIGHT]:
self.player.x += 1
if keys[K_LEFT]:
self.player.x -= 1
if keys[K_UP]:
self.player.y -= 1
if keys[K_DOWN]:
self.player.y += 1
if keys[K_a]:
print('a')
if keys[K_b]:
print('b')
# Update screen
self.on_render()
self.on_cleanup()
if __name__ == "__main__" :
game = Game()
game.on_execute()