|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 wuqramy 于 2020-3-26 13:27 编辑
@DavidCT ,快来,pygame第三期!
首先下载pygame第三方库,在cmd中输入以下命令:
其次上附件:
注!:请把图片文件放在一个文件夹里,然后在图片名前面加上文件夹路径!
资源打包:
Bag2.zip
(170.07 KB, 下载次数: 1)
最后上代码:
这是一个裁剪龟兄的程序,玩法:
1.拖戈鼠标,选中一块区域;
2.移动鼠标;
3.再次点击鼠标;
好玩吗?代码如下:
- import pygame
- import pygame.transform
- import sys
- from pygame.locals import *
- pygame.init()
- size = width,height = 400,350
- bg = (255,255,255)
- clock = pygame.time.Clock()
- screen = pygame.display.set_mode(size)
- pygame.display.set_caption('cutting turtle')
- turtle = pygame.image.load('turtle.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()
- # 延时10毫秒
- #pygame.time.delay(10)
- # 设置帧数
- clock.tick(30)
复制代码
然后还有一个隐身龟兄:
- import pygame
- import pygame.transform
- import sys
- pygame.init()
- size = width,height = 350,300
- bg = (0,0,0)
- clock = pygame.time.Clock()
- screen = pygame.display.set_mode(size)
- pygame.display.set_caption('running turtle')
- turtle = pygame.image.load('C:\\Pythonstudy\\mytest\\Pygamestudy\\images\\turtle.png')
- background = pygame.image.load('C:\\Pythonstudy\\mytest\\Pygamestudy\\images\\grass.jpg')
- position = turtle.get_rect()
- position.center = width // 2,height // 2
- def blit_alpha(target,source,location,opacity):
- x = location[0]
- y = location[1]
- temp = pygame.Surface((source.get_width(),source.get_height())).convert()
- temp.blit(target,(-x,-y))
- temp.blit(source,(0,0))
- temp.set_alpha(opacity)
- target.blit(temp,location)
- while True:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- sys.exit()
- screen.blit(background,(0,0))
- blit_alpha(screen,turtle,position,200)
- pygame.display.flip()
- clock.tick(30)
复制代码
今天就到这里。
如果喜欢,别忘了:
|
评分
-
查看全部评分
|