3

在我之前的问题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()

现在这是奇怪的事情。

当您按 Blitted 到屏幕上时,它会变成红色,就像它的意思一样,画线非常好,但是当 B 消失时消失了,我明白为什么。之后,我添加了额外的 if 代码。(请注意,两个pygame.draw.line(s)是相同的),它在 shell 中打印 hi 和 yay,但它不保留该行。无论如何要解决这个问题?

4

1 回答 1

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()
于 2013-08-13T14:10:21.757 回答