def check_events(screen, game_settings, squares, plants, bullets, icons):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        if game_settings.game_active:
            if event.type == pygame.MOUSEBUTTONDOWN:
                mouse_x, mouse_y = pygame.mouse.get_pos()
                # print mouse_x
                # print mouse_y
                for square in squares:
                    if square.plant_here == False:
                        if square.rect.collidepoint(mouse_x, mouse_y):
                            # print "Square: ",square.square_number
                            if game_settings.chosen_plant == 1:
                                peashooter = Peashooter(screen, square)
                                plants.add(peashooter)
                                square.plant_here = True
                                game_settings.total_sun -= peashooter.sun_cost
                            elif game_settings.chosen_plant == 2:
                                plants.add(Gatling(screen, square))
                                square.plant_here = True
                                # game_settings.total_sun -= Gatling.sun_cost
                            elif game_settings.chosen_plant == 3:
                                plants.add(Sunflower(screen, square))
                                square.plant_here = True
                                # game_settings.total_sun -= Sunflower.sun_cost
                for icon in icons:
                    if icon.rect.collidepoint(mouse_x, mouse_y):
                        game_settings.chosen_plant = icon.slot

            elif event.type == pygame.MOUSEMOTION:
                for square in squares:
                    if square.rect.collidepoint(event.pos):
                        game_settings.highlighted_square = square
Esempio n. 2
0
def check_events(screen, game_settings, squares, plants, bullets, icons):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        if game_settings.game_active:
            if event.type == pygame.MOUSEBUTTONDOWN:
                # we're trying to find out the position of the green squares to make the squares clickable
                mouse_x, mouse_y = pygame.mouse.get_pos()
                # print mouse_x;
                # print mouse_y;
                for square in squares:
                    if square.rect.collidepoint(mouse_x, mouse_y):
                        # determine which square was clicked
                        print "Square: ", square.square_number
                        if (game_settings.chosen_plant == 1):
                            # chosen_plant 1 in icon was clicked, add a Peashooter to that square
                            plants.add(Peashooter(screen, square))
                        elif (game_settings.chosen_plant == 2):
                            # chosen_plant 2 in icon was clicked, add a Gatling to that square
                            plants.add(Gatling(screen, square))
                        elif (game_settings.chosen_plant == 3):
                            # chosen_plant 3 in icon was clicked, add a sunflower to that square
                            plants.add(Sunflower(screen, square))

                for icon in icons:
                    # determine which plant icon was clicked
                    if icon.rect.collidepoint(mouse_x, mouse_y):
                        game_settings.chosen_plant = icon.slot
                        # print "You clicked: ",icon.image;
            elif event.type == pygame.MOUSEMOTION:
                # print event.pos;
                for square in squares:
                    if square.rect.collidepoint(event.pos):
                        # when a mouse moves on top of a square, it turns into a sprite
                        game_settings.highlighted_square = square
Esempio n. 3
0
def check_events(screen, game_settings, squares, plants, bullets, icons):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        if game_settings.game_active:
            if event.type == pygame.MOUSEBUTTONDOWN:
                mouse_x, mouse_y = pygame.mouse.get_pos()
                # print mouse_x;
                # print mouse_y;
                for square in squares:
                    if square.rect.collidepoint(mouse_x, mouse_y):
                        print "Square: ", square.square_number
                        if (game_settings.chosen_plant == 1):
                            plants.add(Peashooter(screen, square))
                        elif (game_settings.chosen_plant == 2):
                            plants.add(Gatling(screen, square))
                        elif (game_settings.chosen_plant == 3):
                            plants.add(Sunflower(screen, square))
                for icon in icons:
                    if icon.rect.collidepoint(mouse_x, mouse_y):
                        game_settings.chosen_plant = icon.slot
                        # print "You clicked: ",icon.image;
                        # plants.add(Peashooter(screen,square));
            elif event.type == pygame.MOUSEMOTION:
                # print event.pos
                for square in squares:
                    if square.rect.collidepoint(event.pos):
                        game_settings.highlighted_square = square
