Esempio n. 1
0
def main(dim):

    import psyco
    psyco.full()
    
    #initialize pygame, the display caption, and the screen
    pygame.init(); pygame.display.set_caption("A* Maze")
    grid = creategrid(dim)
    #reset the screen to the dimensions in the new map
    screen = pygame.display.set_mode((dim[0], dim[1]))

    guy = Guy((10,15),"smiley x 10.bmp",.8, dim)
    r = 2#the guys sight range

    '''
    guy.all_knowing : this guy knows everything
    True -> guy and user can see everything, deleting/adding walls allowed
    False -> guy has a sight radius and updates his knowledge of the map (deleting/adding walls not allowed)
    pressing m will switch modes
    '''
    drawing = deleting = False
    draw_all(screen,grid,guy)


    save = eztext.Input(maxlength=60, color=(255,0,0), prompt='Input save path: ')
    load = eztext.Input(maxlength=60, color=(255,0,0), prompt='Input load path: ')
    loading = saving = False

    while True:#main game loop
        events = pygame.event.get()#get the inputs
        if saving == True:
            #fill the screen black where we type
            screen.fill((0,0,0),(0,0,1000,25))
            save.update(events)#update the text
            save.draw(screen)#draw the text
            pygame.display.update((0,0,1000,25))#update the screen

        elif loading == True:
            screen.fill((0,0,0),(0,0,1000,25))
            load.update(events)
            load.draw(screen)
            pygame.display.update((0,0,1000,25))

        for e in events: #processes key/mouse inputs
            mousex = (pygame.mouse.get_pos()[0])/10
            mousey = (pygame.mouse.get_pos()[1])/10

            #right click will draw a wall,
            #left click will move the guy to your mouse cursor
            if e.type == MOUSEBUTTONDOWN:
                if pygame.mouse.get_pressed()[2] == 1:
                    #can only draw if the guy knows about it, or it will mess with his head
                    if guy.all_knowing == True: 
						if grid[mousex][mousey] == True:
							drawing = True
						else: deleting = True
                    
                elif grid[mousex][mousey]!= False and (mousex,mousey)!=guy.loc:
                    guy.set_path(screen, grid,(mousex,mousey))
            elif e.type == MOUSEBUTTONUP:drawing = deleting = False


            #ESC:quit; hold d: delete walls; s : save map;  l : load map
            #q deletes the entire map; m: change guy settings
            elif e.type == KEYDOWN:
                
                if e.key == K_ESCAPE: pygame.quit();  sys.exit()#quit
                
                elif e.key == K_RETURN:
                    #saving and loading reset the guy so he knows everything
                   if saving == True:
                       write_to_file(grid,dim, save.value)
                       saving = False
                       draw_all(screen,grid,guy)
                       pygame.display.flip()
                       guy.all_knowing = True
                       
                   elif loading == True:
                       try:#'try' to load the file
                           grid,dim = load_from_file(load.value)
                           screen = pygame.display.set_mode((dim[0], dim[1]))
                           draw_all(screen, grid, guy)
                           guy.all_knowing = True
                           loading = False
                       except:#if we can't, raise and exception
                           load.prompt = "Please try again: "
                           load.value = ''
                       
                if not(saving or loading):#we don't want any interferece while we type
                    if e.key == K_s: saving = True
                    elif e.key == K_l: loading = True
                           
                    #erases the map and replaces it with an empty one    
                    elif e.key == K_q:
                        grid = creategrid(dim)
                        draw_all(screen, grid,guy)
                        guy.all_knowing = True
                    elif e.key == K_m:
                        guy.all_knowing = not guy.all_knowing
                        if guy.all_knowing == True:
                            draw_all(screen,grid,guy)
                        else:
                            screen.fill((0,0,0))
                            pygame.display.flip()
                            guy.known_walkability = creategrid(dim)
                            update(screen,grid,guy,r)
                                    
        if len(guy.path) > 0 :#if the guy has a path
            #if the next step in the path isn't a wall
            if grid[guy.path[0][0]][guy.path[0][1]] != False:
                guy.traverse_path()

            #else if he hits a wall, he recalculates the path
            else: guy.set_path(screen, grid,guy.goal, .07)
            
        #if the guy's time limit runs out for finding a path, he can try again
        elif len(guy.path)>0 and guy.loc != guy.goal and len(guy.goal)>0:
            guy.set_path(screen, grid,guy.goal)

        update(screen, grid, guy,r)
        
        if   drawing  == True: grid = change(screen, grid,False, mousex,mousey)
        elif deleting == True: grid = change(screen, grid, True, mousex,mousey)
