def play(self): global rows snake = Snake(config) apple = Apple(config) clock = pygame.time.Clock() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: exit() # Detect collision with apple if snake.x == apple.x and snake.y == apple.y: snake.eat() apple = Apple(config) # collision with body for part in snake.body: if snake.x == part[0] and snake.y == part[1]: snake = Snake(config) direction = snake.vision(apple) snake.pre_direction = snake.direction snake.decision(direction) # collision with wall if snake.collision_with_wall(snake.direction): direction = (snake.direction + 1) % 4 if snake.collision_with_wall(direction): direction = (snake.direction - 1) % 4 if snake.collision_with_wall(direction): snake = Snake(config) snake.direction = direction snake.move() self.display.fill(self.color) pygame.draw.rect(self.display, Color.black, ((0, 0), (config.game_w, config.game_h)), config.wall_offset) apple.draw(self.display) snake.draw(self.display) score = self.font.render(f'Score: {snake.score}', True, Color.black) score_rect = score.get_rect(center=(config.game_w / 2, config.game_h - 10)) self.display.blit(score, score_rect) pygame.display.update() clock.tick(config.fps)
class Board(): def __init__(self, settings): self.boardWidth = getSetting(settings, "boardWidth", 10) self.boardHeight = getSetting(settings, "boardHeight", 10) self.cellSize = getSetting(settings, "cellSize", 10) self.snake = Snake(settings, self.boardWidth / 2, self.boardHeight / 2) self.isDead = False self.item = Item(settings) self.reinitItem() def draw(self, screen): self.move() screen.fill(style.BoardFillColor) for row in range(0, self.boardWidth): for column in range(0, self.boardHeight): x = row * self.cellSize y = column * self.cellSize color = style.BoardFloorColor if self.isDead: color = style.BoardDeadColor pygame.draw.rect(screen, color, (x, y, self.cellSize, self.cellSize), 1) self.item.draw(screen) self.snake.draw(screen) def move(self): if self.snake.direction == Direction.Up: self.tryMove(self.snake.x, self.snake.y - 1) elif self.snake.direction == Direction.Right: self.tryMove(self.snake.x + 1, self.snake.y) elif self.snake.direction == Direction.Down: self.tryMove(self.snake.x, self.snake.y + 1) elif self.snake.direction == Direction.Left: self.tryMove(self.snake.x - 1, self.snake.y) def tryMove(self, x, y): self.isDead = ((x < 0) or (x >= self.boardWidth) or (y < 0) or (y >= self.boardHeight) or self.snake.isContains(x, y)) if self.isDead: return isItemEaten = (x == self.item.x) and (y == self.item.y) if isItemEaten: self.item.eatenUpCount = self.item.eatenUpCount + 1 self.reinitItem() self.snake.moveHead(x, y, isItemEaten) def reinitItem(self): self.item.x = random.randint(0, self.boardWidth - 1) self.item.y = random.randint(0, self.boardHeight - 1)
def play(self): global snake, apple snake = Snake(config) apple = Apple(config) clock = pygame.time.Clock() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: exit() # Detect collision with apple if snake.x == apple.x and snake.y == apple.y: snake.eat() apple = Apple(config) snake.x_change_old, snake.y_change_old = snake.x, snake.y with torch.no_grad(): data = get_data() data = data.reshape(1, 20) data = torch.tensor(data) result = model(data) snake.direction = np.argmax(result) snake.move() self.display.fill(self.color) pygame.draw.rect(self.display, Color.black, ((0, 0), (config.game_w, config.game_h)), 10) apple.draw(self.display) snake.draw(self.display) if snake.x < 0 or snake.y < 0 or snake.x > config.game_w or snake.y > config.game_h: self.play() score = self.font.render(f'Score: {snake.score}', True, Color.black) score_rect = score.get_rect(center=(config.game_w / 2, config.game_h - 10)) self.display.blit(score, score_rect) pygame.display.update() clock.tick(30) # fps
if snake.alive: snake.simulate() spider.simulate() if snake.head_location == food.location: snake.eat() food.consumed() HUD.add_score(DIFFICULTY) if snake.head_location == spider.location: HUD.add_score(spider.score()) snake.eat() spider.consumed() # --- Drawing if snake.alive: food.draw(screen) snake.draw(screen) spider.draw(screen) HUD.draw(screen) else: session_high_score = HUD.score if HUD.score > session_high_score else session_high_score font = pygame.font.Font('freesansbold.ttf', 32) text = font.render(f"Snake Dead. Score: {HUD.score}", True, RED) text2 = font.render(f"Best Score: {session_high_score}", True, RED) screen.blit(text, (10, 10)) screen.blit(text2, (10, 60)) # --- Go ahead and update the screen with what we've drawn. pygame.display.flip() # --- Limit to 60 frames per second clock.tick(DIFFICULTY)