[Python] What should I do if pygame suddenly stops working in pycharm?

Discussão em 'Python' iniciado por Stack, Maio 31, 2025.

  1. Stack

    Stack Membro Participativo

    So basically the title. I'm coding a really simple game in pycharm and I'm using pygame to run it. It was working just fine intitially, all I had to do was type in 'python3 main.py'. After saving and exiting out of pycharm, I went back into the project, and now it's not working.

    ModuleNotFoundError: No module named 'pygame'

    This is my full code for the project. Whenever I run the project by simply pressing the run button, the project runs using pygame, my issue is with the terminal not even knowing what pygame is anymore.

    import pygame
    import random

    # Initial values once game starts
    def reset_game():
    return {
    'player_x': 100,
    'player_y': 300,
    'velocity_y': 0,
    'obstacles': [{'x': 800, 'y': 300 + 40 - 40, 'passed': False}],
    'game_over': False,
    'start_ticks': pygame.time.get_ticks(),
    'elapsed_time': 0,
    'last_obstacle_time': pygame.time.get_ticks(),
    'next_obstacle_delay': random.randint(500, 1000),
    'score': 0
    }

    pygame.init()
    screen = pygame.display.set_mode((800, 400))
    pygame.display.set_caption("Square Jump")
    clock = pygame.time.Clock()

    player_width = 40
    player_height = 40
    player_color = (90, 3, 150)
    gravity = 0.8
    jump_strength = -15
    ground_y = 300
    obstacle_width = 30
    obstacle_height = 40
    obstacle_color = (255, 0, 0)
    obstacle_speed = 6

    button_rect = pygame.Rect(225, 300, 150, 50)
    quit_button_rect = pygame.Rect(425, 300, 150, 50)
    button_color = (70, 130, 180)
    button_hover_color = (100, 160, 210)
    button_text_color = (255, 255, 255)

    state = reset_game()
    running = True
    show_start_screen = True

    while running:
    while show_start_screen and running:
    for event in pygame.event.get():
    if event.type == pygame.QUIT:
    running = False
    elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
    if button_rect.collidepoint(event.pos):
    show_start_screen = False
    state = reset_game()

    screen.fill((44, 212, 132))
    font_title = pygame.font.SysFont(None, 80)
    title_text = font_title.render('Square Jump', True, (255, 255, 255))
    title_rect = title_text.get_rect(center=(400, 150))
    screen.blit(title_text, title_rect)

    mouse_pos = pygame.mouse.get_pos()
    color = button_hover_color if button_rect.collidepoint(mouse_pos) else button_color
    pygame.draw.rect(screen, color, button_rect, border_radius=8)
    font_button = pygame.font.SysFont(None, 36)
    button_text = font_button.render("Start", True, button_text_color)
    button_text_rect = button_text.get_rect(center=button_rect.center)
    screen.blit(button_text, button_text_rect)

    pygame.display.flip()
    clock.tick(60)

    while not state['game_over'] and running and not show_start_screen:
    for event in pygame.event.get():
    if event.type == pygame.QUIT:
    running = False

    keys = pygame.key.get_pressed()
    if keys[pygame.K_UP] and state['player_y'] == ground_y:
    state['velocity_y'] = jump_strength

    state['velocity_y'] += gravity
    state['player_y'] += state['velocity_y']

    if state['player_y'] > ground_y:
    state['player_y'] = ground_y
    state['velocity_y'] = 0

    for obstacle in state['obstacles']:
    obstacle['x'] -= obstacle_speed

    now = pygame.time.get_ticks()
    if now - state['last_obstacle_time'] > state['next_obstacle_delay']:
    state['obstacles'].append({'x': 800, 'y': ground_y + player_height - obstacle_height, 'passed': False})
    state['last_obstacle_time'] = now
    state['next_obstacle_delay'] = random.randint(450, 1200)

    state['obstacles'] = [obs for obs in state['obstacles'] if obs['x'] + obstacle_width > 0]

    player_rect = pygame.Rect(state['player_x'], state['player_y'], player_width, player_height)
    for obstacle in state['obstacles']:
    obstacle_rect = pygame.Rect(obstacle['x'], obstacle['y'], obstacle_width, obstacle_height)
    if player_rect.colliderect(obstacle_rect):
    state['game_over'] = True
    state['elapsed_time'] = (pygame.time.get_ticks() - state['start_ticks']) / 1000
    break
    # Score: if obstacle passed and not already counted
    if not obstacle.get('passed', False) and obstacle['x'] + obstacle_width < state['player_x']:
    obstacle['passed'] = True
    state['score'] += 1

    screen.fill((44, 212, 132))
    ground_height = 20
    pygame.draw.rect(screen, (0, 0, 0), (0, ground_y + player_height, 800, ground_height))
    pygame.draw.rect(screen, player_color, (state['player_x'], state['player_y'], player_width, player_height))
    for obstacle in state['obstacles']:
    triangle_points = [
    (obstacle['x'], obstacle['y'] + obstacle_height),
    (obstacle['x'] + obstacle_width // 2, obstacle['y']),
    (obstacle['x'] + obstacle_width, obstacle['y'] + obstacle_height)
    ]
    pygame.draw.polygon(screen, obstacle_color, triangle_points)
    font = pygame.font.SysFont(None, 36)
    elapsed_time = (pygame.time.get_ticks() - state['start_ticks']) / 1000
    time_text = font.render(f"Time: {elapsed_time:.2f}s", True, (255, 255, 255))
    screen.blit(time_text, (10, 10))
    # Draw score in top right
    score_text = font.render(f"Score: {state['score']}", True, (255, 255, 255))
    score_rect = score_text.get_rect(topright=(790, 10))
    screen.blit(score_text, score_rect)
    pygame.display.flip()
    clock.tick(60)

    while state['game_over'] and running:
    for event in pygame.event.get():
    if event.type == pygame.QUIT:
    running = False
    elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
    if button_rect.collidepoint(event.pos):
    state = reset_game()
    show_start_screen = True
    elif quit_button_rect.collidepoint(event.pos):
    running = False

    screen.fill((44, 212, 132))
    font_big = pygame.font.SysFont(None, 80)
    text = font_big.render('GaMe OvEr', True, (255, 255, 255))
    text_rect = text.get_rect(center=(400, 100)) # Moved up

    screen.blit(text, text_rect)

    font_small = pygame.font.SysFont(None, 40)
    time_text = font_small.render(f"Time Survived: {state['elapsed_time']:.2f}s", True, (255, 255, 255))
    time_rect = time_text.get_rect(center=(400, 180)) # Moved up
    screen.blit(time_text, time_rect)

    score_text = font_small.render(f"Score: {state['score']}", True, (255, 255, 255))
    score_rect = score_text.get_rect(center=(400, 230)) # Moved up
    screen.blit(score_text, score_rect)

    # Draw retry button
    mouse_pos = pygame.mouse.get_pos()
    color = button_hover_color if button_rect.collidepoint(mouse_pos) else button_color
    pygame.draw.rect(screen, color, button_rect, border_radius=8)
    font_button = pygame.font.SysFont(None, 36)
    button_text = font_button.render("Retry", True, button_text_color)
    button_text_rect = button_text.get_rect(center=button_rect.center)
    screen.blit(button_text, button_text_rect)

    # Draw quit button
    quit_color = button_hover_color if quit_button_rect.collidepoint(mouse_pos) else button_color
    pygame.draw.rect(screen, quit_color, quit_button_rect, border_radius=8)
    quit_text = font_button.render("Quit", True, button_text_color)
    quit_text_rect = quit_text.get_rect(center=quit_button_rect.center)
    screen.blit(quit_text, quit_text_rect)

    pygame.display.flip()
    clock.tick(60)

    pygame.quit()


    I'm almost positive that there's no issue with my code, and that this is simply an issue with my macbook, but I don't know for sure.

    I tried reinstalling python 3, I tried using 'brew install pygame', but that did not work. It continued to give me the same error result as above. I'm still relatively new to using pycharm, and just using python in general, so some help would be greatly appreciated. There are a small handful of other posts that pose a similar issue, but I tried those solutions and they wouldn't work, probably because those posts are from a couple years ago.

    Continue reading...

Compartilhe esta Página