コード例 #1
0
ファイル: tmx-loader.py プロジェクト: brenopsouza/j
def demo_pygame(file_name):
    SLICE_SIZE = 80
    SLICE_SIZE_PIXEL = 5120

    pygame = __import__("pygame")

    # parser the map
    world_map = TileMapParser().parse_decode(file_name)
    # init pygame and set up a screen
    pygame.init()

    img_fatguy = pygame.image.load(os.path.join("", "images", "sprite.png"))
    fatguy = Caracter("jonatas", img_fatguy, 10, 115, 115, 20)
    fatguy.set_pos(200, 269)
    fatguy.dx = 0

    clock = pygame.time.Clock()

    pygame.display.set_caption("tiledtmxloader - " + file_name)
    screen_width = min(1024, world_map.pixel_width)
    screen_height = min(768, world_map.pixel_height)
    screen = pygame.display.set_mode((screen_width, screen_height), HWSURFACE | DOUBLEBUF)

    world_map.load(ImageLoaderPygame())

    slices = set_slices(world_map, SLICE_SIZE, SLICE_SIZE_PIXEL)

    # load the images using pygame

    # printer(world_map)

    # an example on how to access the map data and draw an orthoganl map
    # draw the map
    assert world_map.orientation == "orthogonal"

    print "slices =", slices

    running = True
    dirty = True
    # cam_offset is for scrolling
    cam_offset_x = 0
    cam_offset_y = 0

    offset = 0
    actual_slice = slices.pop(0)
    past_slice = actual_slice
    transition = False
    # mainloop
    while running:
        clock.tick(90)

        # eventhandling
        events = pygame.event.get()
        for event in events:
            dirty = True
            if event.type == pygame.QUIT:
                running = False
        if transition:
            join_point = SLICE_SIZE_PIXEL - offset
            screen.blit(past_slice.subsurface(offset, 0, join_point, screen_height), (0, 0))
            screen.blit(
                actual_slice.subsurface(0, 0, (offset + screen_width - SLICE_SIZE_PIXEL), screen_height),
                (join_point, 0),
            )
            if join_point < 0:
                offset = 0
                transition = False
        else:
            screen.blit(actual_slice.subsurface((offset, 0, screen_width, screen_height)), (0, 0))

        offset += 4
        if (offset + screen_width) > SLICE_SIZE_PIXEL and transition == False:
            past_slice = actual_slice
            if len(slices) == 0:
                break
            actual_slice = slices.pop(0)
            transition = True
        screen.blit(fatguy.image, fatguy.get_pos())
        fatguy.update(pygame.time.get_ticks(), screen_width, screen_height)
        pygame.display.flip()
