我正试图找出下面的代码有什么问题:
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文档中我正在使用的函数,但不幸的是我找不到任何追索权。
可以找到原始文件以及资源here。
非常感谢!
答案 0 :(得分:0)
你只是在keydown期间然后在你清理屏幕的每一帧中使用短语。如果你看到这个短语的眨眼,那么30 fps就很幸运。 你应该按下按键检查frase,但是每一帧上的frase都是blit。
...
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)
# Clean background
hangmanSurfaceObj.fill(bgColor)
# Then blit message
msgSurfaceObj = fontObj.render(msg, False, blackColor)
msgRectObj = msgSurfaceObj.get_rect()
msgRectObj.topleft = (10, 20)
hangmanSurfaceObj.blit(msgSurfaceObj, msgRectObj)
...
修改强>
关于控制盒附带的蓝色矩形,这是因为你将控制盒宽200px,然后用drawRects()rountine覆盖前100px。
您可以将控制框设为100px宽:
hangmanSurfaceObj.fill(blueColor, pygame.Rect(400,0,640,480))
或修改drawRects()例程,以使rects不与控件框重叠。