def runGame():
    pygame.init()
    screen = pygame.display.set_mode(SCREENRECT.size)

    background = pygame.Surface(SCREENRECT.size).convert()
    background.fill((0, 0, 0))
    screen.blit(background, (0, 0))
    pygame.display.update()

    clock = pygame.time.Clock()

    spriteGroupList = GameWorld.getSpriteGroups()
    AllSprites = spriteGroupList[0]
    WallSprites = spriteGroupList[1]
    RoadSprites = spriteGroupList[2]

    # for i in range(GameWorld.x):
    # for j in range(GameWorld.y):
    # screen.blit(GameWorld.arrayX[i][j].image, GameWorld.arrayX[i][j].rect)
    AllSprites.draw(screen)

    # -testzombie-
    TestZombie1 = Zombie(ZOMBIESIZE, GameWorld.arrayX[3][3], TILESIZE, GameWorld.arrayX)
    TestZombie2 = Zombie(ZOMBIESIZE, GameWorld.arrayX[3][16], TILESIZE, GameWorld.arrayX)
    TestZombie3 = Zombie(ZOMBIESIZE, GameWorld.arrayX[8][5], TILESIZE, GameWorld.arrayX)
    TestZombie4 = Zombie(ZOMBIESIZE, GameWorld.arrayX[9][15], TILESIZE, GameWorld.arrayX)
    TestZombie5 = Zombie(ZOMBIESIZE, GameWorld.arrayX[12][13], TILESIZE, GameWorld.arrayX)
    TestZombie6 = Zombie(ZOMBIESIZE, GameWorld.arrayX[18][5], TILESIZE, GameWorld.arrayX)
    TestZombie7 = Zombie(ZOMBIESIZE, GameWorld.arrayX[18][13], TILESIZE, GameWorld.arrayX)
    zombietimer = 0

    # ------------
    HealthDisplay = "Health: " + str(Player.health)
    FuelDisplay = "Fuel: " + str(Player.fuel)
    GameWorld.placeHuman(Player, 0, 0)
    GameWorld.placeZombie(TestZombie1, 1, 1)
    GameWorld.placeZombie(TestZombie2, 1, 1)
    GameWorld.placeZombie(TestZombie3, 1, 1)
    GameWorld.placeZombie(TestZombie4, 1, 1)
    GameWorld.placeZombie(TestZombie5, 1, 1)
    GameWorld.placeZombie(TestZombie6, 1, 1)
    GameWorld.placeZombie(TestZombie7, 1, 1)
    movers = pygame.sprite.RenderPlain(
        Player, TestZombie1, TestZombie2, TestZombie3, TestZombie4, TestZombie5, TestZombie6, TestZombie7
    )
    movers.draw(screen)
    # screen.blit(GameWorld.arrayX[0][0].human.image, GameWorld.arrayX[0][0].human.rect)

    pygame.display.update()
    pygame.mixer.music.load("sounds/Contra_Boss_Battle.mid")
    pygame.mixer.music.play(-1)
    mainTitle = Title("images/title.png", 150, 350)
    titles = pygame.sprite.RenderPlain(mainTitle)
    titles.draw(screen)
    pygame.display.update()
    ENTER = False
    while ENTER is False:
        for event in pygame.event.get():
            if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
                pygame.mixer.music.stop()
            elif event.type == KEYDOWN and event.key == K_RETURN:
                ENTER = True
            elif event.type == KEYDOWN and event.key == K_s:
                Player.isSim = True
                ENTER = True

    screen.blit(background, (0, 0))
    AllSprites.draw(screen)
    movers.draw(screen)
    pygame.display.flip()
    ticks = 0
    while 1:
        font = pygame.font.Font("extras/04b03.TTF", 24)
        HealthText = font.render(HealthDisplay, 0, (230, 0, 0))
        FuelText = font.render(FuelDisplay, 0, (230, 0, 0))
        HealthRect = HealthText.get_rect()
        FuelRect = FuelText.get_rect()
        HealthRect.topleft = [1, SCREENRECT.height - 20]
        FuelRect.topleft = [1, SCREENRECT.height - 40]
        screen.blit(FuelText, FuelRect)
        screen.blit(HealthText, HealthRect)
        pygame.display.update()
        clock.tick(30)

        # zombie random timer
        zombietimer = zombietimer + 1
        if zombietimer >= 10:
            zombietimer = 0
            TestZombie1.getNewDirection()
            TestZombie1.move()
            TestZombie2.getNewDirection()
            TestZombie2.move()
            TestZombie3.getNewDirection()
            TestZombie3.move()
            TestZombie4.getNewDirection()
            TestZombie4.move()
            TestZombie5.getNewDirection()
            TestZombie5.move()
            TestZombie6.getNewDirection()
            TestZombie6.move()
            TestZombie7.getNewDirection()
            TestZombie7.move()
            # update hp when zombie moves
            HealthDisplay = "Health: " + str(Player.health)
            Player.updateHealth()
        if Player.isSim == False:
            for event in pygame.event.get():
                if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
                    pygame.mixer.music.stop()
                    return
                elif event.type == KEYDOWN:
                    if (event.key == K_RIGHT) or (event.key == K_LEFT) or (event.key == K_UP) or (event.key == K_DOWN):
                        Player.translateKey(event.key)
                        # update hp when player moves
                        HealthDisplay = "Health: " + str(Player.health)
                        Player.updateHealth()
        else:
            for event in pygame.event.get():
                if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
                    pygame.mixer.music.stop()
                    return
            if ticks == 30:
                Player.doSelfMove()
                # update hp when player moves
                HealthDisplay = "Health: " + str(Player.health)
                Player.updateHealth()

        if Player.health <= 0:
            font = pygame.font.Font("extras/04b03.TTF", 36)
            text = font.render("THE ZOMBIES GOT YOU! GAME OVER!", 0, (230, 0, 0))
            textpos = text.get_rect(centerx=SCREENRECT.width / 2, centery=SCREENRECT.height / 2)
            screen.blit(text, textpos)
            pygame.display.update()
            pygame.mixer.music.fadeout(5000)
            time.sleep(5)
            return
        if Player.tile.helipad == True:
            font = pygame.font.Font("extras/04b03.TTF", 36)
            text = font.render("CONGRATULATIONS, YOU ESCAPED!", 0, (230, 0, 0))
            textpos = text.get_rect(centerx=SCREENRECT.width / 2, centery=SCREENRECT.height / 2)
            screen.blit(text, textpos)
            pygame.display.update()
            pygame.mixer.music.fadeout(5000)
            time.sleep(5)
            return

        if ticks == 30:
            ticks = 0
        else:
            ticks = ticks + 1
        movers.update()
        screen.blit(background, (0, 0))
        AllSprites.draw(screen)
        movers.draw(screen)
        pygame.display.flip()
