My player sprite fails to blit. However, when I uncomment the Rectangle code and comment the sprite code, it blits perfectly fine.
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.
Whenever I uncomment the Rect code and comment the load_image() code the controls function perfectly.
Here is an image of the screen with load_image().
Here is an image of the screen without load_image().
I have tried directly bilting the image instead of using the for e in entities
loop with specified coordinates. 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. Whenever I run screen.blit(player.image, camera.apply(player))
it does not show up at all.
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):
r = target.rect.move(self.state.topleft)
print r
the y co-ordinate is at -25, so it is blitting the image off-screen. I am not sure why this is, but this is the problem.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.