Esempio n. 4
0
def check_events(screen, game_settings, squares, plants, bullets, icons):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        if game_settings.game_active:
            if event.type == pygame.MOUSEBUTTONDOWN:
                mouse_x, mouse_y = pygame.mouse.get_pos()
                for square in squares:
                    if square.plant_here == False:
                        if square.rect.collidepoint(mouse_x, mouse_y):
                            # print "Square: ",square.square_number;
                            if (game_settings.chosen_plant == 1):
                                sunflower = Sunflower(screen, square)
                                if game_settings.total_sun >= sunflower.sun_cost:
                                    plants.add(sunflower)
                                    square.plant_here = True
                                    game_settings.total_sun -= sunflower.sun_cost
                            elif (game_settings.chosen_plant == 2):
                                peashooter = Peashooter(screen, square)
                                if game_settings.total_sun >= peashooter.sun_cost:
                                    plants.add(peashooter)
                                    square.plant_here = True
                                    game_settings.total_sun -= peashooter.sun_cost
                            elif (game_settings.chosen_plant == 3):
                                snowpea = Snowpea(screen, square)
                                if game_settings.total_sun >= snowpea.sun_cost:
                                    plants.add(snowpea)
                                    square.plant_here = True
                                    game_settings.total_sun -= snowpea.sun_cost
                            elif (game_settings.chosen_plant == 4):
                                repeater = Repeater(screen, square)
                                if game_settings.total_sun >= repeater.sun_cost:
                                    plants.add(repeater)
                                    square.plant_here = True
                                    game_settings.total_sun -= repeater.sun_cost
                            elif (game_settings.chosen_plant == 5):
                                gatling = Gatling(screen, square)
                                if game_settings.total_sun >= gatling.sun_cost:
                                    plants.add(gatling)
                                    square.plant_here = True
                                    game_settings.total_sun -= gatling.sun_cost
                            elif (game_settings.chosen_plant == 6):
                                wallnut = Wallnut(screen, square)
                                if game_settings.total_sun >= wallnut.sun_cost:
                                    plants.add(wallnut)
                                    square.plant_here = True
                                    game_settings.total_sun -= wallnut.sun_cost
                    for icon in icons:
                        if icon.rect.collidepoint(mouse_x, mouse_y):
                            game_settings.chosen_plant = icon.slot
                # print mouse_x;
                # print mouse_y;
            elif event.type == pygame.MOUSEMOTION:
                # print event.pos;
                for square in squares:
                    if square.rect.collidepoint(event.pos):
                        game_settings.highlighted_square = square
Esempio n. 5
0
def run_game():
    #初始化游戏并创建一个屏幕对象
    pygame.init()
    pygame.mixer.init()
    screen = pygame.display.set_mode((1000, 600))
    pygame.display.set_caption("Plants VS Zombie")
    ai_setting = Setting()

    #声音的设置
    soundwav = pygame.mixer.Sound("boom.wav")
    pygame.mixer.music.load("first.wav")
    pygame.mixer.music.play(-1)

    #创建play按钮
    play_button = Button(ai_setting, screen, "Play", 400)
    exit_button = Button(ai_setting, screen, "Exit", 500)

    #创建一个射手,一个豌豆编组,一个僵尸编组和一个僵尸Boss编组
    peashooter = Peashooter(ai_setting, screen)
    peas = Group()
    zombies = Group()
    bossGroup = Group()

    #创建一个用于存储游戏统计信息的实例,并创建记分牌
    status = GameStatus(ai_setting)
    scoreboard = Scoreboard(ai_setting, screen, status)

    #开始游戏的主循环
    while True:

        gf.check_events(ai_setting, screen, status, scoreboard, play_button,
                        exit_button, peashooter, zombies, peas)

        if status.game_active:
            peashooter.update()
            gf.update_peas(ai_setting, screen, status, scoreboard, soundwav,
                           peashooter, zombies, bossGroup, peas)
            gf.update_zombies(ai_setting, status, screen, scoreboard,
                              peashooter, zombies, bossGroup, peas)
            gf.update_bossGroup(ai_setting, status, screen, scoreboard,
                                peashooter, zombies, bossGroup, peas)

        gf.update_screen(ai_setting, screen, status, scoreboard, peashooter,
                         zombies, bossGroup, peas, play_button, exit_button)
Esempio n. 6
0
def check_events(screen, settings, squares, plants, bullets):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.MOUSEBUTTONDOWN:
            mouse_x, mouse_y = pygame.mouse.get_pos()
            for square in squares:
                if square.rect.collidepoint(mouse_x, mouse_y):
                    print "Square: ", square.square_number
                    plants.add(Peashooter(screen, square))
        elif event.type == pygame.MOUSEMOTION:
            for square in squares:
                if square.rect.collidepoint(event.pos):
                    settings.highlighted_square = square
Esempio n. 7
0
def enter():
    background = Background()
    game_world.add_object(background, 0)

    ui_slot = Ui_slot()
    game_world.add_object(ui_slot, 0)

    peashooter_ui = Peashooter_ui()
    game_world.add_object(peashooter_ui, 0)

    peashooter = Peashooter()
    game_world.add_object(peashooter, 1)

    global sun
    sun = Sun()
    normal_zombie = Normal_zombie()
