【发布时间】:2012-07-16 19:36:22
【问题描述】:
我正在尝试找出以下代码有什么问题:
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找到。
非常感谢!
【问题讨论】:
-
抱歉,在那个代码中没有任何代码用于绘制字母空白或真实字母,甚至没有定义猜测框架。
-
我找不到任何可以在此处绘制字母的代码。这是所有的代码吗?我所看到的只是绘制hangee的代码,根据猜测次数绘制不同的矩形,更改当前颜色(甚至没有使用)并用白色填充屏幕。
-
天哪!我很抱歉!我复制了错误的文件版本!我将更新问题中的代码。这个新版本没有在屏幕上呈现猜测短语的代码(@pmoleri、@hammythepig),但它会将其打印到 Shell。
-
您的链接仍然缺少资产。您可以将目录放入文件夹中,并在 Dropbox 上共享。然后它会在您更新代码时更新。
-
@monkey 对不起!我包含错误的链接!我在问题中修复了它,但以防万一它不起作用我也会把它 here 。
标签: python python-3.x pygame