def reset(self): #Initiliazing game state self.direction = config.Direction.RIGHT self.head = config.Loc(self.width/2, self.height/2) self.snake = [self.head, config.Loc(self.head.x - config.BLOCK_SIZE, self.head.y),config.Loc(self.head.x - (2 * config.BLOCK_SIZE), self.head.y)] self.score = 0 self.food = None self.placeFood() self.gameIter = 0
def placeFood(self): x = np.random.randint(0, (self.width - config.BLOCK_SIZE) // config.BLOCK_SIZE) * config.BLOCK_SIZE y = np.random.randint(0, (self.height - config.BLOCK_SIZE) // config.BLOCK_SIZE) * config.BLOCK_SIZE self.food = config.Loc(x, y) if self.food in self.snake: self.placeFood()
def getState(self, snakeGame): head = snakeGame.snake[0] pointLeft = config.Loc(head.x - config.BLOCK_SIZE, head.y) pointRight = config.Loc(head.x + config.BLOCK_SIZE, head.y) pointDown = config.Loc(head.x, head.y + config.BLOCK_SIZE) pointUp = config.Loc(head.x, head.y - config.BLOCK_SIZE) dirLeft = snakeGame.direction == config.Direction.LEFT dirRight = snakeGame.direction == config.Direction.RIGHT dirUp = snakeGame.direction == config.Direction.UP dirDown = snakeGame.direction == config.Direction.DOWN state = [ (dirRight and snakeGame.onCollisionEnter2D(pointRight)) or (dirLeft and snakeGame.onCollisionEnter2D(pointLeft)) or (dirUp and snakeGame.onCollisionEnter2D(pointUp)) or (dirDown and snakeGame.onCollisionEnter2D(pointDown)), #Danger from right (dirRight and snakeGame.onCollisionEnter2D(pointDown)) or (dirLeft and snakeGame.onCollisionEnter2D(pointUp)) or (dirUp and snakeGame.onCollisionEnter2D(pointRight)) or (dirDown and snakeGame.onCollisionEnter2D(pointLeft)), #Danger from left (dirRight and snakeGame.onCollisionEnter2D(pointUp)) or (dirLeft and snakeGame.onCollisionEnter2D(pointDown)) or (dirUp and snakeGame.onCollisionEnter2D(pointLeft)) or (dirDown and snakeGame.onCollisionEnter2D(pointRight)), #Move direction dirLeft, dirRight, dirUp, dirDown, #Food location snakeGame.food.x < snakeGame.head.x, #Food left snakeGame.food.x > snakeGame.head.x, #Food right snakeGame.food.y < snakeGame.head.y, #Food up snakeGame.food.y > snakeGame.head.y #Food down ] return np.array(state, dtype=int)
def onCollisionEnter2D(self, loc=None): #Check if snake is on the boundary (i.e. wall is hit by snake) if loc is None: loc = config.Loc(self.head.x, self.head.y) if loc.x > self.width - config.BLOCK_SIZE or loc.x < 0 or loc.y > self.height - config.BLOCK_SIZE or loc.y < 0: return True #If snake hits itself if loc in self.snake[1:]: return True #If any of the conditions are not satisfied, return false return False
def moveSnake(self, dir): clockWise = [config.Direction.RIGHT, config.Direction.DOWN, config.Direction.LEFT, config.Direction.UP] idx = clockWise.index(self.direction) if not self.humanPlay: if np.array_equal(dir, [1, 0, 0]): # No changes newDir = clockWise[idx] elif np.array_equal(dir, [0, 1, 0]): nextIdx = (idx + 1) % 4 # Take a right turn newDir = clockWise[nextIdx] else: #i.e [0, 0, 1] nextIdx = (idx - 1) % 4 newDir = clockWise[nextIdx] self.direction = newDir x = self.head.x y = self.head.y if self.direction == config.Direction.RIGHT: x += config.BLOCK_SIZE elif self.direction == config.Direction.LEFT: x -= config.BLOCK_SIZE elif self.direction == config.Direction.DOWN: y += config.BLOCK_SIZE elif self.direction == config.Direction.UP: y -= config.BLOCK_SIZE self.head = config.Loc(x, y)