鱼C论坛

 找回密码
 立即注册
查看: 2646|回复: 15

[已解决]萌新求解:一个pygame的问题

[复制链接]
发表于 2021-4-22 08:29:29 | 显示全部楼层 |阅读模式

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

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

x
  1. import pygame
  2. import sys
  3. import random
  4. #from pygame.locals import *(这个模块包含了 Pygame 定义的各种常量(常量由大写和下划线组成的))
  5. #初始化pygame
  6. pygame.init()


  7. size = width,height = 600,400
  8. speed = [-3,1]
  9. bg = (255,255,255)

  10. #创建窗口指定的大小
  11. screen = pygame.display.set_mode(size)
  12. #标题
  13. pygame.display.set_caption("案例")

  14. #加载图片
  15. picture = pygame.image.load(r"C:\Users\admin\Pictures\picture.png")

  16. #获取图像位置矩形
  17. position = picture.get_rect()

  18. fullscreen = False

  19. right_head = picture
  20. left_head = pygame.transform.flip(picture,True,False)
  21. up_head = pygame.transform.flip(picture,270,False)
  22. down_head = pygame.transform.flip(picture,False,270)
  23. while True:
  24.     for event in pygame.event.get():
  25.         if event.type == pygame.QUIT:
  26.             sys.exit()
  27.             
  28.         if event.type == pygame.KEYDOWN:
  29.             if event.key == pygame.K_RIGHT:
  30.                 picture = left_head
  31.                 speed = [5,0]
  32.             if event.key == pygame.K_LEFT:
  33.                 picture = right_head
  34.                 speed = [-5,0]
  35.             if event.key == pygame.K_UP:
  36.                 picture = up_head
  37.                 speed = [0,-5]
  38.             if event.key == pygame.K_DOWN:
  39.                 picture = down_head
  40.                 speed = [0,5]
  41.             #全屏
  42.             if event.key == pygame.K_F11:
  43.                 fullscreen = not fullscreen
  44.                 if fullscreen:
  45.                     size = width,height =1920,1080
  46.                     screen = pygame.display.set_mode((1920,1080),pygame.FULLSCREEN|pygame.HWSURFACE)
  47.                     if position.bottom > height:
  48.                         positon.bottom = height
  49.                     if position.right > width:
  50.                         position.right = width
  51.                         
  52.                           [color=Red] else:
  53.                 #full screen = pygame.display.list_modes()
  54.                     size = width,height = 600,400
  55.                     screen = pygame.display.set_mode(size)
  56.                     screen.blit(picture,(random.randint(0,width),(random.randint(0,height))))[/color]

  57.                
  58.             
  59.     #移动图像
  60.     position = position.move(speed)

  61.     if position.left < 0 or position.right > width:
  62.         #翻转图像
  63.         picture = pygame.transform.flip(picture,True,False)#水平翻转(垂直翻转为False)
  64.         speed[0] = -speed[0]

  65.     if position.top < 0 or position.bottom > height:
  66.         speed[1] = -speed[1]
  67.     #填充背景
  68.     screen.fill(bg)
  69.     #更新图像
  70.     screen.blit(picture,position)
  71.     #更新界面
  72.     pygame.display.flip()
  73.     #延时10毫秒
  74.     pygame.time.delay(10)
复制代码