def main():
    global sun_num_surface, choose
    global text
    index = 0
    while True:
        clock.tick(20)
        if not pygame.mixer.music.get_busy():
            pygame.mixer.music.play()
        index += 1
        # 碰撞检测
        for bullet in bulletGroup:
            for zombie in zombieGroup:
                if pygame.sprite.collide_mask(bullet, zombie):
                    zombie.energy -= 1
                    bulletGroup.remove(bullet)
        for wallNut in wallnutGroup:
            for zombie in zombieGroup:
                if pygame.sprite.collide_mask(wallNut, zombie):
                    zombie.ismeetwallnut = True
                    wallNut.zombies.add(zombie)
        for peashooter in peashooterGroup:
            for zombie in zombieGroup:
                if pygame.sprite.collide_mask(peashooter, zombie):
                    zombie.ismeetwallnut = True
                    peashooter.zombies.add(zombie)
        for sunflower in sunFlowerGroup:
            for zombie in zombieGroup:
                if pygame.sprite.collide_mask(sunflower, zombie):
                    zombie.ismeetwallnut = True
                    sunflower.zombies.add(zombie)
        screen.blit(bg_img, (0, 0))
        screen.blit(seedbank_img, (250, 0))
        screen.blit(sun_num_surface, (270, 60))

        screen.blit(flowerSeed, (320, 0))
        screen.blit(peashooterSeed, (382, 0))
        screen.blit(wallnutSeed, (446, 0))
        sunFlowerGroup.update(index)
        sunFlowerGroup.draw(screen)
        peashooterGroup.update(index)
        peashooterGroup.draw(screen)
        bulletGroup.update(index)
        bulletGroup.draw(screen)
        zombieGroup.update(index)
        zombieGroup.draw(screen)
        wallnutGroup.update(index)
        wallnutGroup.draw(screen)
        sunGroup.update(index)
        sunGroup.draw(screen)

        (x, y) = pygame.mouse.get_pos()
        if choose == 1:
            screen.blit(sunflowerImg, (x, y))
        elif choose == 2:
            screen.blit(peashooterImg, (x, y))
        elif choose == 3:
            screen.blit(wallnutImg, (x, y))
        for event in pygame.event.get():
            if event.type == GEN_SUN_EVENT:
                for sprite in sunFlowerGroup:
                    now = time.time()
                    if now - sprite.lasttime >= 5:
                        sun = Sun(sprite.rect)
                        sunGroup.add(sun)
                        sprite.lasttime = now

            if event.type == GEN_BULLET_EVENT:
                for sprite in peashooterGroup:
                    bullet = Bullet(sprite.rect, backgdsize)
                    bulletGroup.add(bullet)

            if event.type == GEN_ZOMBIE_EVENT:
                zombie = Zombie()
                zombieGroup.add(zombie)

            if event.type == GEN_FLAGZOMBIE_EVENT:
                flagzombie = Flagzombie()
                zombieGroup.add(flagzombie)

            if event.type == pygame.QUIT:
                exit()

            if event.type == pygame.MOUSEBUTTONDOWN:
                pressed_key = pygame.mouse.get_pressed()
                if pressed_key[0] == 1:
                    x, y = pygame.mouse.get_pos()
                    print(x, y)
                    if 320 <= x <= 382 and 0 <= y <= 89 and text >= 50:
                        # 点中了太阳花
                        choose = 1
                    elif 383 <= x < 446 and 0 <= y <= 89 and text >= 100:
                        # 点中了豌豆射手
                        choose = 2
                    elif 447 <= x < 511 and 0 <= y <= 89 and text >= 50:
                        # 点中了坚果墙
                        choose = 3
                    elif 250 < x < 1200 and 90 < y < 600:
                        if choose == 1:
                            current_time = time.time()
                            sunflower = Sunflower(current_time)
                            sunflower.rect.x = x
                            sunflower.rect.y = y
                            sunFlowerGroup.add(sunflower)
                            choose = 0

                            text -= 50
                            sun_font = pygame.font.SysFont('arial', 25)
                            sun_num_surface = sun_font.render(
                                str(text), True, (0, 0, 0))
                        elif choose == 2:
                            peashooter = Peashooter()
                            peashooter.rect.y = y
                            peashooter.rect.x = x
                            peashooterGroup.add(peashooter)
                            choose = 0

                            text -= 100
                            sun_font = pygame.font.SysFont('arial', 25)
                            sun_num_surface = sun_font.render(
                                str(text), True, (0, 0, 0))
                        elif choose == 3:
                            wallnut = Wallnut()
                            wallnut.rect.y = y
                            wallnut.rect.x = x
                            wallnutGroup.add(wallnut)
                            choose = 0

                            text -= 50
                            sun_font = pygame.font.SysFont('arial', 25)
                            sun_num_surface = sun_font.render(
                                str(text), True, (0, 0, 0))
                    for sun in sunGroup:
                        if sun.rect.collidepoint(x, y):
                            sunGroup.remove(sun)
                            text += 50
                            sun_font = pygame.font.SysFont('arial', 25)
                            sun_num_surface = sun_font.render(
                                str(text), True, (0, 0, 0))

        pygame.display.update()
