Skip to main content
 首页 » 编程设计

python之为什么我的 pygame 代码没有画出这个圆圈

2025年05月04日8yxwkf

http://pastebin.com/PLdiNg9d

首先,我是 Python 的新手,对于非常草率的代码感到抱歉!无论如何,关于我的问题。我制作了一个类单元格,在它的初始化函数中,它接受几个用来画圆的变量。然后我这样做:

self.image = pygame.Surface([size, size]) 
self.image.fill(WHITE) 
self.image.set_colorkey(WHITE) 
pygame.draw.circle(self.image, color, [posx, posy], size) 
self.rect = self.image.get_rect() 
print('Cell initialized.') 

问题是,它没有在我的屏幕上绘制任何东西。它不会输出错误消息,但仍会打印“Cell initialized”。所以我知道它一直在通过 init 函数。

真正让我感到困惑的是,有一种方法可以让它发挥作用。如果我移动

cell_list = pygame.sprite.Group() 
a = Cell(5, GREEN, 2, 200, 200) 
cell_list.add(a) 

进入我的while循环,然后改变

pygame.draw.circle(self.image, ...) 

到

pygame.draw.circle(screen, ...) 

然后它将绘制我的单元格。任何帮助都将不胜感激,因为我完全被难住了。

请您参考如下方法:

这是一个简单的模板,可在使用 pygame 时使用。此演示在屏幕中心绘制一个圆圈。

from pygame import * 
screen = display.set_mode((500,500)) 
# Fill screen white 
screen.fill((255,255,255)) 
 
# Draw a red circle at the center 
# Remove the 1 to make it a filled circle 
draw.circle(screen, (255,0,0), (250,250), 250, 1) 
 
# Game loop 
running = True 
while running: 
    # Properly quit (pygame will crash without this) 
    for e in event.get(): 
        if e.type == QUIT: 
            running = False 
 
    display.flip() 
quit()