我正在尝试绘制一些pygame圆圈并显示它们,并且当用户按下按键时,它将更新这些计数器的位置。当用户按下按键时,它将在屏幕上的新位置绘制一个圆圈,但不会显示第一张图像。因此,它应该显示一个圆圈,当他们按下一个键时,它会更改圆圈的位置。它没有画出启动圈。
第一个圈子(这些不会出现):
#Draw counters using pygame draw line function. These are the default counters on the start position. These don't move.
countY = 750
count1 = pygame.draw.circle(window, (black),(150, countY), 25, 0)
count2 = pygame.draw.circle(window, (black),(250, countY), 25, 0)
count3 = pygame.draw.circle(window, (255, 255, 255),(450, countY), 25, 0)
count4 = pygame.draw.circle(window, (255, 255, 255),(550, countY), 25, 0)
print("Should draw start counters")
pygame.display.update()
输入密钥后绘制的圆圈:
while game:
for event in pygame.event.get():
pygame.event.get()
#Counter 1 movement
if event.type == pygame.KEYDOWN and event.key == pygame.K_a:
diceRoll = random.randint(1, 4)
window.fill(grey)
grid()
count1 = pygame.draw.circle(window, (black),(150, countY - 72 * diceRoll), 25, 0)
当按下一个键时,用灰色填充背景表面:
if event.type == pygame.KEYDOWN and event.key == pygame.K_a:
diceRoll = random.randint(1, 4)
window.fill(grey) # <--- fills the entire surface with a solid color!
而且您不会再画圆了。
一个简单的解决方法是保留所有圆的列表,并在每一帧绘制它们(有更有效的方法,但为方便起见,让我们保持简单)。
circles = []
countY = 750
circles.append((pygame.color.Color('black'), (150, countY), 25, 0))
circles.append((pygame.color.Color('black'), (250, countY), 25, 0))
circles.append((pygame.color.Color('white'), (450, countY), 25, 0))
circles.append((pygame.color.Color('white'), (550, countY), 25, 0))
while game:
window.fill(grey)
for event in pygame.event.get():
# pygame.event.get() don't call pygame.event.get() twice
if event.type == pygame.KEYDOWN and event.key == pygame.K_a:
diceRoll = random.randint(1, 4)
circles.append((pygame.color.Color('black'), (150, countY - 72 * diceRoll), 25, 0))
for (color, pos, rad, w) in circles:
pygame.draw.circle(window, color, pos, rad, w)
pygame.display.flip()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句