我对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()