2012-07-16 60 views
-1

我试图找出什么是错与如下因素代码:问题与刽子手在pygame的

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

非常感谢!

+1

对不起,但在代码中没有绘制字母空白和真实字母的任何代码,甚至没有定义猜测缩写。 – pmoleri 2012-07-16 20:09:31

+0

我找不到任何可以在这里绘制字母的代码。这是所有的代码?我看到的只是绘制绞线的代码,根据多少猜测画出不同的矩形,改变当前的颜色(甚至不使用),并用白色填充屏幕。 – hammythepig 2012-07-16 20:13:42

+0

OMG!我很抱歉!我复制了错误版本的文件!我将更新问题中的代码。这个新版本没有代码在屏幕上显示猜测短语(@pmoleri,@hammythepig),但它会将其打印到Shell。 – KevinOrr 2012-07-18 15:04:11

回答

0

只有在keydown期间,你才会使用这个短语,然后在每一帧你清理屏幕。在30帧/秒的速度下,如果你看到一个短语,你很幸运。 您应该在按键时检查擦除,但在每一帧上擦除擦除。

 ... 
    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) 

    ... 

编辑

关于连接到控制箱上的蓝色矩形,那是因为你正在控制箱200像素宽,然后覆盖第一个100像素与drawRects()例行事务。

您可以使100像素宽控制箱:

hangmanSurfaceObj.fill(blueColor, pygame.Rect(400,0,640,480)) 

或修改drawRects()函数,因此矩形不重叠的控制箱。

+0

哇我现在看到我的问题。谢谢。我现在无法访问计算机,但只要我这样做,我就会尝试一下。至于奇特的蓝色方块,你有什么想法吗? – KevinOrr 2012-07-19 22:48:45

+0

对不起,您忘记密码了吗? – KevinOrr 2012-07-21 22:35:26