出于某种原因,我的 pygame window 没有打开
For some reason my pygame window isn't opening
正在开发 pygame RPG 游戏。截至目前,我正在尝试制作一个菜单系统,您可以在其中根据对话从图像过渡到图像。但是,由于我无法弄清楚的原因,我无法打开 window!抱歉,如果间距看起来很奇怪,我不确定如何在此处正确设置格式,在我的 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()
您在 game.py
中的缩进不正确,因此您的 Game
class 实例没有 game_loop()
方法 — 缩进 非常 在 Python 中很重要。 class 的所有方法都应比初始 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)
正在开发 pygame RPG 游戏。截至目前,我正在尝试制作一个菜单系统,您可以在其中根据对话从图像过渡到图像。但是,由于我无法弄清楚的原因,我无法打开 window!抱歉,如果间距看起来很奇怪,我不确定如何在此处正确设置格式,在我的 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()
您在 game.py
中的缩进不正确,因此您的 Game
class 实例没有 game_loop()
方法 — 缩进 非常 在 Python 中很重要。 class 的所有方法都应比初始 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)