Pygame 精灵在某个 Y 坐标处随着 layeredUpdates 消失 [英] Pygame sprite disappearing with layeredUpdates at a certain Y coordinate
问题描述
我正在用 pygame 制作这个游戏,并且有一个用 layeredUpdates 绘制的 player
精灵.玩家的 _layer
是 1,出于某种原因,当他到达 y 坐标上的某个点时,他就消失了.ground
精灵的 _layer
为 2,所以 player
精灵应该总是在最上面.奇怪的是他就在某个 y 坐标处消失了.
I am making this game with pygame, and have got a player
sprite drawn with layeredUpdates. The _layer
for the player is 1, and for some reason, when he gets to a certain point on the y coordinate, he just disappears. The ground
sprite has a _layer
of 2, so the player
sprite should always be on top. It's odd that he just disappears at a certain y coordinate.
main.py:
import pygame
from sprites import *
from settings import *
from os import path
class Game:
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
pygame.display.set_caption(TITLE)
pygame.key.set_repeat(500, 100)
self.clock = pygame.time.Clock()
self.load_data()
def load_data(self):
main_folder = path.dirname(__file__)
img_dir = path.join(main_folder, 'img')
self.map_data = []
with open(path.join(main_folder, 'map.txt'), 'rt') as f:
for line in f:
self.map_data.append(line)
self.spritesheet = Spritesheet(path.join(img_dir, SPRITESHEET))
def new(self):
self.all_sprites = pygame.sprite.LayeredUpdates()
self.blocks = pygame.sprite.Group()
self.grounds = pygame.sprite.Group()
for row, tiles in enumerate(self.map_data):
for col, tile in enumerate(tiles):
Ground(self, col, row)
if tile == 'a':
Block(self, col, row)
if tile == 'P':
self.player = Player(self, col, row)
def run(self):
self.playing = True
while self.playing:
self.events()
self.update()
self.draw()
def events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
self.player.move(0,-1)
if event.key == pygame.K_a:
self.player.move(-1,0)
if event.key == pygame.K_s:
self.player.move(0,1)
if event.key == pygame.K_d:
self.player.move(1,0)
def update(self):
self.screen.fill(BLACK)
self.all_sprites.update()
self.clock.tick(FPS)
def draw(self):
self.all_sprites.draw(self.screen)
pygame.display.update()
g = Game()
while True:
g.new()
g.run()
精灵.py
import pygame
from settings import *
class Spritesheet:
def __init__(self, filename):
self.spritesheet = pygame.image.load(filename).convert()
def get_image(self, x, y, width, height):
image = pygame.Surface((width, height))
image.blit(self.spritesheet, (0,0), (x, y, width, height))
return image
class Player(pygame.sprite.Sprite):
def __init__(self, game, x, y):
self.groups = game.all_sprites
pygame.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.image = self.game.spritesheet.get_image(0,32,32,32)
self.image.set_colorkey(BLACK)
self.rect = self.image.get_rect()
self.x = x
self.y = y
self.dir = 'UP'
self._layer = PLAYER_LAYER
def move(self, x_change, y_change):
if x_change > 0:
self.dir = 'RIGHT'
if x_change < 0:
self.dir = 'LEFT'
if y_change > 0:
self.dir = 'DOWN'
if y_change < 0:
self.dir = 'UP'
if not self.collide(x_change, y_change):
self.x += x_change
self.y += y_change
def collide(self, x_change, y_change):
for block in self.game.blocks:
if block.x == self.x + x_change and block.y == self.y + y_change and block.collidable:
return True
return False
def update(self):
self.rect.x = self.x * SCALE
self.rect.y = self.y * SCALE
class Block(pygame.sprite.Sprite):
def __init__(self, game, x, y, collidable=True, groups=None):
pygame.sprite.Sprite.__init__(self, groups or (game.all_sprites, game.blocks))
self.game = game
self.image = pygame.Surface((SCALE, SCALE))
self.rect = self.image.get_rect()
self.x = x
self.y = y
self.collidable = collidable
def update(self):
self.rect.x = self.x * SCALE
self.rect.y = self.y * SCALE
class Ground(Block):
def __init__(self, game, x, y, collidable=False, groups=None):
Block.__init__(self, game, x, y, collidable, groups or (game.all_sprites, game.grounds))
self.image = self.game.spritesheet.get_image(0, 0, 32, 32)
self._layer = GROUND_LAYER
settings.py:
settings.py:
WIN_WIDTH = 640
WIN_HEIGHT = 480
SPRITESHEET = 'sheet.png'
BLACK = (0,0,0)
WHITE = (255,255,255)
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
TITLE = 'Pykemon'
SCALE = 32
FPS = 60
PLAYER_LAYER = 1
TREE_LAYER = 2
GROUND_LAYER = 3
map.txt:
aaaaaaaaaaaaaaaaaaaa
a..................a
a..................a
a..................a
a..................a
a..................a
a..................a
a..................a
a..................a
a....P.............a
a..................a
a..................a
a..................a
a..................a
aaaaaaaaaaaaaaaaaaaa
谢谢.
推荐答案
图层必须按升序排列:
PLAYER_LAYER = 3
TREE_LAYER = 2
GROUND_LAYER = 1
当精灵在分层组中时(pygame.sprite.LayeredUpdates
) 然后可以通过 switch_layer()
:
When a sprite is in layered group (pygame.sprite.LayeredUpdates
) then the layer can be changed by switch_layer()
:
class Player(pygame.sprite.Sprite):
def __init__(self, game, x, y):
self.groups = game.all_sprites
pygame.sprite.Sprite.__init__(self, self.groups)
# [...]
game.all_sprites.change_layer(self, PLAYER_LAYER)
class Ground(Block):
def __init__(self, game, x, y, collidable=False, groups=None):
Block.__init__(self, game, x, y, collidable, groups or (game.all_sprites, game.grounds))
self.image = self.game.spritesheet.get_image(0, 0, 32, 32)
game.all_sprites.change_layer(self, GROUND_LAYER)
注意,改变 _layer
属性不会神奇地改变精灵的层._layer
属性必须在精灵添加到组之前设置.因此,您必须在 super()
调用之前设置图层属性:
Note, changing the _layer
attribute does not magically change the layer of the sprite. The _layer
attribute has to be set before the sprite is add to the group. Thus you have to set the layer attribute before the super()
calls:
class Player(pygame.sprite.Sprite):
def __init__(self, game, x, y):
self._layer = PLAYER_LAYER
self.groups = game.all_sprites
pygame.sprite.Sprite.__init__(self, self.groups)
class Ground(Block):
def __init__(self, game, x, y, collidable=False, groups=None):
self._layer = GROUND_LAYER
Block.__init__(self, game, x, y, collidable, groups or (game.all_sprites, game.grounds))
self.image = self.game.spritesheet.get_image(0, 0, 32, 32)
或者,如果将 layer
关键字参数传递给 add()
.虽然以下有效(如果 self
之前不在 game.all_sprites
中):
Alternatively a layer in a group is created, if a layer
keyword argument is passed to add()
. While the following works (if self
is not in game.all_sprites
before):
game.all_sprites.add(self, layer=PLAYER_LAYER)
这篇关于Pygame 精灵在某个 Y 坐标处随着 layeredUpdates 消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!