鱼C论坛

 找回密码
 立即注册
查看: 1508|回复: 1

[已解决]pygame,Rect对象问题

[复制链接]
发表于 2020-12-31 23:41:56 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 qin_yin 于 2021-1-1 00:12 编辑

请问pygame中的rect对象其中某一个属性发生改变,其他的属性是不是也会发生相应改变:
如top属性 = 10,rect对象会自动根据这个属性,对其他相关的属性作出改变, 提出这个疑惑的原因是,我发现33行到48行的内容中被注释的地方,发现注释和没注释运行起来效果一样,而且属性的值也是一样的
  1. import pygame
  2. import sys

  3. bg = (15, 180, 44)
  4. speed = [1, 1]
  5. size = width, height = 600, 400
  6. set_full_screen = False

  7. # 实例化Clock,后续将用CLock类的tick方法控制帧数
  8. fcolck = pygame.time.Clock()

  9. # 设置窗口模式
  10. screen = pygame.display.set_mode(size, pygame.RESIZABLE)
  11. # 设置窗口标题
  12. pygame.display.set_caption('Test')
  13. # 导入图片
  14. ball = pygame.image.load(r'D:\python库\PYG02.gif')
  15. # 获得图片的矩形信息
  16. ball_rect = ball.get_rect()

  17. resolving_power = pygame.display.list_modes()
  18. mouse_button_up = False

  19. while True:
  20.     for event in pygame.event.get():
  21.         if event.type == pygame.QUIT:
  22.             sys.exit()

  23.         elif event.type == pygame.MOUSEMOTION:
  24.             if mouse_button_up:
  25.                 new_position = ball_rect.move(event.rel)
  26.                 # 限制图像移动范围
  27.                 if new_position.top < 0:
  28.                     new_position.top = 0
  29.                   #  new_position.bottom = new_position.h


  30.                 elif new_position.bottom > height:
  31.                     new_position.bottom = height
  32.                     #new_position.top = height - new_position.h

  33.                 if new_position.left < 0:
  34.                     new_position.left = 0
  35.                   #  new_position.right = new_position.w

  36.                 elif new_position.right > width:
  37.                     new_position.right = width
  38.                    # new_position.left = width - new_position.w
  39.                     print(new_position.left)

  40.                 ball_rect = new_position

  41.         elif event.type == pygame.MOUSEBUTTONUP:
  42.             mouse_button_up = False

  43.         elif event.type == pygame.MOUSEBUTTONDOWN:
  44.             if ball_rect.left < event.pos[0] < ball_rect.right and ball_rect.top < event.pos[1] < ball_rect.bottom:
  45.                 mouse_button_up = True

  46.         elif event.type == pygame.VIDEORESIZE:
  47.             size = width, height = event.w, event.h

  48.         elif event.type == pygame.KEYDOWN:
  49.             if event.key == pygame.K_UP:
  50.                 speed[1] = speed[1] + 1 if speed[1] >= 0 else speed[1] - 1
  51.                 print(speed[1], '上')

  52.             elif event.key == pygame.K_DOWN:
  53.                 speed[1] = speed[1] if speed[1] == 0 else (abs(speed[1]) - 1) * int((speed[1] / abs(speed[1])))
  54.                 print(speed[1], '下')

  55.             elif event.key == pygame.K_LEFT:
  56.                 speed[0] = speed[0] if speed[0] == 0 else (abs(speed[0]) - 1) * int((speed[0] / abs(speed[0])))
  57.                 print(speed[0], '左')

  58.             elif event.key == pygame.K_RIGHT:
  59.                 speed[0] = speed[0] + 1 if speed[0] > 0 else speed[0] - 1
  60.                 print(speed[0], '右')

  61.             elif event.key == pygame.K_ESCAPE:
  62.                 sys.exit()

  63.             elif event.key == pygame.K_F11:
  64.                 set_full_screen = not set_full_screen

  65.                 if set_full_screen:
  66.                     side = width, height = 1920, 870
  67.                     pygame.display.set_mode(side, pygame.FULLSCREEN)

  68.                 else:
  69.                     side = width, height = 600, 400
  70.                     pygame.display.set_mode(side)

  71.     if ball_rect.left < 0 or ball_rect.right > width:
  72.         speed[0] = -speed[0]


  73.     if ball_rect.top < 0 or ball_rect.bottom > height:
  74.         speed[1] = -speed[1]

  75.     # 判断窗口是否被显示界面(屏幕绘制/非图标化,最小化)
  76. #    if pygame.display.get_active():
  77.     #    ball_rect = ball_rect.move(speed)


  78.     screen.fill(bg)
  79.     screen.blit(ball, ball_rect)
  80.     pygame.display.update()
  81.     fcolck.tick(30)
复制代码
最佳答案
2021-1-1 08:21:26
其他属性是会变化的 你可以把它理解成一个窗口 ,width和height一般是不变的 所以当你任意改 top bottom, left right中一个另外对应的一个属性也会改变
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-1-1 08:21:26 | 显示全部楼层    本楼为最佳答案   
其他属性是会变化的 你可以把它理解成一个窗口 ,width和height一般是不变的 所以当你任意改 top bottom, left right中一个另外对应的一个属性也会改变
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-29 13:45

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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