如何在 pygame.Surface 的子类中进行 gfxdraw?

how to gfxdraw in a subclass of pygame.Surface?

我子类化 Surface 的方式有误吗?错误说 gfxdraw.aacircle() 需要 Surface 作为第一个参数,但我不知道该怎么做。

以下可运行代码和异常:

import pygame, sys, os
from pygame.locals import *
from pygame import gfxdraw

BLACK = (0,0,0)
WHITE = (255,255,255)

class basicButton(pygame.Surface): # basic surface for buttons
    def __init__(self, size, radius):
        pygame.Surface.__init__(self, size=(size, size))
        self.fill(BLACK)
        # anti-aliased outline
        pygame.gfxdraw.aacircle(self, radius, radius, radius, WHITE)
        # color filled circle
        pygame.gfxdraw.filled_circle(self, radius, radius, radius, WHITE)
        # set black to be transparent
        self.set_colorkey(BLACK)

# make an instance
quitButtonSurf = basicButton(25, 12)

pygame.init()
screen = pygame.display.set_mode((800, 600))

while True:
    screen.blit(quitButtonSurf)
    pygame.display.flip()

抛出异常:

pygame.gfxdraw.aacircle(self, radius, radius, radius,  WHITE)
  TypeError: surface must be a Surface

不幸的是,这不是您要找的东西,但它应该有用。

import pygame, sys, os
from pygame.locals import *
from pygame import gfxdraw

pygame.init()


BLACK = (0,0,0)
WHITE = (255,255,255)

class basicButton: 
    def __init__(self, size, radius):
        self.image = pygame.Surface((size,size)) # as per your example, assuming a square
        self.image.fill(BLACK)
        pygame.gfxdraw.aacircle(self.image, radius, radius, radius,  WHITE)
        pygame.gfxdraw.filled_circle(self.image, radius, radius, radius,  WHITE)
        self.image.set_colorkey(BLACK)


quitButtonSurf = basicButton(25, 12)

screen = pygame.display.set_mode((800, 600))

while True:
    screen.blit(quitButtonSurf.image,(0,0)) # you'll have to assign a location for this object, I set it to 0,0 simply for testing
    pygame.display.flip()

希望对您有所帮助