为什么我在pygame中得到一个正方形而不是一个圆圈?

时间:2015-08-10 15:42:26

标签: python pygame

我对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()

1 个答案:

答案 0 :(得分:3)

您正在黑色Surface上绘制黑色圆圈,然后将整个Surface放在SCREEN上。黑圈在那里,但你显然看不到它。

试试这个(测试过)。

global SCREEN
pygame.draw.circle(SCREEN, self.color, (self.x, self.y), self.r)
pygame.display.flip()

作为旁注恕我直言,你的很多代码都没用。你是在思考这个问题。例如:

SCREEN_SIZE = SCREEN_WIDTH, SCREEN_HEIGHT = 800, 600
BACKGROUND_COLOR = pygame.Color("white")
SCREEN_COLOR = BACKGROUND_COLOR

相当于:

SCREEN_SIZE =  800, 600
SCREEN_COLOR  = pygame.Color("white")

修改

你仍然得到一个正方形,因为你在外面画了你在屏幕上blit的Surface

此行存在问题,因为self.xself.y位于self.surface之外。

pygame.draw.circle(self.surface, pygame.Color("black"), (self.x, self.y), self.r)

解决方案:

pygame.draw.circle(self.surface, pygame.Color("black"), 
                           (int(self.surface.get_width()/2),
                            int(self.surface.get_height()/2)), 
                           self.r)

或恕我直言更好(但这实际上取决于你如何实现游戏的其他部分):

# ...
class Splatter(object):
    def __init__(self, r):
        self.r = int(r)
        self.surface = pygame.Surface((r*2, r*2))
        self.surface.fill(pygame.Color("red"))
        pygame.draw.circle(self.surface, pygame.Color("black"), 
                           (int(self.surface.get_width()/2),
                            int(self.surface.get_height()/2)), 
                           self.r)

    def display(self, x, y):
        SCREEN.blit(self.surface, (int(x), int(y)))


SCREEN.fill(SCREEN_COLOR)
s = Splatter(20)
s.display(SCREEN.get_width()/2, SCREEN.get_height()/2)
pygame.display.flip()
# ...

在显示时,您需要创建Splatter所需的坐标。