コード例 #2
0
ファイル: Game.py プロジェクト: brenopsouza/j
def main():
    pygame.init()
    clock = pygame.time.Clock()
    running = True

    #Constantes
    SLICE_SIZE_PIXEL = 5120
    SLICE_SIZE = 80
    REPEAT_DELAY = 50 #milisseconds between each KEYDOWN event (when repeating)
    KEY_TIMEOUT = 185 #MAX milisseconds between key pressings
    SCREEN_WIDTH, SCREEN_HEIGHT = (1024, 640)

    screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
    pygame.display.set_caption("Good Intentions")
    img_fatguy = pygame.image.load(os.path.join('', 'images', 'sprite.png'))
    fatguy = Caracter("jonatas", img_fatguy, 10, 115, 115, 25)
    commandHandler = CommandHandler(fatguy)
    cam_speed  = (4,0)
    
    fatguy.set_pos(200,200)
    
    
    world_map = TileMapParser().parse_decode("huge_objects.tmx")
    world_map.load(ImageLoaderPygame())
    
    ground_objects = get_ground_objects(world_map)
    killer_objects = get_killer_objects(world_map)
    print killer_objects
    slices = set_slices(world_map, SLICE_SIZE, SLICE_SIZE_PIXEL)

    key_timeout = -1

    #create sprites groups for collision detection
    playerGroup = pygame.sprite.RenderUpdates()
    playerGroup.add(fatguy)

    objectGroup = pygame.sprite.Group()
    enemyGroup = pygame.sprite.Group()
    sceneGroup = pygame.sprite.Group()

    pygame.key.set_repeat(REPEAT_DELAY, REPEAT_DELAY)
    
    offset = 0
    actual_slice = slices.pop(0)
    past_slice = actual_slice
    transition = False
    while running:
        clock.tick(90)
        
        #blit level----------------------------------------------------------------------------------
        if transition:
            join_point = SLICE_SIZE_PIXEL - offset
            screen.blit(past_slice.subsurface(offset, 0, join_point, SCREEN_HEIGHT),(0,0))
            screen.blit(actual_slice.subsurface(0 ,0, (offset + SCREEN_WIDTH - SLICE_SIZE_PIXEL), SCREEN_HEIGHT),(join_point,0))
            if join_point < 0:
                offset = 0
                transition = False
        else:
            screen.blit(actual_slice.subsurface((offset,0,SCREEN_WIDTH, SCREEN_HEIGHT)), (0, 0))
        
        offset += cam_speed[0]
        if(offset + SCREEN_WIDTH) > SLICE_SIZE_PIXEL and transition == False:
            past_slice = actual_slice
            if len(slices) == 0: break
            actual_slice = slices.pop(0)
            transition = True
        #----------------------------------------------------------------------------------------------
	    
	    
        screen.blit(fatguy.image,  fatguy.get_pos())
        fatguy.update(pygame.time.get_ticks(), SCREEN_WIDTH, SCREEN_HEIGHT, cam_speed)
        
        obj, col_type = fatguy.collides_with_objects(ground_objects)
        if  col_type == 1:
            fatguy.put_on_ground_running(obj[1])
        elif col_type ==  2:
            running = False
        
        obj, col_type = fatguy.collides_with_objects(killer_objects)
        if  col_type != -1:
            running = False

        if key_timeout >= 0:
            if (pygame.time.get_ticks() - key_timeout) > KEY_TIMEOUT:
                commandHandler.actual_state = 0
                key_timeout = -1

        for e in pygame.event.get():
            if e.type == QUIT or (e.type == KEYDOWN and e.key == K_ESCAPE):
                running = False
            elif e.type == KEYDOWN:
                key_timeout = pygame.time.get_ticks()
                fatguy.state = commandHandler.refresh_state(e.key)
            if not fatguy.alive():
                print 'Game Over'
                pygame.time.wait(2000)
                sys.exit()

#        pygame.display.update()
        pygame.display.flip()
コード例 #3
0
ファイル: Game.py プロジェクト: lyadon/j
def main():
    pygame.init()
    clock = pygame.time.Clock()
    running = True
    
    commandHandler = CommandHandler()

    #Constantes
    REPEAT_DELAY = 30 #milisseconds between each KEYDOWN event (when repeating)
    KEY_TIMEOUT = 185 #MAX milisseconds between key pressings
    SCREEN_WIDTH, SCREEN_HEIGHT = (640, 480)

    screen_surface = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
    pygame.display.set_caption("Good Intentions")
    img_fatguy = pygame.image.load(os.path.join('', 'images', 'fat.png'))
    fatguy = Caracter("jonatas", img_fatguy, 10, 115, 115, 25)
#    fatguy.set_pos(0,240)
    fatguy.set_pos(0,325)

    parser = sax.make_parser()
    tmxhandler = TMXHandler()
    parser.setContentHandler(tmxhandler)
    parser.parse("grande.tmx")

    key_timeout = -1

    #create sprites groups for collision detection
    playerGroup = pygame.sprite.RenderUpdates()
    playerGroup.add(fatguy)

    objectGroup = pygame.sprite.Group()
    enemyGroup = pygame.sprite.Group()
    sceneGroup = pygame.sprite.Group()

    pygame.key.set_repeat(REPEAT_DELAY*3, REPEAT_DELAY)
    while running:
        clock.tick(30)

        screen_surface.fill((255,255,255))

        screen_surface.blit(tmxhandler.image, (0,0))
        tmxhandler.image.scroll(-10,0)

        screen_surface.blit(fatguy.image,  fatguy.get_pos())
        fatguy.update(pygame.time.get_ticks(), SCREEN_WIDTH, SCREEN_HEIGHT)

        if key_timeout >= 0:
            if (pygame.time.get_ticks() - key_timeout) > KEY_TIMEOUT:
                commandHandler.actual_state = 0
                key_timeout = -1

        for e in pygame.event.get():
            if e.type == QUIT or (e.type == KEYDOWN and e.key == K_ESCAPE):
                running = False
            elif e.type == KEYDOWN:
                key_timeout = pygame.time.get_ticks()
                fatguy.state = commandHandler.refresh_state(e.key)
            if not fatguy.alive():
                print 'Game Over'
                pygame.time.wait(2000)
                sys.exit()

#        pygame.display.update()
        pygame.display.flip()