Pygame随机点功能

发布于 2025-01-27 07:55:59 字数 388 浏览 3 评论 0原文

我试图创建一个函数,使屏幕上充满点(这是游戏中的要点),

问题是由a for loop创建的点,当我运行程序时,点的积分就四处移动并无法保持到位。

代码:

def random_points():

point_x = random.randint(0, 900)
point_y = random.randint(0, 500)
rand_color = (random.random(), random.random(), (random.random()))
R = random.randint(1, 4)
for _ in range(1, 5):
    pygame.draw.circle(WIN, rand_color, (point_x, point_y), R)

im trying to create a function that fill the screen with dots(that will be the points in the game)

the problem is the points created by a for loop and when i run the program the points just move around and wont stay in place.

the code:

def random_points():

point_x = random.randint(0, 900)
point_y = random.randint(0, 500)
rand_color = (random.random(), random.random(), (random.random()))
R = random.randint(1, 4)
for _ in range(1, 5):
    pygame.draw.circle(WIN, rand_color, (point_x, point_y), R)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

祁梦 2025-02-03 07:55:59

您需要在应用程序循环之前创建一个点列表:

point_list = []
for _ in range(1, 5):
    point_x = random.randint(0, 900)
    point_y = random.randint(0, 500)
    rand_color = (random.random(), random.random(), (random.random()))
    R = random.randint(1, 4)
    point = (rand_color, (point_x, point_y), R)
    point_list.append(point)

从应用程序循环中的列表中绘制点:

run = True
while run:
    for event = pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    WIN.fill(0)
    for color, center, rad in point_list:
        pygame.draw.circle(WIN, color, center, rad)
    pygame.disaply.flip()

You need to create a list of points before the application loop:

point_list = []
for _ in range(1, 5):
    point_x = random.randint(0, 900)
    point_y = random.randint(0, 500)
    rand_color = (random.random(), random.random(), (random.random()))
    R = random.randint(1, 4)
    point = (rand_color, (point_x, point_y), R)
    point_list.append(point)

Draw the points from the list in the application loop:

run = True
while run:
    for event = pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    WIN.fill(0)
    for color, center, rad in point_list:
        pygame.draw.circle(WIN, color, center, rad)
    pygame.disaply.flip()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文