我试图找出以下代码有什么问题:
import pygame, sys, random, linecache
from pygame.locals import *
#Start Pygame, define Pygame objects
pygame.init()
hangmanSurfaceObj = pygame.display.set_mode((640,480), 0)
clock = pygame.time.Clock()
#Define colors
redColor = pygame.Color(255,0,0)
greenColor = pygame.Color(0,255,0)
blueColor = pygame.Color(0,0,255)
whiteColor = pygame.Color(255,255,255)
blackColor = pygame.Color(0,0,0)
tardisBlueColor = pygame.Color(16,35,114)
bgColor = whiteColor
#Import Images
hangmanImage = pygame.image.load('hangmanResources/hangman.png')
#Import sounds
sadTromboneSound = pygame.mixer.music.load('hangmanResources/sadTrombone.mp3')
#Import Font
fontObj = pygame.font.Font('hangmanResources/Avenir_95_Black.ttf', 18)
#Define global variables
currentWord = 0
usrWord = ''
screenWord = []
i = 0
currentChar = ''
guesses = 0
def terminate():
pygame.quit()
sys.exit()
def drawRects(tries):
if tries<=0:
hangmanSurfaceObj.fill(bgColor, pygame.Rect(242,65,65,65))
if tries<=1:
hangmanSurfaceObj.fill(bgColor, pygame.Rect(257,90,35,4))
if tries<=2:
hangmanSurfaceObj.fill(bgColor, pygame.Rect(261,110,27,1))
if tries<=3:
hangmanSurfaceObj.fill(bgColor, pygame.Rect(274,130,1,114))
if tries<=4:
hangmanSurfaceObj.fill(bgColor, pygame.Rect(198,160,76,1))
if tries<=5:
hangmanSurfaceObj.fill(bgColor, pygame.Rect(275,160,75,1))
if tries<=6:
hangmanSurfaceObj.fill(bgColor, pygame.Rect(210,244,64,85))
if tries<=7:
hangmanSurfaceObj.fill(bgColor, pygame.Rect(274,244,51,87))
def newGame(players):
if players == 1:
line_number = random.randint(0, 2283)
usrWord = linecache.getline('hangmanResources/words.txt', line_number)
usrWord = list(usrWord)
del usrWord[-1]
print(usrWord)
screenWord = ['_']*len(usrWord)
print(screenWord)
def checkChar(usrWord):
print('hi')
i=0
addGuess = 1
print(len(usrWord))
while i <= (len(usrWord)-1):
print('hi2')
if currentChar.lower == usrWord[i]:
print('hi3')
screenWord[i] = currentChar
addGuess = 0
return addGuess
newGame(1)
while True:
gameRunning = 1
while gameRunning==1:
for event in pygame.event.get(QUIT):
terminate()
for event in pygame.event.get(KEYDOWN):
if event.key==K_ESCAPE:
terminate()
else:
currentChar = event.unicode
guesses +=checkChar(usrWord)
print(currentChar)
print(screenWord)
msg = ''.join(screenWord)
msgSurfaceObj = fontObj.render(msg, False, blackColor)
msgRectObj = msgSurfaceObj.get_rect()
msgRectObj.topleft = (10, 20)
hangmanSurfaceObj.blit(msgSurfaceObj, msgRectObj)
hangmanSurfaceObj.fill(bgColor)
hangmanSurfaceObj.fill(blueColor, pygame.Rect(400,0,640,480))
hangmanSurfaceObj.blit(hangmanImage, (0,0))
if guesses<=7:
drawRects(guesses)
else:
won=0
gameRunning = 0
pygame.display.update()
clock.tick(30)
newGame(1)
它应该是一个刽子手游戏,有一个悬挂柱,显示字母空白,右侧有一个蓝色矩形(游戏控件最终会去的地方)。
运行的时候出现了挂柱,左边的蓝色矩形出现了,虽然没有弹出错误,但是字母空白没有出现,蓝色矩形的一角连接着一个奇怪的蓝色框. 不过,更重要的是,我遇到的问题是,一旦您输入了一个字符,保存最终屏幕输出数据的字符串就不会改变。例如,如果猜测词是“Turtle”并且我输入“t”,那么它应该从['_','_','_','_','_','_']
变为['t','_','_','t','_','_']
,但事实并非如此。
我使用 Python 3.1.3 和 Pygame 1.9.1。
我在 Python 和 Pygame 文档中搜索了我正在使用的函数,但不幸的是我找不到资源。
可以在此处找到原始文件以及资源。
非常感谢!