鱼C论坛

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

[已解决]pygame模块问题

[复制链接]
发表于 2024-7-11 17:31:36 | 显示全部楼层 |阅读模式
20鱼币
我在测试时调用了一个模块里的函数,可是报错说没有这个“属性”,能帮我看看为什么吗?
test.py
  1. import pygame
  2. import sys
  3. from pygame.locals import *
  4. import button
  5. #button1 = pygame.image.load("NewGame.png")
  6. #button2 = pygame.image.load("LoadGame.png")
  7. #button3 = pygame.image.load("help.png")
  8. #button4 = pygame.image.load("ExitGame.png")

  9. #初始化
  10. RED = (255, 0, 0)
  11. pygame.init()
  12. size = width, height = pygame.display.list_modes()[0][0], pygame.display.list_modes()[0][1]
  13. button.init("NewGame.png", "LoadGame.png", "help.png", "ExitGame.png", p=((size[0] / 2, size[1] /8), \
  14.                                                                           (size[0] / 2, size[1] /4), (size[0] / 2, size[1] /2.6),\
  15.                                                                           (size[0] / 2, size[1] /1.9)))
  16. clock = pygame.time.Clock()
  17. pygame.display.set_caption("test")

  18. #创造窗口
  19. screen = pygame.display.set_mode(size, FULLSCREEN | HWSURFACE)
  20. b1_qosition = (size[0] / 2, size[1] /8)
  21. b2_qosition = (size[0] / 2, size[1] /4)
  22. b3_qosition = (size[0] / 2, size[1] /2.6)
  23. b4_qosition = (size[0] / 2, size[1] /1.9)
  24. while True:
  25.     for event in pygame.event.get():
  26.         if event.type == QUIT:
  27.             sys.exit()
  28.             
  29.     #绘制图片
  30.     #screen.blit(button1, b1_qosition)
  31.     #screen.blit(button2, b2_qosition)
  32.     #screen.blit(button3, b3_qosition)
  33.     #screen.blit(button4, b4_qosition)
  34.     button.draw(screen)
  35.     pygame.draw.rect(screen, RED, (size[0] / 2, size[1] /8, 375, 114), 10)
  36.     pygame.display.flip()
  37.     clock.tick(200)
复制代码


botton.py
  1. import pygame
  2. #按钮图片及位置设置
  3. #button1 = pygame.image.load("NewGame.png")
  4. #button2 = pygame.image.load("LoadGame.png")
  5. #button3 = pygame.image.load("help.png")
  6. #button4 = pygame.image.load("ExitGame.png")
  7. #b1_qosition = (size[0] / 2, size[1] /8)
  8. #b2_qosition = (size[0] / 2, size[1] /4)
  9. #b3_qosition = (size[0] / 2, size[1] /2.6)
  10. #b4_qosition = (size[0] / 2, size[1] /1.9)
  11. button = []
  12. Button = {}
  13. def init(*image, p):
  14.     for i in image:
  15.         b = pygame.image.load(i)
  16.         button.append(b)
  17.         Button[button[image.index(i)]] = p[image.index(i)]

  18. def draw(screen):
  19.     for i in Button:
  20.         screen.blit(i.key, i.values)
复制代码
c   gv
最佳答案
2024-7-11 17:31:37
哈哈,看到你的代码,我想为你理清一下思路,顺便开个玩笑:就像在厨房做饭,发现盐罐子其实是糖罐子,结果炒出来的菜味道有点迷惑。这就是代码没有按照我们预想的方式运行时的感觉吧?

首先,我们来看看你提到的报错问题:“属性”错误(AttributeError)。这个错误通常是因为你在访问一个对象的属性或方法时,这个属性或方法并不存在。

这里有几个地方需要注意和修正:

### 1. `button`模块初始化
在你的 `button.py` 文件中,有一些语法和逻辑上的问题。

  1. # botton.py

  2. import pygame

  3. button = []  # 声明 button 为一个列表
  4. Button = {}

  5. def init(*image, p):
  6.     for i in image:
  7.         b = pygame.image.load(i)
  8.         button.append(b)  # 添加到列表中
  9.     # 位置也要配对起来存储
  10.     for img, pos in zip(button, p):
  11.         Button[img] = pos

  12. def draw(screen):
  13.     for img, pos in Button.items():
  14.         screen.blit(img, pos)
复制代码


### 2. 在 `test.py` 中调用 `button` 模块

