Exemple #1
0
def run_game():
    """The function that runs the game"""

    #Initialize pygame and settings
    pygame.init()
    ai_settings = Settings(
    )  #creating 'ai_settings' from our imported Settings class, which acts as the overall settings for our game

    #Initializing 'screen' surface
    screen = pygame.display.set_mode(
        (ai_settings.screen_width,
         ai_settings.screen_height))  #Initializing screen size
    pygame.display.set_caption(
        "Apple Bucket Game")  #Captioning our screen (the window we play in)
    bg_color = (ai_settings.bg_color)  #Setting background color

    #Making a bucket and apple:
    bucket = Bucket(ai_settings, screen)
    apple = Apple(ai_settings, screen)

    #Creating an instance that represents our stats
    stats = GameStats(ai_settings)

    #Starting the main loop for the game:
    while True:

        abgf.check_events(ai_settings, screen,
                          bucket)  #Checking for events (player input)

        #If still have lives
        if stats.game_active:
            bucket.update()  #Updates bucket position
            apple = abgf.update_apple(
                ai_settings, apple, screen, bucket, stats
            )  #Updating falling apple or redraws if it collided or hit bottom
            apple.blitme()

        abgf.update_screen(ai_settings, screen, bucket,
                           apple)  #Drawing updates things on the screen
Exemple #2
0
def run_game():
    # 初始化pygame
    pygame.init()
    #初始化页面
    ai_settings = Settings()
    screen = pygame.display.set_mode(
        (ai_settings.screen_width, ai_settings.screen_height))
    ship = Ship(screen)
    bucket = Bucket(screen)
    bird = Bird(screen)
    birds = Group()
    bullets = Group()
    pygame.display.set_caption(ai_settings.title)
    # print(pygame.font.get_fonts())
    # 进入游戏页面
    begin_game(ai_settings, screen)
    # game loop
    while True:
        gf.check_event(ship, bucket)
        #  gf.update_screen(ai_settings, screen, ship)
        #如果选择的模式只操作一个的话,那么另外一个的物品会自己运动
        if ai_settings.begin_game_num == 1:
            ship.moving_down = False
            ship.moving_up = False
            ship.moving_left = False
            ship.moving_right = False

            if ai_settings.game_ship_UD == 50:
                ship.add = True
                ai_settings.game_ship_UD = random.randint(0, 100)
            elif ai_settings.game_ship_UD > 50:
                ship.moving_up = True
                ai_settings.game_ship_UD -= 1
            elif ai_settings.game_ship_UD < 50:
                ship.moving_down = True
                ai_settings.game_ship_UD += 1
            if ai_settings.game_ship_LR == 50:
                ai_settings.game_ship_LR = random.randint(0, 100)
            elif ai_settings.game_ship_LR > 50:
                ship.moving_left = True
                ai_settings.game_ship_LR -= 1
            elif ai_settings.game_ship_LR < 50:
                ship.moving_right = True
                ai_settings.game_ship_LR += 1
        if ai_settings.begin_game_num == 2:
            bucket.moving_right = False
            bucket.moving_left = False
            if ai_settings.game_bucket_LR == 50:
                ai_settings.game_bucket_LR = random.randint(0, 100)
            elif ai_settings.game_bucket_LR > 50:
                bucket.moving_right = True
                ai_settings.game_bucket_LR -= 1
            elif ai_settings.game_bucket_LR < 50:
                bucket.moving_left = True
                ai_settings.game_bucket_LR += 1

        ship.update(ai_settings)
        bucket.update(ai_settings)
        if ship.add:
            if pygame.time.get_ticks(
            ) - ai_settings.ball_lastT > ai_settings.ball_interval:
                print(pygame.time.get_ticks())
                new_bullet = Bullet(screen, ship, bucket, ai_settings.score)
                bullets.add(new_bullet)
                ship.bullet = True
                ai_settings.ball_lastT = pygame.time.get_ticks()
            # print(ai_settings.ball_lastT)
        if random.randint(0, 3) == 1:
            if pygame.time.get_ticks(
            ) - ai_settings.bird_lastT > ai_settings.bird_interval:
                new_bird = Bird(screen)
                birds.add(new_bird)
                ai_settings.bird_lastT = pygame.time.get_ticks()
        gf.update_screen(ai_settings, screen, ship, bullets, bucket, birds)
        #    if ship.bullet :
        #        gf.update_screen(ai_settings, screen, ship,bullets,bucket,birds)
        #    else:
        #        gf.update_screen1(ai_settings, screen, ship,bucket,birds)

        if ai_settings.score >= ai_settings.winScore:
            over_game(ai_settings, screen, True)
        if ai_settings.health_point <= 0:
            over_game(ai_settings, screen, False)