Jared Foster
Jared Foster

Reputation: 31

How come my pygame code isn't drawing this circle?

http://pastebin.com/PLdiNg9d

First off, I'm pretty new to Python so sorry for the very sloppy code! Anyways, onto my question. I made a class cell, and in it's init function it accepts several variables that it uses to draw a circle. I then do this:

self.image = pygame.Surface([size, size])
self.image.fill(WHITE)
self.image.set_colorkey(WHITE)
pygame.draw.circle(self.image, color, [posx, posy], size)
self.rect = self.image.get_rect()
print('Cell initialized.')

The problem is, it doesn't draw anything onto my screen. It doesn't output an error message, but it still prints 'Cell initialized.' so I know it's getting all the way through the init function.

What really confuses me is that there is a way I can get it to work. If I move

cell_list = pygame.sprite.Group()
a = Cell(5, GREEN, 2, 200, 200)
cell_list.add(a)

into my while loop, and change

pygame.draw.circle(self.image, ...)

to

pygame.draw.circle(screen, ...)

Then it will draw my cell. Any help at all would be appreciated, because I'm thoroughly stumped.

Upvotes: 2

Views: 316

Answers (2)

knowledge
knowledge

Reputation: 401

Instead of:

pygame.draw.circle(self.image, color, [posx, posy], size)

use:

pygame.draw.circle(self.image, color, [size, size], size)

Explanation: on your surface (self.image) you draw circle on coordinates 0, 0 but because pygamen.draw.circle needs centre so size, size goes here.

If you wanted size to be radius use size//2 instead of size in drawing circle, if you want it to be diameter use size*2 instead of size in image initialization.

You also need to specify rect position, so put this somewhere in Cell.__init__():

self.rect.x = self.posx
self.rect.y = self.posy

Few notes:

  • use self.variable instead of variable (e.g.:self.size instead of just size) because you can use it anywhere in class
  • put pygame.quit() at the end of the program (after while loop)

Upvotes: 0

user4938328
user4938328

Reputation:

This is a simple template to use when working with pygame. This demo draws a circle at the center of the screen.

from pygame import *
screen = display.set_mode((500,500))
# Fill screen white
screen.fill((255,255,255))

# Draw a red circle at the center
# Remove the 1 to make it a filled circle
draw.circle(screen, (255,0,0), (250,250), 250, 1)

# Game loop
running = True
while running:
    # Properly quit (pygame will crash without this)
    for e in event.get():
        if e.type == QUIT:
            running = False

    display.flip()
quit()

Upvotes: 1

Related Questions