日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

用python随机画多个圆_Python Pygame随机绘制不重叠的圆圈

發布時間:2024/10/12 python 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 用python随机画多个圆_Python Pygame随机绘制不重叠的圆圈 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我對python非常陌生,似乎缺少了一些東西.

我想在pygame顯示器上隨機繪制圓圈,但前提是圓圈彼此不重疊.

我相信我必須找到所有圓心之間的距離,并且只有在該距離大于圓半徑* 2時才繪制它.

我嘗試了許多不同的方法,但是都沒有成功,但我總是得到相同的結果-重疊繪制的圓圈.

#!/usr/bin/env python

import pygame, random, math

red = (255, 0, 0)

width = 800

height = 600

circle_num = 10

tick = 2

speed = 5

pygame.init()

screen = pygame.display.set_mode((width, height))

class circle():

def __init__(self):

self.x = random.randint(0,width)

self.y = random.randint(0,height)

self.r = 100

def new(self):

pygame.draw.circle(screen, red, (self.x,self.y), self.r, tick)

c = []

for i in range(circle_num):

c.append('c'+str(i))

c[i] = circle()

for j in range(len(c)):

dist = int(math.hypot(c[i].x - c[j].x, c[i].y - c[j].y))

if dist > int(c[i].r*2 + c[j].r*2):

c[j].new()

pygame.display.update()

else:

continue

while True:

for event in pygame.event.get():

if event.type == pygame.QUIT:

pygame.quit()

quit()

最佳答案

您沒有與其他所有圈子進行核對.我添加了一個變量shouldprint,如果其他圓圈太近,該值將設置為false.

import pygame, random, math

red = (255, 0, 0)

width = 800

height = 600

circle_num = 20

tick = 2

speed = 5

pygame.init()

screen = pygame.display.set_mode((width, height))

class circle():

def __init__(self):

self.x = random.randint(0,width)

self.y = random.randint(0,height)

self.r = 100

def new(self):

pygame.draw.circle(screen, red, (self.x,self.y), self.r, tick)

c = []

for i in range(circle_num):

c.append('c'+str(i))

c[i] = circle()

shouldprint = True

for j in range(len(c)):

if i != j:

dist = int(math.hypot(c[i].x - c[j].x, c[i].y - c[j].y))

if dist < int(c[i].r*2):

shouldprint = False

if shouldprint:

c[i].new()

pygame.display.update()

while True:

for event in pygame.event.get():

if event.type == pygame.QUIT:

pygame.quit()

quit()

總結

以上是生活随笔為你收集整理的用python随机画多个圆_Python Pygame随机绘制不重叠的圆圈的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。