else 中当重新按下F11时 怎么把图像重置在退出全屏后的原屏幕里 求大佬求大佬求大佬
最佳答案
2021-4-22 23:10:02
  1. import pygame
  2. import sys
  3. import random
  4. #from pygame.locals import *(这个模块包含了 Pygame 定义的各种常量(常量由大写和下划线组成的))
  5. #初始化pygame
  6. pygame.init()


  7. size = width,height = 600,400
  8. speed = [-3,1]
  9. bg = (255,255,255)

  10. #创建窗口指定的大小
  11. screen = pygame.display.set_mode(size,pygame.RESIZABLE)
  12. #标题
  13. pygame.display.set_caption("案例")

  14. #加载图片
  15. picture = pygame.image.load('C:/Users/ASUS/Desktop/1-14031F05541a7.png')

  16. #获取图像位置矩形
  17. position = picture.get_rect()

  18. fullscreen = False

  19. right_head = picture
  20. left_head = pygame.transform.flip(picture,True,False)
  21. up_head = pygame.transform.flip(picture,270,False)
  22. down_head = pygame.transform.flip(picture,False,270)
  23. while True:
  24.     for event in pygame.event.get():
  25.         if event.type == pygame.QUIT:
  26.             sys.exit()
  27.             
  28.         if event.type == pygame.KEYDOWN:
  29.             if event.key == pygame.K_RIGHT:
  30.                 picture = left_head
  31.                 speed = [5,0]
  32.             if event.key == pygame.K_LEFT:
  33.                 picture = right_head
  34.                 speed = [-5,0]
  35.             if event.key == pygame.K_UP:
  36.                 picture = up_head
  37.                 speed = [0,-5]
  38.             if event.key == pygame.K_DOWN:
  39.                 picture = down_head
  40.                 speed = [0,5]
  41.             #全屏
  42.             if event.key == pygame.K_F11:
  43.                 fullscreen = not fullscreen
  44.                 if fullscreen:
  45.                     size = width,height =1920,1080
  46.                     screen = pygame.display.set_mode((1920,1080),pygame.FULLSCREEN|pygame.HWSURFACE)
  47.                     if position.bottom > height:
  48.                         positon.bottom = height
  49.                     if position.right > width:
  50.                         position.right = width
  51.                     
  52.                         
  53.                 else:
  54.                     size = width,height = 600,400
  55.                     screen = pygame.display.set_mode(size)
  56.                     if picture.get_width() < width and picture.get_height() < height:
  57.                        picture = picture
  58.                     position=picture.get_rect()
  59.                      #填充背景
  60.                     screen.fill(bg)
  61.                     screen.blit(picture,position)#将一个图形画在另一个图形上(只是修改另一个图像的像素并不是真的画在上面)
  62.                     #更新界面
  63.                     pygame.display.flip()
  64.                     pygame.time.delay(10)
  65.                         
  66.                 #用户调整窗口尺寸
  67.         if event.type == pygame.VIDEORESIZE:
  68.             size = event.size
  69.             width,height = size
  70.             screen = pygame.display.set_mode(size,pygame.RESIZABLE)
  71.                         
  72.     #移动图像
  73.    
  74.     position = position.move(speed)

  75.     if position.left < 0 or position.right > width:
  76.         #翻转图像
  77.         picture = pygame.transform.flip(picture,True,False)#水平翻转(垂直翻转为False)
  78.         speed[0] = -speed[0]

  79.     if position.top < 0 or position.bottom > height:
  80.         speed[1] = -speed[1]
  81.     #填充背景
  82.     screen.fill(bg)
  83.     #更新图像
  84.     screen.blit(picture,position)
  85.     #更新界面
  86.     pygame.display.flip()
  87.     #延时10毫秒
  88.     pygame.time.delay(10)
复制代码

试试这样
记得把路径改下哦~
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-4-22 09:43:37 From FishC Mobile | 显示全部楼层
嗯 ,,在第50行加上#更新图像
    screen.blit(picture,position)试试
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-4-22 21:38:11 | 显示全部楼层
小伤口 发表于 2021-4-22 09:43
嗯 ,,在第50行加上#更新图像
    screen.blit(picture,position)试试

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

使用道具 举报

 楼主| 发表于 2021-4-22 22:22:18 | 显示全部楼层
