python - 我的 python 文件在 cmd 中完美运行,但在 IDLE 中无法运行
问题描述
我使用 python 中的 pygame 模块制作了一个石头剪刀布游戏。它在 IDE(PyCharm)和命令提示符下运行得非常好,一切正常(包括十字按钮),但是当我在 IDLE 中运行代码时按下 GUI 窗口的十字按钮时,游戏崩溃了。
我没有收到任何错误消息,但按十字键一次后游戏冻结;然后我不能再向窗口提供任何输入,但是当我将鼠标悬停在它上面时,十字按钮仍然发红光,但是如果我再次按下它(第二次),那么十字按钮停止发红光并且游戏保持冻结状态。
这是完整的代码。我评论了在 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提供了In IDLE 为什么 Pygame 窗口不能正确关闭的解决方案?. 您需要在代码的最后一行添加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()
推荐阅读
- css - 移动设备水平滚动和图像不居中
- java - 如何在另一个对象数组中打印一个对象的 int 值?
- javascript - Chart JS 使用 onclick 重新动画图表
- codeigniter - 邮件功能一次只能工作一次:Codeigniter
- nginx - 将 nginx 配置为端口 80 和 443 上的反向代理,用于在 8000 上运行的 webapp。如何禁用与端口 8000 的连接?
- jquery - jQuery 切换在 iPhone8 或其他 iOS 设备上不起作用
- wamp - 从 wamp/tmp/ 文件夹中删除 tmp 文件是否安全
- css - 未能找到该字段的元素 selenium 识别
- c# - Windows 窗体中的 C# 文本框占位符转换
- computational-geometry - CGAL/minkowski_sum_2 的不适合多边形问题