-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdude.py
executable file
·137 lines (117 loc) · 4.34 KB
/
dude.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python3
import pygame
import os
os.chdir(os.path.dirname(__file__))
pygame.init()
screen = pygame.display.set_mode((800, 600))
font = pygame.font.Font(None, 50)
image = pygame.image.load('character2.png')
grassImage = pygame.image.load('grass.png')
wallImage = pygame.image.load('wall.png')
flowerImage = pygame.image.load('flower.png')
dudeImage = pygame.image.load('Dude.png')
level = ['##################################',
'##################################',
'## + + ##',
'## + ##',
'## + ######## ##',
'## + ######## #######',
'## + #######',
'######## ##',
'######## ## ##',
'## ## + ##',
'## + ## + ##',
'## + ## ##',
'## ## ##',
'###### ################ #####',
'###### ################ #####',
'## ##'
'## + + ##',
'## + ##',
'## + ## ##',
'########### + ## + ##',
'########### ##### ##',
'## ##### ##',
'## + + ##',
'## + + ##',
'##################################',
'##################################']
level = [list(row) for row in level]
flowers = 0
x = 82
y = 82
dirx = 0
diry = 0
youWon = False
youLost = False
done = False
while done == False:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN or event.type == pygame.KEYUP:
if event.key == pygame.K_ESCAPE:
done = True
elif event.key == pygame.K_LEFT:
if event.type == pygame.KEYDOWN:
dirx = -1
elif dirx == -1:
dirx = 0
elif event.key == pygame.K_RIGHT:
if event.type == pygame.KEYDOWN:
dirx = 1
elif dirx == 1:
dirx = 0
elif event.key == pygame.K_UP:
if event.type == pygame.KEYDOWN:
diry = -1
elif diry == -1:
diry = 0
elif event.key == pygame.K_DOWN:
if event.type == pygame.KEYDOWN:
diry = 1
elif diry == 1:
diry = 0
screen.fill((173, 25, 97))
ty = 0
for row in level:
tx = 0
for tile in row:
if tile == '#':
screen.blit(wallImage, [tx, ty])
elif tile == ' ':
screen.blit(grassImage, [tx, ty])
elif tile == '+':
screen.blit(flowerImage, [tx, ty])
tx += 24
ty += 24
screen.blit(image, [x, y]) #draw image on backbuffer at position x, y
x += dirx * 2 # x = x + dirx
y += diry * 2 # dir = direction
screen.blit(dudeImage, [730, 510])
cornerUL = level[(y ) // 24][(x ) // 24] == '#'
cornerUR = level[(y ) // 24][(x + 32) // 24] == '#'
cornerLL = level[(y + 32) // 24][(x ) // 24] == '#'
cornerLR = level[(y + 32) // 24][(x + 32) // 24] == '#'
corners = [cornerUL, cornerUR, cornerLL, cornerLR]
if any(corners):
x -= dirx * 2
y -= diry * 2
if level[(y + 16) // 24][(x + 16) // 24] == '+':
level[(y + 16) // 24][(x + 16) // 24] = ' '
flowers += 1
text = font.render(str(flowers), 1, (0, 0, 0))
screen.blit(text, (750, 10))
if abs((x + 16) - (730 + 16)) < 20 and abs((y + 16) - (510 + 16)) < 20:
if flowers == 18:
youWon = True
else:
youLost = True
if youLost:
text = font.render('Too bad. You got dumped', 1, (0, 0, 0), (173, 25, 97))
screen.blit(text, (170, 100))
elif youWon:
text = font.render('You repaired your love.. for now', 1, (0, 0, 0), (173, 25, 97))
screen.blit(text, (120, 100))
pygame.display.flip() # exchange screen front to back (back now visable)
pygame.quit()