鱼C论坛

 找回密码
 立即注册
查看: 2942|回复: 16

[学习笔记] Python学习心情记录 2020/3/25

[复制链接]
发表于 2020-3-25 15:02:39 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 wuqramy 于 2020-3-25 15:48 编辑

@DavidCT ,快来,pygame第二期!

首先下载pygame第三方库,在cmd中输入以下命令:
  1. pip install pygame
复制代码


其次上附件:
注!:请把图片文件放在一个文件夹里,然后在图片名前面加上文件夹路径!
资源打包:
Bag2_images.zip (170.07 KB, 下载次数: 6)

最后上代码:
本次增加的功能:可以通过键盘上的上下左右按键控制乌龟的移动
  1. import pygame
  2. import pygame.transform
  3. import sys
  4. from pygame.locals import *
  5. pygame.init()
  6. size = width,height = 600,400
  7. speed = [-2,1]
  8. bg = (255,255,255)
  9. # 加载帧数调节器
  10. clock = pygame.time.Clock()
  11. # 显示窗口
  12. screen = pygame.display.set_mode(size)
  13. # 加载背景
  14. background = pygame.image.load('grass.jpg')
  15. screen.blit(background,(0,0))
  16. # 显示窗口标题
  17. pygame.display.set_caption('running turtle')
  18. # 加载图片
  19. turtle = pygame.image.load('turtle.png')
  20. # 获得图像的位置矩形
  21. position = turtle.get_rect()
  22. l_head = turtle
  23. r_head = pygame.transform.flip(turtle,True,False)
  24. while True:
  25.     for event in pygame.event.get():
  26.         if event.type == QUIT:
  27.             sys.exit()
  28.         if event.type == KEYDOWN:
  29.             if event.key == K_LEFT:
  30.                 turtle = l_head
  31.                 speed = [-1,0]
  32.             if event.key == K_RIGHT:
  33.                 turtle = r_head
  34.                 speed = [1,0]
  35.             if event.key == K_UP:
  36.                 speed = [0,-1]
  37.             if event.key == K_DOWN:
  38.                 speed = [0,1]
  39.     # 移动图片
  40.     position = position.move(speed)
  41.     if position.left < 0 or position.right > width:
  42.         # 翻转图像
  43.         turtle = pygame.transform.flip(turtle,True,False)
  44.         # 反方向移动
  45.         speed[0] = -speed[0]
  46.     if position.top < 0 or position.bottom > height:
  47.         speed[1] = -speed[1]
  48.     # 设置背景
  49.     #screen.fill(bg)
  50.     # 更新背景
  51.     screen.blit(background,(0,0))
  52.     # 更新图片
  53.     screen.blit(turtle,position)
  54.     pygame.display.flip()
  55.     # 延时10毫秒
  56.     #pygame.time.delay(10)
  57.     # 设置帧数
  58.     clock.tick(100)
复制代码

