Пример #1
0
def mainGame():
    score = 0

    IMGS = BIRD_IMGS
    image = IMGS[0]
    ANIMATION_TIME = 5
    img_count = 0

    player = Player(int(cg.settings.screen.width / 5),
                    int(cg.settings.screen.height / 2), -9, 10, -8, 0.3,
                    BIRD_IMGS, -8, False)
    basex = 0

    # create 2 random pipes
    newPipe1 = Pipe(-4, GAME_SPRITES, cg.settings.screen.height,
                    cg.settings.screen.width)
    newPipe1r = newPipe1.getRandomPipe()
    newPipe2 = Pipe(-4, GAME_SPRITES, cg.settings.screen.height,
                    cg.settings.screen.width)
    newPipe2r = newPipe2.getRandomPipe()

    # list of upper pipes
    upperPipes = [
        {
            'x': cg.settings.screen.width + 200,
            'y': newPipe1r[0]['y']
        },
        {
            'x': cg.settings.screen.width + 200 + 650,
            'y': newPipe2r[0]['y']
        },
    ]
    # list of lower pipes
    lowerPipes = [
        {
            'x': cg.settings.screen.width + 200,
            'y': newPipe1r[1]['y']
        },
        {
            'x': cg.settings.screen.width + 200 + 650,
            'y': newPipe2r[1]['y']
        },
    ]

    model = load_model(cg.paths.model)

    webcam = cv2.VideoCapture(0)

    while webcam.isOpened():

        proceed, frame = webcam.read()

        if not proceed:
            break

        img_count += 1

        if player.velocityY < 0:
            if img_count < ANIMATION_TIME:
                image = IMGS[0]
            elif img_count < ANIMATION_TIME * 2:
                image = IMGS[1]
            elif img_count < ANIMATION_TIME * 3:
                image = IMGS[2]
            elif img_count < ANIMATION_TIME * 4:
                image = IMGS[1]
            elif img_count < ANIMATION_TIME * 4 + 1:
                image = IMGS[0]
                img_count = 0
        else:
            image = IMGS[0]
            img_count = 0

        if inference(model, frame):
            if player.positionY > 0:
                player.velocityY = player.flappedAccVel
                player.flapped = True
                image = IMGS[2]
                GAME_SOUNDS['wing'].play()

        if cg.webcam.display:
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            frame = cv2.resize(frame, (128, 128))
            frame = cv2.transpose(frame)
            frame = cv2.flip(frame, flipCode=0)
            frame = pygame.surfarray.make_surface(frame)

        for event in pygame.event.get():
            if event.type == QUIT or (event.type == KEYDOWN
                                      and event.key == K_ESCAPE):
                pygame.quit()
                sys.exit()

        if isCollide(player.positionX, player.positionY, upperPipes,
                     lowerPipes):
            return

        # score check
        playerMidPos = player.positionX + GAME_SPRITES['player'].get_width() / 2
        for pipe in upperPipes:
            pipeMidPos = pipe['x'] + GAME_SPRITES['pipe'][0].get_width() / 2
            if pipeMidPos <= playerMidPos < pipeMidPos + 4:
                score += 1
                GAME_SOUNDS['point'].play()

        if player.velocityY < player.maxVelocityY and not player.flapped:
            player.velocityY += player.accY

        if player.flapped:
            player.flapped = False
        playerHeight = GAME_SPRITES['player'].get_height()
        player.positionY = player.positionY + min(
            player.velocityY, cg.ground.y - player.positionY - playerHeight)

        # move pipes to left
        for upperPipe, lowerPipe in zip(upperPipes, lowerPipes):
            upperPipe['x'] += newPipe1.velocityX
            lowerPipe['x'] += newPipe1.velocityX

        # add new pipe when the first pipe about to cross leftmost part of screen
        if 0 < upperPipes[0]['x'] < 5:
            newpipe = Pipe(-4, GAME_SPRITES, cg.settings.screen.height,
                           cg.settings.screen.width).getRandomPipe()
            upperPipes.append(newpipe[0])
            lowerPipes.append(newpipe[1])

        # remove pipe out of screen
        if upperPipes[0]['x'] < -GAME_SPRITES['pipe'][0].get_width():
            upperPipes.pop(0)
            lowerPipes.pop(0)

        # blit sprites
        SCREEN.blit(GAME_SPRITES['background'], (0, 0))
        for upperPipe, lowerPipe in zip(upperPipes, lowerPipes):
            SCREEN.blit(GAME_SPRITES['pipe'][0],
                        (upperPipe['x'], upperPipe['y']))
            SCREEN.blit(GAME_SPRITES['pipe'][1],
                        (lowerPipe['x'], lowerPipe['y']))

        SCREEN.blit(GAME_SPRITES['base'], (basex, cg.ground.y))

        SCREEN.blit(image, (player.positionX, player.positionY))

        if cg.webcam.display:
            SCREEN.blit(frame, (cg.settings.screen.width - 136, 8))

        myDigits = [int(x) for x in list(str(score))]

        width = 0

        for digit in myDigits:
            width += GAME_SPRITES['numbers'][digit].get_width()

        Xoffset = (cg.settings.screen.width - width) / 2

        for digit in myDigits:
            SCREEN.blit(GAME_SPRITES['numbers'][digit],
                        (Xoffset, cg.settings.screen.height * 0.12))
            Xoffset += GAME_SPRITES['numbers'][digit].get_width()

        pygame.display.update()
        FPSCLOCK.tick(cg.settings.fps)

    webcam.release()
    cv2.destroyAllWindows()