我正在继续一个 Hangman 项目,我遇到了 for 循环并在其中执行一个函数的问题。例如,如果你按下一个名为“CANADA”的关卡,然后按下“B”,由于加拿大没有 B,它应该画出 Hangman 的第一行。这是我到目前为止所做的:
def hangman1():
pygame.draw.line(screen, black, (775, 250), (775, 50), (4))
def hangman2():
pygame.draw.line(screen, black, (750, 250), (800, 250), (4))
def hangman3():
pygame.draw.line(screen, black, (775, 50), (925, 50), (4))
def hangman4():
pygame.draw.line(screen, black, (925, 50), (925, 175), (4))
def hangman5():
pygame.draw.circle(screen, black, (925, 100), 30, (0))
def hangman6():
pygame.draw.line(screen, black, (925, 125), (925, 200), (4))
def hangman7():
pygame.draw.line(screen, black, (885, 160), (965, 160), (4))
def hangman8():
pygame.draw.line(screen, black, (925, 200), (900, 225), (4))
def hangman9():
pygame.draw.line(screen, black, (925, 200), (950, 225), (4))
在多一点代码之后......
letters = list('abcdefghijklmnopqrstuvwxyz')
a = font2.render(str(letters[0]), True, (black))
b = font2.render(str(letters[1]), True, (black))
c = font2.render(str(letters[2]), True, (black))
d = font2.render(str(letters[3]), True, (black))
e = font2.render(str(letters[4]), True, (black))
f = font2.render(str(letters[5]), True, (black))
g = font2.render(str(letters[6]), True, (black))
h = font2.render(str(letters[7]), True, (black))
i = font2.render(str(letters[8]), True, (black))
j = font2.render(str(letters[9]), True, (black))
k = font2.render(str(letters[10]), True, (black))
l = font2.render(str(letters[11]), True, (black))
m = font2.render(str(letters[12]), True, (black))
n = font2.render(str(letters[13]), True, (black))
o = font2.render(str(letters[14]), True, (black))
p = font2.render(str(letters[15]), True, (black))
q = font2.render(str(letters[16]), True, (black))
r = font2.render(str(letters[17]), True, (black))
s = font2.render(str(letters[18]), True, (black))
t = font2.render(str(letters[19]), True, (black))
u = font2.render(str(letters[20]), True, (black))
v = font2.render(str(letters[21]), True, (black))
w = font2.render(str(letters[22]), True, (black))
x = font2.render(str(letters[23]), True, (black))
y = font2.render(str(letters[24]), True, (black))
z = font2.render(str(letters[25]), True, (black))
然后...
hangman = [hangman1, hangman2, hangman3, hangman4, hangman5, hangman6, hangman7, hangman8, hangman9]
for linebyline in hangman:
之后...
elif b1.collidepoint(pygame.mouse.get_pos()):
letter = letters[1]
check = country.count(letter)
if check >= 1:
if letter == letters[0]:
aPosition = 325, 235
a3 = screen.blit((a), (375, 235))
a4 = screen.blit((a), (425, 235))
a1.x, a1.y = -500, -500
elif letter == letters[2]:
cPosition = 300, 235
c1.x, c1.y = -500, -500
elif letter == letters[13]:
nPosition = 450, 235
n1.x, n1.y = -500, -500
elif letter == letters[3]:
dPosition = 600, 235
d1.x, d1.y = -500, -500
else:
b2 = font.render(str(letters[1]), True, (red))
screen.blit(b2, (485, 325))
linebyline()
time.sleep(0.5)
bPosition = -500, -500
b1.x, b1.y = -500, -500
当我按 B 时,它会变成红色,并在 0.5 秒内消失,但不会画线。有什么帮助吗?
编辑:我用另一个模块做了一些测试,这个功能在打印普通文本时工作得很好。但是当我用绘图(Pygame)再次测试它时,它工作了,但是当结合其他东西(如time.sleep()
)时,它显示一个白屏。与绘图相结合时print
不起作用,但打印起作用。此外,如果我添加了 time.sleep(1)
,它将黑屏整整 9 秒,而无需执行任何其他操作。这是我的测试代码:
import pygame, sys, random, time
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((1000, 700))
pygame.display.set_caption("Hangman: Countries")
black = 0, 0, 0
def hangman1():
pygame.draw.line(screen, black, (775, 250), (775, 50), (4))
print 'test'
def hangman2():
pygame.draw.line(screen, black, (750, 250), (800, 250), (4))
print 'test somethin'
def hangman3():
pygame.draw.line(screen, black, (775, 50), (925, 50), (4))
print 'test something else'
def hangman4():
pygame.draw.line(screen, black, (925, 50), (925, 175), (4))
print 'eggs'
def hangman5():
pygame.draw.circle(screen, black, (925, 100), 30, (0))
print 'hangman'
def hangman6():
pygame.draw.line(screen, black, (925, 125), (925, 200), (4))
print 'facebook'
def hangman7():
pygame.draw.line(screen, black, (885, 160), (965, 160), (4))
print 'internet'
def hangman8():
pygame.draw.line(screen, black, (925, 200), (900, 225), (4))
print 'more tests'
def hangman9():
pygame.draw.line(screen, black, (925, 200), (950, 225), (4))
print 'cheese'
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
screen.fill((255, 255, 255))
list1 = [hangman1(), hangman2(), hangman3(), hangman4(), hangman5(), hangman6(), hangman7(), hangman8(), hangman9()]
for greet in list1:
greet
time.sleep(1)
pygame.display.flip()
它在 shell 中同时打印出一堆单词,然后在 9 秒后屏幕变为完全绘制的刽子手,同样的一组单词又出现了。有什么想法,你们中的任何人都在 Pygame 有经验吗?