コード例 #1
0
ファイル: Game.py プロジェクト: Pickersgill/GamesGroup
    def __init__(self, frame, size):
        self.size = size
        # make a new camera based on the size of the frame, then create the frame and add a start button.
        self.camera = Camera((0, 0), self.size[0] // Unit.UNIT,
                             self.size[1] // Unit.UNIT, Unit.UNIT)
        self.player = Player(100, 100, Unit.UNIT, Unit.UNIT)
        self.keyboard = Keyboard()
        self.result = States.STATES[1]

        frame.set_keydown_handler(self.keyboard.key_down)
        frame.set_keyup_handler(self.keyboard.key_up)

        sound = Sound()
        self.tiles = Mapper.get_start_tiles(self.camera)
        sound.play_death()
コード例 #2
0
class Player:
    IDLE = 0
    IDLE_START = 22
    IDLE_END = 23

    RUNNING = 1
    RUNNING_START = 0
    RUNNING_END = 15

    JUMPING = 2
    JUMPING_START = 16
    JUMPING_END = 21

    RIGHT = 0
    LEFT = 1

    def __init__(self, x, y, w, h, image=ResourcePaths.character_sprite_sheet):
        self.pos = Vector(x, y)
        self.dv = Vector(0, 0)
        self.direction = self.LEFT
        self.grounded = False
        self.w = w
        self.h = h
        self.tick_count = 0
        self.state = self.IDLE
        self.live_image = simplegui.load_image(ResourcePaths.heart)
        self.image = simplegui.load_image(image)
        self.cols = self.image.get_width() // self.w
        self.rows = self.image.get_height() // self.h
        self.sprite_index = 22
        self.last_point_standing = self.pos
        self.lives = 2#50
        self.player_sound = Sound()
        self.ui = UI()

    def draw(self, canvas, camera_dims, offset=0):
        x_pos = self.pos.x - offset.x
        y_pos = self.pos.y - offset.y
        canvas.draw_image(self.image, self.get_index_pos()
                          , (self.image.get_width() / self.cols, self.image.get_height() / self.rows)
                          , (x_pos, y_pos), (self.w, self.h))
        if self.lives < 0:
            return self.ui.game_over(canvas, camera_dims)
        else:
            return States.STATES[1]

    def get_index_pos(self):
        index = self.sprite_index + (0 if self.direction == self.RIGHT else 24)
        x_pos = (index % self.cols) * (self.image.get_width() / self.cols)
        y_pos = ((index // self.cols) % self.rows) * (self.image.get_height() / self.rows)
        
        # adding extra half distance for draw:
        x_pos += (self.image.get_width() / self.cols) / 2
        y_pos += (self.image.get_height() / self.rows) / 2
        return x_pos, y_pos

    def draw_lives(self, canvas):
        for n in range(self.lives):
            canvas.draw_image(self.live_image,
                              (self.live_image.get_width() / 2, self.live_image.get_height() / 2),
                              (self.live_image.get_width(), self.live_image.get_height()),
                              (((n % 15) * (Unit.UNIT * 0.6)) + Unit.UNIT, (Unit.UNIT / 2) * (1 + (n // 15))),
                              (Unit.UNIT / 2, Unit.UNIT / 2)
                              )

    def set_state(self, state):
        if state == self.IDLE:
            self.state = self.IDLE

        elif state == self.RUNNING:
            self.state = self.RUNNING

        elif state == self.JUMPING:
            self.state = self.JUMPING

    def tick(self):
        self.tick_count += 1
        if self.tick_count % 4 == 0:
            self.sprite_index += 1
        mid = ((self.cols * self. rows) // 2)
        mid = mid if mid > 0 else 1
        real_index = (self.sprite_index % mid) + 1
        if self.state == self.IDLE and real_index > self.IDLE_END:
            self.sprite_index = self.IDLE_START
        elif self.state == self.RUNNING and real_index > self.RUNNING_END:
            self.sprite_index = self.RUNNING_START
        elif self.state == self.JUMPING and real_index > self.JUMPING_END:
            self.sprite_index = self.JUMPING_START
        self.pos.add(self.dv)

    def in_camera(self, camera):
        # define the bounds for the camera object, minimum and maximum are both a little flexible
        # the UNIT size is added to the bounds, thus rendering anything which is any amount in span
        camera_max_x = camera.pos.x + camera.span[0] + camera.UNIT
        camera_max_y = camera.pos.y + camera.span[1] + camera.UNIT
        camera_min_x = camera.pos.x - camera.UNIT
        camera_min_y = camera.pos.y - camera.UNIT

        # quick logic to see if in bounds, then returns true or false.
        in_x_bound = (self.x >= camera_min_x) and (self.x <= camera_max_x)
        in_y_bound = (self.y >= camera_min_y) and (self.y <= camera_max_y)
        return in_x_bound and in_y_bound

    def set_checkpoint(self):
        current_tile_pos = Vector(((self.pos.x // Unit.UNIT) - 0.5) * Unit.UNIT, ((self.pos.y // Unit.UNIT) + 0.5) * Unit.UNIT)
        self.last_point_standing = current_tile_pos

    def revive(self, camera):
        self.player_sound.play_death()
        self.lives -= 1
        if self.lives > 0:
            self.dv.y = 0
            # Swap between these to decide whether respawn is on
            # self.pos = self.last_point_standing
            self.pos = Vector(400, 300)
            camera.dv.y = 0
            camera.set_relative_pos(self.pos)