You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pygame中无法在Surface上绘制圆形的问题求助

问题分析与解决:Pygame无法在Surface上绘制圆形

你遇到的问题其实是个很容易疏忽的小失误——圆形的绘制位置超出了Surface的可视范围!

看你代码里这一行:

pygame.draw.circle(surf1, (0,0,0), (200,2000), 5)

你的surf1高度只有400像素,但你把圆形的y坐标设成了2000,这相当于把圆画到了Surface的“屏幕外面”,当然看不到啦。

只需要把y坐标改成Surface范围内的值(比如和x坐标一样的200),就能看到预期的黑色圆形了。下面是修正后的完整代码:

import pygame
pygame.init()
width = 400
height = 400
screen = pygame.display.set_mode((width, height))
surf1 = pygame.Surface((width,height))
surf1.fill((0,255,0))
# 修正y坐标为200,回到Surface可视范围内
pygame.draw.circle(surf1, (0,0,0), (200,200), 5)
screen.blit(surf1, (0,0))
exit = False
while not exit:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit = True
    pygame.display.update()
pygame.quit()

另外补充一下,你想用多Surface分层、通过透明色键擦除显示下层内容的思路是完全可行的,后续实现的时候记得给需要透明效果的Surface设置set_colorkey(),或者直接创建带alpha通道的Surface(比如pygame.Surface((w,h), pygame.SRCALPHA))就可以顺利实现需求啦。

内容的提问来源于stack exchange,提问作者Loon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:51:25