Python 3.x 我的类没有定义。Python Pygame
我刚在python pygame中开始一个类似轮滑的游戏,突然遇到了一个我不理解的错误。 代码如下: Main.py 纹理.py 这就是我所有的代码,文件如下Python 3.x 我的类没有定义。Python Pygame,python-3.x,class,pygame,Python 3.x,Class,Pygame,我刚在python pygame中开始一个类似轮滑的游戏,突然遇到了一个我不理解的错误。 代码如下: Main.py 纹理.py 这就是我所有的代码,文件如下 Graphics wood.jpg main.py textures.py 所以这个错误很奇怪。当我运行main.py文件时,错误相当奇怪。通过变量Tiles.Size和Tiles.Wood,它说textures.py文件中的Tiles类没有定义,因为它非常清楚地存在!我被这个小故障困扰了一段时间,然后放弃了。然后我又看了一遍,
Graphics
wood.jpg
main.py
textures.py
所以这个错误很奇怪。当我运行main.py文件时,错误相当奇怪。通过变量Tiles.Size和Tiles.Wood,它说textures.py文件中的Tiles类没有定义,因为它非常清楚地存在!我被这个小故障困扰了一段时间,然后放弃了。然后我又看了一遍,仍然找不到错误。错误是:
Traceback (most recent call last):
File "C:\Users\*********\Desktop\PygameRPG\Main.py", line 26, in <module>
for x in range(0, 640, Tiles.Size):
NameError: name 'Tiles' is not defined
那是我的错误
帮点忙就好了 您在main.py中导入了Textures.py,但您忘记了什么!如果要执行此操作,请执行以下操作: 对于范围为0640的x,平铺。尺寸: 您不应该这样做:
from textures import *
也不要将代码更改为:
对于范围为0、640的x,textures.Tiles.Size:
你还忘了括号。现在,尝试以下代码,因为它必须工作:
main.py:
import pygame
import textures
pygame.init()
Tiles_Size = 32
BLACK = (0, 0, 0)
def create_window():
global window
window = pygame.display.set_mode((800,600),pygame.HWSURFACE|pygame.DOUBLEBUF)
pygame.display.set_caption('RPG')
create_window()
isRunning = True
while isRunning:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a: #Switch to K_A rather than pygame.K_a
isRunning = False # for glitch! :)
window.fill(BLACK)
for x in range(0, 640, textures.TilesClass().Size):
for y in range(0, 480, textures.TilesClass().Size):
window.blit(textures.TilesClass().Wood, (x, y))
textures.py:
import pygame
pygame.init()
class TilesClass:
Size = 32
def Load_Texture(file, Size):
bitmap = pygame.image.load(file)
bitmap = pygame.transform.scale(bitmap, (Size, Size))
surface = pygame.Surface((Size, Size), pygame.HWSURFACE|pygame.SRCALPHA)
surface.blit(bitmap, (0, 0))
return surface
Wood = Load_Texture("Graphics\\wood.jpg", Size)
Tiles是在textures.py中定义的,所以当你导入纹理时,你应该调用textures.Tiles.SizeI,我已经试过了!忘了打字了,我要编辑了!请尝试使用Textures.Tiles.Size当我将其从“导入纹理”切换到“从纹理导入*”时,我没有收到错误,但木材网格也没有出现。当我做Textures.Tiles.Size或Textures.Tiles.Wood时,它说的是NameError:名称“Textures”未定义您能将名称“Textures”更改为更独特的名称吗?比如“mytexturesbutgood”,然后再试一次?嗯,刚试过这个,同样的事情发生了!:好的,你确定导入textures.py时没有大写字母“T”对吗?是的,挑衅!
import pygame
import textures
pygame.init()
Tiles_Size = 32
BLACK = (0, 0, 0)
def create_window():
global window
window = pygame.display.set_mode((800,600),pygame.HWSURFACE|pygame.DOUBLEBUF)
pygame.display.set_caption('RPG')
create_window()
isRunning = True
while isRunning:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a: #Switch to K_A rather than pygame.K_a
isRunning = False # for glitch! :)
window.fill(BLACK)
for x in range(0, 640, textures.TilesClass().Size):
for y in range(0, 480, textures.TilesClass().Size):
window.blit(textures.TilesClass().Wood, (x, y))
import pygame
pygame.init()
class TilesClass:
Size = 32
def Load_Texture(file, Size):
bitmap = pygame.image.load(file)
bitmap = pygame.transform.scale(bitmap, (Size, Size))
surface = pygame.Surface((Size, Size), pygame.HWSURFACE|pygame.SRCALPHA)
surface.blit(bitmap, (0, 0))
return surface
Wood = Load_Texture("Graphics\\wood.jpg", Size)