def update(self, timestamp): super(Level, self).update(timestamp) self.balls = [x for x in self.iter_balls()] self.bullets = [x for x in self.iter_bullets()] self.bricks = [x for x in self.iter_bricks()] self.powerups = [x for x in self.iter_powerups()] if timestamp > 200 and timestamp % 50 == 0: for x in self.iter_bricks(): x.move(Pos([1, 0])) if len(self.balls) == 0: self.game.lives -= 1 ball_pos = self.paddle.pos + [-1, np.random.randint(0, self.paddle.width // 2) * 2] self.balls.append( Ball(self, pos=ball_pos, vel=Vel([0, 0])) ) self.add(*self.balls)
def receive_input(self, char): if char in ['j', 'l']: direction = -4 if char == 'j' else 4 # print(direction) direction = min(self.right - self.paddle.right, direction) # print(direction) direction = max(self.left - self.paddle.left, direction) # print(direction) self.paddle.move(Pos([0, direction])) for it in self.iter_balls(): if it.on_paddle: it.move(Pos([0, direction])) elif char == 'k': for it in self.iter_balls(): it.on_paddle = False it.set_launch_angle(self.paddle.get_ball_angle(it.x)) elif char == 's': if self.paddle.state['shooting']: self.add_paddle_bullet(Pos(self.paddle.pos + Pos([0, 2])), Vel([-1, 0]))
def __init__(self, scene, *args, **kwargs): super(PowerUp, self).__init__(self.sprite, scene, *args, **kwargs) self.vel = Vel(kwargs.get('vel')) / 2 self.fill_foreground((255, 255, 255)) self.alpha = True self.acc = 0.05
def update(self, timestamp): self.vel += Vel([self.acc, 0]) self.vel[0] = max(self.vel[0], 1.5) self.box_constraints() self.move(self.vel)
def __init__(self, scene, *args, **kwargs): super(Bullet, self).__init__(self.sprite, scene, *args, **kwargs) self.vel = Vel([1, 0]) self.fill_foreground((255, 255, 255)) self.alpha = True
def __init__(self, *args, **kwargs): super(PaddleBullet, self).__init__(*args, **kwargs) self.vel = Vel([-1, 0])
def __init__(self, *args, **kwargs): super(AlienBullet, self).__init__(*args, **kwargs) self.vel = Vel([1, 0])
def update(self, timestamp): for target in self.scene.iter_balls(): if (target.top == self.bottom or collides(target, self) and target.vy < 0) \ or (target.bottom == self.top or collides(target, self) and target.vy > 0): if target.left > self.left - 1 and target.right < self.right + 2: self.hit(target) target.switch_y() elif target.left == self.left - 1 and target.vx > 0: self.hit(target) target.switch_x() target.switch_y() elif target.left == self.right + 1 and target.vx < 0: self.hit(target) target.switch_x() target.switch_y() elif (target.left == self.right or collides(target, self) and target.vx < 0) or \ (target.right == self.left or collides(target, self) and target.vx > 0): if self.top <= target.top <= self.bottom: self.hit(target) target.switch_x() if timestamp % 50 == 0: self.scene.add_alien_bullet(Pos([self.bottom, self.left + self.width // 2]), Vel([1, 0]))
def gen(self): # pass self.generate_bricks() ball_pos = self.paddle.pos + Pos([-1, np.random.randint(0, self.paddle.width // 2) * 2]) self.balls.append(Ball(self, pos=ball_pos, vel=Vel([0, 0]))) self.add(*self.bricks, self.background, self.paddle, *self.balls)