python - Continuously capture mouse events with pygame -
i'm using pygame build simple game , have following main loop, mouse()
function capture , process mouse events , keyboard()
keyboard events:
def mainloop(): pygame.event.pump() keyboard(pygame.key.get_pressed()) events = pygame.event.get() mouse(events) event in events: if event.type == pygame.quit: pygame.quit() return false return true
when player clicks on tile have following function called mouse()
:
def objreach(obj, pos): try: path = obj.reach(pos, move=false) # a* (path-finding algorithm) step in path: sleep(1.0/obj.speed) objmove(obj, step) except exception e: sendmsg(str(e))
the problem is, while object walking path (while for
loop running) mouse events don't captured, if player clicks on tile in middle of way, nothing happens. want player able change paths.
i tried using mainloop
inside for
loop, partially effective - captures mousebuttonup
event, not both , down events, necessary determine if player merely clicking or dragging , dropping. here's mouse()
function clarify:
def mouse(events): global clickpos global releasepos event in events: if event.type == mousebuttondown: clickpos = getpos(pygame.mouse.get_pos()) # getpos() transforms screen coordinates in game coordinates elif event.type == mousebuttonup: releasepos = getpos(pygame.mouse.get_pos()) if event.button == main_button: # simple click if clickpos == releasepos: if player.privilege > 1: objmove(player, getpos(pygame.mouse.get_pos())) else: objreach(player, getpos(pygame.mouse.get_pos())) # drag , drop else: obj = player.place.matrix[clickpos[0]][clickpos[1]][-1] objthrow(obj, clickpos, releasepos) elif event.button == secondary_button: pass # todo: ...
i'm not familiar multithreading , don't think it's supposed used pygame, thing think of. ideas?
instead of mucking around multithreading , directly causing object move within objreach
function , blocking mainloop, try modifying object has obj
list. act queue contains coordinates of each position move to.
then, when mouse clicked, use pathfinding algorithm find path object should travel along , replace queue it:
import time def objreach(obj, pos): try: obj.path = obj.reach(pos, move=false) # a* (path-finding algorithm) obj.last_move = time.time() obj.move_after = 1 # move every 1 second. except exception e: sendmsg(str(e))
then, within mainloop, once every second (using timer of sort keep track of when 1 second has passed), make object pop next coordinate in list , move it.
it this:
def mainloop(): pygame.event.pump() keyboard(pygame.key.get_pressed()) events = pygame.event.get() mouse(events) event in events: if event.type == pygame.quit: pygame.quit() return false if time.time() - obj.last_move > obj.move_after: obj.last_move = time.time() objmove(obj, obj.path.pop()) return true
this way, code makes object move relocated within mainloop can still respond events.
if user clicks new tile, path automatically overridden, , mainloop right thing correctly.
if structured code correctly, extend game can handle multiple objects moving around on tiles simultaneously, moving @ different speeds.
Comments
Post a Comment