Pygame,我的圆圈在我将它存储在一个变量后变成了一个矩形,我该如何防止这种情况发生
Pygame, my circle turns to a rect after I stored it in a variable, how do I prevent that from happening
我需要将一个 Circle 存储在一个变量中,但是在我完成之后它变成了一个 rect
circle_1 = pygame.draw.circle(screen, (0, 0, 0), (300, 300), 30)
Print(circle_1)
打印returns
<rect(270, 270, 60, 60)>
但我无法使用它。
我的圈子是预定义的,但它不会在 canvas 上显示,这是问题的示例
> 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()
My circle
turns to a rect
.
实际上,不,不是。根据那些绘图函数的documentation,调用的目的是立即绘制东西,不是给你一个对象你可以稍后再画:
Draw several simple shapes to a Surface.
根据对您问题的分析,您似乎认为您正在存储画圆的行为,以便稍后完成。事实并非如此。相反,您所做的实际上是绘制圆并保存该绘制操作的 结果 - 稍后评估结果将不会实际绘制或重绘圆。
那么,如果 draw
函数没有返回一些东西供以后绘制,那么 是 它返回的是什么?也可以在上述文档中找到:
The functions return a rectangle representing the bounding area of changed pixels.
换句话说,它告诉您绘图操作改变的最小矩形 - 这将是一个边长与直径相同且以同一点为中心的正方形。
显然,PyGame 的作者认为这些信息可能对某些目的有用,而不是重画圆的目的:-)
实现您想要实现的目标的一种方法是简单地使用 函数 来绘制 "predefined" 圆并调用它,而不是尝试求值从上次调用返回的矩形。
我需要将一个 Circle 存储在一个变量中,但是在我完成之后它变成了一个 rect
circle_1 = pygame.draw.circle(screen, (0, 0, 0), (300, 300), 30)
Print(circle_1)
打印returns
<rect(270, 270, 60, 60)>
但我无法使用它。 我的圈子是预定义的,但它不会在 canvas 上显示,这是问题的示例
> 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()
My
circle
turns to arect
.
实际上,不,不是。根据那些绘图函数的documentation,调用的目的是立即绘制东西,不是给你一个对象你可以稍后再画:
Draw several simple shapes to a Surface.
根据对您问题的分析,您似乎认为您正在存储画圆的行为,以便稍后完成。事实并非如此。相反,您所做的实际上是绘制圆并保存该绘制操作的 结果 - 稍后评估结果将不会实际绘制或重绘圆。
那么,如果 draw
函数没有返回一些东西供以后绘制,那么 是 它返回的是什么?也可以在上述文档中找到:
The functions return a rectangle representing the bounding area of changed pixels.
换句话说,它告诉您绘图操作改变的最小矩形 - 这将是一个边长与直径相同且以同一点为中心的正方形。
显然,PyGame 的作者认为这些信息可能对某些目的有用,而不是重画圆的目的:-)
实现您想要实现的目标的一种方法是简单地使用 函数 来绘制 "predefined" 圆并调用它,而不是尝试求值从上次调用返回的矩形。