在我之前的问题For Loop Functions in Python中, 我无法放置包含命令的函数来为刽子手游戏画一条线。它并没有完全划清界限,我首先怀疑它是for循环或函数的问题。现在我意识到Pygame有一些小故障。
我尝试在加拿大使用此代码解决问题:
b2 = font.render(str(letters[1]), True, (red))
screen.blit(b2, (bPosition))
if hangman1x == -500 and hangman1y == -500:
hangman1x = (775, 250)
hangman1y = (775, 50)
pygame.draw.line(screen, black, (hangman1x), (hangman1y), (5))
pygame.display.flip()
time.sleep(0.5)
bPosition = -500, -500
b1.x, b1.y = -500, -500
if hangman1x == (775, 250) and hangman1y == (775, 50):
print 'hi'
width = 6
pygame.draw.line(screen, black, (hangman1x), (hangman1y), (5))
print 'yay'
pygame.display.flip()
现在这是奇怪的事情。
当你按下B blitting到屏幕上时,它会变成红色,就像它的意思一样,画出完美的线条,然后消失,当B消失时,我理解为什么。在那之后,我添加了额外的代码。 (请注意,pygame.draw.line
(s)都是相同的),它在shell中打印hi和yay,但它不保留该行。无论如何要解决这个问题?
答案 0 :(得分:1)
在调用pygame.draw.line()之后,您可能会将屏幕重绘为完全白色,这将在线上绘制并隐藏它。我不是像你那样画线,而是从那个
构建一个刽子手类画class Hangman():
def __init__(self):
self.lines = 0 #Number of lines to be drawn
def draw(self,screen):
#TODO draw to screen based on self.lines
#More code setting up pygame
drawlist = []
myMan = Hangman()
drawlist.append(myMan)
#mainloop
while 1:
screen.fill('#000000')
for item in drawlist:
item.draw(screen)
这样你每帧重绘你的刽子手,因此他总是被展示
编辑添加了一个正在运行的示例
#!/usr/bin/python
import pygame
pygame.init()
class Hangman():
def __init__(self):
self.lines = 0 #Number of lines to be drawn
def hang(self):
self.lines += 1
def draw(self,screen):
for x in range(self.lines):
coord1 = (x*10,20)
coord2 = (x*10,50)
pygame.draw.line(screen,(0,0,0),coord1,coord2)
size = screenWidth,screenHeight = 200,70
screen = pygame.display.set_mode(size)
pygame.display.flip()
myman = Hangman()
drawlist = []
drawlist.append(myman)
#mainloop
running = True
while running:
#EVENT HANDLING#
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == 32: #Spacebar
myman.hang()
#DRAWING#
screen.fill((255,255,255))
for item in drawlist:
item.draw(screen)
pygame.display.flip()