示例#1
0
class PySnake(MyGame):
    "贪吃蛇游戏"

    def __init__(self):
        super(PySnake, self).__init__(GAME_NAME, SCREEN_SIZE, FPS,
                                      "resources/Minecraft.ttf", 16,
                                      "resources/pysnake.png")
        self.background.fill(BACKGROUND_COLOR)
        for i in range(CELL_SIZE, SCREEN_WIDTH, CELL_SIZE):
            pygame.draw.line(self.background, GRID_COLOR, (i, 0),
                             (i, SCREEN_HEIGHT))
        for i in range(CELL_SIZE, SCREEN_HEIGHT, CELL_SIZE):
            pygame.draw.line(self.background, GRID_COLOR, (0, i),
                             (SCREEN_WIDTH, i))

        self.field = Field(self, COLUMNS, ROWS)
        self.apple_counter = 0
        self.apple = Apple(self)
        # self.snake = Snake(self)
        self.snake = Snake(self, SNAKE_DEFAULT_X, SNAKE_DEFAULT_Y,
                           SNAKE_DEFAULT_LENGTH, RIGHT, SNAKE_DEFAULT_SPEED,
                           SNAKE_COLOR_SKIN, SNAKE_COLOR_BODY,
                           SNAKE_COLOR_HEAD)

        # 控制按键设定
        self.key_bind(KEY_EXIT, self.quit)
        self.key_bind(KEY_UP, self.snake.turn, direction=UP)
        self.key_bind(KEY_DOWN, self.snake.turn, direction=DOWN)
        self.key_bind(KEY_LEFT, self.snake.turn, direction=LEFT)
        self.key_bind(KEY_RIGHT, self.snake.turn, direction=RIGHT)
        self.key_bind(pygame.K_EQUALS, self.snake.speed_up)
        self.key_bind(pygame.K_MINUS, self.snake.speed_down)
        self.key_bind(KEY_RESPAWN, self.restart)

        self.add_draw_action(self.show_score)

    def restart(self):
        if not self.snake.alive:
            self.apple_counter = 0
            self.field.clear()  # 移除尸体
            self.apple.drop()
            self.snake.respawn()

    def show_score(self):
        text = "Apple {}".format(self.apple_counter)
        output = self.font.render(text, True, (255, 255, 33))
        self.screen.blit(output, (0, 0))

        if not self.snake.alive:
            text = " GAME OVER"
            output = self.font.render(text, True, (255, 33, 33), WHITE)
            self.screen.blit(output, (320 - 54, 240 - 10))
            text = " press R to restart"
            output = self.font.render(text, True, GREY, DARK_GREY)
            self.screen.blit(output, (320 - 94, 260 - 10))

        if not self.running and self.snake.alive:
            text = " GAME PAUSED"
            output = self.font.render(text, True, LIGTH_GREY, DARK_GREY)
            self.screen.blit(output, (320 - 54, 240 - 10))
示例#2
0
class PySnake(MyGame):
    """贪吃蛇游戏"""
    def __init__(self):
        super(PySnake, self).__init__(GAME_NAME, ICON,
                                      SCREEN_WIDTH, SCREEN_HEIGHT,
                                      DISPLAY_MODE, LOOP_SPEED,
                                      FONT_NAME, FONT_SIZE)
        self.draw_background_line()
        self.snake = Snake(self)
        self.apple = Apple(self)
        self.apple_count = 0
        # 绑定按键
        self.add_key_binding(KEY_UP, self.snake.turn, direction=UP)
        self.add_key_binding(KEY_DOWN, self.snake.turn, direction=DOWN)
        self.add_key_binding(KEY_LEFT, self.snake.turn, direction=LEFT)
        self.add_key_binding(KEY_RIGHT, self.snake.turn, direction=RIGHT)
        self.add_key_binding(KEY_REBORN, self.restart)
        self.add_key_binding(KEY_EXIT, self.game_quit)
        self.add_draw_action(self.show_score)

    def draw_background_line(self):
        self.background.fill(COLOR_BG)
        for col in range(CELL_SIZE, SCREEN_WIDTH, CELL_SIZE):
            self.draw.line(self.background, COLOR_GRID,
                           (col, 0), (col, SCREEN_HEIGHT))
        for row in range(CELL_SIZE, SCREEN_HEIGHT, CELL_SIZE):
            self.draw.line(self.background, COLOR_GRID,
                           (0, row), (SCREEN_WIDTH, row))

    def show_score(self):
        text = "Apple %d" % self.apple_count
        self.draw_text(text, (0, 0), COLOR_SCORE)

        if not self.snake.alive:
            self.draw_text(" GAME OVER ",
                           (SCREEN_WIDTH // 2 - 65, SCREEN_HEIGHT // 2 - 40),
                           COLOR_DEFEAT, WHITE)
            self.draw_text(" PRESS R TO RESTART ",
                           (SCREEN_WIDTH // 2 - 120, SCREEN_HEIGHT // 2),
                           GREY, LIGHT_GREY)

        if not self.running and self.snake.alive:
            self.draw_text(" GAME PAUSED ",
                           (SCREEN_WIDTH // 2 - 80, SCREEN_HEIGHT // 2 - 20),
                           LIGHT_GREY, DARK_GREY)

    def restart(self):
        if not self.snake.alive:
            self.apple_count = 0
            self.apple.drop()
            self.snake.reborn()
            self.running = True