小伤口 发表于 2021-4-22 09:43
嗯 ,,在第50行加上#更新图像
    screen.blit(picture,position)试试
  1. import pygame
  2. import sys
  3. import random
  4. #from pygame.locals import *(这个模块包含了 Pygame 定义的各种常量(常量由大写和下划线组成的))
  5. #初始化pygame
  6. pygame.init()


  7. size = width,height = 600,400
  8. speed = [-3,1]
  9. bg = (255,255,255)

  10. #创建窗口指定的大小
  11. screen = pygame.display.set_mode(size,pygame.RESIZABLE)
  12. #标题
  13. pygame.display.set_caption("案例")

  14. #加载图片
  15. picture = pygame.image.load(r"C:\Users\admin\Pictures\picture.png")

  16. #获取图像位置矩形
  17. position = picture.get_rect()

  18. fullscreen = False

  19. right_head = picture
  20. left_head = pygame.transform.flip(picture,True,False)
  21. up_head = pygame.transform.flip(picture,270,False)
  22. down_head = pygame.transform.flip(picture,False,270)
  23. while True:
  24.     for event in pygame.event.get():
  25.         if event.type == pygame.QUIT:
  26.             sys.exit()
  27.             
  28.         if event.type == pygame.KEYDOWN:
  29.             if event.key == pygame.K_RIGHT:
  30.                 picture = left_head
  31.                 speed = [5,0]
  32.             if event.key == pygame.K_LEFT:
  33.                 picture = right_head
  34.                 speed = [-5,0]
  35.             if event.key == pygame.K_UP:
  36.                 picture = up_head
  37.                 speed = [0,-5]
  38.             if event.key == pygame.K_DOWN:
  39.                 picture = down_head
  40.                 speed = [0,5]
  41.             #全屏
  42.             if event.key == pygame.K_F11:
  43.                 fullscreen = not fullscreen
  44.                 if fullscreen:
  45.                     size = width,height =1920,1080
  46.                     screen = pygame.display.set_mode((1920,1080),pygame.FULLSCREEN|pygame.HWSURFACE)
  47.                     if position.bottom > height:
  48.                         positon.bottom = height
  49.                     if position.right > width:
  50.                         position.right = width
  51.                     else:
  52.                         if position.right > width:
  53.                             picture = pygame.transform.flip(picture,True,False)
  54.                             speed[0] = -speed[0]

  55.                         if position.bottom > height:
  56.                             speed[1] = -speed[1]
  57.                         
  58.                 else:
  59.                     size = width,height = 600,400
  60.                     screen = pygame.display.set_mode(size)
  61.                     if picture.get_width() < width and picture.get_height() < height:
  62.                        picture = picture
  63.                     #如果图片的超过了原屏幕执行
  64.                     elif position not in size:
  65.                         x = random.randint(0,width)
  66.                         y = random.randint(0,height)
  67.                         screen.blit(picture,(x,y))
  68.                     
  69.                         
  70.                 #用户调整窗口尺寸
  71.         if event.type == pygame.VIDEORESIZE:
  72.             size = event.size
  73.             width,height = size
  74.             screen = pygame.display.set_mode(size,pygame.RESIZABLE)
  75.                         
  76.     #移动图像
  77.     position = position.move(speed)

  78.     if position.left < 0 or position.right > width:
  79.         #翻转图像
  80.         picture = pygame.transform.flip(picture,True,False)#水平翻转(垂直翻转为False)
  81.         speed[0] = -speed[0]

  82.     if position.top < 0 or position.bottom > height:
  83.         speed[1] = -speed[1]
  84.     #填充背景
  85.     screen.fill(bg)
  86.     #更新图像
  87.     screen.blit(picture,position)
  88.     #更新界面
  89.     pygame.display.flip()
  90.     #延时10毫秒
  91.     pygame.time.delay(10)
复制代码

