【发布时间】:2023-03-22 18:32:01
【问题描述】:
我正在使用 Pygame 开发一款游戏,它进展顺利……直到它决定不回应。这是我的代码:(对不起,如果格式化对stackoverflow来说不起作用)
#MODULES USED (use from to make calling functions easier)
from random import *
from pygame import *
import pygame
from pygame.locals import *
pygame.init()
from time import *
#INITIALISE THE PYGAME WINDOW
pygame.event.pump()
screen = display.set_mode([500, 500])
blue = [230, 242, 255]
screen.fill(blue)
pygame.display.update()
default = pygame.image.load("default.jpg")
screen.blit(default, (0,0))
pygame.display.update()
click = pygame.mouse.get_pressed()
#BASIC HEXAPAWN
#ALL POSSIBLE COMPUTER'S MOVE BOARDS AS ARRAY HERE
#TThe moves from the board images are left to right
class Board:
def __init__(self, board, moves):
self.board = board
self.moves = moves
boards1 = [pygame.image.load("a1.jpg"), pygame.image.load("a2.jpg"), pygame.image.load("a3.jpg")] #move1 boards
#irrelevant stuff removed, just initialising the other boards.
#GAME MAIN LOOP
while True:
#START GAME - 1st move
print("You play as O, computer is X")
currentboard = "O O O\n# # #\nX X X"
print(currentboard)
#PLAYER MOVE 1
screen.blit(boards1[0], (0, 250))
pygame.display.update()
if boards1[0].get_rect().collidepoint(pygame.mouse.get_pos()) and click:
screen.blit(boards1[0], (0,0))
pmove = 0
#note: I haven't added the other board options yet.
currentboard = boards1[pmove]
#[insert more unnecessary code here]
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
break
pygame.quit()
基本上每当我运行代码时,Pygame 窗口看起来都不错,但是当我尝试单击图像时,它会停止响应。此外,窗口总是停留在加载光标上,不知道为什么。
我已经尝试了所有我能找到的方法,但是不行,没有用。
如果有人可以提供帮助,我将不胜感激。
谢谢:)
艾丽莎
编辑:我不知道其他人也可以编辑我的帖子
清理一些东西,当我运行我的代码时,没有错误,没有Traceback 唯一的问题是没有响应。
也很抱歉我真的不擅长解释事情:/
【问题讨论】:
-
当我运行您的代码时,我在 Traceback 中看到
currentboard = boards1[pmove] NameError: name 'pmove' is not defined异常。您需要在for循环之外声明pmove变量,例如游戏循环中的第一行。这应该让你开始 -
如果 Windows 报告您的程序不工作,这意味着您没有正确处理事件。你能把你的问题减少到minimal reproducible example,因为这样会让人们更容易帮助你吗?如here 所述,重新组织您的游戏循环也可能会有所帮助。基本上是事件处理⟶更新状态⟶更新显示。
-
@smoggers
pmove已经在for循环之外。当我运行它时没有错误......我想知道发生了什么。 -
我刚刚注意到帖子中有一个
pmove = 0...以前没有。我应该检查一下 -
是的,对不起,我的意思是在
if语句之外,而不是for循环