正在开发 pygame RPG 游戏。到目前为止,我正在尝试制作一个菜单系统,您可以根据对话在其中转换图像。但是,由于我无法弄清楚的原因,我无法打开窗户!抱歉,如果间距看起来很奇怪,我不知道如何正确设置它的格式,在我的 pycharm 上,间距肯定是正确的。这是第一个文件 (game.py
) 中的代码:
import pygame
class Game():
def __init__(self):
pygame.init()
self.running, self.playing = True, False
self.UP_KEY, self.DOWN_KEY, self.START_KEY, self.BACK_KEY = False, False, False, False
self.DISPLAY_W, self.DISPLAY_H = 800, 700
self.display = pygame.Surface((self.DISPLAY_W,self.DISPLAY_H))
self.window = pygame.display.set_mode(((self.DISPLAY_W,self.DISPLAY_H)))
self.font_name = pygame.font.get_default_font()
self.BLACK, self.WHITE = (0, 0, 0), (255, 255, 255)
def game_loop(self):
while self.playing:
self.check_events()
if self.START_KEY:
self.playing= False
self.display.fill(self.BLACK)
self.draw_text('Thanks for Playing', 20, self.DISPLAY_W/2, self.DISPLAY_H/2)
self.window.blit(self.display, (0,0))
pygame.display.update()
self.reset_keys()
def check_events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running, self.playing = False, False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
self.START_KEY = True
if event.key == pygame.K_BACKSPACE:
self.BACK_KEY = True
if event.key == pygame.K_DOWN:
self.DOWN_KEY = True
if event.key == pygame.K_UP:
self.UP_KEY = True
def reset_keys(self):
self.UP_KEY, self.DOWN_KEY, self.START_KEY, self.BACK_KEY = False, False, False, False
def draw_text(self, text, size, x, y ):
font = pygame.font.Font(self.font_name,size)
text_surface = font.render(text, True, self.WHITE)
text_rect = text_surface.get_rect()
text_rect.center = (x,y)
self.display.blit(text_surface,text_rect)
这是我在游戏一旁边的第二个文件 (main.py) 中的代码:
from game import Game
g = Game()
while g.running:
g.playing = True
g.game_loop()
答案 0 :(得分:4)
您在 game.py
中的缩进不正确,因此您的 Game
类实例没有 game_loop()
方法 - 缩进在 Python 中非常。一个类的所有方法都应该比最初的 class
语句缩进 1 级。
下面是如何正确地做到这一点。我强烈建议您阅读并开始遵循PEP 8 - Style Guide for Python Code。
import pygame
class Game():
def __init__(self):
pygame.init()
self.running, self.playing = True, False
self.UP_KEY, self.DOWN_KEY, self.START_KEY, self.BACK_KEY = False, False, False, False
self.DISPLAY_W, self.DISPLAY_H = 800, 700
self.display = pygame.Surface((self.DISPLAY_W,self.DISPLAY_H))
self.window = pygame.display.set_mode(((self.DISPLAY_W,self.DISPLAY_H)))
self.font_name = pygame.font.get_default_font()
self.BLACK, self.WHITE = (0, 0, 0), (255, 255, 255)
def game_loop(self):
while self.playing:
self.check_events()
if self.START_KEY:
self.playing= False
self.display.fill(self.BLACK)
self.draw_text('Thanks for Playing', 20, self.DISPLAY_W/2, self.DISPLAY_H/2)
self.window.blit(self.display, (0,0))
pygame.display.update()
self.reset_keys()
def check_events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running, self.playing = False, False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
self.START_KEY = True
if event.key == pygame.K_BACKSPACE:
self.BACK_KEY = True
if event.key == pygame.K_DOWN:
self.DOWN_KEY = True
if event.key == pygame.K_UP:
self.UP_KEY = True
def reset_keys(self):
self.UP_KEY, self.DOWN_KEY, self.START_KEY, self.BACK_KEY = False, False, False, False
def draw_text(self, text, size, x, y ):
font = pygame.font.Font(self.font_name,size)
text_surface = font.render(text, True, self.WHITE)
text_rect = text_surface.get_rect()
text_rect.center = (x,y)
self.display.blit(text_surface,text_rect)