def debug_window(): from graphics import GraphWin win = GraphWin('Floor', SCREEN_WIDTH_PX, SCREEN_HEIGHT_PX) win.setBackground("black") for circle in circle_grid(): for [x, y] in circle.points(factor=1.): win.plotPixel(x, y, "white") # for circle in circle_grid(): # for [x, y] in circle.points(factor=2.): # win.plotPixel(x, y, "white") for donut_slice in donut_grid(): for [x, y] in donut_slice: win.plotPixel(x, y, "white") sleep(0.001) # gaze = RandomPoint.pick() # stimulus = random.choice(STIMULI_COORDENATES) # for i in np.arange(0,1000,0.01): # gaze = gaze.add(gaze.pick(r=1)) # x, y = (gaze.x*10)+stimulus[0]+50, (gaze.y*6)+stimulus[1]+50 # win.plotPixel(x, y, "white") # if x > stimulus[0]+100 or x < stimulus[0]: # gaze = RandomPoint.pick() # stimulus = random.choice(STIMULI_COORDENATES) # if y > stimulus[1]+100 or y < stimulus[1]: # gaze = RandomPoint.pick() # stimulus = random.choice(STIMULI_COORDENATES) win.getKey() win.close()
class Game: window = None snake = None food = None def __init__(self): self.window = GraphWin( title="dj-snake", width=TILE_SIZE * BOARD_LENGTH, height=TILE_SIZE * BOARD_LENGTH, ) self.window.setBackground(BACKGROUND_COLOR) for x in range(1, BOARD_LENGTH): for y in range(1, BOARD_LENGTH): self.window.plotPixel(x * TILE_SIZE, y * TILE_SIZE, SNAKE_COLOR) self.snake = Snake(self.window) self.spawnFood() def spawnFood(self): x, y = randint(0, BOARD_LENGTH - 1), randint(0, BOARD_LENGTH - 1) while any(block.coordinates == (x, y) for block in self.snake.blocks): x, y = randint(0, BOARD_LENGTH), randint(0, BOARD_LENGTH) self.food = Block(x, y, self.window, FOOD_COLOR) def checkIllegal(self): headCoordinates = self.snake.blocks[0].coordinates if any(headCoordinates == block.coordinates for block in self.snake.blocks[1:]): return True elif any(x < 0 or x >= BOARD_LENGTH for x in headCoordinates): return True return False def play(self): while True: time.sleep(UPDATE_INTERVAL) self.snake.changeDirection(self.window.checkKey()) if self.snake.blocks[0].coordinates == self.food.coordinates: self.snake.grow() self.food.destroy() self.spawnFood() if not self.checkIllegal(): self.snake.update() self.getEnvironmentParams() else: break def getEnvironmentParams(self): # return information about left, fleft, front, fright and right # 3 possible things detected: wall, snake, food # ie 5 parameters in total delta = dict() if self.snake.direction == 'Up': delta["front"] = (0, -1) delta["left"] = (-1, 0) delta["right"] = (1, 0) delta["fleft"] = (-1, -1) delta["fright"] = (1, -1) elif self.snake.direction == 'Down': delta["front"] = (0, 1) delta["left"] = (1, 0) delta["right"] = (-1, 0) delta["fleft"] = (1, 1) delta["fright"] = (-1, 1) elif self.snake.direction == 'Right': delta["front"] = (1, 0) delta["left"] = (0, -1) delta["right"] = (0, 1) delta["fleft"] = (1, -1) delta["fright"] = (1, 1) elif self.snake.direction == 'Left': delta["front"] = (-1, 0) delta["left"] = (0, 1) delta["right"] = (0, -1) delta["fleft"] = (-1, 1) delta["fright"] = (-1, -1) sight = { "front": "wall", "left": "wall", "right": "wall", "fleft": "wall", "fright": "wall" } for direction, step in delta.items(): scanner = self.snake.blocks[0].coordinates while scanner[0] >= 0 and scanner[0] <= BOARD_LENGTH and scanner[ 1] >= 0 and scanner[1] <= BOARD_LENGTH: scanner = (scanner[0] + step[0], scanner[1] + step[1]) if scanner == self.food.coordinates: sight[direction] = 'food' break elif scanner in [b.coordinates for b in self.snake.blocks]: sight[direction] = 'snake' break params = (1 if sight["left"] == "wall" else 0, 1 if sight["fleft"] == "wall" else 0, 1 if sight["front"] == "wall" else 0, 1 if sight["fright"] == "wall" else 0, 1 if sight["right"] == "wall" else 0, 1 if sight["left"] == "snake" else 0, 1 if sight["fleft"] == "snake" else 0, 1 if sight["front"] == "snake" else 0, 1 if sight["fright"] == "snake" else 0, 1 if sight["right"] == "snake" else 0, 1 if sight["left"] == "food" else 0, 1 if sight["fleft"] == "food" else 0, 1 if sight["front"] == "food" else 0, 1 if sight["fright"] == "food" else 0, 1 if sight["right"] == "food" else 0) return params