Python 为什么赢了';你能画个圆圈吗?
我是python的初学者,我试图在鼠标所在的位置画一个圆圈(我还有一个鼠标和背景图像)。这是我的密码:Python 为什么赢了';你能画个圆圈吗?,python,pygame,geometry,Python,Pygame,Geometry,我是python的初学者,我试图在鼠标所在的位置画一个圆圈(我还有一个鼠标和背景图像)。这是我的密码: while True: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit if event.type == MOUSEBUTTONDOWN: color = (100,10
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit
if event.type == MOUSEBUTTONDOWN:
color = (100,100,100)
posx,posy = pygame.mouse.get_pos()
screen.lock()
pygame.draw.circle(screen, color, (posx,posy), 50)
screen.unlock()
screen.blit(background,(0,0))
x,y = pygame.mouse.get_pos()
x -= mousec.get_width()/2
y -= mousec.get_height()/2
screen.blit(mousec, (x,y))
pygame.display.update()
每当我点击时,什么都不会发生。为什么它不画一个圆呢?谢谢你的帮助 我对pygame几乎一无所知,所以我帮不了你什么忙。。。但我认为你所做的总是在你的圈子里后退。试试这个:
pygame.init()
screen = pygame.display.set_mode((640, 480))
screen.fill((0,0,0))
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == MOUSEBUTTONDOWN:
# draw background first (however)
screen.fill((0,0,0))
# draw your other layers (mouse image)
# draw the circle
color = (255,255,255)
posx,posy = pygame.mouse.get_pos()
pygame.draw.circle(screen, color, (posx,posy), 50)
pygame.display.update()
基本上,在您的示例中发生的情况是,当鼠标按下事件进行绘制时,您将使用背景再次在其上绘制。我不确定什么是mousec
,但每次都会在背景上画出来。因此,您将永远不会看到鼠标单击绘制的圆
我的示例首先填充背景一次,然后当鼠标按下时,它将再次填充背景以绘制上一个状态,然后绘制圆
另一种方法,使用您的确切示例,是在检查事件时仅记录鼠标位置,并将圆的绘制推迟到图层之后。您将“记住”在每个循环上不断重画该圆的最后一个鼠标位置:
last_mouse_pos = None
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit
if event.type == MOUSEBUTTONDOWN:
last_mouse_pos = pygame.mouse.get_pos()
elif event.type == KEYDOWN and event.unicode == 'c':
# clear the circle when pressing the 'c' key
last_mouse_pos = None
screen.blit(background,(0,0))
x,y = pygame.mouse.get_pos()
x -= mousec.get_width()/2
y -= mousec.get_height()/2
screen.blit(mousec, (x,y))
if last_mouse_pos:
color = (100,100,100)
posx,posy = last_mouse_pos
pygame.draw.circle(screen, color, (posx,posy), 50)
pygame.display.update()
这种方法的不同之处在于,您总是在每个循环上绘制所有内容,而不是仅响应事件中的更改
更新
针对您在评论中提出的问题。。。修改第二个示例以保留所有鼠标点击的一种方法是将它们保存在集合中,并每次将它们拉回来
mouse_clicks = set()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit
if event.type == MOUSEBUTTONDOWN:
mouse_clicks.add(pygame.mouse.get_pos())
elif event.type == KEYDOWN and event.unicode == 'c':
# clear the circle when pressing the 'c' key
mouse_clicks.clear()
screen.blit(background,(0,0))
x,y = pygame.mouse.get_pos()
x -= mousec.get_width()/2
y -= mousec.get_height()/2
screen.blit(mousec, (x,y))
for pos in mouse_clicks:
color = (100,100,100)
posx,posy = pos
pygame.draw.circle(screen, color, (posx,posy), 50)
pygame.display.update()
非常感谢。这就解决了问题。每当你再次点击时,它会删除最后一个圆圈,你知道我如何使它保持所有圆圈不变吗?我想我得把screen.blit(background,(0,0))移出循环。。。但我不确定。只需在循环之前将背景和鼠标图像闪烁一次,这样您就只能在顶部绘制新的圆圈。这将在我的第一个示例中起作用。第二个示例需要将位置附加到列表中,以便每次循环时都可以循环该列表并绘制所有圆。“清除”键可能会清空列表。非常感谢!现在鼠标图像不会移动,因为它已脱离循环。我现在就要开始工作了,谢谢你的帮助!刚才为您添加了一个示例。我用了一套。尽管一组是无序的,但我认为你们的圆圈是实心的,所以顺序并不重要。集合是唯一的,可以更快地添加新项目。因此,如果你在同一个点上点击多次,它仍然只有一个圆。