为什么当我使用子进程中的call()运行python文件时,会得到ModuleNotFoundE

2024-09-30 01:22:23 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我正在制作一个flappy bird游戏,每当玩家死亡时,我关闭程序并再次运行它:

call(["python", "app.py"])
quit(1)

每当call(["python", "app.py"])运行时,我就得到ModuleNotFoundError: No module named 'pygame' 即使我已经安装了pygame

应用程序:

import pygame as py
from subprocess import call



# Initialize Pygame
py.init()
# Window
icon = py.image.load(('flappyicon.jpg'))
window = py.display.set_mode((800, 600))
window.fill((155, 152, 13))
py.display.set_caption('Flappy Bird')
py.display.set_icon(icon)

# Player
playerImg = py.image.load('flappy.png')
playerImg = py.transform.scale(playerImg, (45, 45))
playerY = 250
playerX = 30
# Player Physics
playerYDownVelocity = 0


def player(playerX, playerY):
    window.blit(playerImg, (playerX, playerY))


# Game Loop
lost = False
running = True
while running:
    window.fill((155, 152, 13))
    for event in py.event.get():
        if event.type == py.QUIT:
            running = False
        if event.type == py.KEYDOWN:
            if event.key == py.K_SPACE:
                playerYDownVelocity = -0.4

            if event.key == py.K_r:
                if lost == True:
                    print('elooor')
                    rerun = 'app.py'
                    call(["python", "app.py"])
                    quit(1)

    if playerY >= 550:
        # gameOver()
        lost = True
        playerY = 550
    elif playerY <= 0:
        playerY = 0

    # Physics
    playerYDownVelocity += 0.001
    playerY += playerYDownVelocity
    player(playerX, playerY)
    py.display.update()

当我从pycharm运行它时,它起作用了。我在谷歌上搜索了一下,但什么也没找到


Tags: pyeventappifdisplaycallwindowpygame
1条回答
网友
1楼 · 发布于 2024-09-30 01:22:23

好吧有两件事

  1. first if you are making this game withing a virtual environment(and you most definitely should, make sure pygame is installed withing that virtual environment, otherwise it is just installed globally in your system and not in your virtual environment and that is the most likely reason for that error

  2. two, you should consider using Popen() instead of call() but this part is just a friendly suggestion

相关问题 更多 >

    热门问题