-2

Im building a pacman game using pygame on my MacBook, I've written the code in visual studio code and have used pip install pygame in the terminal, however, when I run the python script for my game I get an error.

all my files are in one folder called PacMan

files I have so far (3 in total)

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 

but i keep getting this error when i run main.py from my python

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'

does anyone know why?

user1767316
  • 3,276
  • 3
  • 37
  • 46
  • 1
    You haven't installed the library. `pip install pygame`, or whatever the installation directions for the library say. – Carcigenicate Nov 04 '20 at 15:06

1 Answers1

0

I had this same problem a few days ago. My solution was to use a virtual environment. To do this, open a terminal in your folder and use the following commands:

pip install pipenv
pipenv shell
pipenv install pygame

After this, you need to select the correct python interpreter. In VS Code, hit ctrl+shift+p Then click the correct interpreter (should have the name of the folder). Then try to run the file. If that doesn't work, try to run this in the terminal: pipenv run {name of the python file, example: app.py}

Hope that works for you

dejanualex
  • 3,872
  • 6
  • 22
  • 37