|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import pygame
import sys
from pygame.locals import *
pygame.init()
size=width,height=800,600
bg=255,255,255
screen=pygame.display.set_mode(size)
pygame.display.set_caption('frist')
turtle=pygame.image.load('turtle.png')
#0-没有选中 1选中了 2结束选中
push = 0
#0没有拖动 1拖动中 2 结束拖动
pull = 0
position=turtle.get_rect()
position.center=width//2,height//2
push_rect=pygame.Rect(0,0,0,0)
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
elif event.type==MOUSEBUTTONDOWN:
if event.button==1:
#开始选择
if push ==0 and pull ==0:
pos_start=event.pos
push=1
elif push ==2 and pull==0:
cap = screen.subsurface(push_rect).copy()
cap_rect=cap.get_rect()
pull=1
elif push==2 and pull==2:
push=0
pull=0
elif event.type==MOUSEBUTTONUP:
if event.button==1:
#选择完成
if push == 1 and pull == 0:
pos_stop=event.pos
push==2
if push == 2 and pull == 1:
pull=2
screen.fill(bg)
screen.blit(turtle,position)
if push:
mouse_pos = pygame.mouse.get_pos()
if push==1:
pos_stop=mouse_pos
push_rect.left,push_rect.top=pos_start
push_rect.width,push_rect.height=pos_stop[0]-pos_start[0],pos_stop[1]-pos_start[1]
pygame.draw.rect(screen,(0,0,0),push_rect,1)
if pull:
if pull==1:
cap_rect.center=mouse_pos
screen.blit(cap,cap_rect)
pygame.display.flip()
elif event.type==MOUSEBUTTONUP:
if event.button==1:
#选择完成
if push == 1 and pull == 0:
pos_stop=event.pos
push==2
看到这个最后一句你该明白了
|
|