【发布时间】:2021-01-02 19:46:57
【问题描述】:
当我运行这个程序时,我希望游戏在随机位置生成一个较大的可移动点和数百个较小的不可移动点。但是,当我运行程序时,较小的点会不断消失和重生。我认为它与 pygame.display.update() 函数有关,但我不完全确定。我该如何解决这个问题?
from pygame import *
import random as rd
p_1_x = 200
p_1_y = 200
p_1_change_x = 0
init()
screen = display.set_mode((800, 600))
def p_1(x, y):
player_1 = draw.circle(screen, (0, 0, 0), (x, y), 15)
def drawwing():
for i in range(0, 250):
x = rd.randint(100, 700)
y = rd.randint(100, 500)
dot = draw.circle(screen, (55, 255, 155), (x, y), 5)
while True:
for events in event.get():
if events.type == QUIT:
quit()
if events.type == KEYDOWN:
if events.key == K_RIGHT:
p_1_change_x = 1
if events.key == K_LEFT:
p_1_change_x = -1
if events.type == KEYUP:
if events.key == K_RIGHT or K_LEFT:
p_1_change_x = 0
p_1_change_y = 0
screen.fill((255, 255, 255))
p_1_x += p_1_change_x
p_1(p_1_x, p_1_y)
display.update()
drawwing()
display.update()
【问题讨论】:
-
这是你的程序,不是 pygame。每次通过循环时,您都会明确地调用您的 drawwing() 函数!如果你想在后面的房间里有一个静态的星空,你必须保存所有小点的位置,或者使用可重复的算法生成它们,而不是使用 randint()。
-
保存头寸的最佳方法是什么?
-
查看我的答案以获得一种可能的解决方案。注意:您不需要两个更新调用。此外,如果您希望大点出现在小点之前,请在小点之后而不是之前绘制它。