Mad*_*ung 3 python geometry pygame
我是python的初学者,我试图在鼠标所在的地方画一个圆圈(我也有一个鼠标和背景图像)。这是我的代码:
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()
Run Code Online (Sandbox Code Playgroud)
每当我点击什么都没有发生。为什么不画圆?谢谢您的帮助!
我对 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()
Run Code Online (Sandbox Code Playgroud)
基本上,在您的示例中发生的情况是,当鼠标按下事件进行绘制时,您将再次使用背景将其绘制回来。我不确定是什么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()
Run Code Online (Sandbox Code Playgroud)
这种方法的不同之处在于您总是在每个循环中绘制所有内容,而不是仅响应事件的变化。
更新
为了回答您在评论中的问题......修改第二个示例以保留所有鼠标点击的方法是将它们保存在 a 中set并每次将它们拉回来。
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()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6583 次 |
| 最近记录: |