Esempio n. 2
0
def main(dim):
    #initialize pygame, the display caption, and the screen
    pygame.init(); pygame.display.set_caption("A* Maze")
    grid = creategrid(dim)
    screen = pygame.display.set_mode((dim[0], dim[1]))

    guy = Guy((10,15),"smiley x 10.bmp", screen)


    save = eztext.Input(maxlength=60, color=(255,0,0),
                        prompt='Input save path: ')
    load = eztext.Input(maxlength=60, color=(255,0,0),
                        prompt='Input load path: ')

    #we arent loading or saving
    loading = saving = False
    #we aren't drawing or deleting
    drawing = deleting = False

    #guy moving on a path
    moving = False
    
    #user control direction booleans
    direction = [0,0]
    #user movment
    userm = False

    #draw everything on the screen
    draw_all(screen,grid,guy)

    clock = pygame.time.Clock()#
    while True:#main game loop
        clock.tick(1000)#limits the speed of the game

        events = pygame.event.get()#get the inputs
        if saving == True:
            #fill the screen black where we type
            screen.fill((0,0,0),(0,0,1000,25))
            save.update(events)#update the text
            save.draw(screen)#draw the text
            pygame.display.update((0,0,1000,25))#update the screen

        elif loading == True:
            screen.fill((0,0,0),(0,0,1000,25))
            load.update(events)
            load.draw(screen)
            pygame.display.update((0,0,1000,25))

        
        for e in events: #processes key/mouse inputs
            mousex = (pygame.mouse.get_pos()[0])/10
            mousey = (pygame.mouse.get_pos()[1])/10

            #right click will draw a wall or delete
            #left click will move the guy to your mouse cursor
            if e.type == MOUSEBUTTONDOWN:
                #right mouse button pressed
                if pygame.mouse.get_pressed()[2] == 1:
                    #if we start by seleting a wall, then we are deleting
                    #else we start drawing
                    if grid[mousex][mousey] == True:
                        drawing = True
                    else:
                        deleting = True

                #set a new path for the guy
                elif grid[mousex][mousey]!= False and (mousex,mousey)!=guy.loc:
                    draw_all(screen, grid,guy)#redraw everythin
                    moving = False#the guy stops moving
                    guy.set_path(grid,(mousex,mousey))
                    #no user movement possible at this point
                    userm = False

            #stop deleting/drawing walls
            elif e.type == MOUSEBUTTONUP:drawing = deleting = False


            #ESC->quit; s ->save map;  l -> load map
            #q -> deletes the entire map
            #SPACE -> moves guy once a path is known
            elif e.type == KEYDOWN:

                #escape to quit
                if e.key == K_ESCAPE: pygame.quit();  sys.exit()

                #sends in the text input for map I/O
                elif e.key == K_RETURN:
                   if saving == True:
                       write_to_file(grid,dim, save.value)
                       draw_all(screen,grid,guy)
                       pygame.display.flip()
                       saving = False
                       
                   elif loading == True:
                       try:#'try' to load the file
                           grid,dim = load_from_file(load.value)
                           screen = pygame.display.set_mode((dim[0], dim[1]))
                           draw_all(screen, grid, guy)
                           loading = False
                       except:#if we can't, raise an exception
                           load.prompt = "Please try again: "
                           load.value = ''
                       
                if not(saving or loading):
                    #we don't want any interferece while we type
                    if e.key == K_s: saving = True
                    elif e.key == K_l: loading = True
                    elif e.key == K_SPACE: moving = True
                        
                    #erases the map and replaces it with an empty one    
                    elif e.key == K_q:
                        grid = creategrid(dim)
                        draw_all(screen, grid,guy)

                    #controls the guys direction
                    elif e.key == K_RIGHT: direction[0] =  1
                    elif e.key == K_LEFT:  direction[0] = -1
                    elif e.key == K_UP  :  direction[1] = -1
                    elif e.key == K_DOWN:  direction[1] =  1

                    #if we are inputing movement and we 'can' move
                    if direction!=[0,0] and userm == False and moving == False:
                        draw_all(screen,grid,guy)
                        userm = True

            elif e.type == KEYUP:
                #reset arrow keys to say we dont won't to go in that direction
                if e.key == K_RIGHT: direction[0] = 0
                elif e.key == K_LEFT: direction[0] = 0
                elif e.key == K_UP: direction[1] = 0
                elif e.key == K_DOWN: direction[1] = 0
                
        if len(guy.path) > 0 :#if the guy has a path
            #if the next step in the path isn't a wall
            if grid[guy.path[0][0]][guy.path[0][1]] != False:
                if moving == True: guy.traverse_path()
            
        elif len(guy.path) == 0: moving = False
        
        if moving == False and userm == True:
            guy.user_control(direction, grid)
            
        update(screen, grid, guy)
        
        if   drawing  == True: grid = change(screen, grid,False, mousex,mousey)
        elif deleting == True: grid = change(screen, grid, True, mousex,mousey)