次のコードの何が問題になっているのかを調べようとしています。
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)
これは、絞首刑のポストが表示され、文字の空白が表示され、右側に青い長方形が表示された絞首刑執行人のゲームであると想定されています(ゲームコントロールは最終的に移動します)。
実行すると、ぶら下がっている支柱が表示され、左側に青い長方形が表示されます。エラーは表示されませんが、文字の空白は表示されず、青い長方形の隅に奇妙な青いボックスが接続されています。 。ただし、さらに重要なのは、文字を入力すると、最終的な画面出力のデータを保持する文字列が変更されないという問題です。たとえば、推測語が「カメ」で、「t」と入力すると、から['_','_','_','_','_','_']
に変わるはず['t','_','_','t','_','_']
ですが、変わりません。
Python3.1.3とPygame1.9.1を使用しました。
使用している関数についてPythonとPygameのドキュメントを検索しましたが、残念ながら頼りになるものは見つかりませんでした。
元のファイルとリソースは、ここにあります。
本当にありがとう!