我的 python 文件在 cmd 中运行良好,但在 IDLE 中不起作用
My python file works perfectly in cmd, but doesn't work in IDLE
我使用 python 中的 pygame 模块制作了一个剪刀石头布游戏。它 运行 在 IDE(PyCharm) 和命令提示符下运行良好,一切正常(包括十字按钮),但是当我按下 GUI 的十字按钮时 window 当代码在 IDLE 中 运行ning 时,游戏崩溃。
我没有收到任何错误消息,但是我按了一次十字键后游戏卡住了;然后我无法再向 window 提供任何输入,但是当我将鼠标悬停在它上面时十字按钮仍然呈红色亮起,但是如果我再次按下它(第二次),则十字按钮停止呈红色亮起并且游戏继续冷冻。
这是完整的代码。我对 运行 在 cmd 中完美但在 IDLE 中不 运行 的代码部分进行了评论(请参阅#Game-loop 评论下方的几行):-
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()
如果您对我的问题有任何进一步的疑问,请告诉我。提前致谢!
pygame FAQ has offered an solution for In IDLE why does the Pygame window not close correctly?. You need to add pygame.quit() 在代码的最后一行。
#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()
我使用 python 中的 pygame 模块制作了一个剪刀石头布游戏。它 运行 在 IDE(PyCharm) 和命令提示符下运行良好,一切正常(包括十字按钮),但是当我按下 GUI 的十字按钮时 window 当代码在 IDLE 中 运行ning 时,游戏崩溃。
我没有收到任何错误消息,但是我按了一次十字键后游戏卡住了;然后我无法再向 window 提供任何输入,但是当我将鼠标悬停在它上面时十字按钮仍然呈红色亮起,但是如果我再次按下它(第二次),则十字按钮停止呈红色亮起并且游戏继续冷冻。
这是完整的代码。我对 运行 在 cmd 中完美但在 IDLE 中不 运行 的代码部分进行了评论(请参阅#Game-loop 评论下方的几行):-
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()
如果您对我的问题有任何进一步的疑问,请告诉我。提前致谢!
pygame FAQ has offered an solution for In IDLE why does the Pygame window not close correctly?. You need to add pygame.quit() 在代码的最后一行。
#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()