def game_loop(): while True: display.fill((0, 0, 0)) maze.draw(display) ball.draw(display) ball.handle_input() ball.step(maze) pygame.display.update() clock.tick(60)
def main(): pygame.init() # fenetre = pygame.display.set_mode((width, height), pygame.FULLSCREEN) fenetre = pygame.display.set_mode((width, height), pygame.RESIZABLE) fenetre.fill(BLACK) x, y = get_start() player_position = (x + 32, y + 32) end_rect, player = maze.build_game(player_position, x, y) maze.draw(fenetre, end_rect, player) # time in millisecond from start program current_time = pygame.time.get_ticks() # how long to show or hide delay = st.get_system_refresh_period() move_delay = 7000 # time of next change change_time = current_time + delay j = 0 th = mv.save_data() maze.draw(fenetre, end_rect, player) win = False while not win: for event in pygame.event.get(): if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE): pygame.quit() sys.exit() win = maze.update(fenetre, end_rect) current_time = pygame.time.get_ticks() if current_time >= change_time: change_time = current_time + delay st.draw_stimuli(j, fenetre) j += 1 if win: mv.stop(th) win_message(fenetre)
def draw(): surface.fill(BLUE)#background maze.draw(surface) pygame.display.flip()
if is_direction_safe(maze.E, x, y): solution_matrix[x][y] = '→' if find_path(x, y + 1): return True # check if NORTH is safe if is_direction_safe(maze.N, x, y): solution_matrix[x][y] = '↑' if find_path(x - 1, y): return True # check if WEST is safe if is_direction_safe(maze.W, x, y): solution_matrix[x][y] = '←' if find_path(x, y - 1): return True # check if SOUTH is safe if is_direction_safe(maze.S, x, y): solution_matrix[x][y] = '↓' if find_path(x + 1, y): return True solution_matrix[x][y] = 0 return False maze.dig(maze.SIZE[0] // 2, maze.SIZE[1] // 2) maze.draw() # maze.check() backtracking_solution()
def findRoute(df): end = len(df)-1 visited=[] for i in df: visited.append( [False] *len(i) ) a,b=0,0 # route is 2d array having path name route=[] visited[0][0]=True while a!=end or b!=end: if b+1<=end and df[a][b+1]==1 and visited[a][b+1]!="deadend" and visited[a][b+1]!=True: b = b+1 visited[a][b] = True route.append("right") elif a+1<=end and df[a+1][b]==1 and visited[a+1][b]!="deadend" and visited[a+1][b]!=True: a = a+1 visited[a][b] = True route.append("down") elif b-1>=0 and df[a][b-1]==1 and visited[a][b-1]!=True and visited[a][b-1]!="deadend": b-=1 visited[a][b]=True route.append("left") elif a-1>=0 and df[a-1][b]==1 and visited[a-1][b]!=True and visited[a-1][b]!="deadend": a-=1 visited[a][b]=True route.append("up") else: visited[a][b]="deadend" if a-1>=0 and visited[a-1][b]==True: a-=1 route.append("up") elif a+1<=end and visited[a+1][b]==True: a+=1 route.append("down") elif b-1>=0 and visited[a][b-1]==True: b-=1 route.append("left") elif b+1<=end and visited[a][b+1]==True: b+=1 route.append("right") else: print("no route") break route_matrix=[] for i in visited: test=[] for k in i: if k==True: test.append(1) else: test.append(0) route_matrix.append(test) # route matrix is array showing 1 as path 0 as wall for i in route_matrix: print(i) draw(df) draw_path(route)