我正在与Pygame合作一个游戏项目。我只是刚刚开始从事该项目,所以有点卡住了。我制作了三个文件,每个文件包含执行不同功能的代码。第一个文件“ alien_apocalypse.py ”包含类“ AlienApocalypse ”,该类用于启动和监视游戏中的用户事件,并包含一些导入的模块,例如游戏设置 (这是第二个文件-' game_settings.py ')和一个类文件,其中包含游戏角色' knight.py '之一的所有属性。我正在尝试运行“ alien_apocalypse.py ”,该命令用于显示我的角色骑士和显示器的底部中间,但未显示任何内容。我在装有macOS Mojave的Mac上运行它,IDE是PyCharm。这些是文件:
import sys
import pygame
from game_settings import GameSettings
from knight import Knight
def run_game(self=None):
"""Start the main loop for the game"""
while True:
# Watch for keyboard and mouse actions
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# Redraw the screen during each pass through the loop.
self.screen_window.fill(self.settings.background_color)
self.knight.blitme()
# Make the most recently drawn screen visible.
pygame.display.flip()
class AlienApocalypse:
"""Overall class to manage game assets and behaviour"""
def __init__(self):
"""Initialize the game and create game resources"""
pygame.init()
self.settings = GameSettings()
self.screen_window = pygame.display.set_mode((2880, 1800))
pygame.display.set_caption("Alien Apocalypse")
"""Setting background color"""
self.background_color = (230, 230, 255)
self.knight = Knight(self)
@classmethod
def run_game(cls):
pass
if __name__ == "__main__":
"""Make a game instance, and run the game"""
ac = AlienApocalypse
ac.run_game()
class GameSettings:
"""This class stores all the game settings"""
def __init__(self):
"""Initialize the game's settings attributes"""
# Screen settings
self.screen_width = 2880
self.screen_height = 1800
self.background_color = (230, 230, 255)
import pygame
class Knight:
"""A class that manages the character knight"""
def __init__(self, ac_game):
"""Initialize the knight and set its starting position."""
self.screen_window = ac_game.screen_window
self.screen_window_rect = ac_game.screen_window.get_rect()
# Load the character - Knight image and get its rect.
image_file = "/Users/johnphillip/Downloads/craftpix-891165-assassin" \
"-mage-viking-free-pixel-art-game-heroes/PNG/Knight" \
"/knight.bmp "
self.image = pygame.image.load(image_file)
self.rect = self.image.get_rect()
# Start each new character at the bottom center of the screen.
self.rect.midbottom = self.screen_window_rect.midbottom
def blitme(self):
"""Draw the character at its current location."""
self.screen_window.blit(self.image, self.rect)
我不知道是我的代码还是系统故障。
答案 0 :(得分:0)
我到目前为止检测到的问题(现在显示窗口):
您必须设置适当的视频驱动程序
然后您必须初始化pygame.display
您是在类(ac.run_game())内部调用run_game()函数,而不是另一个。
类中的run_game()不执行任何操作(通过)
您必须将类中当前的run_game()替换为外部类,以便可以访问“自身”(如变量和函数)。
如果不存在,则不能将self等于None作为默认值(self是类本身及其包含的所有内容,因此,如果将其等于None,则是在“杀死”自己(该类)!!! )
您的Alien_apocalypse.py可能看起来像这样:
import pygame
from game_settings import GameSettings
from knight import Knight
class AlienApocalypse:
"""Overall class to manage game assets and behaviour"""
def __init__(self):
"""Initialize the game and create game resources"""
pygame.init()
self.settings = GameSettings()
drivers = ['windib', 'directx']
found = False
for driver in drivers:
if not os.getenv('SDL_VIDEODRIVER'):
os.putenv('SDL_VIDEODRIVER', driver)
try:
pygame.display.init()
except pygame.error:
print('Driver: {0} failed.'.format(driver))
continue
found = True
break
if not found:
raise Exception('No suitable video driver found!')
self.screen_window = pygame.display.set_mode((2880, 1800))
pygame.display.set_caption("Alien Apocalypse")
"""Setting background color"""
self.background_color = (230, 230, 255)
self.knight = Knight(self)
def run_game(self):
"""Start the main loop for the game"""
while True:
# Watch for keyboard and mouse actions
# for event in pygame.event.get():
# if event.type == pygame.QUIT:
# sys.exit()
# Redraw the screen during each pass through the loop.
self.screen_window.fill(self.settings.background_color)
self.knight.blitme()
# Make the most recently drawn screen visible.
pygame.display.flip()
if __name__ == "__main__":
"""Make a game instance, and run the game"""
ac = AlienApocalypse()
ac.run_game()