确保在 `test.py` 中正确导入使用了 `button` 模块。

  1. # test.py

  2. import pygame
  3. import sys
  4. from pygame.locals import *
  5. import button  # 确保这个导入没问题

  6. # 初始化
  7. RED = (255, 0, 0)
  8. pygame.init()

  9. size = width, height = pygame.display.list_modes()[0]
  10. button.init("NewGame.png", "LoadGame.png", "help.png", "ExitGame.png", p=[(width // 2, height // 8),
  11.                                                                         (width // 2, height // 4),
  12.                                                                         (width // 2, height // 2.6),
  13.                                                                         (width // 2, height // 1.9)])
  14. clock = pygame.time.Clock()
  15. pygame.display.set_caption("test")

  16. # 创造窗口
  17. screen = pygame.display.set_mode(size, FULLSCREEN | HWSURFACE)

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

  22.     # 绘制图片
  23.     button.draw(screen)
  24.     pygame.draw.rect(screen, RED, (width // 2 - 187, height // 8 - 57, 375, 114), 10)
  25.     pygame.display.flip()
  26.     clock.tick(200)
复制代码


### 补充说明
1. **路径问题**:确保你的图片路径正确,如果图片不在当前目录,可能会导致加载失败。
2. **除法问题**:Python 的 `/` 操作符默认返回浮点数,使用 `//` 可以保证返回整数,这对于屏幕坐标很重要。

这样修改后,应该能够解决你提到的问题。如果还有其他疑问,欢迎随时提问!祝你编程愉快,就像吃到自己做的美味菜肴一样开心!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。

最佳答案

查看完整内容

哈哈,看到你的代码,我想为你理清一下思路,顺便开个玩笑:就像在厨房做饭,发现盐罐子其实是糖罐子,结果炒出来的菜味道有点迷惑。这就是代码没有按照我们预想的方式运行时的感觉吧? 首先,我们来看看你提到的报错问题:“属性”错误(AttributeError)。这个错误通常是因为你在访问一个对象的属性或方法时,这个属性或方法并不存在。 这里有几个地方需要注意和修正: ### 1. `button`模块初始化 在你的 `button.py` ...

本帖被以下淘专辑推荐:

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

使用道具 举报

发表于 2024-7-11 17:31:37 | 显示全部楼层    本楼为最佳答案   
哈哈,看到你的代码,我想为你理清一下思路,顺便开个玩笑:就像在厨房做饭,发现盐罐子其实是糖罐子,结果炒出来的菜味道有点迷惑。这就是代码没有按照我们预想的方式运行时的感觉吧?

首先,我们来看看你提到的报错问题:“属性”错误(AttributeError)。这个错误通常是因为你在访问一个对象的属性或方法时,这个属性或方法并不存在。

这里有几个地方需要注意和修正:

### 1. `button`模块初始化
在你的 `button.py` 文件中,有一些语法和逻辑上的问题。

  1. # botton.py

  2. import pygame

  3. button = []  # 声明 button 为一个列表
  4. Button = {}

  5. def init(*image, p):
  6.     for i in image:
  7.         b = pygame.image.load(i)
  8.         button.append(b)  # 添加到列表中
  9.     # 位置也要配对起来存储
  10.     for img, pos in zip(button, p):
  11.         Button[img] = pos

  12. def draw(screen):
  13.     for img, pos in Button.items():
  14.         screen.blit(img, pos)
复制代码


### 2. 在 `test.py` 中调用 `button` 模块

确保在 `test.py` 中正确导入使用了 `button` 模块。

  1. # test.py

  2. import pygame
  3. import sys
  4. from pygame.locals import *
  5. import button  # 确保这个导入没问题

  6. # 初始化
  7. RED = (255, 0, 0)
  8. pygame.init()

  9. size = width, height = pygame.display.list_modes()[0]
  10. button.init("NewGame.png", "LoadGame.png", "help.png", "ExitGame.png", p=[(width // 2, height // 8),
  11.                                                                         (width // 2, height // 4),
  12.                                                                         (width // 2, height // 2.6),
  13.                                                                         (width // 2, height // 1.9)])
  14. clock = pygame.time.Clock()
  15. pygame.display.set_caption("test")

  16. # 创造窗口
  17. screen = pygame.display.set_mode(size, FULLSCREEN | HWSURFACE)

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

  22.     # 绘制图片
  23.     button.draw(screen)
  24.     pygame.draw.rect(screen, RED, (width // 2 - 187, height // 8 - 57, 375, 114), 10)
  25.     pygame.display.flip()
  26.     clock.tick(200)
复制代码


### 补充说明
1. **路径问题**:确保你的图片路径正确,如果图片不在当前目录,可能会导致加载失败。
2. **除法问题**:Python 的 `/` 操作符默认返回浮点数,使用 `//` 可以保证返回整数,这对于屏幕坐标很重要。

这样修改后,应该能够解决你提到的问题。如果还有其他疑问,欢迎随时提问!祝你编程愉快,就像吃到自己做的美味菜肴一样开心!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-7-11 17:36:43 | 显示全部楼层
说的再详细点
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-21 14:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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