不知道为什么 图片超过原屏幕那总是执行不了 else和elif、if也是
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-22 23:10:02 | 显示全部楼层    本楼为最佳答案   
  1. import pygame
  2. import sys
  3. import random
  4. #from pygame.locals import *(这个模块包含了 Pygame 定义的各种常量(常量由大写和下划线组成的))
  5. #初始化pygame
  6. pygame.init()


  7. size = width,height = 600,400
  8. speed = [-3,1]
  9. bg = (255,255,255)

  10. #创建窗口指定的大小
  11. screen = pygame.display.set_mode(size,pygame.RESIZABLE)
  12. #标题
  13. pygame.display.set_caption("案例")

  14. #加载图片
  15. picture = pygame.image.load('C:/Users/ASUS/Desktop/1-14031F05541a7.png')

  16. #获取图像位置矩形
  17. position = picture.get_rect()

  18. fullscreen = False

  19. right_head = picture
  20. left_head = pygame.transform.flip(picture,True,False)
  21. up_head = pygame.transform.flip(picture,270,False)
  22. down_head = pygame.transform.flip(picture,False,270)
  23. while True:
  24.     for event in pygame.event.get():
  25.         if event.type == pygame.QUIT:
  26.             sys.exit()
  27.             
  28.         if event.type == pygame.KEYDOWN:
  29.             if event.key == pygame.K_RIGHT:
  30.                 picture = left_head
  31.                 speed = [5,0]
  32.             if event.key == pygame.K_LEFT:
  33.                 picture = right_head
  34.                 speed = [-5,0]
  35.             if event.key == pygame.K_UP:
  36.                 picture = up_head
  37.                 speed = [0,-5]
  38.             if event.key == pygame.K_DOWN:
  39.                 picture = down_head
  40.                 speed = [0,5]
  41.             #全屏
  42.             if event.key == pygame.K_F11:
  43.                 fullscreen = not fullscreen
  44.                 if fullscreen:
  45.                     size = width,height =1920,1080
  46.                     screen = pygame.display.set_mode((1920,1080),pygame.FULLSCREEN|pygame.HWSURFACE)
  47.                     if position.bottom > height:
  48.                         positon.bottom = height
  49.                     if position.right > width:
  50.                         position.right = width
  51.                     
  52.                         
  53.                 else:
  54.                     size = width,height = 600,400
  55.                     screen = pygame.display.set_mode(size)
  56.                     if picture.get_width() < width and picture.get_height() < height:
  57.                        picture = picture
  58.                     position=picture.get_rect()
  59.                      #填充背景
  60.                     screen.fill(bg)
  61.                     screen.blit(picture,position)#将一个图形画在另一个图形上(只是修改另一个图像的像素并不是真的画在上面)
  62.                     #更新界面
  63.                     pygame.display.flip()
  64.                     pygame.time.delay(10)
  65.                         
  66.                 #用户调整窗口尺寸
  67.         if event.type == pygame.VIDEORESIZE:
  68.             size = event.size
  69.             width,height = size
  70.             screen = pygame.display.set_mode(size,pygame.RESIZABLE)
  71.                         
  72.     #移动图像
  73.    
  74.     position = position.move(speed)

  75.     if position.left < 0 or position.right > width:
  76.         #翻转图像
  77.         picture = pygame.transform.flip(picture,True,False)#水平翻转(垂直翻转为False)
  78.         speed[0] = -speed[0]

  79.     if position.top < 0 or position.bottom > height:
  80.         speed[1] = -speed[1]
  81.     #填充背景
  82.     screen.fill(bg)
  83.     #更新图像
  84.     screen.blit(picture,position)
  85.     #更新界面
  86.     pygame.display.flip()
  87.     #延时10毫秒
  88.     pygame.time.delay(10)
复制代码

试试这样
记得把路径改下哦~
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-4-24 22:37:22 | 显示全部楼层
小伤口 发表于 2021-4-22 23:10
试试这样
记得把路径改下哦~

哦哦 可以了
能再问一下嘛 在拖延窗口/屏幕大小的时候,在窗口/屏幕重叠图像时,图像的循环颤抖怎么解决呀,哇!!这个bug真的不会解决呀,这个bug时检测两者之间的碰撞,可我知道的碰撞的只有rect和精灵呀,没有surface的
蓝受
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-25 00:15:50 From FishC Mobile | 显示全部楼层
wuyanzulqq 发表于 2021-4-24 22:37
哦哦 可以了
能再问一下嘛 在拖延窗口/屏幕大小的时候,在窗口/屏幕重叠图像时,图像的循环 ...

我一般写游戏都不让用户改大小,就是为了防止这样的问题,所以俺也没解决掉这个
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-4-25 11:40:46 | 显示全部楼层
小伤口 发表于 2021-4-25 00:15
我一般写游戏都不让用户改大小,就是为了防止这样的问题,所以俺也没解决掉这个

看弹幕说可以用 “每次改变都按比例重置乌龟的位置”   不知道行不行,
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-25 18:55:28 From FishC Mobile | 显示全部楼层
wuyanzulqq 发表于 2021-4-25 11:40
看弹幕说可以用 “每次改变都按比例重置乌龟的位置”   不知道行不行,

这个可以解决鬼畜问题,但俺觉得运用到实际就不是很可行,不可能角色走了一段路 用户改一下窗口大小,角色又回到原地吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-25 23:22:49 From FishC Mobile | 显示全部楼层
wuyanzulqq 发表于 2021-4-25 22:10

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

