gpt4 book ai didi

python - Pygame:画线

转载 作者:太空宇宙 更新时间:2023-11-04 06:12:06 28 4
gpt4 key购买 nike

在我之前的问题中 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 位 block 传输到屏幕上时,它会变成红色,就像它的本意一样,可以很好地绘制线条,但是当 B 消失时,它就会消失,我明白为什么。之后,我添加了额外的 if 代码。 (注意 pygame.draw.line(s) 是相同的),它在 shell 中打印 hi 和 yay,但不保留该行。无论如何要解决这个问题?

最佳答案

在调用 pygame.draw.line() 之后,您可能会将屏幕重绘为完全白色,这将绘制线条并将其隐藏。我不会像你那样画线,而是从中构建一个 hangman 类绘制

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)

这样你就在每一帧重画刽子手,因此他总是被展示

EDIT 添加了一个运行示例

#!/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()

关于python - Pygame:画线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18211091/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com