Example #1
0
def main(stdscr):
    fps = 5
    stdscr.timeout(1000 // fps)
    # hide cursor
    curses.curs_set(0)

    # init head at top left
    border = 2
    stdscr.border(border)
    # h = Head(border, border, stdscr, '@')
    f = Food(border, border, stdscr, '$')
    s = Snake(border, border, stdscr)
    score = 0
    while True:
        stdscr.clear()

        # index of keyboard
        # index = -1 if no key inserted in loop
        stdscr.addstr(1, 10, f'score: {score}      type ESC to exit')
        # h.render()
        f.render()
        s.render()

        key = stdscr.getch()
        log('key:', key)
        s.update(key)
        # ESC == 27
        if s.collid() or key == 27:
            break

        if s.head.coor == f.coor:
            f.reset()
            s.grow()
            score += 1
Example #2
0
def main():
    board_size = 15
    v = View(board_size)
    s = Snake(board_size)

    key_mapping = {
        'H': s.DIR_UP,
        'M': s.DIR_RIGHT,
        'P': s.DIR_DOWN,
        'K': s.DIR_LEFT
    }

    clear = lambda: os.system('cls')
    clear()

    while True:
        if msvcrt.kbhit():
            msvcrt.getch()  # skip 0xe0
            key = msvcrt.getch().decode('utf-8')
            if key in key_mapping:
                if not s.update(key_mapping[key]):
                    return
        else:
            if not s.update(None):
                return
        #print(s.get_food_x_y())
        #print(s.get_coord_list())

        v.update_gameboard(s.get_coord_list(), s.get_food_x_y())
        time.sleep(0.1)
        clear = lambda: os.system('cls')
        clear()
Example #3
0
File: main.py Project: yztcit/Snake
def run_game():
    # 游戏配置信息
    game_setting = Setting()

    # 初始化游戏,并创建一个屏幕对象
    pygame.init()
    fps_clock = pygame.time.Clock()
    # 图标
    icon = pygame.image.load("res/snake.png")
    screen = pygame.display.set_mode(
        [game_setting.screen_width, game_setting.screen_width])
    pygame.display.set_caption("Greedy Snake")
    pygame.display.set_icon(icon)

    # 创建一条蛇
    snake = Snake(game_setting, screen)
    # 创建一个食物
    food = Food(game_setting, screen, snake)

    # 游戏的主循环
    while True:
        gf.keyboard_listener(game_setting, snake)

        snake.update()

        gf.snake_eat_food(snake, food)

        gf.update_screen(game_setting, screen, snake, food)
        fps_clock.tick(game_setting.snake_speed)
Example #4
0
class App:
    windowWidth = 800
    windowHeight = 600
    game = 0
    human = True
    robot = None

    def __init__(self, ml = False):
        self._running = True
        self._display_surf = None
        if ml:
            speed = 10
        else:
            speed = 200
        self.game = Snake(speed)
        self.human = not ml

    def on_init(self):
        pygame.init()
        self._display_surf = pygame.display.set_mode((self.windowWidth, self.windowHeight), pygame.HWSURFACE)

        pygame.display.set_caption('Snake ML')
        self.game.on_init()

        if not self.human:
            self.robot = Robot(100, 1)

        self._running = True

    def on_event(self, event):
        if event.type == QUIT:
            self._running = False

    def on_loop(self):
        pass

    def on_render(self):
        self._display_surf.fill((0,0,255))
        self.game.on_render(self._display_surf)
        pygame.display.flip()

    def on_cleanup(self):
        pygame.quit()

    def on_execute(self):
        if self.on_init() == False:
            self._running = False

        if not self.human:
            population = 10
            print "Starting population generation...",
            sys.stdout.flush()
            genLearn = GeneticLearning(0, population, 0.001, self.game.height * self.game.width, 1)
            genIndex = 0
            self.robot.network = genLearn.generationWeights[genIndex]
            print "done! starting"
            genLearn.onStart(genIndex)

        while( self._running ):
            pygame.event.pump()

            if self.human:
                keys = pygame.key.get_pressed()
            else:
                gameState = self.game.getGameState()
                keys = self.robot.calculateKey(gameState)

            self.game.update(keys)

            keys = pygame.key.get_pressed()
            if (keys[K_ESCAPE]):
                self._running = False

            if not self.human and (self.game.hasWon() <= 1 or self.game.player.dead):
                genLearn.onComplete(genIndex, self.game.score)

                if genIndex == population - 1:
                    genLearn.onAllComplete(cross = False, createNew=1)
                    genIndex = 0
                    print "All done, scores:"
                    print genLearn.generationResult
                else:
                    genIndex += 1
                self.robot.network = genLearn.generationWeights[genIndex]

                self.game.reset()

                pygame.event.pump()
                keys = pygame.key.get_pressed()
                if (keys[K_ESCAPE]):
                    self._running = False

                genLearn.onStart(genIndex)

            self.on_render()
        self.on_cleanup()
class SnakeGame:
    FONT_SIZE = 18
    PRIME_NUMBERS = [
        2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67,
        71, 73, 79, 83, 89, 97
    ]
    BLOCK_SIZE = 20  # Size of blocks

    def __init__(self, screen_size):
        self.screen_size = screen_size // self.BLOCK_SIZE  # The width and height of the screen in number of blocks

    def mul(self, t, n):
        return (t[0] * n, t[1] * n)

    def on_grid_random(self):
        """
        This function calculate a random position for a object on the screen

        :returns:
        tuple: Random position
        """
        x = random.randint(0, self.screen_size - 2)
        y = random.randint(0, self.screen_size - 2)
        return (x, y)

    def collision(self, c1, c2):

        return (c1[0] == c2[0]) and (c1[1] == c2[1])

    def prime_apple_randomizer(self):
        """
        This function choose a random prime number from the self.PRIME_NUMBERS list
        :return:
        int: Random prime number
        """
        number = random.choice(self.PRIME_NUMBERS)

        return int(number)

    def normal_apple_randomizer(self):
        """
        This function chosse a not-prime random number between 0 and 99
        :return:
        int:
        """
        number = random.randint(0, 99)
        while number in self.PRIME_NUMBERS:
            number = random.randint(0, 99)
        return number

    def main(self, screen):
        screen.fill((0, 0, 0))
        tutorial1 = """Geraldo é uma cobrinha sapeca e que vive faminta. Sua comida favorita é a fruta maça."""
        tutorial2 = """Porém Geraldo é bem especifico, ele só come maçãs que são de numero primo no pé."""
        tutorial3 = """INSTRUÇÔES"""
        tutorial4 = """Para isso ajude Geraldo a se alimentar capturando apenas as  maçãs com numeros primos"""
        tutorial5 = """E utilizando as setas do teclado para se mover"""

        ##### TUTORIAL ########################
        score_font = pygame.font.Font('freesansbold.ttf', 13)
        score_screen = score_font.render(f'{tutorial1}', True, (255, 255, 255))
        score_rect = score_screen.get_rect()
        score_rect.midtop = (600 / 2, 10)
        screen.blit(score_screen, score_rect)
        score_font = pygame.font.Font('freesansbold.ttf', 13)
        score_screen = score_font.render(f'{tutorial2}', True, (255, 255, 255))
        score_rect = score_screen.get_rect()
        score_rect.midtop = (600 / 2, 50)
        screen.blit(score_screen, score_rect)
        score_font = pygame.font.Font('freesansbold.ttf', 13)
        score_screen = score_font.render(f'{tutorial3}', True, (255, 255, 255))
        score_rect = score_screen.get_rect()
        score_rect.midtop = (600 / 2, 100)
        screen.blit(score_screen, score_rect)
        score_font = pygame.font.Font('freesansbold.ttf', 13)
        score_screen = score_font.render(f'{tutorial4}', True, (255, 255, 255))
        score_rect = score_screen.get_rect()
        score_rect.midtop = (600 / 2, 150)
        screen.blit(score_screen, score_rect)
        score_font = pygame.font.Font('freesansbold.ttf', 13)
        score_screen = score_font.render(f'{tutorial5}', True, (255, 255, 255))
        score_rect = score_screen.get_rect()
        score_rect.midtop = (600 / 2, 200)
        screen.blit(score_screen, score_rect)
        pygame.display.update()
        pygame.time.wait(9000)

        snake_skin = pygame.Surface((self.BLOCK_SIZE, self.BLOCK_SIZE))
        snake_skin.fill((255, 255, 255))
        self.snake = Snake(snake_skin, self.screen_size)

        prime_apple_sprite = pygame.Surface((self.BLOCK_SIZE, self.BLOCK_SIZE))
        prime_apple_sprite.fill((255, 0, 0))
        prime_apple = Apple(
            prime_apple_sprite,  # sprite
            self.on_grid_random(),  # pos
            self.prime_apple_randomizer(),  # num
            pygame.font.SysFont("arial", self.FONT_SIZE)  # font
        )
        normal_apple_sprite = pygame.Surface(
            (self.BLOCK_SIZE, self.BLOCK_SIZE))
        normal_apple_sprite.fill((255, 0, 0))
        normal_apple = Apple(
            normal_apple_sprite,  # sprite
            self.on_grid_random(),  # pos
            self.normal_apple_randomizer(),  # num
            pygame.font.SysFont("arial", self.FONT_SIZE)  # font
        )
        clock = pygame.time.Clock()

        while True:
            """
            This is the main looping of the game, resposible for update the screen,snake and apples
            """
            clock.tick(10 if self.snake.fast else 5)
            for event in pygame.event.get():
                if event.type == QUIT:
                    pygame.quit()
                    exit()

                if event.type == KEYDOWN:
                    self.snake.listen(event)
                    if event.key == K_ESCAPE:
                        return

            if self.snake.collision(prime_apple.pos):
                prime_apple.change(self.on_grid_random(),
                                   self.prime_apple_randomizer())
                normal_apple.change(self.on_grid_random(),
                                    self.normal_apple_randomizer())
                self.snake.grow()
                self.snake.counter = self.snake.counter + 1

            if self.snake.collision(normal_apple.pos):
                self.snake.snake_reset()
                prime_apple.change(self.on_grid_random(),
                                   self.prime_apple_randomizer())
                normal_apple.change(self.on_grid_random(),
                                    self.normal_apple_randomizer())

            if self.snake.boundry_collision(
            ):  # Check the collision with boudaries
                game_over = True
                self.game_over_screen(screen)
                return

            self.snake.update()

            screen.fill((0, 0, 0))

            prime_apple.drawn(screen, 20)
            normal_apple.drawn(screen, 20)
            self.snake.drawn(screen, self.BLOCK_SIZE)

            pygame.display.update()

    def game_over_screen(self, screen):
        """
        This is the Game over menu looping. Responsible for the game-over screen and score
        """
        while True:
            game_over_font = pygame.font.Font('freesansbold.ttf', 75)
            game_over_screen = game_over_font.render(f'Game Over', True,
                                                     (255, 255, 255))
            game_over_rect = game_over_screen.get_rect()
            game_over_rect.midtop = (600 / 2, 10)
            screen.blit(game_over_screen, game_over_rect)
            score_font = pygame.font.Font('freesansbold.ttf', 30)
            score_screen = score_font.render(
                f'Pontuação final: {self.snake.counter}', True,
                (255, 255, 255))
            score_rect = score_screen.get_rect()
            score_rect.midtop = (600 / 2, 100)
            screen.blit(score_screen, score_rect)
            pygame.display.update()
            pygame.time.wait(500)
            while True:
                for event in pygame.event.get():
                    if event.type == QUIT:
                        pygame.quit()
                        exit()
                    elif event.type == KEYDOWN:
                        if event.key == K_RETURN or event.key == K_KP_ENTER:
                            self.main(screen)
                        elif event.key == K_ESCAPE:
                            return