-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGUI PyGame.py
49 lines (39 loc) · 1.37 KB
/
GUI PyGame.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
import pygame
def block_moment(screen, block, x, y): # screen: object of window, block: image object, x & y: coordinates
screen.fill((78, 56, 78))
screen.blit(block, (x, y))
pygame.display.flip()
def game():
pygame.init()
size = (500, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption('PyGame')
# adding background
screen.fill((78, 56, 78))
# load resources
block = pygame.image.load('block.png').convert()
x = 0
y = 0
screen.blit(block, (x, y))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
y -= 10
block_moment(screen, block, x, y)
elif event.key == pygame.K_DOWN:
y += 10
block_moment(screen, block, x, y)
elif event.key == pygame.K_RIGHT:
x -= 10
block_moment(screen, block, x, y)
elif event.key == pygame.K_LEFT:
x += 10
block_moment(screen, block, x, y)
elif event.type == pygame.QUIT:
running = False
# update the screen
pygame.display.flip()
if __name__ == '__main__':
game()