使用道具 举报

 楼主| 发表于 2021-4-25 23:43:11 | 显示全部楼层
  1. import pygame
  2. import sys
  3. import random
  4. from pygame.locals import *#(这个模块包含了 Pygame 定义的各种常量(常量由大写和下划线组成的))
  5. #初始化pygame
  6. pygame.init()


  7. size = width,height = 600,400
  8. speed = [-3,1]
  9. bg = (255,255,255)

  10. #创建窗口指定的大小
  11. screen = pygame.display.set_mode(size,pygame.RESIZABLE)
  12. #标题
  13. pygame.display.set_caption("案例")

  14. #加载图片
  15. opicture = pygame.image.load(r"C:\Users\admin\Pictures\picture.png")
  16. picture = opicture
  17. #获取图像位置矩形
  18. opicture_rect = opicture.get_rect()
  19. position = picture_rect = opicture_rect
  20. #设置放大缩小的比率
  21. ratio = 1.0

  22. fullscreen = False

  23. right_head = picture
  24. left_head = pygame.transform.flip(picture,True,False)
  25. up_head = pygame.transform.flip(picture,True,False)
  26. down_head = pygame.transform.flip(picture,False,True)
  27. while True:
  28.     for event in pygame.event.get():
  29.         if event.type == pygame.QUIT:
  30.             sys.exit()
  31.             
  32.         if event.type == pygame.KEYDOWN:
  33.             if event.key == pygame.K_RIGHT:
  34.                 picture = left_head
  35.                 speed = [5,0]
  36.             if event.key == pygame.K_LEFT:
  37.                 picture = right_head
  38.                 speed = [-5,0]
  39.             if event.key == pygame.K_UP:
  40.                 picture = up_head
  41.                 speed = [0,-5]
  42.             if event.key == pygame.K_DOWN:
  43.                 picture = down_head
  44.                 speed = [0,5]
  45.             #全屏
  46.             if event.key == pygame.K_F11:
  47.                 fullscreen = not fullscreen
  48.                 if fullscreen:
  49.                     size = width,height =1920,1080
  50.                     screen = pygame.display.set_mode((1920,1080),pygame.FULLSCREEN|pygame.HWSURFACE)
  51.                     if position.bottom > height:
  52.                         positon.bottom = height
  53.                     if position.right > width:
  54.                         position.right = width
  55.                     else:
  56.                         if position.right > width:
  57.                             picture = pygame.transform.flip(picture,True,False)
  58.                             speed[0] = -speed[0]

  59.                         if position.bottom > height:
  60.                             speed[1] = -speed[1]
  61.                         
  62.                 else:
  63.                     size = width,height = 600,400
  64.                     screen = pygame.display.set_mode(size)
  65.                     if picture.get_width() < width and picture.get_height() < height:
  66.                        picture = picture
  67.                     position=picture.get_rect()
  68.                      #填充背景
  69.                     screen.fill(bg)
  70.                     screen.blit(picture,position)#将一个图形画在另一个图形上(只是修改另一个图像的像素并不是真的画在上面)
  71.                     #更新界面
  72.                     pygame.display.flip()
  73.                     pygame.time.delay(10)
  74.                     #放大、缩小图像(+、-),空格恢复原始尺寸
  75.             if event.key == K_EQUALS or event.key == K_MINUS or event.key == K_SPACE:
  76.                 #最大放大一倍、缩小一倍
  77.                 if event.key == K_EQUALS and ratio < 2:
  78.                     ratio += 0.1
  79.                 if event.key == K_MINUS and ratio > 0.5:
  80.                      ratio -= 0.1
  81.                 if event.key == K_SPACE:
  82.                     ratio = 1.0
  83.                     
  84.                 picture = pygame.transform.smoothscale(opicture,(int(opicture_rect.width * ratio),\
  85.                                                        int(opicture_rect.height * ratio)))
  86.                
  87.                     
  88.                         
  89.                 #用户调整窗口尺寸
  90.         if event.type == VIDEORESIZE and not fullscreen:
  91.             size = event.size
  92.             width,height = size
  93.             screen = pygame.display.set_mode(size,pygame.RESIZABLE)
  94.             rect = pygame.Rect(position)
  95.             if rect.top > height or rect.left > width:
  96.                 position=picture.get_rect()
  97.                 #填充背景
  98.                 screen.fill(bg)
  99.                 screen.blit(picture,position)
  100.                 pygame.display.flip()
  101.                 pygame.time.delay(10)
  102.             
  103.             
  104.                         
  105.     #移动图像
  106.     position = position.move(speed)

  107.     if position.left < 0 or position.right > width:
  108.         #翻转图像
  109.         picture = pygame.transform.flip(picture,True,False)#水平翻转(垂直翻转为False)
  110.         speed[0] = -speed[0]

  111.     if position.top < 0 or position.bottom > height:
  112.         speed[1] = -speed[1]
  113.     #填充背景
  114.     screen.fill(bg)
  115.     #更新图像
  116.     screen.blit(picture,position)
  117.     #更新界面
  118.     pygame.display.flip()
  119.     #延时10毫秒
  120.     pygame.time.delay(10)
