2012-07-16 83 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