超级玛丽又是一样的问题 'MainMenu' object has no attribute 'background'
1.main.py
```python
# 游戏主要入口
from source import tools
from source.states import main_menu
def main():
game = tools.Game()
state = main_menu.MainMenu()
game.run(state)
if __name__ == '__main__':
main()
2.tools.py
```python
# 工具和游戏主控
import os
import pygame
import random
class Game:
def __init__(self):
self.screen = pygame.display.get_surface()
self.clock = pygame.time.Clock()
def run(self, state):
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.display.quit()
elif event.type == pygame.KEYDOWN:
self.keys = pygame.key.get_pressed()
elif event.type == pygame.KEYUP:
self.keys = pygame.key.get_pressed()
state.update(self.screen)
pygame.display.update()
self.clock.tick(60) # 每秒钟60帧
def load_graphics(path,accept=('.jpg','.png','.bmp','.gif')):
graphics = {}
for pic in os.listdir(path):
name, ext = os.path.splitext(pic)
if ext.lower() in accept:
img = pygame.image.load(os.path.join(path, pic))
if img.get_alpha():
img = img.convert_alpha()
else:
img = img.convert()
graphics[name] = img
return graphics
def get_iamge(sheet, x, y, width, height, colorkey, scale): # colorkey为快速抠图的底色
image = pygame.Surface((width, height))
image.blit(sheet, (0, 0), (x, y, width, height)) # 0,0代表画到哪个位置,x,y,w,h 代表sheet里哪个区域要取出来
image.set_colorkey(colorkey)
image = pygame.transform.scale(image, (int(width*scale), int(height*scale)))
return image
3.main_menu.py
```python
# 主菜单文件
import pygame
from Tools.scripts.var_access_benchmark import C
from .. import tools
from .. import constants as C
from .. import setup
class MainMenu:
def __int__(self):
self.setup_background()
self.setup_player()
self.setup_cursor()
def setup_background(self):
self.background = setup.GRAPHICS['level_1']
self.background_rect = self.background.get_rect()
self.background = pygame.transform.scale(self.background, (int(self.background_rect.width * C.BG_MULTI),
int(self.background_rect.height * C.BG_MULTI)))
self.viewport = setup.SCREEN.get_rect()
def setup_player(self):
pass
def setup_cursor(self):
pass
def update(self, surface):
surface.blit(self.background, self.viewport)
错误还是那个
D:\SuperMario\Scripts\python.exe C:\Users\fbyzb\PycharmProjects\SuperMario\main.py
pygame 2.4.0 (SDL 2.26.4, Python 3.11.4)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "C:\Users\fbyzb\PycharmProjects\SuperMario\main.py", line 13, in
main()
File "C:\Users\fbyzb\PycharmProjects\SuperMario\main.py", line 8, in main
game.run(state)
File "C:\Users\fbyzb\PycharmProjects\SuperMario\source\tools.py", line 22, in run
state.update(self.screen)
File "C:\Users\fbyzb\PycharmProjects\SuperMario\source\states\main_menu.py", line 28, in update
surface.blit(self.background, self.viewport)
^^^^^^^^^^^^^^^
AttributeError: 'MainMenu' object has no attribute 'background'
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
Process finished with exit code 1
自己写的代码吗?还是哪里搞的?
看着init方法传参数self好像有点问题,你改改试试。
init的时候不传,里面调用的两个传上,试试这样OK不