Python PYGAME-如何读入外部类
好的,我尝试在迷宫中移动玩家,要做到这一点,系统必须读取外部文件,我在主类中创建了一个函数。 但我无法将此功能与移动它的播放器链接。 我在这里什么也没找到,因为我看到的所有主题都只使用了一个文件 这是主文件Python PYGAME-如何读入外部类,python,pygame,Python,Pygame,好的,我尝试在迷宫中移动玩家,要做到这一点,系统必须读取外部文件,我在主类中创建了一个函数。 但我无法将此功能与移动它的播放器链接。 我在这里什么也没找到,因为我看到的所有主题都只使用了一个文件 这是主文件 import pygame as pg from os import system, environ from design import maze, constants as cst from objects import player system('clear') environ['
import pygame as pg
from os import system, environ
from design import maze, constants as cst
from objects import player
system('clear')
environ['SDL_VIDEO_CENTERED'] = '1'
pg.init()
def main():
screen = maze.GameBoard()
ui = screen.lab_struct()
screen.draw_objects()
hero = player.McGyver(screen)
pg.display.flip()
while True:
ev = pg.event.wait()
key_pressed = pg.key.get_pressed()
if ev.type == pg.QUIT or key_pressed[pg.K_ESCAPE]:
break
elif ev.type == pg.KEYDOWN:
hero.del_mac()
hero.update_mac(ev.key, ui)
hero.show_mac()
pg.display.flip()
这是迷宫文件
import pygame as pg
from random import sample
from design import constants as cst
class GameBoard(list):
def __init__(self):
self.master = pg.display.set_mode((cst.WINSIZE, cst.WINSIZE + 55))
self.title = pg.display.set_caption(cst.GAME_TITLE)
self.icon = pg.image.load(cst.MACGYVER_PIC)
pg.display.set_icon(self.icon)
self.wall = pg.image.load(cst.FULLWALL_PIC).convert_alpha()
self.guard = pg.image.load(cst.GUARDIAN_PIC).convert_alpha()
self.bkg = pg.image.load(cst.BKG_PIC).convert_alpha()
self.itempic = (pg.image.load(cst.NEEDLE_PIC).convert_alpha(),
pg.image.load(cst.PIPE_PIC).convert_alpha(),
pg.image.load(cst.ETHER_PIC).convert_alpha())
pg.key.set_repeat(200, 200)
# ------------------------------------------------------------------------
def lab_struct(self):
with open('design/labyrinth') as maze:
maze = ''.join(maze.read().splitlines())
self.gdpos = divmod(maze.find('G'), 15)
#self.extend([self.gdpos])
self.extend([divmod(idx, 15) for idx, value in enumerate(maze)
if value == '0'])
self.itempos = sample(self[1:], 3)
def draw_objects(self):
self.master.blit(self.wall, (0, 0))
gdy, gdx = self.gdpos
self.master.blit(self.guard, (gdx * 50, gdy * 50))
for y, x in self:
self.master.blit(self.bkg, (x * 50, y * 50),
(x * 50, y * 50, 50, 50))
for it, (y, x) in zip(self.itempic, self.itempos):
self.master.blit(it, (x * 50, y * 50))
这是播放器文件
import pygame as pg
from design import constants as cst
class McGyver(object):
def __init__(self, screen):
self.bkg = pg.image.load(cst.BKG_PIC).convert_alpha()
self.macpic = pg.image.load(cst.MACGYVER_PIC).convert_alpha()
self.macpos = (0, 0)
self.screen = screen
self.screen.master.blit(self.macpic, self.macpos)
self.arrows = {pg.K_UP: (-1, 0),
pg.K_DOWN: (1, 0),
pg.K_LEFT: (0, -1),
pg.K_RIGHT: (0, 1)}
# -------------------------------------------------------------------------
def del_mac(self):
y, x = self.macpos
self.screen.master.blit(self.bkg, (x * 50, y * 50),
(x * 50, y * 50, 50, 50))
# -------------------------------------------------------------------------
def update_mac(self, key, maze):
y, x = self.macpos
self.maze = maze
offy, offx = self.arrows.get(key, (0, 0))
if (y + offy, x + offx) in self.maze:
self.macpos = (y + offy, x + offx)
# -------------------------------------------------------------------------
def show_mac(self):
y, x = self.macpos
self.screen.master.blit(self.macpic, (x * 50, y * 50))
我想将[lab_struct]中的[maze]用于[mac_update]函数,但我有此错误
Traceback (most recent call last):
File "main.py", line 4, in <module>
main()
File "/home/user/game.py", line 44, in main
hero.update_mac(ev.key, ui)
File "/home/user/objects/player.py", line 36, in update_mac
if (y + offy, x + offx) in self.maze:
TypeError: argument of type 'NoneType' is not iterable
回溯(最近一次呼叫最后一次):
文件“main.py”,第4行,在
main()
文件“/home/user/game.py”,第44行,主
英雄。更新_mac(ev.key,ui)
文件“/home/user/objects/player.py”,第36行,在update_mac中
如果在self.maze中(y+offy,x+offx):
TypeError:类型为“NoneType”的参数不可编辑
如果它能帮助任何人,我在多次测试后找到了解决方案。。感谢@Rabbid76。。对于反射的方式,@furas对于调试的方式:)
def lab_struct(self):
必须返回self
而不是返回maze
,因为maze
是一个带有字符串的文件,我想要一个元组作为位置。。我不想像我说的那样读取迷宫文件(我的错误),但我想使用完整的函数
非常感谢显然,您的
ui
变量是None
。你能把它的值打印出来看看是不是这样吗def lab_struct(self):
必须返回maze
,因为您接受maze作为参数(update_mac(self,key,maze)
),这可能是`。。。在迷宫,而不是在self.maze`?正确,没有。@rabbi76:好的,我现在就做