<p>很难理解你想要什么,但也许你想存储以前的鼠标点击位置,以便绘制矩形?你知道吗</p>
<p>你所要做的就是把它们存储在一个不同的变量中。如果你想一次只点击两个位置,你就用它。或者可以使用Python列表来存储任意数量的单击位置。你知道吗</p>
<pre><code>import pygame, sys
SIZE = 640, 480
screen = pygame.display.set_mode(SIZE)
# empty list
all_clicks = []
drawn = True
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
# store mouse click position in the list:
all_clicks.append(event.pos)
# event.pos already has the info you'd get by calling pygame.mouse.get_pos()
drawn = False
print(all_clicks)
# at every second click:
if not len(all_clicks) % 2 and not drawn:
# draw a rectangle having the click positions as coordinates:
# pick the minimal x coordinate of both clicks as left position for rect:
x = min(all_clicks[-1][0], all_clicks[-2][0])
# ditto for top positionn
y = min(all_clicks[-1][1], all_clicks[-2][1])
# and calculate width and height in the same way
w = max(all_clicks[-1][0], all_clicks[-2][0]) - x
h = max(all_clicks[-1][1], all_clicks[-2][1]) - y
pygame.draw.rect(screen, (255, 255, 255), (x, y, w, h))
drawn = True
# update screen:
pygame.display.flip()
# pause a while (30ms) least our game use 100% cpu for nothing:
pygame.time.wait(30)
</code></pre>