Ejemplo n.º 2
0
def main():
    global sunnum, font, fontImg
    # peashooter = Peashooter()
    clickimage = []
    # sunflower = SunFlower()
    wallnut = WallNut()
    p1 = []
    peaList = []
    sunFlowerList = []

    sunList = []
    zombieList = []
    zombie = Zombie()
    zombieList.append(zombie)

    enemy_zombie_list = []

    flower_product_list = []

    # FPS
    block = pygame.time.Clock()

    index = 0
    # 是否点击卡片
    is_pick = False
    # 区域种植判断
    is_plant = False
    # 太阳下落时间
    SUN_EVENT = pygame.USEREVENT + 1
    pygame.time.set_timer(SUN_EVENT, 3000)  # 3秒出现一个

    # 太阳花产能
    FLOWER_PRODUCT_SUM_EVENT = pygame.USEREVENT + 1
    pygame.time.set_timer(FLOWER_PRODUCT_SUM_EVENT, 5000)

    #
    ZOMBIE_EVENT = pygame.USEREVENT + 1
    pygame.time.set_timer(ZOMBIE_EVENT, 5000)

    z = Zone()
    bulletList = []
    PAUSE = False
    FINAL = 0

    while 1:

        pygame.mixer.music.play()  # 播放音乐
        block.tick(25)  # FPS为25

        # 鼠标点击
        press = pygame.mouse.get_pressed()
        # 鼠标坐标
        x, y = pygame.mouse.get_pos()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == SUN_EVENT:
                sun = Sun()
                sunList.append(sun)
            # 太阳花产能
            if event.type == FLOWER_PRODUCT_SUM_EVENT:
                for flower in sunFlowerList:
                    if not flower.isProductSum:
                        sun = Sun()
                        sun.rect.left = flower.rect.left
                        sun.rect.top = flower.rect.top
                        sun.belong = flower
                        flower_product_list.append(sun)
                        flower.isProductSum = True  # 无法生产

            if event.type == ZOMBIE_EVENT:
                zombie = Zombie()
                zombieList.append(zombie)

            # 暂停,点击ESC
            if event.type == pygame.KEYDOWN:
                if event.key == 27:
                    PAUSE = not PAUSE

            if event.type == pygame.MOUSEMOTION:
                if 537 <= x <= 749 and 378 <= y <= 481:
                    if press[0]:
                        PAUSE = not PAUSE
                if not is_pick:
                    if press[0]:
                        if 330 <= x <= 330 + card.get_rect().width and 7 <= y <= 7 + card.get_rect().height:
                            peashooter = Peashooter()
                            clickimage.append(peashooter)
                            pick_type = 'pea'
                            is_pick = True
                        if 380 <= x <= 380 + card_1.get_rect().width and 7 <= y <= 7 + card_1.get_rect().height:
                            sunflower = SunFlower()
                            clickimage.append(sunflower)
                            pick_type = 'flower'
                            is_pick = True

                else:
                    if z.is_plant_zone(x, y):
                        if z.getIndex(x, y) and not is_plant:
                            if pick_type == 'pea':
                                p = Peashooter()
                                a, b = z.getIndex(x, y)
                                p.zone = z.getGridPos(a, b)
                                if not z.plantInfo[b][a]:
                                    p1.append(p)
                                    is_plant = True
                                    if press[0]:
                                        peaList.append(p)
                                        enemy_zombie_list.append(p)
                                        # bullet = p.shot()
                                        # bulletList.append(bullet)
                                        clickimage.clear()
                                        p1.clear()
                                        is_pick = False
                                        z.plantInfo[b][a] = 1
                                        sunnum = str(int(sunnum) - 100)
                                        fontImg = font.render(sunnum, True, (0, 0, 0))

                                        # print(z.plantInfo[a][b])
                            elif pick_type == 'flower':
                                f = SunFlower()
                                a, b = z.getIndex(x, y)
                                f.zone = z.getGridPos(a, b)
                                if not z.plantInfo[b][a]:
                                    p1.append(f)
                                    is_plant = True
                                    if press[0]:
                                        sunFlowerList.append(f)
                                        enemy_zombie_list.append(f)
                                        clickimage.clear()
                                        p1.clear()
                                        is_pick = False
                                        z.plantInfo[b][a] = 1
                                        sunnum = str(int(sunnum) - 100)
                                        fontImg = font.render(sunnum, True, (0, 0, 0))
                        else:
                            p1.clear()
                            is_plant = False

                    if press[2]:
                        is_pick = False
                        clickimage.clear()
                        p1.clear()
                        is_plant = False

                        # if 330 <= x <= 405 and 180 <= y <= 274:

                    # else:
                    #     p1.clear()

                for sun in sunList:
                    if sun.rect.collidepoint((x, y)):
                        if press[0]:
                            # 点击太阳消失
                            sunList.remove(sun)
                            # 收集加分
                            # global sunnum, font, fontImg
                            sunnum = str(int(sunnum) + 25)
                            fontImg = font.render(sunnum, True, (0, 0, 0))

                for sun in flower_product_list:
                    if sun.rect.collidepoint((x, y)):
                        if press[0]:
                            # 点击太阳消失
                            flower_product_list.remove(sun)
                            # 收集加分
                            sunnum = str(int(sunnum) + 25)
                            fontImg = font.render(sunnum, True, (0, 0, 0))
                            # 收集后可继续生产
                            sun.belong.isProductSum = False

        # 打印图片
        if int(zombie.rect.left) < 200:
            FINAL += 1
            PAUSE = True
        if not PAUSE:
            screen.blit(backgroud, (0, 0))
            screen.blit(card_slot, (250, 0))
            screen.blit(card, (330, 7))
            screen.blit(card_1, (380, 7))
            screen.blit(card_2, (430, 6))
            screen.blit(fontImg, (275, 60))

            if index > 13:
                index = 0
            # screen.blit(peashooter.images[index % 13], peashooter.rect)
            # screen.blit(sunflower.images[index % 18], sunflower.rect)
            # screen.blit(wallnut.images[index % 16], wallnut.rect)

            for image in clickimage:
                screen.blit(image.images[0], (x, y))
            for p in p1:
                screen.blit(p.images[0], p.zone)
            # 豌豆射手
            for pea in peaList:
                if pea.is_live:
                    if index % 99 == 1:
                        bullet = pea.shot()
                        bulletList.append(bullet)
                    screen.blit(pea.images[index % 13], pea.zone)
                else:
                    peaList.remove(pea)
                    enemy_zombie_list.remove(pea)
            # 太阳花
            for sunFlower in sunFlowerList:
                if sunFlower.is_live:
                    screen.blit(sunFlower.images[index % 18], sunFlower.zone)
                else:
                    sunFlowerList.remove(sunFlower)
                    enemy_zombie_list.remove(sunFlower)
            # 阳光
            for sun in sunList:
                screen.blit(sun.images[index % 22], sun.rect)
                sun.down()

            # 产能
            for sun in flower_product_list:
                screen.blit(sun.images[index % 22], sun.rect)

            for bullet in bulletList:
                if bullet.status:
                    screen.blit(bullet.images, bullet.rect)
                    bullet.move()
                    bullet.hit(zombieList)
                else:
                    bulletList.remove(bullet)

            for zombie in zombieList:
                if zombie.is_live:
                    zombie.changimage()
                    screen.blit(zombie.images[index % 20], zombie.rect)
                    zombie.move()
                    zombie.attack(enemy_zombie_list)
                else:
                    zombieList.remove(zombie)

            pygame.display.update()

            index += 1
        else:
            if FINAL > 0:
                screen.blit(final_draw, (350, 70))
            else:
                screen.blit(pause_draw, (450, 100))

        pygame.display.update()
Ejemplo n.º 3
0
            elif event.type == pygame.KEYUP:
                key_monitor.key_up(event)

        # Game loop
        if game.started() and (time.time()*1000.0)-last_time > FRAME_TIME:
            last_time = time.time()*1000.0
            game.tick()
            key_monitor.tick()

            witch.update()
            zombie.update()
            monster.update()
            skeleton.update()

            witch.move(SPEED)
            zombie.move(SPEED)
            monster.move(SPEED)
            skeleton.move(SPEED)

            screen.fill(black)

            game.check_piece([witch,zombie,monster,skeleton], key_monitor)

            game.draw_background(screen)

            screen.blit(witch.get_image(), witch.get_rect())
            screen.blit(zombie.get_image(), zombie.get_rect())
            screen.blit(monster.get_image(), monster.get_rect())
            screen.blit(skeleton.get_image(), skeleton.get_rect())

            pygame.display.flip()