/
crashman.py
256 lines (214 loc) · 8.41 KB
/
crashman.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#crashman
import pygame
from pygame.locals import *
import sys
SCREEN_SIZE = (1400, 800) #resolution of the game
global HORIZ_MOV_INCR
HORIZ_MOV_INCR = 10 #speed of movement
global FPS
global clock
global time_spent
def RelRect(actor, camera):
return pygame.Rect(actor.rect.x-camera.rect.x, actor.rect.y-camera.rect.y, actor.rect.w, actor.rect.h)
class Camera(object):
'''Class for center screen on the player'''
def __init__(self, screen, player, level_width, level_height):
self.player = player
self.rect = screen.get_rect()
self.rect.center = self.player.center
self.world_rect = Rect(0, 0, level_width, level_height)
def update(self):
if self.player.centerx > self.rect.centerx + 25:
self.rect.centerx = self.player.centerx - 25
if self.player.centerx < self.rect.centerx - 25:
self.rect.centerx = self.player.centerx + 25
if self.player.centery > self.rect.centery + 25:
self.rect.centery = self.player.centery - 25
if self.player.centery < self.rect.centery - 25:
self.rect.centery = self.player.centery + 25
self.rect.clamp_ip(self.world_rect)
def draw_sprites(self, surf, sprites):
for s in sprites:
if s.rect.colliderect(self.rect):
surf.blit(s.image, RelRect(s, self))
class Obstacle(pygame.sprite.Sprite):
'''Class for create obstacles'''
def __init__(self, x, y):
self.x = x
self.y = y
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("cat5.jpg").convert()
self.rect = self.image.get_rect()
self.rect.topleft = [self.x, self.y]
class Crashman(pygame.sprite.Sprite):
'''class for player and collision'''
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.movy = 0
self.movx = 0
self.x = x
self.y = y
self.contact = False
self.jump = False
self.image = pygame.image.load('actions/idle_right.png').convert()
self.rect = self.image.get_rect()
self.run_left = ["actions/run_left000.png","actions/run_left001.png",
"actions/run_left002.png", "actions/run_left003.png",
"actions/run_left004.png", "actions/run_left005.png",
"actions/run_left006.png", "actions/run_left007.png"]
self.run_right = ["actions/run_right000.png","actions/run_right001.png",
"actions/run_right002.png", "actions/run_right003.png",
"actions/run_right004.png", "actions/run_right005.png",
"actions/run_right006.png", "actions/run_right007.png"]
self.direction = "right"
self.rect.topleft = [x, y]
self.frame = 0
def update(self, up, down, left, right):
if up:
if self.contact:
self.jump = True
self.movy -= 20
if down:
if self.contact and self.direction == "right":
self.image = pygame.image.load('actions/down_right.png').convert_alpha()
if self.contact and self.direction == "left":
self.image = pygame.image.load('actions/down_left.png').convert_alpha()
if not down and self.direction == "right":
self.image = pygame.image.load('actions/idle_right.png').convert_alpha()
if not down and self.direction == "left":
self.image = pygame.image.load('actions/idle_left.png').convert_alpha()
if left:
self.direction = "left"
self.movx = -HORIZ_MOV_INCR
if self.contact:
self.frame += 1
self.image = pygame.image.load(self.run_left[self.frame]).convert_alpha()
if self.frame == 6: self.frame = 0
else:
self.image = self.image = pygame.image.load("actions/jump_left.png").convert_alpha()
if right:
self.direction = "right"
self.movx = +HORIZ_MOV_INCR
if self.contact:
self.frame += 1
self.image = pygame.image.load(self.run_right[self.frame]).convert_alpha()
if self.frame == 6: self.frame = 0
else:
self.image = self.image = pygame.image.load("actions/jump_right.png").convert_alpha()
if not (left or right):
self.movx = 0
self.rect.right += self.movx
self.collide(self.movx, 0, world)
if not self.contact:
self.movy += 0.3
if self.movy > 10:
self.movy = 10
self.rect.top += self.movy
if self.jump:
self.movy += 2
self.rect.top += self.movy
if self.contact == True:
self.jump = False
self.contact = False
self.collide(0, self.movy, world)
def collide(self, movx, movy, world):
self.contact = False
for o in world:
if self.rect.colliderect(o):
if movx > 0:
self.rect.right = o.rect.left
if movx < 0:
self.rect.left = o.rect.right
if movy > 0:
self.rect.bottom = o.rect.top
self.movy = 0
self.contact = True
if movy < 0:
self.rect.top = o.rect.bottom
self.movy = 0
class Level(object):
'''Read a map and create a level'''
def __init__(self, open_level):
self.level1 = []
self.world = []
self.all_sprite = pygame.sprite.Group()
self.level = open(open_level, "r")
def create_level(self, x, y):
for l in self.level:
self.level1.append(l)
for row in self.level1:
for col in row:
if col == "X":
obstacle = Obstacle(x, y)
self.world.append(obstacle)
self.all_sprite.add(self.world)
if col == "P":
self.crashman = Crashman(x,y)
self.all_sprite.add(self.crashman)
x += 25
y += 25
x = 0
def get_size(self):
lines = self.level1
#line = lines[0]
line = max(lines, key=len)
self.width = (len(line))*25
self.height = (len(lines))*25
return (self.width, self.height)
def tps(orologio,fps):
temp = orologio.tick(fps)
tps = temp / 1000.
return tps
pygame.init()
screen = pygame.display.set_mode(SCREEN_SIZE, FULLSCREEN, 32)
screen_rect = screen.get_rect()
background = pygame.image.load("world/background.jpg").convert_alpha()
background_rect = background.get_rect()
level = Level("level/level1")
level.create_level(0,0)
world = level.world
crashman = level.crashman
mposx=0
mpoxy=0
# pygame.mouse.set_visible(0)
camera = Camera(screen, crashman.rect, level.get_size()[0], level.get_size()[1])
all_sprite = level.all_sprite
FPS = 30
clock = pygame.time.Clock()
up = down = left = right = False
x, y = 0, 0
while True:
for event in pygame.event.get():
if event.type == QUIT or event.type == KEYDOWN and event.key == K_ESCAPE:
pygame.quit()
sys.exit()
if event.type == MOUSEBUTTONDOWN:
mposx,mposy = event.pos
print('Crashman is at %s' %self.x)
print(mposx,mposy)
if event.type == KEYDOWN and event.key == K_SPACE:
up = True
if event.type == KEYDOWN and event.key == K_s:
down = True
if event.type == KEYDOWN and event.key == K_a:
left = True
if event.type == KEYDOWN and event.key == K_d:
right = True
if event.type == KEYUP and event.key == K_SPACE:
up = False
if event.type == KEYUP and event.key == K_s:
down = False
if event.type == KEYUP and event.key == K_a:
left = False
if event.type == KEYUP and event.key == K_d:
right = False
asize = ((screen_rect.w // background_rect.w + 1) * background_rect.w, (screen_rect.h // background_rect.h + 1) * background_rect.h)
bg = pygame.Surface(asize)
for x in range(0, asize[0], background_rect.w):
for y in range(0, asize[1], background_rect.h):
screen.blit(background, (x, y))
time_spent = tps(clock, FPS)
camera.draw_sprites(screen, all_sprite)
crashman.update(up, down, left, right)
camera.update()
pygame.display.flip()