Esempio n. 9
0
def check_events(screen, game_settings, squares, plants, bullets, icons, tick):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        if game_settings.game_active:
            # plants.add(Sunflower(screen,1,game_settings));
            if event.type == pygame.MOUSEBUTTONDOWN:
                mouse_x, mouse_y = pygame.mouse.get_pos()
                # print mouse_x;
                # print mouse_y;
                for square in squares:

                    if square.rect.collidepoint(mouse_x, mouse_y):
                        # print "Square: ",square.square_number;
                        if (game_settings.chosen_plant == 1
                                and game_settings.total_sun >= 50):
                            # for plant in plants:
                            # if plant.square == square and (plant.name=='gatling' or plant.name=='gunner3' or plant.name=='peashooter'):
                            # 	game_log = 'You can\'t downgrade your hero'
                            # else:
                            # 	plants.remove(plant);
                            # 	plants.add(Peashooter(screen,square,game_settings));
                            # 	game_settings.total_sun -= 50
                            remove_plant_in_square(square, plants)
                            plants.add(
                                Peashooter(screen, square, game_settings))
                            game_settings.total_sun -= 50
                            gunner_sound = pygame.mixer.Sound(
                                './images/gunner.wav')
                            gunner_sound.play()

                        elif (game_settings.chosen_plant == 2
                              and game_settings.total_sun >= 200):
                            for plant in plants:
                                if plant.square == square:
                                    if plant.name == 'peashooter':
                                        plants.remove(plant)
                                        plants.add(
                                            Gatling(screen, square,
                                                    game_settings))
                                        game_settings.total_sun -= 200
                                        gunner_sound = pygame.mixer.Sound(
                                            './images/gunner.wav')
                                        gunner_sound.play()
                        elif (game_settings.chosen_plant == 3
                              and game_settings.total_sun >= 1000):
                            for plant in plants:
                                if plant.square == square:
                                    if plant.name == 'gatling':
                                        plants.remove(plant)
                                        plants.add(
                                            Gunner3(screen, square,
                                                    game_settings))
                                        game_settings.total_sun -= 1000
                                        gunner_sound = pygame.mixer.Sound(
                                            './images/gunner3.wav')
                                        gunner_sound.play()
                        elif (game_settings.chosen_plant == 4
                              and game_settings.total_sun >= 5200):
                            for plant in plants:
                                if plant.square == square:
                                    if plant.name == 'gunner3':
                                        plants.remove(plant)
                                        plants.add(
                                            Gunner4(screen, square,
                                                    game_settings))
                                        game_settings.total_sun -= 5200
                                        gunner_sound = pygame.mixer.Sound(
                                            './images/gunner4.wav')
                                        gunner_sound.play()
                        elif (game_settings.chosen_plant == 5
                              and game_settings.total_sun >= 500):
                            remove_plant_in_square(square, plants)
                            plants.add(Sunflower(screen, square,
                                                 game_settings))
                            game_settings.total_sun -= 500
                            gunner_sound = pygame.mixer.Sound(
                                './images/mineral.wav')
                            gunner_sound.play()
                        elif (game_settings.chosen_plant == 6
                              and game_settings.total_sun >=
                              (30 + int(tick / 50))):
                            plants.add(Robot(screen, square, game_settings))
                            game_settings.total_sun -= (30 + int(tick / 50))

                for icon in icons:
                    if icon.rect.collidepoint(mouse_x, mouse_y):
                        game_settings.chosen_plant = icon.slot
                        # print "You clicked: ",icon.image;
                        icon.chosen = True
                    else:
                        icon.chosen = False
                        # plants.add(Peashooter(screen,square));
            elif event.type == pygame.MOUSEMOTION:
                # print event.pos
                for square in squares:
                    if square.rect.collidepoint(event.pos):
                        game_settings.highlighted_square = square