-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrolling_characters_using_keys .py
More file actions
66 lines (55 loc) · 1.85 KB
/
Copy pathcontrolling_characters_using_keys .py
File metadata and controls
66 lines (55 loc) · 1.85 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import pygame
from time import *
from pygame.locals import *
pygame.init()
#WIDTH=600
#HIEGHT=600
screen = pygame.display.set_mode((600, 600))
player_x = 200
player_y = 200
keys = [False, False, False, False]
player = pygame.image.load('C:/Users/prave/OneDrive - OMO Scholengroep Helmond/Desktop/Jetlearn/Pro_Game_Developer/images/rocket.png')
background=pygame.image.load('C:/Users/prave/OneDrive - OMO Scholengroep Helmond/Desktop/Jetlearn/Pro_Game_Developer/images/space_background.png')
while player_y < 600:
screen.blit(background, (0, 0))
screen.blit(player, (player_x, player_y))
pygame.display.flip()
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
exit(0)
#check if any keyboard buttoN is pressed
if event.type == pygame.KEYDOWN:
if event.key == K_UP:
keys[0] = True
elif event.key == K_LEFT:
keys[1] = True
elif event.key == K_DOWN:
keys[2] = True
elif event.key == K_RIGHT:
keys[3] = True
#check if keyboard button is released
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
keys[0] = False
elif event.key == pygame.K_LEFT:
keys[1] = False
elif event.key == pygame.K_DOWN:
keys[2] = False
elif event.key == pygame.K_RIGHT:
keys[3] = False
if keys[0]:
if player_y > 0:
player_y -= 4
elif keys[2]:
if player_y < 536:
player_y += 4
if keys[1]:
if player_x > 0:
player_x -= 2
elif keys[3]:
if player_x < 536:
player_x += 2
player_y += 3
sleep(0.05)
print("GAME OVER!")