如何在PyGame中绘制一个圆形?

14
嗨,我在绘制一个圆时遇到了问题,错误信息是“模块对象没有'circle'属性”。我做错了什么吗?
还有,我怎样才能在圆里放置数字呢?
例如:(第一次点击是带有0的圆,第二次是带有1的圆,依此类推)
import pygame

WHITE =     (255, 255, 255)
BLUE =      (  0,   0, 255)
GREEN =     (  0, 255,   0)
RED =       (255,   0,   0)
TEXTCOLOR = (  0,   0,  0)
(width, height) = (200, 300)

running = True

def main():
    global running, screen
    
    pygame.init()
    screen = pygame.display.set_mode((width, height))
    pygame.display.set_caption("TUFF")
    screen.fill(background_color)
    pygame.display.update()
    
    while running:
        ev = pygame.event.get()

        for event in ev:

            if event.type == pygame.MOUSEBUTTONUP:
                drawCircle()
                pygame.display.update()

            if event.type == pygame.QUIT:
                running = False

def getPos():
    pos = pygame.mouse.get_pos()
    return (pos)

def drawCircle():
    pos=getPos()
    pygame.draw.circl(screen, BLUE, pos, 20)


if __name__ == '__main__':
    main()
5个回答

17
你打错了函数名称,正确的名称是pygame.draw.circle
def drawCircle():
    pos=getPos()
    pygame.draw.circle(screen, BLUE, pos, 20) # Here <<<

4

这里是如何正确地画圆的方法

import pygame

window = pygame.display.set_mode((500,500))
red = (200,0,0)

circleX = 100
circleY = 100
radius = 10

active = True

while active:
   for event in pygame.event.get():
      if event.type == pygame.QUIT:
         active = False

   pygame.draw.circle(window,red,(circleX,circleY),radius) # DRAW CIRCLE

   pygame.display.update()

4

以下是如何在pygame中绘制圆形的方法:

import pygame
pygame.init()
screen = pygame.display.set_mode((x, y)) #x and y are height and width

pygame.draw.circle(screen, (r,g,b), (x, y), R, w) #(r, g, b) is color, (x, y) is center, R is radius and w is the thickness of the circle border.

3
如果你想要椭圆形,可以使用函数:pygame.draw.ellipse(surface, color, bounding_rectangle, width=0)。这将绘制一个由指定矩形外接的椭圆形!

2
维护一个计数器来记录你绘制的圆的数量,并将数字写在与圆心坐标相同的位置。 同时,命令应为 pygame.draw.circle 而不是 pygame.draw.circl。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接