def explode(self): particles = paint_images( self.res.explosion_particles, lambda x: (10, 90, 10, x[3])) Explosion.spawn_particles(self.world, self.cl, self.res, b2_coords(self.body.position) * PPM, particles, 15, 5, self.game_object_group, timeout=1) self.dispose()
def __init__(self, res: Resources, pos, text, *groups, image=None): super().__init__(*groups) if image is None: image = paint_images([res.image_button], lambda x: (255, 255, 255, x[3]))[0] self.pos = pos self.set_init_image(image) render = res.font24.render(text, True, (0, 0, 0)) self.image.blit(render, ((self.image.get_width() - render.get_width()) / 2, 12)) self.init_rect = pygame.Rect(self.pos, self.image.get_size()) self.rect = self.init_rect.copy() self.mouse_down = False self.clicked = False self.pressed = False self.animation_state = 0 self._connected = lambda: None self.hover = False self.hover_state = 0 self.TIME_ANIMATION = 0.3 self.BACK_ANIMATION_COEFFICIENT = 2 self.SCALE_COEFFICIENT = 0.2 self.HOVER_TIME = 0.15 self.HOVER_COEFFICIENT = 0.15
def __init__(self, world, cl, res: Resources, pos, vector, game_object_group, *groups): super().__init__(world, cl, game_object_group, *groups) self.draw_shadow = False self.ignore_ray_casting = True self.set_init_image(paint_images([res.image_bullet], lambda x: (120, 0, 200, x[3]))[0]) self.vector = pygame.Vector2(vector).normalize() pos = pygame.Vector2(pos) + self.vector * 100 self.rect.center = pos self.res = res self.impulse = 2.5 radius = 12 shape = b2CircleShape() shape.radius = radius / PPM fd = B2Factory.create_fixture(shape, 0.3, 0, 1) self.body = B2Factory.create_body( world, b2_dynamicBody, fd, b2_coords(pos) / PPM) self.body.userData = self self.body.fixedRotation = True self.body.ApplyLinearImpulse(b2_coords(self.vector) * self.impulse, self.body.GetWorldPoint((0, 0)), True) self.timer = 0 self.timeout = 3 self.collide = False
def update(self, dt: float, events): if self.is_broken: self.broken_timer += dt if self.broken_timer > self.dispose_timeout: for i in self.tires: particles = paint_images(self.res.explosion_particles, lambda x: (0, 0, 0, x[3])) FireballExplosion.explosion.spawn_particles( self.world, self.cl, self.res, b2_coords(i.body.position) * PPM, particles, 3, 2, self.game_object_group) i.dispose() particles = paint_images( self.res.explosion_particles, lambda x: (*self.color_particles, x[3])) FireballExplosion.explosion.spawn_particles( self.world, self.cl, self.res, b2_coords(self.body.position) * PPM, particles, 7, 10, self.game_object_group) self.dispose() return # Update turn curr_angle = self.joints[0].angle v = self.control_state & (C_LEFT | C_RIGHT) desired_angle = 0 if v == C_LEFT: desired_angle = self.lock_angle elif v == C_RIGHT: desired_angle = -self.lock_angle turn = desired_angle - curr_angle if abs(turn) > self.speed_tire_rotate / 2 * dt: if turn > 0: turn = self.speed_tire_rotate * dt else: turn = -self.speed_tire_rotate * dt else: turn = 0 new_angle = curr_angle + turn if new_angle > self.lock_angle * 2: new_angle = 0 self.joints[0].SetLimits(new_angle, new_angle) self.joints[1].SetLimits(new_angle, new_angle)
def on_explosion(self, obj_from, power): if type(obj_from) == BombExplosion: if power > 200: particles = paint_images(self.res.explosion_particles, lambda x: (0, 0, 0, x[3])) Explosion.spawn_particles(self.world, self.cl, self.res, b2_coords(self.body.position) * PPM, particles, 15, 10, self.game_object_group) self.dispose()
def __init__(self, world, cl, res: Resources, pos, game_object_group, *groups): super().__init__(world, cl, game_object_group, *groups) power = 2500 radius = 500 count_rays = 30 Explosion.apply_impulses(world, pos, power, radius, count_rays, self) particles = paint_images(res.explosion_particles, lambda x: (0, 0, 0, x[3])) Explosion.spawn_particles(world, cl, res, pos, particles, 15, 10, self.game_object_group)
def process_image_to_shadow(image: pygame.Surface, color): """Makes image black like shadow with alpha""" res = paint_images([image], lambda x: (*color[:3], color[3] * x[3] // 255))[0] # Make blur width = 20 pil_im = image2pil(res, ) new = Image.new('RGBA', (pil_im.width + 2 * width, pil_im.height + 2 * width), color=SECONDARY_SHADOWS_COLOR) new.paste(pil_im, (width, width)) image_filter: ImageFilter = eval(f'ImageFilter.{SHADOWS_FILTER}') new = new.filter(image_filter) res = pil2image(new) return res
def update(self, dt, events): self.dollars.value = self.asm.main.dollars self.sprite_group.update(dt, events) self.spawn_timer += dt if self.spawn_timer >= self.spawn_timeout: self.spawn_timer = 0 self.spawn_random_car() self.world.Step(dt, 6, 2) self.background_group.update(dt, events) if self.spawn_counter: self.spawn_counter -= 1 self.spawn_random_car() for i in self.background_group: if type(i) == PlayerCar: i.energy -= dt * 0.3 if i.energy <= 0: i.break_down() for i in events: if i.type == pygame.MOUSEBUTTONDOWN: if i.button == pygame.BUTTON_RIGHT: Explosion.apply_impulses(self.world, pygame.mouse.get_pos(), 1500, 500, 15, self) particles = paint_images(self.res.explosion_particles, lambda x: (0, 0, 0, x[3])) Explosion.spawn_particles(*self.obj_args, pygame.mouse.get_pos(), particles, 10, 10, self.background_group) if i.type == pygame.KEYDOWN: if i.key == pygame.K_SPACE: self.spawn_counter += 5 if self.button_play.is_clicked(): self.asm.push(LevelState(self.asm, self.res)) if self.button_drift.is_clicked(): self.asm.push(DriftState(self.asm, self.res)) if self.button_shop.is_clicked(): self.asm.push(ShopState(self.asm, self.res)) if self.button_exit.is_clicked(): self.asm.pop()
def load(self): self.sprite_group = Group() self.label_font = self.res.font64 self.button = Button(self.res, ((WIDTH - 140), HEIGHT - 90), 'Назад', self.sprite_group) self.dollars = Dollars(self.res, self.sprite_group) skins = [i.copy() for i in self.asm.main.skins] image_tire = paint_images([self.res.image_tire], lambda x: (61, 67, 71, x[3]))[0] self.skins = [] for i in skins: im = image_tire margin = 10 new = pygame.Surface( (i.get_width() + margin * 2, i.get_height() + margin * 2)).convert_alpha() new.fill((0, 0, 0, 0)) for x in [-1, 1]: for y in [-1, 1]: new.blit( im, (Car.tire_shift[0] * x + i.get_width() // 2 - im.get_width() // 2 + margin, Car.tire_shift[1] * y + i.get_height() // 2 - im.get_height() // 2 + margin)) new.blit(i, (margin, margin)) self.skins.append(new) self.buttons = [ Button(self.res, ((WIDTH / 2 + 120 * i - len(self.skins) / 2 * 120), HEIGHT / 2 - 120), '', self.sprite_group, image=el) for i, el in enumerate(self.skins) ] self.title_font = self.res.font30 self.cost = [500] * 5 + [10000] self.bought = self.asm.main.saved_skins self.current = self.asm.main.current_skin self.save()
def break_down(self): if self.is_broken: return self.is_broken = True for i in self.joints: self.world.DestroyJoint(i) for i in self.tires: i: Tire i.body.linearDamping = 2 i.body.angularDamping = 2 i.control_state = 0 i.ignore_ray_casting = True impulse = pygame.Vector2(i.body.position - self.body.position) impulse = impulse.normalize() * 2 i.body.ApplyLinearImpulse(impulse, i.body.GetWorldPoint((0, 0)), True) self.body.linearDamping = 5 self.body.angularDamping = 5 # Более темное изображение после уничтожения self.init_image = paint_images( [self.init_image], lambda x: (*map(lambda a: max(0, a - 50), x[:3]), x[3]))[0]
def __init__(self, res: Resources, image_car): self.image_tire = paint_images([res.image_tire], lambda x: (61, 67, 71, x[3]))[0] self.image_car = image_car
def highlight_shadow(self, color): if not DRAW_SHADOWS: return self.highlight = True self.highlight_color = paint_images([self.init_shadow], lambda x: (*color[:3], x[3]))[0] self.highlight_timer = 0