pygame,我将其存储在变量中后,我的圈子变成了rect,如何防止这种情况发生

时间:2018-12-30 23:34:04

标签: pygame data-conversion

我需要将一个Circle存储在一个变量中,但是在完成之后,它已经变成了一个矩形

circle_1 = pygame.draw.circle(screen, (0, 0, 0), (300, 300), 30)
Print(circle_1)

打印返回

<rect(270, 270, 60, 60)>

但是我不能使用它。 我的圈子是预定义的,但不会在画布上显示,这是问题的一个示例

> import pygame, sys
> 
> 
> pygame.init() screen = pygame.display.set_mode((600, 600)) 
> predefined_circle = pygame.draw.circle(screen,(0, 0, 0),(300, 300), 30)
> 
> def update():
>     screen.fill((200, 0, 0))
>     while 1:
>         for event in pygame.event.get():
>             if event.type == pygame.QUIT: sys.exit()
>         # It shows my circle if I dirctly tip pygame.draw.circle(screen,(0, 0, 0),(300, 300), 30) into it
>         predefined_circle
>         pygame.display.update()
> 
> update()

这样您可以更好地与我正在尝试实现的代码相关联,这是我正在执行的代码,但是由于我已经在上面进行了尽可能详尽的解释,因此无需阅读。 请注意,注释应解释其下面的代码块正在执行的所有操作。

# Creating the canvas which can paint any wanted Object from another class
class Canvas:
# Initialising the screen and setting all needed variables
def __init__(self, painting):
    pygame.init()
    self.screen_size = (600, 600)
    self.background = (25, 255, 255)
    self.screen = pygame.display.set_mode(self.screen_size)
    self.paint = painting

# Let the user set the name of the canvas
def set_screen_name(self):
    return self.screen

# Draw the everything you want to
def update(self):
    # Paint the canvas
    self.screen.fill(self.background)
    # Make the game be quittable
    while 1:
        for event in pygame.event.get():
            if event.type == pygame.QUIT: sys.exit()

        # Draw the defined Circle and then update the Canvas
        # it only draws a circle if directly tip pygame.draw.circle(surface, color, position, radius)

        self.paint
        pygame.display.update()


# Draw any circle you like
class Cir:
# Get all the required Information's to Draw a circle
def __init__(self, canvas, what_color, position, radius, line=0):
    self.can = canvas
    self.color = what_color
    self.pos = position
    self.r = radius
    self.line = line
    self.cir = None

# Create the circle with the acquired Information's
def create(self):
    self.cir = pygame.draw.circle(self.can, self.color, self.pos, self.r, self.line)
    return self.cir


# So far there is no Surface for the Cir class
# And there is no Object that cloud be painted for the Canvas class
# I initialise a canvas instance without anything that needs to be painted
get_surface = Canvas(None)

# Now I can access set_screen_name from the Canvas class and give the surface a name
# Which the Cir class can now use as a surface
screen = get_surface.set_screen_name()

c1 = pygame.draw.circle(screen, (0,0,0), (300, 300), 30)
print(c1)

# I'm initialising a circle
init_circle = Cir(screen, (0, 255, 0), (300, 300), 30)

# Create the initialised circle
circle_1 = init_circle.create()

# Give the Canvas class the created circle
paint = Canvas(circle_1)

# Draw the circle
paint.update()

1 个答案:

答案 0 :(得分:1)

  

我的circle变成了rect

实际上,不,不是。按照这些绘制函数的documentation,调用的目的是立即绘制一些东西,不是给您一个可以稍后绘制的对象:< / p>

  

在Surface上绘制几个简单的形状。

从对问题的分析看来,您似乎认为自己正在存储绘制圆的 act ,以便以后可以进行。事实并非如此。相反,您实际上是在绘制圆并保存该绘制动作的结果-以后评估结果实际上不会绘制或重绘该圆。

因此,如果draw函数没有返回供以后绘制的内容,那么它返回的是 呢?也可以在上述文档中找到

  

函数返回一个 矩形 ,代表更改后的像素的边界区域。

换句话说,它告诉您通过绘图操作更改的最小矩形-这是一个正方形,其边长与直径相同,并以同一点为中心。

很明显,PyGame的作者认为此信息出于某种目的可能是方便的,而不仅仅是重绘圆圈的目的:-)


一种实现您要实现的目标的方法是简单地具有一个 function 来绘制“预定义”圆并进行调用,而不是尝试评估上一次调用返回的矩形