Pygame - 设计代码
Pygame - designing the code
我为糟糕的标题道歉。我有使用 C# (XNA) 或 java (LibGDX) 编写游戏代码的经验,但我最近开始专门使用 python 和 pygame,但我遇到了麻烦弄清楚如何设计代码。这就是我认为它会做的事情:
game.py 文件:
import pygame
import entities
width = 400
height = 300
# Set up the window size
screen = pygame.display.set_mode((width, height), 0, 32)
player = entities.Player(width / 2, height / 2) # The player
#main game loop is here...
entities.py 文件:
import pygame
import game
class Player:
def __init__(self, x, y):
# Texture for the player
self.texture = pygame.image.load('player.png')
def draw(self):
""" Draws the player """
game.screen.blit(self.texture, (self.position.x, self.position.y))
#class Enemy: ...
但是这段代码给我错误。我认为问题在于我在 game.py
中导入 entities.py
,然后在 entities.py
中导入 game.py
。
我将如何在 python 中正确设计它?
您应该将变量传递到相关的 类,而不是将它们设为全局变量并导入它们。因此,一种可能的解决方案是使 screen
成为 Player:
的属性
class Player:
def __init__(self, x, y, screen):
self.screen = screen
...
def draw(self):
""" Draws the player """
self.screen.blit(...)
现在,无需在 entities.py 中导入 game
,您可以像这样实例化播放器:
player = entities.Player(width / 2, height / 2, screen)
可能还有其他方法可以做到这一点,但这应该能让您有所了解。
我建议使用 pygame 已经提供的功能。
看看 classes Sprite
and Group
(及其更复杂的子classes)。
因此您的播放器 class 可能看起来像:
class Player(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
# Texture for the player
self.image = pygame.image.load('player.png')
self.rect = self.image.get_rect(topleft=(x, y))
# no need for a draw function
# the sprite class will handle this
# just make sure you store the coordinates of
# your entity in self.rect
def update(self, some_game_state):
pass # do something with some_game_state
然后,在您的游戏中,您可以将所有实体放入一个(或多个)组中,然后在该组中调用 draw
和 update
。
entities = pygame.sprite.Group(player) # add other sprite here, too
# main loop
while True:
...
# pass the game state to all entities
# how you design this is up to you
# (pass multiple values, pass a single instace of the game class,
# create a class that encapsulates the state necesseray for the entities etc.)
entites.update(some_game_state)
# pass the main surface to the draw function
entities.draw(screen)
...
我为糟糕的标题道歉。我有使用 C# (XNA) 或 java (LibGDX) 编写游戏代码的经验,但我最近开始专门使用 python 和 pygame,但我遇到了麻烦弄清楚如何设计代码。这就是我认为它会做的事情:
game.py 文件:
import pygame
import entities
width = 400
height = 300
# Set up the window size
screen = pygame.display.set_mode((width, height), 0, 32)
player = entities.Player(width / 2, height / 2) # The player
#main game loop is here...
entities.py 文件:
import pygame
import game
class Player:
def __init__(self, x, y):
# Texture for the player
self.texture = pygame.image.load('player.png')
def draw(self):
""" Draws the player """
game.screen.blit(self.texture, (self.position.x, self.position.y))
#class Enemy: ...
但是这段代码给我错误。我认为问题在于我在 game.py
中导入 entities.py
,然后在 entities.py
中导入 game.py
。
我将如何在 python 中正确设计它?
您应该将变量传递到相关的 类,而不是将它们设为全局变量并导入它们。因此,一种可能的解决方案是使 screen
成为 Player:
class Player:
def __init__(self, x, y, screen):
self.screen = screen
...
def draw(self):
""" Draws the player """
self.screen.blit(...)
现在,无需在 entities.py 中导入 game
,您可以像这样实例化播放器:
player = entities.Player(width / 2, height / 2, screen)
可能还有其他方法可以做到这一点,但这应该能让您有所了解。
我建议使用 pygame 已经提供的功能。
看看 classes Sprite
and Group
(及其更复杂的子classes)。
因此您的播放器 class 可能看起来像:
class Player(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
# Texture for the player
self.image = pygame.image.load('player.png')
self.rect = self.image.get_rect(topleft=(x, y))
# no need for a draw function
# the sprite class will handle this
# just make sure you store the coordinates of
# your entity in self.rect
def update(self, some_game_state):
pass # do something with some_game_state
然后,在您的游戏中,您可以将所有实体放入一个(或多个)组中,然后在该组中调用 draw
和 update
。
entities = pygame.sprite.Group(player) # add other sprite here, too
# main loop
while True:
...
# pass the game state to all entities
# how you design this is up to you
# (pass multiple values, pass a single instace of the game class,
# create a class that encapsulates the state necesseray for the entities etc.)
entites.update(some_game_state)
# pass the main surface to the draw function
entities.draw(screen)
...