Ошибка TypeError для блиттинга - PullRequest
0 голосов
/ 25 апреля 2020

Вот мое сообщение об ошибке

C:/Users/deepa/OneDrive/Desktop/python development/FLAPPY BIRD/main.py:57: DeprecationWarning: an integer is required (got type float).  Implicit conversion to integers using __int__ is deprecated, and may be removed in a future version of Python.
      SCREEN.blit(GAME_SPRITES['base'], (basex, GROUNDY))
    Traceback (most recent call last):
      File "C:/Users/deepa/OneDrive/Desktop/python development/FLAPPY BIRD/main.py", line 217, in <module>
        mainGame()
      File "C:/Users/deepa/OneDrive/Desktop/python development/FLAPPY BIRD/main.py", line 144, in mainGame
        SCREEN.blit(GAME_SPRITES['pipe'][0], (upperpipe['x'], upperpipe['y']))
    TypeError: invalid destination position for blit

Код

import random
import sys
import pygame
from pygame.locals import *
FPS = 32
SCREENWIDTH = 289
SCREENHEIGHT = 511
SCREEN = pygame.display.set_mode((SCREENWIDTH, SCREENHEIGHT))
ROUNDY = SCREENHEIGHT * 0.9
GAME_SPRITES = {}
GAME_SOUNDS = {}
PLAYER = 'GALLERY/sprites/bird.png'
BACKGROUND = 'GALLERY/sprites/background4.jpg'
PIPE = 'GALLERY/sprites/pipe.png'

def welcomeScreen():

    playerx = int(SCREENWIDTH / 5)
    playery = int((SCREENHEIGHT - GAME_SPRITES['player'].get_height()) / 2)
    messagex = int((SCREENWIDTH - GAME_SPRITES['message'].get_width()) / 2)
    messagey = int(SCREENHEIGHT * 0.13)
    basex = 0
    while True:
        for event in pygame.event.get():
              if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
                pygame.quit()
                sys.exit()    
            elif event.type == KEYDOWN and (event.key == K_SPACE or event.key == K_UP):
                return
            else:
                SCREEN.blit(GAME_SPRITES['background'], (0, 0))
                SCREEN.blit(GAME_SPRITES['player'], (playerx, playery))
                SCREEN.blit(GAME_SPRITES['message'], (messagex, messagey))
                SCREEN.blit(GAME_SPRITES['base'], (basex, GROUNDY))
                pygame.display.update()
                FPSCLOCK.tick(FPS)

def mainGame():
    score = 0
    playerx = int(SCREENWIDTH/5)
    playery = int(SCREENWIDTH/2)
    basex = 0
    newpipe1= getRandomPipe()
    newpipe2 = getRandomPipe()
    upperpipes = [
        {'x': SCREENWIDTH + 200, 'y': newpipe1[0]['y']},
        {'x': SCREENWIDTH + 200 + SCREENHEIGHT/2, 'y': newpipe1[1]['y']}
    ] 
    lowerpipes = [
        {'x': SCREENWIDTH + 200, 'y': newpipe2[0]['y']},
        {'x': SCREENWIDTH + 200 + (SCREENHEIGHT/2), 'y': newpipe2[1]['y']}
    ] 
    pipe_velocx = -4
    player_velocityY = -9 
    player_maxvelicotyY = 10 
    player_minvelocityY = -8 
    player_acceY = 1
    playerFlapAccVelo = -8 
    playerFlapped = False 

    while True:
        for event in pygame.event.get():
            if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
                pygame.quit()
                sys.exit()
            if event.type == KEYDOWN or (event.type == K_SPACE or event.type == K_UP):
                if playery > 0:
                    player_velocityY = playerFlapAccVelo
                    playerFlapped = True
                    GAME_SOUNDS['wing'].play()

        crashTest = isCollide(playerx, playery, upperpipes, lowerpipes)
        if crashTest: 
            return

        playerMid_position = playerx + GAME_SPRITES['player'].get_width()/2
        for pipe in upperpipes:
            pipeMidpos = pipe['x'] + GAME_SPRITES['pipe'][0].get_width()/2
            if pipeMidpos <= playerMid_position < playerMid_position +4:
                score +=1
                print(f'Your score is: {score}')
                GAME_SOUNDS['point'].play()

        if player_velocityY < player_maxvelicotyY and not playerFlapped:
            player_velocityY += player_acceY

        if playerFlapped:
            playerFlapped = False
        playerHeight = GAME_SPRITES['player'].get_height()
        playery = playery + min(player_velocityY, GROUNDY - playery - playerHeight)

        #moves pipes to the left
        for upperpipe, lowerpipe in zip(upperpipes, lowerpipes):
            upperpipe['x'] += pipe_velocx
            lowerpipe['x'] += pipe_velocx

        #Add a new pipe when the 1st pipe is about to cross the screen
        if 0 < upperpipes[0]['x']<5:
            newpipe = getRandomPipe()
            upperpipes.append(newpipe[0])
            lowerpipes.append(newpipe[1])

        #if the pipe is out of the screen remove it
        if upperpipes[0]['x'] < -GAME_SPRITES['pipe'][0].get_width():
            upperpipes.pop(0)
            lowerpipes.pop(0)

        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, GROUNDY))
        SCREEN.blit(GAME_SPRITES['player'],(playerx, playery))
        myDigits = [int(x) for  x in list(str(score))]
        width = 0
        for digits in myDigits:
            width += GAME_SPRITES['numbers'][digits].get_width()
        Xoffset = (SCREENWIDTH - width)/2

        for digits in myDigits:
            SCREEN.blit(GAME_SPRITES['numbers'][digits], (Xoffset, SCREENHEIGHT * 0.12))
            Xoffset += GAME_SPRITES['numbers'][digits].get_width()
        pygame.display.update()
        FPSCLOCK.tick(FPS)


