Pygame从0实战5

发布时间:2024-11-23 14:57

职场实用技巧:《从0到1》 #生活乐趣# #阅读乐趣# #新书推荐#

最新推荐文章于 2024-01-20 19:43:01 发布

youaresherlock 于 2018-03-06 20:47:04 发布

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

1.Pygame从0实战5

    如何实现一个裁剪工具
思路:在界面上鼠标第一次按下左键拖拽选择要裁剪的正方形 
第二次鼠标左键拖拽实现将裁剪的图形移动到释放鼠标左键的位置

第三次点击的时候是取消裁剪

"""

rect(Surface, color, Rect, width=0)

# create a new surface that references its parent. Returns a new Surface that shares its pixels with its new parent.

The new Surface is considered a child of the original. Modifications to either Surface pixels will effect each other.

capture = screen.subsurface(select_rect).copy()

Pygame.Surface blit(source, dest, area=None, special_flags = 0) - > Rect:

Draws a source Surface onto this Surface. The draw can be positioned with the dest argument.Dest can

either be pair of coordinates representing the upper left corner of the source. A Rect can also be passed as

the destination and the topleft corner of the rectangle will be used as the position for the blit.

"""

import pygame

import sys

from pygame.locals import *

pygame.init()

size = width, height = 800, 600

clock = pygame.time.Clock()

screen = pygame.display.set_mode(size)

pygame.display.set_caption("裁剪小工具")

turtle = pygame.image.load("turtle.png")

background = pygame.image.load("background.jpg")

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.blit(background, (0, 0))

screen.blit(turtle, position)

'''

pygame.mouse: pygame module to work with the mouse

pygame.mouse.get_pos(): get the mouse cursor 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() draw a rectangle shape

rect(Surface, color, Rect, width= 1) -> Rect

width是矩形边框长度为一个像素

'''

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)


网址:Pygame从0实战5 https://www.yuejiaxmz.com/news/view/213526

相关内容

小白如何做一个Python人工智能语音助手
本地生活素人培训班:从0-1落地实操课程,方法技术,实战应用,案例解析
国服激战2烹饪0
智能家居系统:语音互动与情感体验智能家居系统:语音互动与情感体验 随着人工智能的不断发展,智能家居系统成为改善生活质量、
Python从0到100(七十三):Python OpenCV
5+2=0及2+5=0评论分析和总结.pdf
精准击破德育“5+2=0”困局
使用Spring@Scheduled(cron = “0 0/10 * * * ?”) 实现定时任务
你怎么看教育中的“5+2=0”?(0632)
【乐家创城活动】不要5+2=0而是5+2≥7

随便看看