1 回答
TA贡献1934条经验 获得超2个赞
我建议从上一个鼠标位置到当前鼠标位置绘制一条线。另外,在线的起点和终点画一个点。这会导致一个回合的开始和结束。
跟踪鼠标 (, ) 的上一个位置,并在主应用程序循环(而不是事件循环)中绘制线条:lastx
lasty
例如:
https://i.stack.imgur.com/mojlK.gif
import pygame
width, height = 800, 600
pygame.init() #As necessary as import, initalizes pygame
gameDisplay = pygame.display.set_mode((width,height)) #Makes window
pygame.display.set_caption('Demo') #Titles window
clock = pygame.time.Clock() #Keeps time for pygame
gameDisplay.fill((0,0,255))
class Draw:
def __init__(self):
self.color = (255, 0, 0)
def update(self, from_x, from_y, to_x, to_y):
pygame.draw.circle(gameDisplay, self.color, (from_x, from_y), 5)
pygame.draw.line(gameDisplay, self.color, (from_x, from_y), (to_x, to_y), 10)
pygame.draw.circle(gameDisplay, self.color, (to_x, to_y), 5)
end = False
down = False
line = Draw()
while not end:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
lastx, lasty = event.pos
down = True
if event.type == pygame.MOUSEBUTTONUP:
down = False
if event.type == pygame.QUIT:
end = True
x, y = pygame.mouse.get_pos()
if down:
line.update(lastx, lasty, x, y)
lastx, lasty = x, y
pygame.display.update()
clock.tick(60)
pygame.quit()
添加回答
举报