pythonclasspygamedraw

How do I draw a circle in pygame using a class?


I am trying to build a little space invaders type of game, and I want to draw many things while using a class (in order to be more efficient). However, while my program does run when I want it to, it never shows a circle, only a blank background.

The code I tried looks like this:

import math
import random
import time
import datetime
import os
import re
import sys
import pygame

pygame.init()

canvas = pygame.display.set_mode((600,600))

class Invader:
    def __init__(self,xp,yp):
        self.xp = int(xp)
        self.yp = int(yp)
    def Movement(self):
        pygame.draw.circle(canvas,(0,255,0),(self.xp,self.yp),0)
        

invader = Invader(300,300)
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    invader.Movement()
    pygame.display.update()

I was expecting to see the circle on the displayed window, but like I said, nothing appeared except a black background.


Solution

  • You need to pass a radius:

    pygame.draw.circle(canvas, (0, 255, 0), (self.xp, self.yp), 100)
    canvas.fill((0, 0, 0))
    

    Code

    import sys
    import pygame
    
    pygame.init()
    canvas = pygame.display.set_mode((600, 600))
    
    class Invader:
        def __init__(self, xp, yp):
            self.xp = int(xp)
            self.yp = int(yp)
    
        def movement(self):
            pygame.draw.circle(canvas, (0, 255, 0), (self.xp, self.yp), 100) 
    
    
    invader = Invader(300, 300)
    
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        canvas.fill((0, 0, 0)) 
        invader.movement()
        pygame.display.update()