what is the best way to use loops to detect events while the main loop is running?
Posted
by
yao jiang
on Game Development
See other posts from Game Development
or by yao jiang
Published on 2012-10-16T23:09:14Z
Indexed on
2012/10/17
5:27 UTC
Read the original article
Hit count: 322
I am making an "game" that has pathfinding using pygame. I am using Astar algo.
I have a main loop which draws the whole map. In the loop I check for events. If user press "enter" or "space", random start and end are selected, then animation starts and it will try to get from start to end.
My draw function is stupid as hell right now, it works as expected but I feel that I am doing it wrong. It'll draw everything to the end of the animation. I am also detecting events in there as well. What is a better way of implementing the draw function such that it will draw one "step" at a time while checking for events?
animating = False;
while loop:
check events:
if not animating:
# space or enter press will choose random start/end coords
if enter_pressed or space_pressed:
start, end = choose_coords
route = find_route(start, end)
draw(start, end, grid, route)
else:
# left click == generate an event to block the path
# right click == user can choose a new destination
if left_mouse_click:
gen_event()
reroute()
elif right_mouse_click:
new_end = new_end()
new_start = current_pos()
route = find_route(new_start, new_end)
draw(new_start, new_end, grid, route)
# draw out the grid
def draw(start, end, grid, route_coord):
# draw the end coords
color = red;
pick_image(screen, color, width*end[1],height*end[0]);
pygame.display.flip();
# then draw the rest of the route
for i in range(len(route_coord)):
# pausing because we want animation
time.sleep(speed);
# get the x/y coords
x,y = route_coord[i];
event_on = False;
if grid[x][y] == 2:
color = green;
elif grid[x][y] == 3:
color = blue;
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 3:
print "destination change detected, rerouting";
# get mouse position, px coords
pos = pygame.mouse.get_pos();
# get grid coord
c = pos[0] // width;
r = pos[1] // height;
grid[r][c] = 4;
end = [r, c];
elif event.button == 1:
print "user generated event";
pos = pygame.mouse.get_pos();
# get grid coord
c = pos[0] // width;
r = pos[1] // height;
# mark it as a block for now
grid[r][c] = 1;
event_on = True;
if check_events([x,y]) or event_on: # there is an event
# mark it as a block for now
grid[y][x] = 1;
pick_image(screen, event_x, width*y, height*x);
pygame.display.flip();
# then find a new route
new_start = route_coord[i-1];
marked_grid, route_coord = find_route(new_start, end, grid);
draw(new_start, end, grid, route_coord);
return; # just end draw here so it wont throw the "index out of range" error
elif grid[x][y] == 4:
color = red;
pick_image(screen, color, width*y, height*x);
pygame.display.flip();
# clear route coord list, otherwise itll just add more unwanted coords
route_coord_list[:] = [];
© Game Development or respective owner