def isCollide(playerx, playery, upperpipes, lowerpipes):
    return False

def getRandomPipe():
    '''
    generate position of two pipe for blitting on the screen
    '''
    pipeHeight = GAME_SPRITES['pipe'][0].get_height()
    offset = SCREENHEIGHT/3
    y2 = offset + random.randrange(0, int(SCREENHEIGHT - GAME_SPRITES['base'].get_height() - 1.2 * offset)) 
    pipex = SCREENWIDTH + 10 
    y1 = pipeHeight - y2 + offset
    pipe = [
        {'x': 'pipex', 'y': '-y1'}, #upper pipe
        {'x': 'pipex', 'y': 'y2'} #lower pipe
    ]
    return pipe

if __name__ == "__main__":

    pygame.init()  

    # this will control the fps of the game suppose i have given fps as 40 so it will rem ain under 40 onlly it will not go beyond 40
    FPSCLOCK = pygame.time.Clock()

    pygame.display.set_caption('FLAPY BIRD')

    GAME_SPRITES['number'] = (
        # this .convert_alpha() is used for faster blitting of your image in the game
        pygame.image.load('GALLERY/sprites/0.png').convert_alpha(),
        pygame.image.load('GALLERY/sprites/1.png').convert_alpha(),
        pygame.image.load('GALLERY/sprites/2.png').convert_alpha(),
        pygame.image.load('GALLERY/sprites/3.png').convert_alpha(),
        pygame.image.load('GALLERY/sprites/4.png').convert_alpha(),
        pygame.image.load('GALLERY/sprites/5.png').convert_alpha(),
    )

    GAME_SPRITES['message'] = pygame.image.load('GALLERY/sprites/message.png').convert_alpha()
    GAME_SPRITES['base'] = pygame.image.load('GALLERY/sprites/base.png').convert_alpha()
    GAME_SPRITES['pipe'] = (pygame.transform.rotate(pygame.image.load(PIPE).convert_alpha(), 180),
        pygame.image.load(PIPE).convert_alpha()
    )

    GAME_SOUNDS['die'] = pygame.mixer.Sound('GALLERY/audio/die.wav')
    GAME_SOUNDS['hit'] = pygame.mixer.Sound('GALLERY/audio/hit.wav')
    GAME_SOUNDS['point'] = pygame.mixer.Sound('GALLERY/audio/point.wav')
    GAME_SOUNDS['swoosh'] = pygame.mixer.Sound('GALLERY/audio/swoosh.wav')
    GAME_SOUNDS['wing'] = pygame.mixer.Sound('GALLERY/audio/wing.wav')
    GAME_SPRITES['background'] = pygame.image.load(BACKGROUND).convert()
    GAME_SPRITES['player'] = pygame.image.load(PLAYER).convert_alpha()

    while True:
        welcomeScreen()
        mainGame()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...