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)
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
class GameScene(QGraphicsScene): """Custom QGraphicsScene""" def __init__(self, food_count, update_rate, statistic_list): super().__init__() self.sceneRect = QRectF(0.0, 0.0, 500.0, 500.0) self.setSceneRect(self.sceneRect) self.width = int(self.width()) self.height = int(self.height()) self.snake = None self.timer = QTimer() self.timer_snake = QTimer() self.update_rate = update_rate self.food_count = food_count self.food_items = [] self.score = 0 self.statistic_list = statistic_list self.time = 0 self.game_status = False self.startUpdate() # Signals self.timer.timeout.connect(self.update) self.timer_snake.timeout.connect(self.killSnake) def startUpdate(self): self.timer.start(self.update_rate) self.timer_snake.start(self.update_rate) self.game_status = True def pauseUpdate(self): self.timer.stop() self.timer_snake.stop() def drawGrid(self): self.addRect(0, 0, 500, 500, QPen(QColor('white'), 1), QBrush(QColor('white'))) for row in range(0, self.height + 20, 20): for column in range(0, self.width + 20, 20): self.addLine(0, row, self.width, row, QPen(QColor('black'), 1)) self.addLine(column, 0, column, self.height, QPen(QColor('black'), 1)) def createSnake(self): self.snake = Snake(0.0, 0.0) self.createFood() self.time = time.time() return self.snake def update(self): self.clear() self.drawGrid() self.placeFood() self.updateScore() self.catchFood() self.drawSnake() def killSnake(self): if self.snake.x not in range(0, self.width) or \ self.snake.y not in range(0, self.height) or\ self.snake.pos() in self.snake.positions[2:]: self.time = round(time.time() - self.time, 3) self.statistic_list.addItem('Game finished: {} s. Score: {}'\ .format(self.time, self.score)) self.pauseUpdate() self.game_status = False def createFood(self): for item in range(self.food_count): random_x = abs(round(randint(0, self.width) / 20) * 20 - 20) random_y = abs(round(randint(0, self.height) / 20) * 20 - 20) food_item = QRectF(random_x, random_y, 20, 20) while food_item in self.food_items or food_item in self.snake.positions: random_x = abs(round(randint(0, self.width) / 20) * 20 - 20) random_y = abs(round(randint(0, self.height) / 20) * 20 - 20) food_item = QRectF(random_x, random_y, 20, 20) self.food_items.append(food_item) def placeFood(self): for item in self.food_items: self.addRect(item, QPen(QColor('white'), 1), QBrush(QColor('red'))) def updateScore(self): self.addItem(ScoreText('Score: {}'.format(self.score), -30, -40)) def catchFood(self): for index, item in enumerate(self.food_items): if self.snake.pos() == (item.x(), item.y()): self.score += 10 del self.food_items[index] self.snake.eat() if not self.food_items: self.createFood() def drawSnake(self): for index, pos in enumerate(self.snake.positions): self.addRect(pos[0], pos[1], 20, 20, QPen(QColor('white'), 1), QBrush(QColor('green'))) if index == 0: self.addRect(pos[0] + 8, pos[1] + 8, 4, 4, QPen(QColor('yellow'), 1), QBrush(QColor('yellow'))) def getStatus(self): return self.game_status
def test_eat(): snake = Snake() before = snake.length snake.eat() assert before == snake.length - 1
f = open(args.dataset, 'w') f.write( 'w0,w1,w2,w3,a0,a01,a1,a12,a2,a23,a3,a30,b0,b01,b1,b12,b2,b23,b3,b30,direction' + '\n') rows = 0 pbar = tqdm(total=100, position=0, leave=True) snake = Snake(config) apple = Apple(config) while rows < args.count: # 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) if snake.collision_with_wall(snake.direction): direction = (snake.direction + 1) % 4 if snake.collision_with_wall(direction): direction = (snake.direction - 1) % 4