我一直在youtube上关注Meloonatic Melons的Pygame教程(我改变了一些代码,但直到现在它都工作得更好),我遇到了一个问题。我已经制作了2个额外的文件来实现名为全局和 textureLoader 。错误在这里:
import pygame, sys, time, random
from scripts.textureLoader import *
from scripts.globals import *
pygame.init()
#Important Stuff
#Variables
clock = pygame.time.Clock()
FPS = 120
tileSize = 32
#Colours
white = (0, 0, 0)
black = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
orange = (255, 165, 0)
#Fonts
fpsFont = pygame.font.Font("C:\\Windows\\Fonts\\Verdana.ttf", 20)
#Display
def display():
global displayHeight, displayWidth, gameDisplay, gameCaption
displayHeight, displayWidth = 600, 800
gameDisplay = pygame.display.set_mode((displayWidth, displayHeight),
pygame.HWSURFACE | pygame.DOUBLEBUF)
gameCaption = pygame.display.set_caption("Testy RPG")
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
Globals.cameraMove = 1
elif event.key == pygame.K_DOWN:
Globals.cameraMove = 2
elif event.key == pygame.K_LEFT:
Globals.cameraMove = 3
elif event.key == pygame.K_RIGHT:
Globals.cameraMove = 4
elif event.type == pygame.KEYUP:
Globals.cameraMove = 0
#Logic
# - FPS Counter
FPSColour = black
cFPS = int(clock.get_fps())
if cFPS < 10:
FPSColour = red
elif cFPS > 10 and cFPS < 60:
FPSColour = orange
else:
FPSColour = green
# - Camera
if Globals.cameraMove == 1:
Globals.cameraY -= 1
elif Globals.cameraMove == 2:
Globals.cameraY += 1
elif Globals.cameraMove == 3:
Globals.cameraX -= 1
elif Globals.cameraMove == 4:
Globals.cameraX += 1
# - Player
#Render Graphics
gameDisplay.blit(Background.NightSky, (0, 0)) #this is where the error is
# - Render Tiles
for x in range(0, 640, tileSize):
for y in range(0, 480, tileSize):
gameDisplay.blit(Tiles.Grass, (x + Globals.cameraX, y +
Globals.cameraY))
gameDisplay.blit(fpsFont.render("FPS: " + str(cFPS), True, FPSColour),
(0, 0))
gameDisplay.blit(PlayerFrontBack.PlayerFront, (0, 0))
pygame.display.update()
clock.tick(120)
pygame.quit()
sys.exit()
这是我的textureLoader文件:
import pygame
pygame.init()
class Tiles:
global Size
Size = 32
def loadTexture(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
Grass = loadTexture("Graphics\\grass.png", Size)
Stone = loadTexture("Graphics\\stone.png", Size)
Water = loadTexture("Graphics\\water.png", Size)
class Background:
global backgroundWidth, backgroundHeight
backgroundWidth = 800
backgroundHeight = 600
def loadBackground(file, size):
backgroundImg = pygame.image.load(file)
backgroundImg = pygame.transform.scale(backgroundImg, (backgroundWidth, backgroundHeight))
backgroundSurf = pygame.Surface((backgroundWidth, backgroundHeight), pygame.HWSURFACE | pygame.SRCALPHA)
backgroundSurf.blit(backgroundImg, (0, 0))
return backgroundSurf
NightSky = loadBackground("Graphics\\sky.png", (backgroundWidth, backgroundHeight))
class PlayerFrontBack:
global playerWidth, playerHeight
playerWidth = 19
playerHeight = 55
def loadPlayerFrontBack(file, size):
playerImg = pygame.image.load(file)
playerImg = pygame.transform.scale(playerImg, (playerWidth, playerHeight))
playerSurf = pygame.Surface((playerWidth, playerHeight), pygame.HWSURFACE | pygame.SRCALPHA)
playerSurf.blit(playerImg, (0, 0))
return playerSurf
PlayerFront = loadPlayerFrontBack("Graphics\\manStill.png", (19, 55))
class PlayerLeftRight:
global playerWidth2
playerWidth2 = 15
def loadPlayerLeftRight(file, size):
playerImg = pygame.image.load(file)
playerImg = pygame.transform.scale(playerImg, (playerWidth2, playerHeight))
playerSurf = pygame.Surface((playerWidth2, playerHeight), pygame.HWSURFACE | pygame.SRCALPHA)
playerSurf.blit(playerImg, (0, 0))
return playerSurf
这是我的全局文件:
class Globals:
cameraX = 0
cameraY = 0
cameraMove = 0
追溯是:
"D:\Programming\Python\Python 3.6.2\python.exe"
D:/Programming/Python/Assets/Projects/Games/rpg.py
Traceback (most recent call last):
File "D:/Programming/Python/Assets/Projects/Games/rpg.py", line 81, in
<module>
gameDisplay.blit(Background.NightSky, (0, 0))
NameError: name 'gameDisplay' is not defined
感谢您查看我的代码,祝您找到最佳解决方案!
答案 0 :(得分:0)
您只需要调用display()
来声明和初始化函数中的全局变量。
你的代码看起来很奇怪,我会重构很多东西。我建议将其发布在codereview.stackexchange.com。