def control_p1(): keyboard = Keyboard() position = Vec2(0, 0) if keyboard.is_key_pressed(Keyboard.DOWN): position = Vec2(0, 200) if keyboard.is_key_pressed(Keyboard.UP): position = Vec2(0, -200) return position
def control_p2(): keyboard = Keyboard() position = Vec2(0, 0) if keyboard.is_key_pressed(Keyboard.K_s): position = Vec2(0, 200) if keyboard.is_key_pressed(Keyboard.K_w): position = Vec2(0, -200) return position
class Player(GameObject): def __init__(self, x, y): super().__init__(x, y) self.sprite = Sprite("engineer.jpg") self.keyboard = Keyboard() def update(self, time_elapsed): if (self.keyboard.is_key_pressed(Keyboard.RIGHT)): self.velocity = Vec2(100, 0) if (self.keyboard.is_key_pressed(Keyboard.LEFT)): self.velocity = Vec2(-100, 0) if (self.keyboard.is_key_pressed(Keyboard.DOWN)): self.velocity = Vec2(0, 100) if (self.keyboard.is_key_pressed(Keyboard.UP)): self.velocity = Vec2(0, -100) def draw(self, graphics): self.sprite.set_x(self.position.x) self.sprite.set_y(self.position.y) graphics.add(self.sprite)
class Ball(RigidBody): def __init__(self, position): super().__init__(position, 1, BoxCollider(position, (27, 26)), 1.2) self.keyboard = Keyboard() self.sprite = Sprite(conf.ASSETS + 'ball.png') self.moving = False def update(self, time_elapsed): if (self.keyboard.is_key_pressed(Keyboard.RETURN)): self.velocity = Vec2(-70, 70) self.moving = True self.position += self.velocity * time_elapsed def draw(self, graphics): self.sprite.x = self.position.x self.sprite.y = self.position.y graphics.add(self.sprite)