Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw a circle in PyGame?

Hi I am having a problem with drawing a circle ERROR: module object has no attribute 'circle'. what am I doing wrong?

And also how can I put numbers in circles?

For example: (first click is circle with 0 second is circle with 1 and so on)

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:
                draw_circle()
                pygame.display.update()

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

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

def draw_circle():
    pos=get_pos()
    pygame.draw.circle(screen, BLUE, pos, 20)


if __name__ == '__main__':
    main()
like image 368
Tuff Avatar asked Oct 20 '17 07:10

Tuff


4 Answers

You've mistyped the name of the function. The correct name is pygame.draw.circle.

def drawCircle():
    pos=getPos()
    pygame.draw.circle(screen, BLUE, pos, 20) # Here <<<
like image 175
Ericson Willians Avatar answered Oct 02 '22 23:10

Ericson Willians


Here is how you can draw a circle in 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.
like image 20
Irfan wani Avatar answered Oct 02 '22 21:10

Irfan wani


Here is how you can draw a circle properly

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()
like image 34
Kasun Kalhara Avatar answered Oct 02 '22 21:10

Kasun Kalhara


Mainter a counter for the number of circles you have drawn and write the number with the co-ordinates same as the centre of the circle. Also the command is pygame.draw.circle and not pygame.draw.circl

like image 21
Arijit Sur Avatar answered Oct 02 '22 22:10

Arijit Sur