Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在Mac上使用Pygame的Pacman游戏_Python_Pygame - Fatal编程技术网

Python 在Mac上使用Pygame的Pacman游戏

Python 在Mac上使用Pygame的Pacman游戏,python,pygame,Python,Pygame,我在MacBook上使用pygame构建了一个pacman游戏,我在visual studio代码中编写了代码,并在终端中使用了pip install pygame,但是,当我运行游戏的python脚本时,我得到了一个错误 我所有的文件都在一个叫做PacMan的文件夹中 我目前拥有的文件(共3个) main.py: from app_class import * if __name__ == '__main__': app = App() app.run() app_clas

我在MacBook上使用pygame构建了一个pacman游戏,我在visual studio代码中编写了代码,并在终端中使用了pip install pygame,但是,当我运行游戏的python脚本时,我得到了一个错误

我所有的文件都在一个叫做PacMan的文件夹中

我目前拥有的文件(共3个)

main.py:

from app_class import *

if __name__ == '__main__':
    app = App()
    app.run()
app_class.py:

    import pygame
    import sys
    from settings import *


    pygame.init()
    vec = pygame.math.Vector2


    class App:
        def __init__(self):
            self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
            self.clock = pygame.time.Clock()
            self.running = True
            self.state = 'start'
        def run(self):
            while self.running:
                if self.state == 'start':
                    self.start_events()
                    self.start_update()
                    self.start_draw()
                self.clock.tick(FPS)
            pygame.quit()
            sys.exit()

    ############################################################### Helper Functions ############################
        def draw_text(self, words, screen, pos, size, colour, font_name, centered=False):
            font = pygame.font.SysFont(font_name, size)
            text = font.render(words, False, colour)
            text_size = text.get_size()
            if centered:
                pos[0] = pos[0]-text_size[0]//2
                pos[1] = pos[1]-text_size[1]//2
            screen.blit(text, pos)


    ############################################################### INTRO Functions ############################
        
        def start_events(self):
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.running = False
                if event.type == pygame.KEYDOWN and event.key == pygame.K_Space:
                    self.state = 'playing'

        
        def start_update(self):
            pass

        def start_draw(self):
            self.screen.fill(BLACK)
            self.draw_text('PUSH SPACE BAR',self.screen, (WIDTH//2, HIEGHT//2), START_TEXT_SIZE, (170, 132, 58), START_FONT)
            pygame.display.update()
settings.py:

    #screen settings

WIDTH, HEIGHT = 448, 596
FPS = 60
#colour settings
BlACK = (0,0,0)

#font settings 
START_TEXT_SIZE = 16
START_FONT = 'arial black'
#player settings
#mob settings 
但是,当我从python运行main.py时,总是会出现这个错误

Traceback (most recent call last):
  File "/Users/therealfawcett/Documents/University/Third-Year/ICP - 3025 - Apps Artificial Intelligence/Assignments /PacMan/main.py", line 1, in <module>
    from app_class import *
  File "/Users/therealfawcett/Documents/University/Third-Year/ICP - 3025 - Apps Artificial Intelligence/Assignments /PacMan/app_class.py", line 1, in <module>
    import pygame
ModuleNotFoundError: No module named 'pygame'
回溯(最近一次呼叫最后一次):
文件“/Users/therealfwcett/Documents/University/Third Year/ICP-3025-Apps-Artificial Intelligence/Assignments/PacMan/main.py”,第1行,在
从app_类导入*
文件“/Users/therealfwcett/Documents/University/Third Year/ICP-3025-Apps-Artificial Intelligence/Assignments/PacMan/app_class.py”,第1行,在
导入pygame
ModuleNotFoundError:没有名为“pygame”的模块

有人知道为什么吗?

使用Python3时,您需要使用pip3导入

pip3 install pygame

几天前我也有同样的问题。我的解决方案是使用虚拟环境。为此,请在文件夹中打开一个终端并使用以下命令:

pip安装pipenv
管壳
pipenv安装pygame
在此之后,您需要选择正确的python解释器。在VS代码中,按ctrl+shift+p 然后单击正确的解释器(应该有文件夹的名称)。 然后尝试运行该文件。如果不起作用,请尝试在终端中运行: pipenv运行{python文件的名称,例如:app.py}


希望对您有用

您尚未安装库
pip安装pygame
,或者库的任何安装说明。