【发布时间】:2020-06-07 22:27:29
【问题描述】:
我正在尝试在屏幕上的随机位置绘制 x 个圆圈,这些圆圈没有交叉点。 我的预期结果: x 数量的圆圈被绘制到屏幕上的随机位置。 就像我用油漆画的这张漂亮的图表一样
实际结果:每次程序循环时,我都会在新的随机位置绘制一个圆圈。
到目前为止我尝试过的代码:
import pygame
import random
def draw_food(screen,color,x,y,radius):
'''This function draws/creates the food sprite, The screen variable tells the food what screen to be drawn to. the color variable sets the color the radius variable sets how large of a circle the food will be.'''
pygame.draw.circle(screen,green,(x,y),radius)
pygame.init()
width = 800
height = 600
screen = pygame.display.set_mode((width,height))
white = (255, 255, 255)
green = (0, 255, 0)
running = True
# main program loop
while running:
# Did the user click the window close button?
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Fill the background with white
screen.fill((white))
# Draw the food
draw_food(screen,green,random.randint(0,width),random.randint(0,height),20)
# Flip the display
pygame.display.flip()
我确实看过其他类似的问题,但我能找到的所有问题都是针对不同的编程语言或不同的有用的。 我也是python和pygame的初学者。
【问题讨论】:
-
嘿,这是您的实际代码还是类似于您的代码,因为有多种情况(我认为)缺少缩进
-
您是否希望圆圈没有任何交点?
-
嗯,它没有正确格式化哎呀。
-
是的,我希望他们没有交集
-
@Nova4026,那么您应该在您的问题上添加该约束;)