[英]Python/Pygame - Sprite fails to blit
My player sprite fails to blit. 我的播放器精灵无法启动。 However, when I uncomment the Rectangle code and comment the sprite code, it blits perfectly fine. 但是,当我取消对Rectangle代码的注释并注释Sprite代码时,它会完全消失。
I do not receive an error message when loading the image, the sprite simply does not show up. 加载图像时我没有收到错误消息,精灵根本不显示。 The controls also act a bit wonky, with it seeming to "float" in the air unless it collides with a wall and jumping not functioning right. 控件也表现得有些呆板,除非它与墙壁碰撞并且跳跃无法正常工作,否则它似乎在空中“漂浮”。 I know that it can find the image because a direct blit
works. 我知道它可以找到图像,因为直接blit
可以工作。
Whenever I uncomment the Rect code and comment the load_image() code the controls function perfectly. 每当我取消对Rect代码的注释并对load_image()代码进行注释时,控件就可以正常运行。
Here is an image of the screen with load_image(). 这是带有load_image()的屏幕图像。
Here is an image of the screen without load_image(). 这是没有load_image()的屏幕图像。
I have tried directly bilting the image instead of using the for e in entities
loop with specified coordinates. 我已经尝试过直接对图片进行帐单处理,而不是for e in entities
具有指定坐标的for e in entities
循环中使用for e in entities
。 It worked, however as specific x
and y
values were used, the image obviously stayed in the same place, therefore staying at the coordinates on the screen instead of displaying the player's movements. 它起作用了,但是当使用特定的x
和y
值时,图像显然停留在同一位置,因此停留在屏幕上的坐标上,而不显示玩家的动作。 Whenever I run screen.blit(player.image, camera.apply(player))
it does not show up at all. 每当我运行screen.blit(player.image, camera.apply(player))
它根本不会显示。
I have omitted certain things in the code (level array, other classes, etc) but I believe that the necessities are here. 我已经省略了代码中的某些内容(级别数组,其他类等),但我相信这里是必须的。 If this has a very simple fix, well, I'll feel pretty dumb :/ 如果这个解决方法非常简单,那么,我会觉得很愚蠢:/
class Entity(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
class Player(Entity):
def __init__(self, x, y):
Entity.__init__(self)
self.xlevel = 0
self.ylevel = 0
self.onGround = False
# self.image = Surface((32, 32))
# self.image.fill(Color("#000000"))
# self.image.convert()
# self.rect = Rect(x, y, 32, 32)
self.image, self.rect = load_image("zur.png", colorkey=((255, 255, 255)))
class Camera(object):
def __init__(self, camera_func, width, height):
self.camera_func = camera_func
self.state = Rect(0, 0, width, height)
def apply(self, target):
return target.rect.move(self.state.topleft)
def update(self, target):
self.state = self.camera_func(self.state, target.rect)
def simple_camera(camera, target_rect):
l, t, _, _ = target_rect
_, _, w, h = camera
return Rect(-l + HALF_WIDTH, -t + HALF_HEIGHT, w, h)
def complex_camera(camera, target_rect):
l, t, _, _ = target_rect
_, _, w, h = camera
l, t, _, _ = -l+HALF_WIDTH, -t+HALF_HEIGHT, w, h # center player
l = min(0, l) # stop scrolling at the left edge
l = max(-(camera.width-WIN_WIDTH), l) # stop scrolling at the right edge
t = max(-(camera.height-WIN_HEIGHT), t) # stop scrolling at the bottom
t = min(0, t) # stop scrolling at the top
return Rect(l, t, w, h)
def load_image(name, colorkey=None):
fullname = os.path.join('assets', name)
try:
image = pygame.image.load(fullname)
except pygame.error, message:
print 'Cannot load image ' + name + ". Exiting."
raise SystemExit, message
image = image.convert()
if colorkey is not None:
if colorkey is -1:
colorkey = image.get_at((0, 0))
image.set_colorkey(colorkey, RLEACCEL)
return image, image.get_rect()
def main():
global cameraX, cameraY
global mouseX, mouseY
global bulletExists
mouseX = 0
mouseY = 0
bulletExists = False
pygame.init()
screen = pygame.display.set_mode(DISPLAY, FLAGS, DEPTH)
pygame.display.set_caption("Tale of Zur")
clock = pygame.time.Clock()
up = down = left = right = running = False
x = y = 0
background_images = []
platforms = []
background_images.append(pygame.image.load("assets/starting_background_1.jpg").convert())
entities = pygame.sprite.Group()
player = Player(32, 32)
bulletx = int(player.xlevel)
bullety = int(player.ylevel)
bullet = Bullet(player.rect.left, player.rect.top)
bullet.update(player.rect.left, player.rect.top)
screen.blit(background_images[0], [0, 0])
if bulletExists is True:
for i in range(10):
if up and i is not 10:
screen.blit(bullet.image, [bullet.bulletxpos + i, player.rect.top])
else:
screen.blit(bullet.image, [bullet.bulletxpos - i, player.rect.top])
camera.update(player)
player.update(up, down, left, right, running, platforms)
for e in entities:
screen.blit(e.image, camera.apply(e))
pygame.display.update()
If you do this (in the Camera.apply() method): 如果执行此操作(在Camera.apply()方法中):
r = target.rect.move(self.state.topleft)
print r
the y co-ordinate is at -25, so it is blitting the image off-screen. y坐标为-25,因此会在屏幕外闪烁图像。 I am not sure why this is, but this is the problem. 我不确定为什么会这样,但这就是问题所在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.