-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
217 lines (176 loc) · 5.89 KB
/
game.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import pygame
import time
SPEED = 2
DEBUG_MODE = False
pygame.display.init()
screen = pygame.display.set_mode((640, 480))#, pygame.FULLSCREEN)
screen.fill((40,60,80))
lev = ['###########################',
'# #',
'# # #',
'############### # #',
'# # #####',
'# # #',
'# ############# #',
'# # #',
'# # #',
'# # # #',
'# # # ############',
'# # # #',
'# # # #',
'# # #',
'# # ########### #',
'# # # #',
'# #### # #',
'# # # # #',
'# # # #',
'###########################',
]
wall = pygame.image.load('gfx/wall.png')
grass = pygame.image.load('gfx/grass.png')
debug = pygame.image.load('gfx/debugtile.png')
tiles = {'#': wall,
' ': grass,
}
left1 = pygame.image.load('gfx/player_left1.png')
left2 = pygame.image.load('gfx/player_left2.png')
right1 = pygame.image.load('gfx/player_right1.png')
right2 = pygame.image.load('gfx/player_right2.png')
up1 = pygame.image.load('gfx/player_up1.png')
up2 = pygame.image.load('gfx/player_up2.png')
down1 = pygame.image.load('gfx/player_down1.png')
down2 = pygame.image.load('gfx/player_down2.png')
idle = pygame.image.load('gfx/player_idle.png')
animation = [(left1, left2),
(right1, right2),
(up1, up2),
(down1, down2),
(idle, idle),
]
playerx = 48
playery = 240
xdir = 0
ydir = 0
clock = pygame.time.Clock()
running = True
while running:
t = time.time()
screen.fill((40,60,80))
# level zeichnen
for y in range(len(lev)):
for x in range(len(lev[0])):
tileno = lev[y][x]
tile = tiles[tileno]
screen.blit(tile, (x * 24, y * 24))
# spieler zeichnen
if xdir < 0:
animdir = 0
elif xdir > 0:
animdir = 1
elif ydir < 0:
animdir = 2
elif ydir > 0:
animdir = 3
else:
animdir = 4
animphase = 0 if int(t * 1000) % 400 > 200 else 1
player_sprite = animation[animdir][animphase]
screen.blit(player_sprite, (playerx, playery))
# tastatur-events abfragen
while True:
e = pygame.event.poll()
if not e:
break
if e.type == pygame.KEYDOWN:
if e.key == pygame.K_LEFT:
xdir = -SPEED
ydir = 0
elif e.key == pygame.K_RIGHT:
xdir = SPEED
ydir = 0
if e.key == pygame.K_UP:
ydir = -SPEED
xdir = 0
elif e.key == pygame.K_DOWN:
ydir = SPEED
xdir = 0
elif e.key == pygame.K_ESCAPE:
running = False
elif e.key == pygame.K_F12:
DEBUG_MODE = not DEBUG_MODE
elif e.type == pygame.KEYUP:
if e.key == pygame.K_LEFT:
if xdir < 0:
xdir = 0
elif e.key == pygame.K_RIGHT:
if xdir > 0:
xdir = 0
if e.key == pygame.K_UP:
if ydir < 0:
ydir = 0
elif e.key == pygame.K_DOWN:
if ydir > 0:
ydir = 0
# richtungen und positionen erstmal sichern
newxdir = xdir
newydir = ydir
newplayerx = playerx + newxdir
newplayery = playery + newydir
# kollisions-relevante tiles ermitteln (tilesize = 24x24)
x1 = int(newplayerx / 24)
x2 = int((newplayerx + 23) / 24)
y1 = int(newplayery / 24)
y2 = int((newplayery + 23) / 24)
collisiontile1 = lev[y1][x1]
collisiontile2 = lev[y1][x2]
collisiontile3 = lev[y2][x1]
collisiontile4 = lev[y2][x2]
if DEBUG_MODE:
screen.blit(debug, (x1 * 24, y1 * 24))
screen.blit(debug, (x2 * 24, y1 * 24))
screen.blit(debug, (x1 * 24, y2 * 24))
screen.blit(debug, (x2 * 24, y2 * 24))
# supergenialer smooth-um-die-ecken-lauf-algorithmus
if xdir < 0:
if collisiontile1 == '#' and collisiontile3 == '#':
newxdir = 0
elif collisiontile1 == '#' and collisiontile3 == ' ':
newxdir = 0
newydir = SPEED
elif collisiontile1 == ' ' and collisiontile3 == '#':
newxdir = 0
newydir = -SPEED
elif xdir > 0:
if collisiontile2 == '#' and collisiontile4 == '#':
newxdir = 0
elif collisiontile2 == '#' and collisiontile4 == ' ':
newxdir = 0
newydir = SPEED
elif collisiontile2 == ' ' and collisiontile4 == '#':
newxdir = 0
newydir = -SPEED
elif ydir < 0:
if collisiontile1 == '#' and collisiontile2 == '#':
newydir = 0
elif collisiontile1 == '#' and collisiontile2 == ' ':
newydir = 0
newxdir = SPEED
elif collisiontile1 == ' ' and collisiontile2 == '#':
newydir = 0
newxdir = -SPEED
elif ydir > 0:
if collisiontile3 == '#' and collisiontile4 == '#':
newydir = 0
elif collisiontile3 == '#' and collisiontile4 == ' ':
newydir = 0
newxdir = SPEED
elif collisiontile3 == ' ' and collisiontile4 == '#':
newydir = 0
newxdir = -SPEED
# geaenderte position anwenden
playerx += newxdir
playery += newydir
# buffer wechseln (gerendertes bild anzeigen) und auf 60 fps begrenzen
pygame.display.flip()
clock.tick(60)
pygame.quit()