我在
pygame
方面还比较新手。我想画一个圆形,但是却画出了一个正方形。我的代码如下。感谢帮助澄清问题。import sys, pygame
import random
pygame.init()
SCREEN_SIZE = SCREEN_WIDTH, SCREEN_HEIGHT = 800, 600
BACKGROUND_COLOR = pygame.Color("white")
SCREEN_COLOR = BACKGROUND_COLOR
SCREEN = pygame.display.set_mode(SCREEN_SIZE)
class Splatter:
def __init__(self, (x, y), r, color):
self.x = x
self.y = y
self.r = r
self.color = color
self.surface = pygame.Surface((r*2, r*2))
self.surface.set_alpha(192)
self.rect = self.surface.get_rect()
def display(self):
self.rect.centerx = self.x
self.rect.centery = self.y
pygame.draw.circle(self.surface, self.color, (self.x, self.y), self.r)
SCREEN.blit(self.surface, self.rect)
def main():
SCREEN.fill(SCREEN_COLOR)
s = Splatter((SCREEN.get_width()/2, SCREEN.get_height()/2), 10, pygame.Color("black"))
s.display()
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
if '__main__' == __name__:
main()
编辑
根据评论和回答,我已经修改了代码。然而,我仍然得到一个正方形而不是一个圆。我的最终目标是在画布上绘制一个圆形。因此,我可以通过移动画布来实现绘制圆形,而不是每次都绘制一个圆形。
import sys, pygame
import random
pygame.init()
SCREEN_SIZE = SCREEN_WIDTH, SCREEN_HEIGHT = 800, 600
SCREEN_COLOR = pygame.Color("white")
SCREEN = pygame.display.set_mode(SCREEN_SIZE)
class Splatter(object):
def __init__(self, (x, y), r):
self.x = x
self.y = y
self.r = r
self.surface = pygame.Surface((r*2, r*2))
self.surface.fill(pygame.Color("red"))
self.rect = self.surface.get_rect()
pygame.draw.circle(self.surface, pygame.Color("black"), (self.x, self.y), self.r)
def display(self):
self.rect.centerx = self.x
self.rect.centery = self.y
SCREEN.blit(self.surface, self.rect)
SCREEN.fill(SCREEN_COLOR)
s = Splatter((SCREEN.get_width()/2, SCREEN.get_height()/2), 20)
s.display()
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
Surface
是黑色的。另外,以防不清楚,pygame.Surface((r*2, r*2))
仍然创建一个矩形(即无论您如何命名变量都没有关系)。 - David Mašek