Search code examples
pythonpygamepython-idle

My python file works perfectly in cmd, but doesn't work in IDLE


I made a rock-paper-scissors game using the pygame module in python. It runs perfectly well in the IDE(PyCharm) and the command prompt and everything works (including the cross button), but when I press the cross-button of the GUI window when the code is running in IDLE, the game crashes.

I don't get any error messages, but the game freezes after I press the cross button once; then I cant give any more inputs to the window, but the cross button still glows red when I hover over it, but if I press it again (2nd time), then the cross button stops glowing red and the game remains frozen.

This is the complete code. I commented on the part of the code which runs perfectly in cmd but doesn't run in IDLE (see just a few lines below the #Game-loop comment):-

import random
import pygame

# Initialize the pygame
pygame.init()

# create the screen
screen = pygame.display.set_mode((800, 600))

# Title and icon
pygame.display.set_caption("RPS Pro")
icon = pygame.image.load('icon.png')
pygame.display.set_icon(icon)

# Images
rock = pygame.image.load('rock.png')
paper = pygame.image.load('paper.png')
scissors = pygame.image.load('scissors.png')
swords = pygame.image.load('swords.png')

x_swords = 330
y_swords = 250
x_UI = 80
y_UI = 250
x_player = 650
y_player = 250

screen.fill((255, 215, 0))

UI_score = 0
player_score = 0

#Game-loop
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False #this doesnt seem to work in python IDLE but works perfectly in cmd and PyCharm

        if event.type == pygame.KEYDOWN:

            font = pygame.font.SysFont('comicsans', 30, False, False)

            UI_value = random.randint(1, 3)

            if UI_value == 1 and event.key == pygame.K_r:
                screen.fill((255, 215, 0))
                screen.blit(swords, (x_swords, y_swords))
                screen.blit(rock, (x_UI, y_UI))
                screen.blit(rock, (x_player, y_player))
                score_counter = font.render(str(UI_score) + " - " + str(player_score), 1, (0, 0, 0))
                screen.blit(score_counter, (370, 20))

            elif UI_value == 1 and event.key == pygame.K_p:
                screen.fill((255, 215, 0))
                screen.blit(swords, (x_swords, y_swords))
                player_score += 1
                screen.blit(rock, (x_UI, y_UI))
                screen.blit(paper, (x_player, y_player))
                score_counter = font.render(str(UI_score) + " - " + str(player_score), 1, (0, 0, 0))
                screen.blit(score_counter, (370, 20))

            elif UI_value == 1 and event.key == pygame.K_s:
                screen.fill((255, 215, 0))
                screen.blit(swords, (x_swords, y_swords))
                UI_score +=1
                screen.blit(rock, (x_UI, y_UI))
                screen.blit(scissors, (x_player, y_player))
                score_counter = font.render(str(UI_score) + " - " + str(player_score), 1, (0, 0, 0))
                screen.blit(score_counter, (370, 20))

            elif UI_value == 2 and event.key == pygame.K_r:
                screen.fill((255, 215, 0))
                screen.blit(swords, (x_swords, y_swords))
                UI_score += 1
                screen.blit(paper, (x_UI, y_UI))
                screen.blit(rock, (x_player, y_player))
                score_counter = font.render(str(UI_score) + " - " + str(player_score), 1, (0, 0, 0))
                screen.blit(score_counter, (370, 20))

            elif UI_value == 2 and event.key == pygame.K_p:
                screen.fill((255, 215, 0))
                screen.blit(swords, (x_swords, y_swords))
                screen.blit(paper, (x_UI, y_UI))
                screen.blit(paper, (x_player, y_player))
                score_counter = font.render(str(UI_score) + " - " + str(player_score), 1, (0, 0, 0))
                screen.blit(score_counter, (370, 20))

            elif UI_value == 2 and event.key == pygame.K_s:
                screen.fill((255, 215, 0))
                screen.blit(swords, (x_swords, y_swords))
                player_score += 1
                screen.blit(paper, (x_UI, y_UI))
                screen.blit(scissors, (x_player, y_player))
                score_counter = font.render(str(UI_score) + " - " + str(player_score), 1, (0, 0, 0))
                screen.blit(score_counter, (370, 20))

            elif UI_value == 3 and event.key == pygame.K_r:
                screen.fill((255, 215, 0))
                screen.blit(swords, (x_swords, y_swords))
                player_score += 1
                screen.blit(scissors, (x_UI, y_UI))
                screen.blit(rock, (x_player, y_player))
                score_counter = font.render(str(UI_score) + " - " + str(player_score), 1, (0, 0, 0))
                screen.blit(score_counter, (370, 20))

            elif UI_value == 3 and event.key == pygame.K_p:
                screen.fill((255, 215, 0))
                screen.blit(swords, (x_swords, y_swords))
                UI_score += 1
                screen.blit(scissors, (x_UI, y_UI))
                screen.blit(paper, (x_player, y_player))
                score_counter = font.render(str(UI_score) + " - " + str(player_score), 1, (0, 0, 0))
                screen.blit(score_counter, (370, 20))

            elif UI_value == 3 and event.key == pygame.K_s:
                screen.fill((255, 215, 0))
                screen.blit(swords, (x_swords, y_swords))
                screen.blit(scissors, (x_UI, y_UI))
                screen.blit(scissors, (x_player, y_player))
                score_counter = font.render(str(UI_score) + " - " + str(player_score), 1, (0, 0, 0))
                screen.blit(score_counter, (370, 20))

    pygame.display.update()

Please let me know if you have any further inquiries about my problem. Thanks in advance!


Solution

  • pygame FAQ has offered an solution for In IDLE why does the Pygame window not close correctly?. You need to add pygame.quit() at the end line of your code.

    #Game-loop
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False #this doesnt seem to work in python IDLE but works perfectly in cmd and PyCharm
    
        pygame.display.update()
    
    pygame.quit()