Exemplo n.º 1
0
    def on_init(self):
        pygame.init()
        self._display_surf = pygame.display.set_mode(
            self.size, pygame.HWSURFACE | pygame.DOUBLEBUF)
        self._display_surf.fill(BLACK)
        self._running = True

        # Create Score Board
        self.score = 0
        self.displayScore(self.score, 45)

        # Create Initial Food
        self.initFood = Food(RED, 10, 10)
        self._display_surf.blit(self.initFood.image, self.initFood.rect)

        # display the initial Snake array
        for i in range(len(self.snake)):
            self._display_surf.blit(self.snake[i].image, self.snake[i].rect)

        # display the board
        for i in range(len(self.boarder)):
            self._display_surf.blit(self.boarder[i].image,
                                    self.boarder[i].rect)

        pygame.display.update()
Exemplo n.º 2
0
    def gameRestart(self):
        # Erase the Board
        self._display_surf = pygame.display.set_mode(
            self.size, pygame.HWSURFACE | pygame.DOUBLEBUF)
        self._display_surf.fill(BLACK)
        self._running = True

        # Recreate the Snake
        self.snake = [
            Snake(WHITE, 10, 10, 150, 260),
            Snake(WHITE, 10, 10, 140, 260),
            Snake(WHITE, 10, 10, 130, 260)
        ]

        # Create Score Board
        self.score = 0
        self.displayScore(self.score, 45)

        # Create Initial Food
        self.initFood = Food(RED, 10, 10)
        self._display_surf.blit(self.initFood.image, self.initFood.rect)

        # set current move to nothing
        self.move = ''

        # draw in the boarder
        for i in range(len(self.boarder)):
            self._display_surf.blit(self.boarder[i].image,
                                    self.boarder[i].rect)

        # display the initial Snake array
        for i in range(len(self.snake)):
            self._display_surf.blit(self.snake[i].image, self.snake[i].rect)
        pygame.display.update()
Exemplo n.º 3
0
    def eatFood(self):
        # Create a new Food at random location and display it
        self.initFood = Food(RED, 10, 10)
        self._display_surf.blit(self.initFood.image, self.initFood.rect)

        # Create Score Board
        self.score += 1
        self.displayScore(self.score, 45)

        # for i in range(len(self.snake)):
        #     self._display_surf.blit(self.snake[i].image, self.snake[i].rect)
        #
        # Store the last and second to last blocks of the snake
        lastSnakeBlock = self.snake[-1]
        secondToLastBlock = self.snake[-2]

        # if the last two blocks are on the same horizontal line and the last block is to the left of the
        # second to last block, add a block to the left side of the last block
        if lastSnakeBlock.rect.y == secondToLastBlock.rect.y and lastSnakeBlock.rect.x < secondToLastBlock.rect.x:
            newX = lastSnakeBlock.rect.x - 10
            newSnakeBlock = Snake(lastSnakeBlock.color, lastSnakeBlock.width,
                                  lastSnakeBlock.height, newX,
                                  lastSnakeBlock.rect.y)
            self.snake.append(newSnakeBlock)

        # if the last two blocks are on the same horizontal line and the last block is to the right of the
        # second to last block, add a block to the right side of the last block
        if lastSnakeBlock.rect.y == secondToLastBlock.rect.y and lastSnakeBlock.rect.x > secondToLastBlock.rect.x:
            newX = lastSnakeBlock.rect.x + 10
            newSnakeBlock = Snake(lastSnakeBlock.color, lastSnakeBlock.width,
                                  lastSnakeBlock.height, newX,
                                  lastSnakeBlock.rect.y)
            self.snake.append(newSnakeBlock)

        # if the last two blocks are on the same vertical line and the last block is above the
        # second to last block, add a block above the last block
        if lastSnakeBlock.rect.x == secondToLastBlock.rect.x and lastSnakeBlock.rect.y < secondToLastBlock.rect.y:
            newY = lastSnakeBlock.rect.y - 10
            newSnakeBlock = Snake(lastSnakeBlock.color, lastSnakeBlock.width,
                                  lastSnakeBlock.height, lastSnakeBlock.rect.x,
                                  newY)
            self.snake.append(newSnakeBlock)

        # if the last two blocks are on the same vertical line and the last block is below the
        # second to last block, add a block below the last block
        if lastSnakeBlock.rect.x == secondToLastBlock.rect.x and lastSnakeBlock.rect.y > secondToLastBlock.rect.y:
            newY = lastSnakeBlock.rect.y + 10
            newSnakeBlock = Snake(lastSnakeBlock.color, lastSnakeBlock.width,
                                  lastSnakeBlock.height, lastSnakeBlock.rect.x,
                                  newY)
            self.snake.append(newSnakeBlock)

        for i in range(len(self.snake)):
            self._display_surf.blit(self.snake[i].image, self.snake[i].rect)
Exemplo n.º 4
0
    def on_init(self):
        pygame.init()
        self._display_surf = pygame.display.set_mode(
            self.size, pygame.HWSURFACE | pygame.DOUBLEBUF)
        self._display_surf.fill(BLACK)
        self._running = True

        self.i = 0

        self.child = []

        self.population = {}

        self.fitness = 0

        self.stepsWithNoChange = 0

        # Create Score Board
        self.score = 0
        self.displayScore(self.score, 45)

        # Generate Generation Count
        self.generationCount = 0

        # Create Initial Food
        self.initFood = Food(RED, 10, 10)
        self._display_surf.blit(self.initFood.image, self.initFood.rect)

        # display the initial Snake array
        for i in range(len(self.snake)):
            self._display_surf.blit(self.snake[i].image, self.snake[i].rect)

        # display the board
        for i in range(len(self.boarder)):
            self._display_surf.blit(self.boarder[i].image,
                                    self.boarder[i].rect)

        pygame.display.update()
Exemplo n.º 5
0
from Classes import Snake, Food
from SetupScreen import WINDOW_HEIGHT, WINDOW_WIDTH, setup_window
import curses

W = setup_window()
S = Snake(Window=W)
F = Food(Window=W, Snake=S)
i = 0
while True:
    i += 1

    W.clear()

    W.border(0)

    # Write the game score on the screen
    W.addstr(WINDOW_HEIGHT - 1, int(WINDOW_WIDTH * 1 / 10), S.game_score)

    W.getch()
    if i > 10:
        break

print("{}".format(S.head_coords))
curses.endwin()