在附上一个飞檐走壁的龟兄代码:
  1. import pygame
  2. import pygame.transform
  3. import sys
  4. from pygame.locals import *
  5. pygame.init()
  6. size = width,height = 600,400
  7. bg = (255,255,255)
  8. clock = pygame.time.Clock()
  9. # 显示窗口
  10. screen = pygame.display.set_mode(size)
  11. # 显示窗口标题
  12. pygame.display.set_caption('running turtle')
  13. ratio = 1.0
  14. # 加载图片
  15. oturtle = pygame.image.load('turtle.png')
  16. turtle = oturtle
  17. # 获得图像的位置矩形
  18. oturtle_rect = oturtle.get_rect()
  19. position = turtle_rect = oturtle_rect
  20. speed = [5,0]
  21. turtle_right = pygame.transform.rotate(turtle,90)
  22. turtle_top = pygame.transform.rotate(turtle,180)
  23. turtle_left = pygame.transform.rotate(turtle,270)
  24. turtle_bottom = turtle
  25. turtle = turtle_top
  26. fullscreen = False
  27. l_head = turtle
  28. r_head = pygame.transform.flip(turtle,True,False)
  29. while True:
  30.     for event in pygame.event.get():
  31.         if event.type == QUIT:
  32.             sys.exit()
  33.         if event.type == KEYDOWN:
  34.             if event.key == K_LEFT:
  35.                 turtle = l_head
  36.                 speed = [-1,0]
  37.             if event.key == K_RIGHT:
  38.                 turtle = r_head
  39.                 speed = [1,0]
  40.             if event.key == K_UP:
  41.                 speed = [0,-1]
  42.             if event.key == K_DOWN:
  43.                 speed = [0,1]
  44.             # 设置全屏
  45.             if event.key == K_F11:
  46.                 fullscreen = not fullscreen
  47.                 if fullscreen:
  48.                     screen = pygame.display.set_mode((1366,768),FULLSCREEN | HWSURFACE)
  49.                     width,height = 1366,768
  50.                 else:
  51.                     screen = screen = pygame.display.set_mode(size)
  52.             if event.key == K_EQUALS or event.key == K_MINUS or event.key == K_SPACE:
  53.                 if event.key == K_EQUALS and ratio < 2:
  54.                     ratio += 0.1
  55.                 if event.key == K_MINUS and ratio > 0.5:
  56.                     ratio -= 0.1
  57.                 if event.key == K_SPACE:
  58.                     ratio = 1.0
  59.                 turtle = pygame.transform.smoothscale(oturtle,\
  60.                     (int(oturtle_rect.width * ratio),\
  61.                     int(oturtle_rect.height * ratio)))
  62.                 l_head = turtle
  63.                 r_head = pygame.transform.flip(turtle,True,False)
  64.         # 用户调整窗口尺寸
  65.         if event.type == VIDEORESIZE:
  66.             size = event.size
  67.             width,height = size
  68.             screen = screen = pygame.display.set_mode(size,RESIZABLE)
  69.     if position.right > width:
  70.         turtle = turtle_right
  71.         position = turtle_rect = turtle.get_rect()
  72.         position.left = width - turtle_rect.width
  73.         speed = [0,5]
  74.     if position.bottom > height:
  75.         turtle = turtle_bottom
  76.         position = turtle_rect = turtle.get_rect()
  77.         position.left = width - turtle_rect.width
  78.         position.top = height - turtle_rect.height
  79.         speed = [-5,0]
  80.     if position.left < 0:
  81.         turtle = turtle_left
  82.         position = turtle_rect = turtle.get_rect()
  83.         position.top = height - turtle_rect.height
  84.         speed = [0,-5]
  85.     if position.top < 0:
  86.         turtle = turtle_top
  87.         position = turtle_rect = turtle.get_rect()
  88.         speed = [5,0]
  89.     # 移动图片
  90.     position = position.move(speed)
  91.     if position.left < 0 or position.right > width:
  92.         # 翻转图像
  93.         turtle = pygame.transform.flip(turtle,True,False)
  94.         # 反方向移动
  95.         speed[0] = -speed[0]
  96.     if position.top < 0 or position.bottom > height:
  97.         speed[1] = -speed[1]
  98.     # 设置背景
  99.     screen.fill(bg)
  100.     # 更新图片
  101.     screen.blit(turtle,position)
  102.     # 更新背景
  103.     pygame.display.flip()
  104.     # 延时10毫秒
  105.     #pygame.time.delay(10)
  106.     # 设置帧数
  107.     clock.tick(60)
复制代码


哈哈,有不有趣呢?
如果你喜欢,一定别忘了:
don&#039;t forget.gif

评分

参与人数 1荣誉 +1 鱼币 +1 收起 理由
新雨花石 + 1 + 1 感谢楼主无私奉献!

查看全部评分

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-3-25 15:34:37 | 显示全部楼层
你也换鼠标光标啦
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-25 15:41:13 | 显示全部楼层
本帖最后由 wuqramy 于 2020-3-25 15:44 编辑
乘号 发表于 2020-3-25 15:34
你也换鼠标光标啦


你不试试代码吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-25 15:51:30 | 显示全部楼层
乘号 发表于 2020-3-25 15:34
你也换鼠标光标啦

其实你可以用别人的图
直接拽到桌面就行了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-25 16:19:55 | 显示全部楼层
乘号 发表于 2020-3-25 15:34
你也换鼠标光标啦

就是和小甲鱼一样的那个吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-25 17:26:28 | 显示全部楼层
这两个高级了。楼主厉害了

004.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-25 17:32:43 | 显示全部楼层
DavidCT 发表于 2020-3-25 17:26
这两个高级了。楼主厉害了

哈哈,飞檐走壁的乌龟!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-25 17:33:45 | 显示全部楼层
DavidCT 发表于 2020-3-25 17:26
这两个高级了。楼主厉害了

其实还能加声音,但是文件太大无法上传...
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-25 17:38:15 | 显示全部楼层
wuqramy 发表于 2020-3-25 17:33
其实还能加声音,但是文件太大无法上传...

直接放到github啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-25 17:48:00 | 显示全部楼层
DavidCT 发表于 2020-3-25 17:38
直接放到github啊

github是啥
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-25 17:53:47 | 显示全部楼层
石头学习来了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-25 17:54:59 | 显示全部楼层

哈哈你好花石!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-25 18:00:52 | 显示全部楼层
支持 大佬!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-25 18:01:09 | 显示全部楼层

谢谢 支持!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-25 18:48:36 | 显示全部楼层
六小鸭 发表于 2020-3-25 16:19
就是和小甲鱼一样的那个吗

嗯嗯
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-25 18:55:50 | 显示全部楼层

我也换了

评分

参与人数 1贡献 +3 收起 理由
zedi + 3 鱼C有你更精彩^_^

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-27 09:24:00 | 显示全部楼层
过来测试了, cut-turtle_1.PNG
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-18 13:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表