|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 黎明§末日 于 2017-9-3 13:23 编辑
由于小甲鱼在视频中演示的例子有些小问题,so供上参考
代码有点乱
第81课 (自带详细注释)
修复以下几个bug:
- 出界问题
- 放大缩小后的方向(我用的是比较强硬的方法,期待大家有别的想法)
- 缩放后的边界问题
还有一些,但记不清了
- '''导入模块'''
- # 导入pygame
- import pygame
- # 导入sys
- import sys
- # 导入pygame的locals的所有内容
- from pygame.locals import *
- '''设置变量'''
- # pygame初始化
- pygame.init()
- # 设置初始尺寸
- init_size = [600, 400]
- full_size = [1024, 768]
- # 设置初始移动速度
- speed = [-5, 5]
- # 设置背景颜色 RGB
- bg = (255, 255, 255)
- # 打开纪录事件文件
- f = open('record.txt', 'w')
- # 是否属于全屏
- fullscreen = False
- # 设置可调节尺寸的桌布
- screen = pygame.display.set_mode(init_size, RESIZABLE)
- # 设置初始标题
- pygame.display.set_caption("初次见面,请大家多多关照!")
- # 初始图片比例
- ratio = 1.0
- # 导入图片,为此设置一个常量
- oturtle = pygame.image.load("turtle.png")
- # 设置一个常变的图片变量
- turtle = oturtle
- # 获得固定图片的位置矩形
- oturtle_rect = oturtle.get_rect()
- # 设置位置和常变图片的位置矩形
- position = turtle_rect = oturtle_rect
- # 朝左的图像变量
- l_head = turtle
- # 朝右的图像变量
- r_head = pygame.transform.flip(turtle, True, False)
- # 设置倍数:宽
- multiple_width = 1
- # 设置倍数:高
- multiple_height = 1
- '''主循环'''
- # 死循环
- while True:
- '''事件循环'''
- # 获得事件信息
- for event in pygame.event.get():
- # 将事件的信息写入事件文件
- f.write(str(event) + '\n')
- # 判断是否点击关闭按钮
- if event.type == pygame.QUIT:
- # 事件文件关闭
- f.close()
- # 退出
- sys.exit()
- '''键盘事件'''
- # 判断是否有键盘操作
- if event.type == KEYDOWN:
- '''键盘改变方向'''
- # 如果输入键为 ←
- if event.key == K_LEFT:
- # 如果要出界
- if position.left > 0:
- # 头朝左
- turtle = l_head
- print('左')
- # 设置移动方向和速度
- speed = [-5, 0]
- # 如果输入键为 →
- if event.key == K_RIGHT:
- # 如果要出界
- if position.right < init_size[0]:
- turtle = r_head
- # 头朝右
- print('右')
- # 设置移动方向和速度
- speed = [5, 0]
- # 如果输入键为 ↑
- if event.key == K_UP:
- # 如果要出界
- if position.top > 0:
- print('上')
- # 设置移动方向和速度
- speed = [0, -5]
- # 如果输入键为 ↓
- if event.key == K_DOWN:
- # 如果要出界
- if position.bottom < init_size[1]:
- print('下')
- # 设置移动方向和速度
- speed = [0, 5]
- '''全屏'''
- # 如果输入键为 F11
- if event.key == K_F11:
- # 设置变量'fullscreen',更改其状态
- fullscreen = not fullscreen
- # 设置倍数:宽
- multiple_width = (position[0] // init_size[0])
- # 设置倍数:高
- multiple_height = (position[1] // init_size[1])
- # 如果'fullscreen'为True
- if fullscreen:
- # 使其改变为全屏,并开启硬件加速
- screen = pygame.display.set_mode(full_size, FULLSCREEN | HWSURFACE)
- # 通过缩放倍数来设置图片位置的宽
- position[0] = full_size[0]*multiple_width
- # 将结果四舍五入
- position[0] = int(position[0]+0.5)
- # 通过缩放倍数来设置图片位置的高
- position[1] = full_size[1]*multiple_height
- # 将结果四舍五入
- position[1] = int(position[1] + 0.5)
- # 保存旧的宽和高
- old_width, old_height = init_size[0], init_size[1]
- # 设置宽和高
- init_size = full_size
- # 刷新图片位置
- screen.blit(turtle, position)
- # 否则
- else:
- # 回到全屏前尺寸
- screen = pygame.display.set_mode([old_width, old_height], RESIZABLE)
- # 设置边框位置(易发bug)
- init_size = [old_width, old_height]
- # 通过缩放倍数来设置图片位置的宽
- position[0] = init_size[0]*multiple_width
- # 通过缩放倍数来设置图片位置的高
- position[1] = init_size[1]*multiple_height
- # 刷新图片位置
- screen.blit(turtle, position)
- '''缩放图片'''
- # 如果为'=','-',' '
- if event.key == K_EQUALS or event.key == K_MINUS or event.key == K_SPACE:
- # 如果输入键为'='
- if event.key == K_EQUALS and ratio < 2:
- # 放大缩放倍数
- ratio += 0.1
- # 如果输入键为'-'
- if event.key == K_MINUS and ratio > 0.5:
- # 缩小缩放倍数
- ratio -= 0.1
- # 如果输入键为' '
- if event.key == K_SPACE:
- # 恢复缩放倍数
- ratio = 1.0
- # 保留一位小数
- ratio = float('%1f' % ratio)
- # 使用图片常量的缩放结果更改常变的图片变量
- turtle = pygame.transform.smoothscale(oturtle, (int(oturtle_rect.width * ratio), int(oturtle_rect.height * ratio)))
- # 获得旧的位置
- old_position = position
- # 获得新的图片矩形(易发bug)
- position = turtle.get_rect()
- # 取得旧位置
- position[0], position[1] = int(old_position[0]), int(old_position[1])
- # 获得新尺寸朝左的图像变量
- l_head = turtle
- # 获得新尺寸朝右的图像变量
- r_head = pygame.transform.flip(l_head, True, False)
- '''更改桌布'''
- # 如果更改了桌布尺寸
- if event.type == VIDEORESIZE:
- # 更改桌布尺寸
- size = event.size
- # 输出更改后的桌布尺寸
- # 设置桌布
- screen = pygame.display.set_mode(size, RESIZABLE)
- # 移动图片
- position = position.move(speed)
- '''如果图片在左右两边出界'''
- if position.left < 0:
- # 回到边框
- position.left = 0
- # 更改移动方向
- speed[0] = -speed[0]
- if position.right > init_size[0]:
- # 回到边框
- position.right = init_size[0]
- # 更改移动方向
- speed[0] = -speed[0]
- '''如果图片在上下两边出界'''
- if position.top < 0:
- # 回到边框
- position.top = 0
- # 更改移动方向
- speed[1] = -speed[1]
- if position.bottom > init_size[1]:
- # 回到边框
- position.bottom = init_size[1]
- # 更改移动方向
- speed[1] = -speed[1]
- '''强行转向'''
- if speed[0] < 0:
- turtle = l_head
- if speed[0] > 0:
- turtle = r_head
- '''刷新图片'''
- # 绘制桌布
- screen.fill(bg)
- # 刷新图片位置
- screen.blit(turtle, position)
- # 绘制
- pygame.display.flip()
- # 暂停
- pygame.time.delay(10)
复制代码
(此代码已测试)如有问题请回复
###我叫分割线
第82课 (自带注释)
修复以下几个bug:
- '''导入模块'''
- import pygame
- import sys
- from pygame.locals import *
- pygame.init()
- '''设置变量'''
- size = width, height = 640, 480
- bg = (255, 255, 255)
- clock = pygame.time.Clock()
- screen = pygame.display.set_mode(size)
- pygame.display.set_caption('FishC Demo')
- turtle = pygame.image.load("turtle.png")
- position1 = turtle.get_rect()
- # 使图片居中
- position1.center = width//2, height//2
- # 0 -> 未选择,1 -> 选择中,2 -> 完成选择
- select = 0
- select_rect = pygame.Rect(0, 0, 0, 0)
- # 0 -> 未拖拽,1 -> 拖拽中,2 -> 完成拖拽
- drag = 0
- '''主循环'''
- while True:
- for event in pygame.event.get():
- if event.type == QUIT:
- sys.exit()
- elif event.type == MOUSEBUTTONDOWN:
- if event.button == 1:
- print('鼠标按下左键')
- if select == 0 and drag == 0:
- # 第一次点击,选择范围
- start = event.pos
- select = 1
- if select == 2 and drag == 0:
- # 第二次点击,推拽图像
- capture = screen.subsurface(select_rect).copy()
- cap_rect = capture.get_rect()
- drag = 1
- if select == 2 and drag == 2:
- # 第三次点击,初始化
- select = 0
- drag = 0
- elif event.type == MOUSEBUTTONUP:
- if event.button == 1:
- print('鼠标松开左键')
- if select == 1 and drag == 0:
- # 第一次释放,结束选择
- stop = event.pos
- select = 2
- if select == 2 and drag == 1:
- # 第二次释放,结束拖拽
- drag = 2
- position2 = pygame.mouse.get_pos()
- screen.fill(bg)
- screen.blit(turtle, position1)
- if select != 0:
- position2 = pygame.mouse.get_pos()
- if select == 1:
- stop = position2
- # 判断(从下往上拖时的bug)
- if start[0] < stop[0] and start[1] < stop[1]:
- select_rect.x, select_rect.y = start
- select_rect.width, select_rect.height = stop[0] - start[0], stop[1] - start[1]
- if start[0] > stop[0] and start[1] > stop[1]:
- select_rect.x, select_rect.y = stop
- select_rect.width, select_rect.height = start[0] - stop[0], start[1] - stop[1]
- pygame.draw.rect(screen, (0, 0, 0), select_rect, 1)
- print(select_rect)
- if drag != 0:
- if drag == 1:
- cap_rect.center = position2
- screen.blit(capture, cap_rect)
- pygame.display.flip()
- clock.tick(10)
复制代码
(此代码已测试)如有问题请回复
###我叫分割线
希望大家帮忙看看有没有bug
别水
回复‘吾爱鱼c’见隐藏:
呵呵,隐藏内容果然是‘隐藏内容’ |
-
turtle.png(如果没有将无法执行)
评分
-
查看全部评分
|