【问题标题】:What is the quickest way to draw multiple circles in pygame?在pygame中绘制多个圆圈的最快方法是什么?
【发布时间】:2020-09-16 00:07:26
【问题描述】:

我正在为一个项目制作一个 agario 克隆,我想知道在 pygame 中绘制许多点的最快方法是什么。

from pygame import *
import random as rd 

x = rd.randint(100, 700)
y = rd.randint(100, 500)


# I would like to draw about 50 dots of this type. 
dot = draw.circle(screen, (0, 0, 0), (x, y), 5)

【问题讨论】:

  • 我会使用for 循环。是否存在性能问题?
  • 我目前对性能没有任何顾虑。
  • 它究竟是如何工作的?我可以在 for 循环中定义点变量吗?

标签: python pygame draw


【解决方案1】:

下面是一些使用 pygame 绘制圆圈的基本代码:

import pygame as pg
import random as rd 

pg.init()  # initialize pygame  
screen = pg.display.set_mode((500,500))  # create main screen

for ctr in range(25):  # 25 circles
    x = rd.randint(50, 450)
    y = rd.randint(50, 450)
   
    # I would like to draw about 50 dots of this type. 
    dot = pg.draw.circle(screen, (100, 200, 100), (x, y), 15)
    pg.display.update()  # update screen
    
while True:  # main pygame loop, always include this
    for event in pg.event.get(): # required for OS events
      if event.type == pg.QUIT:  # user closed window
         pg.quit()

输出

【讨论】:

    【解决方案2】:

    使用简单的python"for" loop with a "range"的任何简单方法:

    随着循环的迭代,它执行循环体的内容,变量i从0递增到N-1(即49)。变量i 可以是任何有效的变量名,但对于简单的编号循环,通常使用ijk

    from pygame import *
    import random as rd 
    
    # Draw 50 dots 
    for i in range( 0, 50 ):
        x = rd.randint(100, 700)
        y = rd.randint(100, 500)
        dot = draw.circle(screen, (0, 0, 0), (x, y), 5)
    

    【讨论】:

      【解决方案3】:

      我为点做了一个类:

      class Dot():
          SIZE = 5
          def __init__(self, x, y):
              self.x = x
              self.y = y
      
          def draw(self):
              draw.circle(screen, self.color, (self.x, self.y), Dot.SIZE)
      

      然后我做了一个数组并像这样生成NUMBER_OF_DOTS

      dots = []
      
      for i in range(NUMBER_OF_DOTS):
          x = rd.randint(100, 700)
          y = rd.randint(100, 500)
          dots.append(Dot(x,y))
      

      while循环中,用白色填充整个场景后重绘:

      当真时: screen.fill((255, 255, 255)) ... 对于点中的点: dot.draw()

      全部来源:

      from pygame import *
      import random as rd
      
      SCREEN_WIDTH = 800
      SCREEN_HEIGHT = 600
      NUMBER_OF_DOTS = 300
      
      class Dot():
          SIZE = 5
          def __init__(self, x, y):
              self.x = x
              self.y = y
              self.color = random_color()
      
          def draw(self):
              draw.circle(screen, self.color, (self.x, self.y), Dot.SIZE)
          
      def random_color():
          r = rd.randint(0, 255)
          g = rd.randint(0, 255)
          b = rd.randint(0, 255)
          return (r, g, b)
      
      
      init()
      screen = display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
      
      dots = []
      
      # generate random dots all over the screen
      for i in range(NUMBER_OF_DOTS):
          x = rd.randint(100, 700)
          y = rd.randint(100, 500)
          dots.append(Dot(x,y))
      
      
      # main while loop
      while True:
          screen.fill((255, 255, 255))
          
          for dot in dots:
              dot.draw()
      
      
          display.update()
          time.delay(1) # Speed down
      

      【讨论】:

        【解决方案4】:

        如果您想在主应用程序循环中连续绘制相同的圆圈,那么您必须生成一个随机位置列表:

        import pygame as pg
        import random as rd 
        
        pg.init()
        screen = pg.display.set_mode((800,600))
        
        cpts = []
        for i in range(25):
            x = rd.randint(100, 700)
            y = rd.randint(100, 500)
            cpts.append((x, y))
            
        run = True
        while run: 
            for event in pg.event.get(): 
                if event.type == pg.QUIT: 
                    run = False
        
            for cpt in cpts: 
                pg.draw.circle(screen, (255, 255, 255), cpt, 15)
            pg.display.update()
        

        请参阅Pygame unresponsive display 的答案以获取最低 pygame 应用程序。

        【讨论】:

          猜你喜欢
          • 2017-07-11
          • 1970-01-01
          • 2020-09-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-04-10
          • 2012-11-18
          • 1970-01-01
          相关资源
          最近更新 更多