复制代码

多加了放大和缩小的键,又有新bug了,在放大图像的时候:图像一边移动一边放大,放大的图像会移出右下,我知道放大的是像素,position还是原来的position,然后我用放大后的尺寸,放在blit的第一个参数里,再赋给opicture,可是为什么有循环颤抖呀(我想法错了嘛
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-4-25 23:48:55 From FishC Mobile | 显示全部楼层
小伤口 发表于 2021-4-25 23:22
?????

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

使用道具 举报

发表于 2021-4-26 09:24:40 From FishC Mobile | 显示全部楼层
wuyanzulqq 发表于 2021-4-25 23:43
多加了放大和缩小的键,又有新bug了,在放大图像的时候:图像一边移动一边放大,放大的图像 ...


对不起哈,电脑没在身边,俺也对pygame放大缩小之类的不清楚。实在不行,就先跳过吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-26 09:26:22 From FishC Mobile | 显示全部楼层
wuyanzulqq 发表于 2021-4-25 23:48
我好low啊

不low,不low,期待你的游戏哦
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-4-26 09:49:49 | 显示全部楼层
小伤口 发表于 2021-4-26 09:24
对不起哈,电脑没在身边,俺也对pygame放大缩小之类的不清楚。实在不行,就先跳过吧{:10_256 ...
  1. import pygame
  2. import sys
  3. import random
  4. from pygame.locals import *#(这个模块包含了 Pygame 定义的各种常量(常量由大写和下划线组成的))
  5. #初始化pygame
  6. pygame.init()


  7. size = width,height = 600,400
  8. speed = [-3,1]
  9. bg = (255,255,255)

  10. #创建窗口指定的大小
  11. screen = pygame.display.set_mode(size,pygame.RESIZABLE)
  12. #标题
  13. pygame.display.set_caption("案例")

  14. #加载图片
  15. opicture = pygame.image.load(r"C:\Users\admin\Pictures\picture.png")
  16. picture = opicture
  17. #获取图像位置矩形
  18. opicture_rect = opicture.get_rect()
  19. position = picture_rect = opicture_rect
  20. #设置放大缩小的比率
  21. ratio = 1.0

  22. fullscreen = False

  23. # 方向, 1右0左
  24. direction=1

  25. right_head = picture
  26. left_head = pygame.transform.flip(picture,True,False)
  27. up_head = pygame.transform.flip(picture,True,False)
  28. down_head = pygame.transform.flip(picture,False,True)
  29. while True:
  30.     for event in pygame.event.get():
  31.         #纠正放大、缩小后图片的位置
  32.         t = pygame.display.get_surface().get_rect()
  33.         limit = t.width, t.height
  34.         p = position
  35.         if p.left < 0:
  36.             p.left = 0
  37.             speed[0] *= -1
  38.             picture  = pygame.transform.flip(picture ,True,False)
  39.             direction = 0
  40.         elif p.right > limit[0]:
  41.             p.right = limit[0]
  42.             speed[0] *= -1
  43.             picture  = pygame.transform.flip(picture ,True, False)
  44.             direction = 1
  45.         elif p.top < 0:
  46.             p.top = 0
  47.             speed[1] *= -1
  48.             picture  = pygame.transform.flip(picture ,False, True)
  49.         elif p.bottom > limit[1]:
  50.             p.bottom = limit[1]
  51.             speed[1] *= -1
  52.             picture  = pygame.transform.flip(picture ,False, True)
  53.         if event.type == pygame.QUIT:
  54.             sys.exit()
  55.             
  56.         if event.type == pygame.KEYDOWN:
  57.             if event.key == pygame.K_RIGHT:
  58.                 picture = left_head
  59.                 speed = [5,0]
  60.             if event.key == pygame.K_LEFT:
  61.                 picture = right_head
  62.                 speed = [-5,0]
  63.             if event.key == pygame.K_UP:
  64.                 picture = up_head
  65.                 speed = [0,-5]
  66.             if event.key == pygame.K_DOWN:
  67.                 picture = down_head
  68.                 speed = [0,5]
  69.             #全屏
  70.             if event.key == pygame.K_F11:
  71.                 fullscreen = not fullscreen
  72.                 if fullscreen:
  73.                     size = width,height =1920,1080
  74.                     screen = pygame.display.set_mode((1920,1080),pygame.FULLSCREEN|pygame.HWSURFACE)
  75.                     if position.bottom > height:
  76.                         positon.bottom = height
  77.                     if position.right > width:
  78.                         position.right = width
  79.                     else:
  80.                         if position.right > width:
  81.                             picture = pygame.transform.flip(picture,True,False)
  82.                             speed[0] = -speed[0]

  83.                         if position.bottom > height:
  84.                             speed[1] = -speed[1]
  85.                         
  86.                 else:
  87.                     size = width,height = 600,400
  88.                     screen = pygame.display.set_mode(size)
  89.                     if picture.get_width() < width and picture.get_height() < height:
  90.                        picture = picture
  91.                     position=picture.get_rect()
  92.                      #填充背景
  93.                     screen.fill(bg)
  94.                     screen.blit(picture,position)#将一个图形画在另一个图形上(只是修改另一个图像的像素并不是真的画在上面)
  95.                     #更新界面
  96.                     pygame.display.flip()
  97.                     pygame.time.delay(10)
  98.             if event.key in (K_EQUALS,K_MINUS,K_SPACE):
  99.                 if event.key == K_EQUALS and ratio < 2:
  100.                     ratio += 0.1
  101.                 if event.key == K_MINUS and ratio > 0.5:
  102.                      ratio -= 0.1
  103.                 if event.key == K_SPACE:
  104.                     ratio = 1.0
  105.                 op = opicture_rect
  106.                 picture = pygame.transform.smoothscale(opicture,(int(op.width*ratio),int(op.height*ratio)))
  107.                 if direction == 0:
  108.                     picture = pygame.transform.flip(picture,True,False)
  109.                 p = picture.get_rect()
  110.                 position.width,position.height = p.width,p.height
  111.                     
  112.                         
  113.                 #用户调整窗口尺寸
  114.         if event.type == VIDEORESIZE and not fullscreen:
  115.             size = event.size
  116.             width,height = size
  117.             screen = pygame.display.set_mode(size,pygame.RESIZABLE)
  118.             rect = pygame.Rect(position)
  119.             if rect.top > height or rect.left > width:
  120.                 position=picture.get_rect()
  121.                 #填充背景
  122.                 screen.fill(bg)
  123.                 screen.blit(picture,position)
  124.                 pygame.display.flip()
  125.                 pygame.time.delay(10)
  126.             
  127.             
  128.                         
  129.     #移动图像
  130.     position = position.move(speed)

  131.     if position.left < 0 or position.right > width:
  132.         #翻转图像
  133.         picture = pygame.transform.flip(picture,True,False)#水平翻转(垂直翻转为False)
  134.         speed[0] = -speed[0]

  135.     if position.top < 0 or position.bottom > height:
  136.         speed[1] = -speed[1]
  137.     #填充背景
  138.     screen.fill(bg)
  139.     #更新图像
  140.     screen.blit(picture,position)
  141.     #更新界面
  142.     pygame.display.flip()
  143.     #延时10毫秒
  144.     pygame.time.delay(10)
复制代码

图像会移出界面的bug改好了,新的bug又来了
每次放大、缩小 图像一点也不  “老实”  
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-4-26 09:50:20 | 显示全部楼层
小伤口 发表于 2021-4-26 09:26
不low,不low,期待你的游戏哦

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-24 11:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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