如果您不想通过单击来绘制线条,则必须在第一次单击时保存起始位置,并在第二次单击时结束线条。这些行必须存储到一个列表中。添加一个留置权列表和一个变量line_start,并通过None对其进行初始化:
lines = []
line_start = None
设置第一次单击开始,第二次单击结束行 (MOUSEBUTTONDOWN)。将完成的行添加到行列表中:
if e.type == pygame.MOUSEBUTTONDOWN:
# [...]
if line_start:
lines.append((line_start, e.pos))
line_start = None
else:
line_start = e.pos
在循环中画线。如果一条线开始 bunt 未完成从开始位置到当前鼠标位置画一条线:
for line in lines:
pygame.draw.line(screen, pygame.Color('lawngreen'), *line)
if line_start:
pygame.draw.line(screen, pygame.Color('lawngreen'), line_start, pygame.mouse.get_pos())
小例子:
import pygame
pygame.init()
SCREEN_WIDTH = 1500
SCREEN_HEIGHT = 750
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
clock = pygame.time.Clock()
background = pygame.image.load(r'C:\Users\ga-sa\Downloads\honeycomb.png')
background = pygame.transform.scale(BACKGROUND, (SCREEN_WIDTH, SCREEN_HEIGHT))
img1 = pygame.image.load(r"C:\Users\ga-sa\Downloads\As.png")
img2 = pygame.image.load(r"C:\Users\ga-sa\Downloads\AssetsXOR.png")
img3 = pygame.image.load(r"C:\Users\ga-sa\Downloads\AssetsNOT.png")
img4 = pygame.image.load(r"C:\Users\ga-sa\Downloads\AssetsAND.png")
images = [img1, img2, img3, img4]
current_image = -1
img_rects = [images[i].get_rect(topleft=(20 + 80 * i, 20)) for i in range(len(images))]
img_angles = [0 for _ in range(len(images))]
lines = []
line_start = None
LeftButton = 0
while 1:
clock.tick(60)
for e in pygame.event.get():
if e.type == pygame.QUIT:
pygame.quit()
exit(0)
if e.type == pygame.MOUSEBUTTONDOWN:
mouse_rect = pygame.Rect(e.pos, (1, 1))
current_image = mouse_rect.collidelist(img_rects)
if line_start:
lines.append((line_start, e.pos))
line_start = None
else:
line_start = e.pos
if e.type == pygame.MOUSEMOTION:
if e.buttons[LeftButton]:
rel = e.rel
if 0 <= current_image < len(images):
img_rects[current_image].x += rel[0]
img_rects[current_image].y += rel[1]
keys = pygame.key.get_pressed()
if 0 <= current_image < len(img_angles):
if keys[pygame.K_RIGHT]:
img_angles[current_image] -= 1
if keys[pygame.K_LEFT]:
img_angles[current_image] += 1
screen.blit(background,(0,0))
for line in lines:
pygame.draw.line(screen, pygame.Color('lawngreen'), *line)
if line_start:
pygame.draw.line(screen, pygame.Color('lawngreen'), line_start, pygame.mouse.get_pos())
for i in range(len(images)):
rotated_image = pygame.transform.rotate(images[i], img_angles[i])
rotated_rect = rotated_image.get_rect(center = img_rects[i].center)
screen.blit(rotated_image, rotated_rect)
pygame.display.flip()
如果你想在拖动图标的同时画一条橡皮线,你必须保存线的开头。添加一个变量line_start并用`None:
初始化
line_start = None
设置鼠标按下时的起始位置(MOUSEBUTTONDOWN):
if e.type == pygame.MOUSEBUTTONDOWN:
mouse_rect = pygame.Rect(e.pos, (1, 1))
current_image = mouse_rect.collidelist(img_rects)
line_start = e.pos
鼠标释放时设置line_start = None(MOUSEBUTTONUP):
if e.type == pygame.MOUSEBUTTONUP:
line_start = None
如果设置了line_start,则从开始位置到当前鼠标位置画一条线:
if line_start:
pygame.draw.line(screen, pygame.Color('lawngreen'), line_start, pygame.mouse.get_pos())
小例子:
import pygame
pygame.init()
SCREEN_WIDTH = 1500
SCREEN_HEIGHT = 750
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
clock = pygame.time.Clock()
background = pygame.image.load(r'C:\Users\ga-sa\Downloads\honeycomb.png')
background = pygame.transform.scale(BACKGROUND, (SCREEN_WIDTH, SCREEN_HEIGHT))
img1 = pygame.image.load(r"C:\Users\ga-sa\Downloads\As.png")
img2 = pygame.image.load(r"C:\Users\ga-sa\Downloads\AssetsXOR.png")
img3 = pygame.image.load(r"C:\Users\ga-sa\Downloads\AssetsNOT.png")
img4 = pygame.image.load(r"C:\Users\ga-sa\Downloads\AssetsAND.png")
images = [img1, img2, img3, img4]
current_image = -1
img_rects = [images[i].get_rect(topleft=(20 + 80 * i, 20)) for i in range(len(images))]
img_angles = [0 for _ in range(len(images))]
line_start = None
LeftButton = 0
while 1:
clock.tick(60)
for e in pygame.event.get():
if e.type == pygame.QUIT:
pygame.quit()
exit(0)
if e.type == pygame.MOUSEBUTTONDOWN:
mouse_rect = pygame.Rect(e.pos, (1, 1))
current_image = mouse_rect.collidelist(img_rects)
line_start = e.pos
if e.type == pygame.MOUSEBUTTONUP:
line_start = None
if e.type == pygame.MOUSEMOTION:
if e.buttons[LeftButton]:
rel = e.rel
if 0 <= current_image < len(images):
img_rects[current_image].x += rel[0]
img_rects[current_image].y += rel[1]
keys = pygame.key.get_pressed()
if 0 <= current_image < len(img_angles):
if keys[pygame.K_RIGHT]:
img_angles[current_image] -= 1
if keys[pygame.K_LEFT]:
img_angles[current_image] += 1
screen.blit(background,(0,0))
if line_start:
pygame.draw.line(screen, pygame.Color('lawngreen'), line_start, pygame.mouse.get_pos())
for i in range(len(images)):
rotated_image = pygame.transform.rotate(images[i], img_angles[i])
rotated_rect = rotated_image.get_rect(center = img_rects[i].center)
screen.blit(rotated_image, rotated_rect)
pygame.display.flip()
如果你想在拖动图标的过程中画线,你需要一个线列表。每一行都是一个点列表:
lines = []
按下鼠标时开始一个新列表 (MOUSEMOTION):
if e.type == pygame.MOUSEBUTTONDOWN:
mouse_rect = pygame.Rect(e.pos, (1, 1))
current_image = mouse_rect.collidelist(img_rects)
lines.append([e.pos]) # <---
鼠标移动时在行列表的最后一行添加一个点(MOUSEMOTION):
if e.type == pygame.MOUSEMOTION:
if e.buttons[LeftButton]:
rel = e.rel
if 0 <= current_image < len(images):
img_rects[current_image].x += rel[0]
img_rects[current_image].y += rel[1]
lines[-1].append(e.pos) # <---
在循环中画线,取消pygame.draw.lines:
for line in lines:
if len(line) > 1:
pygame.draw.lines(screen, pygame.Color('lawngreen'), False, line)
如果你想要直线,你必须在移动鼠标时将直线的第二个点替换为新的鼠标位置:
while 1:
clock.tick(60)
for e in pygame.event.get():
# [...]
if e.type == pygame.MOUSEMOTION:
if e.buttons[LeftButton]:
rel = e.rel
if 0 <= current_image < len(images):
img_rects[current_image].x += rel[0]
img_rects[current_image].y += rel[1]
# lines[-1].append(e.pos)
if len(lines[-1]) < 2:
lines[-1].append(e.pos)
else:
lines[-1][1] = e.pos
小例子:
您可以通过更改straight_lines 的值在两种实现之间切换。
import pygame
pygame.init()
SCREEN_WIDTH = 1500
SCREEN_HEIGHT = 750
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
clock = pygame.time.Clock()
background = pygame.image.load(r'C:\Users\ga-sa\Downloads\honeycomb.png')
background = pygame.transform.scale(BACKGROUND, (SCREEN_WIDTH, SCREEN_HEIGHT))
img1 = pygame.image.load(r"C:\Users\ga-sa\Downloads\As.png")
img2 = pygame.image.load(r"C:\Users\ga-sa\Downloads\AssetsXOR.png")
img3 = pygame.image.load(r"C:\Users\ga-sa\Downloads\AssetsNOT.png")
img4 = pygame.image.load(r"C:\Users\ga-sa\Downloads\AssetsAND.png")
images = [img1, img2, img3, img4]
current_image = -1
img_rects = [images[i].get_rect(topleft=(20 + 80 * i, 20)) for i in range(len(images))]
img_angles = [0 for _ in range(len(images))]
lines = []
# straight_lines = False
straight_lines = True
LeftButton = 0
while 1:
clock.tick(60)
for e in pygame.event.get():
if e.type == pygame.QUIT:
pygame.quit()
exit(0)
if e.type == pygame.MOUSEBUTTONDOWN:
mouse_rect = pygame.Rect(e.pos, (1, 1))
current_image = mouse_rect.collidelist(img_rects)
lines.append([e.pos])
if e.type == pygame.MOUSEMOTION:
if e.buttons[LeftButton]:
rel = e.rel
if 0 <= current_image < len(images):
img_rects[current_image].x += rel[0]
img_rects[current_image].y += rel[1]
if len(lines[-1]) < 2 or not straight_lines:
lines[-1].append(e.pos)
else:
lines[-1][1] = e.pos
keys = pygame.key.get_pressed()
if 0 <= current_image < len(img_angles):
if keys[pygame.K_RIGHT]:
img_angles[current_image] -= 1
if keys[pygame.K_LEFT]:
img_angles[current_image] += 1
screen.blit(background,(0,0))
for line in lines:
if len(line) > 1:
pygame.draw.lines(screen, pygame.Color('lawngreen'), False, line)
for i in range(len(images)):
rotated_image = pygame.transform.rotate(images[i], img_angles[i])
rotated_rect = rotated_image.get_rect(center = img_rects[i].center)
screen.blit(rotated_image, rotated_rect)
pygame.display.flip()