|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 lzb1001 于 2022-6-22 12:56 编辑
# p16_Demo_turtle_pure.py
import pygame
import sys
from pygame.locals import *
pygame.init()
size = width, height = 800, 600
bg=(255, 255, 255)
clock = pygame.time.Clock()
screen = pygame.display.set_mode(size)
pygame.display.set_caption('FishC Demo')
turtle = pygame.image.load(r'd:\\work\\turtle_left.png')
select = 0
select_rect = pygame.Rect(0, 0, 0, 0)
drag = 0
position = turtle.get_rect()
position.center = width // 2, height // 2
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
elif event.type == MOUSEBUTTONDOWN:
if event.button == 1:
if select == 0 and drag == 0: # ①
pos_start = event.pos
select = 1
elif select == 2 and drag == 0: # ③
capture = screen.subsurface(select_rect).copy()
cap_rect = capture.get_rect()
drag = 1
elif select == 2 and drag == 2: # ⑤
select = 0
drag = 0
elif event.type == MOUSEBUTTONUP:
if event.button == 1: # ②
if select == 1 and drag == 0:
pos_stop = event.pos
select = 2
if select == 2 and drag == 1: # ④
drag = 2
screen.fill(bg)
screen.blit(turtle, position)
if select: # ⑥
mouse_pos = pygame.mouse.get_pos()
if select == 1:
pos_stop = mouse_pos
select_rect.left, select_rect.top = pos_start
select_rect.width, select_rect.height = pos_stop[0] - pos_start[0], pos_stop[1] - pos_start[1]
pygame.draw.rect(screen, (0, 0, 0), select_rect, 1)
if drag: # ⑦
if drag == 1:
cap_rect.center = mouse_pos
screen.blit(capture, cap_rect)
pygame.display.flip()
clock.tick(30)
------------------------------------------
Windows 10 专业版 | Python 3.7.6
------------------------------------------
【我的问题】
1、按裁剪图片的正常操作步骤的话,代码运行先后顺序是不是①-⑥-②-③-⑥-⑦-④-⑤?
2、代码中蓝色部分为何不用elif?
3、select == 0 与 select = 0 有何区别?
4、为何有的需要使用and而有的又不需要?
5、if select:中的 if drag == 1:以及 if drag == 1:没搞明白意思
6、代码的先后顺序和缩进的使用?比如下面两行代码出现的位置和缩进没搞明白
……
screen.fill(bg)
screen.blit(turtle, position)
……
7、以上代码存在bug,比如选择图像范围时鼠标拖拽的位置和方向有可能导致返回错误提示,比如:
Traceback (most recent call last):
File "D:\work\p16_Demo_turtle.py", line 88, in <module>
capture = screen.subsurface(select_rect).copy()
pygame.error: Out of memory
8、拖拽后小乌龟身上被选择的部分如果不显示(即已被裁剪并拖拽离开小乌龟本体),那么又该如何修改代码呢?
******************************
感谢大神不吝赐教,为新手解疑释惑。
赠人玫瑰,手有余香,好人一生平安! |
|