当我尝试打开我的 python 游戏时,它立即崩溃

时间:2020-12-21 15:06:24

标签: python pygame pygame-surface

当我尝试打开 Python 游戏时,它立即崩溃。这是一个活泼的鸟类游戏克隆。我使用 Python 3.7.7、Pygame 2.0.0 和 Visual Studio Code IDE 制作了它。出现以下错误:

<块引用>
Traceback (most recent call last):
 File "c:\Users\KIIT\Desktop\Go Alien Go\Go Alien Go.py", line 190, in <module>
   pygame.image.load(os.path.join('data','0.png')).convert_alpha(),
FileNotFoundError: No such file or directory.

最近三天我一直在努力解决这个问题,我搜索了很多,但找不到解决问题的方法。

这是我的代码:

import random 
import sys 
import pygame
from pygame.locals import * 
import os

pygame.init()

FPS = 32
SCREENWIDTH = 289
SCREENHEIGHT = 511
SCREEN = pygame.display.set_mode((SCREENWIDTH, SCREENHEIGHT))
GROUNDY = SCREENHEIGHT * 0.4
GAME_SPRITES = {}
GAME_SOUNDS = {}
PLAYER = os.path.join('data','alien.jpg')
BACKGROUND = os.path.join('data','background.jpg')
PIPE = os.path.join('data','pipe.jpg')

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+(SCREENWIDTH/2), 'y':newPipe2[0]['y']},
    ]
    
    lowerPipes = [
        {'x': SCREENWIDTH+200, 'y':newPipe1[1]['y']},
        {'x': SCREENWIDTH+200+(SCREENWIDTH/2), 'y':newPipe2[1]['y']},
    ]

    pipeVelX = -4

    playerVelY = -9
    playerMaxVelY = 10
    playerMinVelY = -8
    playerAccY = 1

    playerFlapAccv = -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 and (event.key == K_SPACE or event.key == K_UP):
                if playery > 0:
                    playerVelY = playerFlapAccv
                    playerFlapped = True


        crashTest = isCollide(playerx, playery, upperPipes, lowerPipes) 
        if crashTest:
            return     

        playerMidPos = playerx + 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
                print(f"Your score is {score}") 


        if playerVelY <playerMaxVelY and not playerFlapped:
            playerVelY += playerAccY

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

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

    
        if 0<upperPipes[0]['x']<5:
            newpipe = getRandomPipe()
            upperPipes.append(newpipe[0])
            lowerPipes.append(newpipe[1])


        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 digit in myDigits:
            width += GAME_SPRITES['numbers'][digit].get_width()
        Xoffset = (SCREENWIDTH - width)/2

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

def isCollide(playerx, playery, upperPipes, lowerPipes):
    if playery> GROUNDY - 25  or playery<0:
        return True
    
    for pipe in upperPipes:
        pipeHeight = GAME_SPRITES['pipe'][0].get_height()
        if(playery < pipeHeight + pipe['y'] and abs(playerx - pipe['x']) < GAME_SPRITES['pipe'][0].get_width()):
            return True

    for pipe in lowerPipes:
        if (playery + GAME_SPRITES['player'].get_height() > pipe['y']) and abs(playerx - pipe['x']) < GAME_SPRITES['pipe'][0].get_width():
            return True

    return False

def getRandomPipe():
    
    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}, 
        {'x': pipeX, 'y': y2} 
    ]
    return pipe






if __name__ == "__main__":
    
    pygame.init() 
    FPSCLOCK = pygame.time.Clock()
    pygame.display.set_caption('Go Alien Go by Aaishwarya Halder')
    GAME_SPRITES['numbers'] = ( 
        pygame.image.load(os.path.join('data','0.png')).convert_alpha(),
        pygame.image.load(os.path.join('data','1.png')).convert_alpha(),
        pygame.image.load(os.path.join('data','2.png')).convert_alpha(),
        pygame.image.load(os.path.join('data','3.png')).convert_alpha(),
        pygame.image.load(os.path.join('data','4.png')).convert_alpha(),
        pygame.image.load(os.path.join('data','5.png')).convert_alpha(),
        pygame.image.load(os.path.join('data','6.png')).convert_alpha(),
        pygame.image.load(os.path.join('data','7.png')).convert_alpha(),
        pygame.image.load(os.path.join('data','8.png')).convert_alpha(),
        pygame.image.load(os.path.join('data','9.png')).convert_alpha(),
    )

    GAME_SPRITES['message'] =pygame.image.load(os.path.join('data','message.png')).convert_alpha()
    GAME_SPRITES['base'] =pygame.image.load(os.path.join('data','base.png')).convert_alpha()
    GAME_SPRITES['pipe'] =(pygame.transform.rotate(pygame.image.load( PIPE).convert_alpha(), 180), 
    pygame.image.load(PIPE).convert_alpha()
    )


    GAME_SPRITES['background'] = pygame.image.load(BACKGROUND).convert()
    GAME_SPRITES['player'] = pygame.image.load(PLAYER).convert_alpha()

    while True:
        welcomeScreen() 
        mainGame() 

2 个答案:

答案 0 :(得分:2)

发生错误的原因是文件 0.png 不存在或不在同一目录中。 如果是后者,则您必须提供文件的完整文件路径。当您在正确的位置时,可以使用终端/cmd 和关键字 pwd 来完成。

答案 1 :(得分:0)

0.png 不存在 xD。如果是,则说明您没有将其放置在正确的目录中。