【问题标题】:My class isn't defined. Python Pygame我的班级没有定义。 Python Pygame
【发布时间】:2018-07-06 17:33:26
【问题描述】:

当我遇到一个我不理解的错误时,我刚刚在 python pygame 中开始了一个 rougelike 游戏。 代码如下:

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, Tiles.Size):
        for y in range(0, 480, Tiles.Size):
            window.blit(Tiles.Wood, (x, y))


pygame.quit()

Textures.py

import pygame

pygame.init()

class Tiles:

    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)

这就是我拥有的所有代码,文件如下

Graphics
    wood.jpg
main.py
textures.py

所以这个错误很奇怪。当我运行main.py 文件时,错误很奇怪。使用变量Tiles.SizeTiles.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

那是我的错误。

一些帮助会很棒!

【问题讨论】:

  • Tiles是在textures.py中定义的,所以当你导入纹理时,你应该调用Textures.Tiles.Size
  • 我已经尝试过了!
  • 忘记输入了,我要编辑!
  • 请尝试Textures.Tiles.Size

标签: python-3.x class pygame


【解决方案1】:

您在 main.py 中导入了 Textures.py,但您忘记了一些东西!如果你想这样做:

for x in range(0, 640, Tiles().Size):

你不应该这样做:

from textures import *

也不把代码改成这样:

for x in range(0, 640, 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)

【讨论】:

  • 当我将它从import Textures 切换到from Textures import * 时,我没有收到错误,但木格也没有出现。当我做Textures.Tiles.SizeTextures.Tiles.Wood 它说NameError: name 'Textures' is not defined
  • 你能把'Textures'这个名字改成更独特的名字吗?喜欢“mytexturesbutgood”然后再试一次?
  • 嗯,刚试过这个,同样的事情发生了! :(
  • 好的,你确定导入的 textures.py 没有大写的 'T' 对吗?
  • 是的,绝对!
猜你喜欢
  • 2013-04-25
  • 1970-01-01
  • 2014-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-12
  • 1970-01-01
  • 2012-10-19
相关资源
最近更新 更多