鱼C论坛

 找回密码
 立即注册
查看: 1378|回复: 2

[已解决]pygame问题

[复制链接]
发表于 2021-1-4 20:31:27 | 显示全部楼层 |阅读模式

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

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

x
  1. import pygame
  2. import sys


  3. size = width, height = 800, 600
  4. screen = pygame.display.set_mode(size)

  5. # 加载图片背景
  6. game_background = pygame.image.load(r'D:\python库\我的项目\打飞机小游戏\image\bg.png')
  7. # 绘制 surface(背景图片) 对象
  8. screen.blit(game_background, (0, 0))

  9. # 加载我方飞机素材
  10. our_aircraft = pygame.image.load(r'D:\python库\我的项目\打飞机小游戏\image\玩家对象.png')

  11. # 获得我方飞机的矩形范围
  12. our_aircraft_rect = our_aircraft.get_rect()
  13. # 初始化我方飞机位置
  14. our_aircraft_rect = our_aircraft_rect.move(400, 400)

  15. # 绘制 surface(我方飞机) 对象
  16. screen.blit(our_aircraft, our_aircraft_rect)

  17. # 画面刷新
  18. pygame.display.flip()

  19. # 用判断发生点击事件是否点击到我方飞机
  20. click_event = False

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

  25.         if event.type == pygame.MOUSEBUTTONDOWN:
  26.             if our_aircraft_rect.left < event.pos[0] < \
  27.                     our_aircraft_rect.right and our_aircraft_rect.top < event.pos[1] < our_aircraft_rect.bottom:
  28.                 click_event = True

  29.         if event.type == pygame.MOUSEBUTTONUP:
  30.             click_event = False

  31.         if event.type == pygame.MOUSEMOTION and click_event:
  32.             if click_event:
  33.                 our_aircraft_rect = our_aircraft_rect.move(event.rel[0], event.rel[1])

  34.     if our_aircraft_rect.top < 0:
  35.         our_aircraft_rect.top = 0

  36.     elif our_aircraft_rect.bottom > height:
  37.         our_aircraft_rect = height

  38.     if our_aircraft_rect.left < 0:
  39.         print(1)
  40.         our_aircraft_rect.left = 0

  41.     elif our_aircraft_rect.right > width:
  42.         our_aircraft_rect.right = width

  43.     # 重绘窗口
  44.     screen.blit(game_background, (0, 0))
  45.     screen.blit(our_aircraft, our_aircraft_rect)
  46.     # 更新窗口
  47.     pygame.display.update()
复制代码


为什么当我把图像拖动到窗口底部的时候就会报错:
f our_aircraft_rect.left < 0:
AttributeError: 'int' object has no attribute 'left'



最佳答案
2021-1-5 20:10:20
改成这样(图片路径自己改回来):

  1. import pygame
  2. import sys


  3. size = width, height = 800, 600
  4. screen = pygame.display.set_mode(size)

  5. # 加载图片背景
  6. game_background = pygame.image.load(r'E:\Python\项目\飞机大战\images\background.png')
  7. # 绘制 surface(背景图片) 对象
  8. screen.blit(game_background, (0, 0))

  9. # 加载我方飞机素材
  10. our_aircraft = pygame.image.load(r'E:\Python\项目\飞机大战\images\me1.png')

  11. # 获得我方飞机的矩形范围
  12. our_aircraft_rect = our_aircraft.get_rect()
  13. # 初始化我方飞机位置
  14. our_aircraft_rect = our_aircraft_rect.move(400, 400)

  15. # 绘制 surface(我方飞机) 对象
  16. screen.blit(our_aircraft, our_aircraft_rect)

  17. # 画面刷新
  18. pygame.display.flip()

  19. # 用判断发生点击事件是否点击到我方飞机
  20. click_event = False

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

  25.         if event.type == pygame.MOUSEBUTTONDOWN:
  26.             if our_aircraft_rect.left < event.pos[0] < \
  27.                     our_aircraft_rect.right and our_aircraft_rect.top < event.pos[1] < our_aircraft_rect.bottom:
  28.                 click_event = True

  29.         if event.type == pygame.MOUSEBUTTONUP:
  30.             click_event = False

  31.         if event.type == pygame.MOUSEMOTION and click_event:
  32.             if click_event:
  33.                 our_aircraft_rect = our_aircraft_rect.move(event.rel[0], event.rel[1])

  34.     if our_aircraft_rect.top < 0:
  35.         our_aircraft_rect.top = 0

  36.     elif our_aircraft_rect.bottom > height:
  37.         our_aircraft_rect.bottom = height

  38.     if our_aircraft_rect.left < 0:
  39.         print(1)
  40.         our_aircraft_rect.left = 0

  41.     elif our_aircraft_rect.right > width:
  42.         our_aircraft_rect.right = width

  43.     # 重绘窗口
  44.     screen.blit(game_background, (0, 0))
  45.     screen.blit(our_aircraft, our_aircraft_rect)
  46.     # 更新窗口
  47.     pygame.display.update()
复制代码
捕获.PNG
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-1-5 20:09:23 | 显示全部楼层
第 51 行你直接赋值为 1 了。。。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-5 20:10:20 | 显示全部楼层    本楼为最佳答案   
改成这样(图片路径自己改回来):

  1. import pygame
  2. import sys


  3. size = width, height = 800, 600
  4. screen = pygame.display.set_mode(size)

  5. # 加载图片背景
  6. game_background = pygame.image.load(r'E:\Python\项目\飞机大战\images\background.png')
  7. # 绘制 surface(背景图片) 对象
  8. screen.blit(game_background, (0, 0))

  9. # 加载我方飞机素材
  10. our_aircraft = pygame.image.load(r'E:\Python\项目\飞机大战\images\me1.png')

  11. # 获得我方飞机的矩形范围
  12. our_aircraft_rect = our_aircraft.get_rect()
  13. # 初始化我方飞机位置
  14. our_aircraft_rect = our_aircraft_rect.move(400, 400)

  15. # 绘制 surface(我方飞机) 对象
  16. screen.blit(our_aircraft, our_aircraft_rect)

  17. # 画面刷新
  18. pygame.display.flip()

  19. # 用判断发生点击事件是否点击到我方飞机
  20. click_event = False

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

  25.         if event.type == pygame.MOUSEBUTTONDOWN:
  26.             if our_aircraft_rect.left < event.pos[0] < \
  27.                     our_aircraft_rect.right and our_aircraft_rect.top < event.pos[1] < our_aircraft_rect.bottom:
  28.                 click_event = True

  29.         if event.type == pygame.MOUSEBUTTONUP:
  30.             click_event = False

  31.         if event.type == pygame.MOUSEMOTION and click_event:
  32.             if click_event:
  33.                 our_aircraft_rect = our_aircraft_rect.move(event.rel[0], event.rel[1])

  34.     if our_aircraft_rect.top < 0:
  35.         our_aircraft_rect.top = 0

  36.     elif our_aircraft_rect.bottom > height:
  37.         our_aircraft_rect.bottom = height

  38.     if our_aircraft_rect.left < 0:
  39.         print(1)
  40.         our_aircraft_rect.left = 0

  41.     elif our_aircraft_rect.right > width:
  42.         our_aircraft_rect.right = width

  43.     # 重绘窗口
  44.     screen.blit(game_background, (0, 0))
  45.     screen.blit(our_aircraft, our_aircraft_rect)
  46.     # 更新窗口
  47.     pygame.display.update()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-29 07:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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