def test():
    game = gamebase.GameBase((400,400),30)
    count = 0
    img = utils.load_image("explosion_01.png")
    imgs = images.cut_sheet(img,(26,26))
    anim = Animation(imgs, 2)
    anim2 = Animation(imgs, 4)
    while True:
        game.clock.tick(60)
        events = game.get_events()
        game.screen.blit(anim.get(),(0,0))
        game.screen.blit(anim2.get(),(30,0))
        if anim2.has_finished():
            anim.start(2)
        pygame.display.flip()
        game.screen.fill((0,0,0))
        count += 1
Exemplo n.º 2
0
def main():
    # Initiating, screen, clock etc.
    os.environ["SDL_VIDEO_CENTERED"] = "1"
    pygame.init()
    pygame.mouse.set_visible(False)
    pygame.display.init()
    pygame.display.set_caption("shmup & build")
    
    surf = pygame.Surface((32,32),SRCALPHA)
    icon = pygame.image.load(os.path.join("data","python_icon.png"))
    icon.set_colorkey((0,0,0))
    surf.blit(icon,(0,0))
    pygame.display.set_icon(surf)
    screen = pygame.display.set_mode(WINDOWSIZE, DOUBLEBUF|HWSURFACE)
    presurf = pygame.Surface(SCREENSIZE,HWSURFACE)
    clock = pygame.time.Clock()
    ga.count = 0
    quitflag = False
    ga.lm = LineManager()
    aManager = AttackerManager()
    aManager.reg_unit(15,LaserDrone,((10,10),))
    aManager.reg_unit(30,LaserDrone,((10,18),))
    aManager.reg_unit(52,LaserDrone,((11,2),))
    aManager.reg_unit(66,LaserDrone,((5,16),))
    for i in range(20):
        aManager.reg_unit(rnd(20,1000),LaserDrone,((rnd(0,12),rnd(0,25)),))
    
    #---- LOADING GRAPHICS ----
    # gui
    images.button01 = utils.load_image("button_01.png")
    
    # cursors
    images.isocursor01 = utils.load_image("iso_cursor_01.png")
    images.cursor01 = utils.load_image("cursor_01.png")
    
    # shmup images
    images.player_ship = utils.load_image("ship_04.png")
    images.ship_02 = utils.load_image("ship_08.png")
    images.ship_05 = utils.load_image("ship_05.png")
    images.ship_06 = utils.load_image("ship_07.png")
    
    # projectiles
    images.beam01 = utils.load_image("beam_01.png")
    img = utils.load_image("beam_01_end.png")
    images.beam01_end = images.cut_sheet(img, (12,6))
    img = utils.load_image("beam_02.png")
    images.beam02 = images.cut_sheet(img, (8,6))
    
    # explotions
    img = utils.load_image("explosion_01.png")
    images.explosion_01 = images.cut_sheet(img,(26,26))
    
    
    # floor tiles
    images.stationFloor01 = utils.load_image("station_floor_01.png")
    images.stationDec01 = utils.load_image("station_decoration_03.png")
    images.gridSquare = utils.load_image("iso_square_1.png")
    
    # buildings
    make_buildingset("building03",  (2,2), "building_03.png", PowerGen)
    make_buildingset("building04",  (2,2), "building_04.png", Barrack)
    make_buildingset("building06",  (1,1), "building_06.png", Barrier)
    make_buildingset("building07",  (2,1), "building_08.png", Barrier2)
    make_buildingset("road", (1,1), "road_01.png", Road)
    make_buildingset("comcenter01",(3,3),"building_05.png", CommandCenter)
    make_buildingset("cannon01", (2,1), "cannon_01.png",Cannon)
    
    # walkers
    images.repairdrone_01 = images.cut_sheet(utils.load_image("robot_01.png"),
    (11,15))
    
    images.square02 = utils.load_image("iso_square_2.png")
    images.square03 = utils.load_image("iso_square_3.png")
    
    #--------
    
    #sprite groups
    generic = pygame.sprite.Group()
    ga.generic = generic
    cursors = pygame.sprite.OrderedUpdates()
    guiGroup = pygame.sprite.Group()
    bottomDec = pygame.sprite.Group()
    stars = pygame.sprite.Group()
    topDec = pygame.sprite.Group()
    projectiles = pygame.sprite.Group()
    shmupObjects = ga.shmupObjects = pygame.sprite.LayeredUpdates()
    ga.stationGridGround = pygame.sprite.OrderedUpdates()
    buildings = pygame.sprite.LayeredUpdates()
    ndBuildings = pygame.sprite.Group() # non drawing part of building
    
    #tying sprite classes to groups
    IsoCursor.groups = (cursors,)
    Cursor.groups = (cursors,)
    BuildingPiece.groups = (buildings,)
    DistantStar.groups = (stars,)
    Explosion.groups = (generic,)
    DyingAnim.groups = (generic,)
    for cls in Projectile.__subclasses__():
        cls.groups = (projectiles,)
    for cls in Building.__subclasses__():
        cls.groups = (ndBuildings,)
    for cls in Walker.__subclasses__():
        cls.groups = (generic,)
    for cls in ShmupObject.__subclasses__():
        cls.groups = (shmupObjects,)
    for cls in gui.Button.__subclasses__():
        cls.groups = (guiGroup,)
    
    
    #isoSpaces
    ga.shmupGrid = isospace.IsoGrid((TX, TY), (250,20))
    ga.stationGrid = isospace.IsoGrid((TX, TY), ga.shmupGrid.get_mt((22,0)))
    
    #player
    playership_start = ga.shmupGrid.get_tl((PS_ROW,18))
    playerShip = PlayerShip(playership_start,(10,10))
    
    #decorations
    stationDec = Decoration(
    (ga.stationGrid.get_left((0,24))-1,  ga.stationGrid.get_top((0,0))),
    images.stationDec01, bottomDec)
    
    # gui components
    barrackButton = gui.BuildingButton((40,220),(4,2), Barrack, images.building04)
    cannonButton = gui.BuildingButton((90,250),(4,4), Cannon, images.cannon01)
    roadButton = gui.BuildingButton((40,250), (10,10), Road, images.road)
    roadButton.add_img(images.road, (10+ TX,10-TY))
    PowerGenButton = gui.BuildingButton((40,280),(4,-2), PowerGen, images.building03)
    Barrier2Button = gui.BuildingButton((90,280),(8,2), Barrier2, images.building07)
    
    #cursors
    isoCursor =ga.isoCursor = IsoCursor()
    cursor = ga.cursor = Cursor()
    
    #Station
    stationform = nested2d_fromstring(shapes.station, {"#":1, " ":0})
    station = ga.station = Station()
    station.floor = Nested2d(stationform.x_len,stationform.y_len)
    station.make_maps(nested2d_fromstring(shapes.station, {"#":True, " ":False}))
    
    #pathfinding
    ga.ptfstart = None

    for i in stationform.loop_all():
        i,x,y = i
        if i:
            station.floor[x][y] = Tile(ga.stationGrid.get_tl((x,y)),(x,y))
            station.floor[x][y].add(ga.stationGridGround)
            
    for x in range(-10,SHMUPGRID_X):
        for y in range(SHMUPGRID_Y):
            GridSquare(ga.shmupGrid.get_tl((x,y)),(x,y)).add(ga.stationGridGround)

    
    for i in range(600):
        spawn_star()
        stars.update()
    ## -------- MAIN LOOP --------  
    while not quitflag:
        clock.tick(60)
        ga.count += 1
        ga.station_iso = ga.stationGrid.get_iso(get_mousepos())
        ga.shmup_iso = ga.shmupGrid.get_iso(get_mousepos())
        events = pygame.event.get()
        for event in events:
            if event.type == QUIT:
                quitflag = True
                
            elif event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    quitflag = True
                elif event.key == K_TAB:
                    ga.lm.toggle()
                building_cls = BUILD_HOTKEYS.get(event.key)
                if building_cls:
                    station.load_build(building_cls)
                    isoCursor.load_hint(building_cls)
                    
            elif event.type == MOUSEBUTTONDOWN:
                if event.button == 1:
                    if ga.shmupGrid.get_iso(get_mousepos())[0] < 22:
                        #playerShip.fire()
                        pass
                    station.build()
                    guiGroup.update(get_mousepos())
                    
                if event.button == 3:
                    isoCursor.unload_hint()
                    station.unload_build()
                    
                    
        # check keys and buttons held down
        if pygame.mouse.get_pressed()[0]:
            station.road_drag(True)
            playerShip.is_firing = True
        else:
            station.road_drag(False)
            playerShip.is_firing = False
        # correct mouse pos
        mx, my = get_mousepos()
        ga.iso_pos_station = ga.stationGrid.get_iso((mx, my))
        ga.iso_pos_space = ga.stationGrid.get_iso((mx, my))
        
        aManager.update(ga.count)
        
        spawn_star()
        generic.update()
        cursors.update()
        stars.update()
        projectiles.update()
        buildings.update()
        ndBuildings.update()
        shmupObjects.update()
        
        ip = ga.stationGrid.get_iso((mx,my))
        
        presurf.fill((0,0,8))
        
        stars.draw(presurf)
        bottomDec.draw(presurf)
        ga.stationGridGround.draw(presurf)
        projectiles.draw(presurf)
        shmupObjects.draw(presurf)
        buildings.draw(presurf)
        generic.draw(presurf)
        
        
        pygame.draw.polygon(presurf,(130,140,140),((0,326-155),(310,326),(0,326)))
        pygame.draw.polygon(presurf,(100,100,100),((0,326-150),(300,326),(0,326)))        
        guiGroup.draw(presurf)
        
        if pygame.mouse.get_focused():
            cursors.draw(presurf)
        
        pos = ga.stationGrid.get_iso(get_mousepos())
        if pos:
            if 0 <= pos[0] < STATIONGRID_X and 0 <= pos[1] < STATIONGRID_Y:
                pos = ga.stationGrid.get_tl(pos)
                
                
        ga.lm.draw(presurf)
        #pygame.draw.rect(presurf, (0,0,0), ga.stationGrid.get_rect(), 1)
        pygame.transform.scale(presurf, WINDOWSIZE, screen)
        pygame.display.flip()
        
        # print info in window caption
        pygame.display.set_caption("fps" + str(int(clock.get_fps())) + str(ga.station_iso) + str(get_mousepos()))