Пример #1
0
    def play_game(self):
        """Main method of this Snake. Initializes
        """

        # randomly set starting position, and generate food
        self.snake.append(snake_node.SnakeNode(random.randint(0, self.size - 1),
                                    random.randint(0, self.size - 1)))
        self.generate_food()

        # initialize screen using curses
        if self.use_curses:
            screen = curses.initscr()
            curses.start_color()
            screen.timeout(100)
            curses.noecho()

            # main while loop to continue until player is dead
            while not self.dead:
                # print the UI
                if self.use_curses:
                    screen.clear()
                    screen.addstr(0, 0, "Choose W, A, S, D to move, or Q to quit.",
                                  curses.A_BOLD)
                    screen.addstr(1, 0, self.print_board())
                    screen.refresh()
                else:
                    print(print_board())

                # if ate, make new food
                if self.did_eat:
                    self.generate_food()

                # handle user input, if unchanged, use last input
                inp = screen.getch()
                if inp != -1:
                    self.input = inp

                # if input is 'q', quit game
                if self.input == ord('q'):
                    break

                # check if input is good input
                elif self.input not in [ord('w'), ord('a'), ord('s'), ord('d'), -1]:
                    screen.addstr(0, self.size + 3, "Invalid Input")
                elif inp != -1:
                    self.input = inp

                self.update()

            # print "Game Over!" for 3 seconds
            screen.clear()
            screen.addstr(0, 0, "Game Over!", curses.A_BOLD)
            screen.refresh()
            curses.napms(3000)

            # end window
            curses.endwin()
Пример #2
0
def test_print_board():
    """Tests Snake.py::print_board() by creating a sample board and testing
    if the ouput is the same as the board.
    """
    test_game = snake.Snake(5, use_curses=False)
    test_game.snake.append(snake_node.SnakeNode(2, 2))
    assert test_game.print_board() == "Score: 0\n==============\n||      " + \
    "    ||\n||          ||\n||    O     ||\n||          ||\n||          " + \
    "||\n==============\n"
Пример #3
0
def test_generate_food():
    """Tests Snake.py::generate_food() by calling it and checking if one has
    been generated.
    """
    test_game = snake.Snake(5, use_curses=False)
    number_of_food = 0
    test_game.snake.append(snake_node.SnakeNode(2, 2))
    test_game.generate_food()
    index = 0
    while index < 25:
        if test_game.board[index] == 1:
            number_of_food += 1
        index += 1

    assert number_of_food == 1
Пример #4
0
def test_update():
    """Tests Snake.py::update() by creating a Snake and moving it in multiple
    directions and checking if those are translated onto the board.
    """
    test_game = snake.Snake(5, use_curses=False)
    test_game.snake.append(snake_node.SnakeNode(2, 2))
    assert test_game.print_board() == "Score: 0\n==============\n||      " + \
    "    ||\n||          ||\n||    O     ||\n||          ||\n||          " + \
    "||\n==============\n"
    test_game.input = ord("d")
    test_game.update()
    assert test_game.print_board() == "Score: 0\n==============\n||      " + \
    "    ||\n||          ||\n||      O   ||\n||          ||\n||          " + \
    "||\n==============\n"
    test_game.input = ord("w")
    test_game.update()
    assert test_game.print_board() == "Score: 0\n==============\n||      " + \
    "    ||\n||      O   ||\n||          ||\n||          ||\n||          " + \
    "||\n==============\n"
Пример #5
0
    def update(self):
        """Updates Snake's position based on user input
        """
        # if snake is one node
        if len(self.snake) == 1:
            if self.did_eat:
                self.snake.append(snake_node.SnakeNode(self.snake[0].pos[0],
                                                       self.snake[0].pos[1]))
                self.did_eat = False

            # move head
            if   self.input == ord('w'):
                self.snake[0].pos[1] -= 1
            elif self.input == ord('a'):
                self.snake[0].pos[0] -= 1
            elif self.input == ord('s'):
                self.snake[0].pos[1] += 1
            else:
                self.snake[0].pos[0] += 1

            # check if dead
            if self.calc_index(self.snake[0].pos) > self.size**2 - 1:
                self.dead = True
                return

            # check if ate
            if self.board[self.calc_index(self.snake[0].pos)] == 1:
                self.score += 1
                self.did_eat = True

        # if snake is multiple nodes
        else:
            # move from tail to head
            i = len(self.snake) - 1
            while i != 0:
                self.snake[i].pos = self.snake[i - 1].pos

                # if tail and did eat
                if i == len(self.snake) - 1 and self.did_eat:
                    # add new SnakeNode to self.snake
                    self.snake.append(snake_node.SnakeNode(self.snake[i].pos[0],
                                                self.snake[i].pos[1]))
                    self.did_eat = False
                i -= 1

            # move head by creating new SnakeNode for head,
            # for each input, check if out of bounds
            newHead = snake_node.SnakeNode(self.snake[0].pos[0],
                                           self.snake[0].pos[1])
            if   self.input == ord('w'):
                newHead.pos[1] -= 1
                if newHead.pos[1] < 0:
                    newHead.pos[1] = self.size - 1

            elif self.input == ord('a'):
                newHead.pos[0] -= 1
                if newHead.pos[0] < 0:
                    newHead.pos[0] = self.size - 1

            elif self.input == ord('s'):
                newHead.pos[1] += 1
                if newHead.pos[1] > self.size - 1:
                    newHead.pos[1] = 0

            else:
                newHead.pos[0] += 1
                if newHead.pos[0] > self.size - 1:
                    newHead.pos[0] = 0

            self.snake[0] = newHead

            # check if dead
            for node in self.snake[1:]:
                if node.pos == newHead.pos:
                    self.dead = True
                    return
            if self.calc_index(newHead.pos) > self.size**2 - 1:
                self.dead = True
                return

            # check if ate
            if self.board[self.calc_index(self.snake[0].pos)] == 1:
                self.did_eat = True
                self.score += 1