如何使用精灵表格为2D平台游戏设置动画?

时间:2016-12-13 19:11:17

标签: python pygame 2d

我正在使用python中的2D平台游戏,我想让角色动画化,使用精灵表左右移动,与左,右,上,下按钮对应。

任何帮助都将不胜感激。

子画面: sprite sheet

我的代码:

#! /usr/bin/python

import pygame
from pygame import *
import time, os

WIN_WIDTH = 800
WIN_HEIGHT = 640
BLACK    = (   0,   0,   0) 
WHITE    = ( 255, 255, 255) 
BLUE     = (   0,   0, 255)
HALF_WIDTH = int(WIN_WIDTH / 2)
HALF_HEIGHT = int(WIN_HEIGHT / 2)

BRICK = pygame.image.load('brick.png').convert_alpha()

SPRITE = pygame.image.load('sprte.png').convert_alpha()

DISPLAY = (WIN_WIDTH, WIN_HEIGHT)
DEPTH = 32
FLAGS = 0
CAMERA_SLACK = 30

def main():
    global cameraX, cameraY
    pygame.init()
    screen = pygame.display.set_mode(DISPLAY, FLAGS, DEPTH)
    pygame.display.set_caption("2d Platformer")
    timer = pygame.time.Clock()

    up = down = left = right = running = False
    bg = Surface((32,32))
    bg.convert()
    bg.fill(Color("#000000"))
    entities = pygame.sprite.Group()
    player = Player(32, 58)
    platforms = []

    x = y = 0
    level = [
        "                                                                                      ",
        "                                                                                      ",
        "                                                                                      ",
        "                                                                                      ",
        "                                                                                      ",
        "                                                                                      ",
        "                                                                                      ",
        "                                                                                      ",
        "                                                                                      ",
        "                                                                                      ",
        "                                                                                      ",
        "                                                                                      ",
        "                                                                                      ",
        "                                                                                      ",
        "                                                                                      ",
        "                                                                                      ",
        "                                                                                      ",
        "                                                                                      ",
        "                                                                                      ",
        "                  PPPPPPPPPPP                                                         ",
        "                                                                                      ",
        "                                                                                      ",
        "                                                                                      ",
        "                                                                                      ",
        "PPPPPPPPEEPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP   PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP",]

    for row in level:
        for col in row:
            if col == "P":
                p = Platform(x, y)
                platforms.append(p)
                entities.add(p)
            if col == "E":
                e = Platform(x, y)
                platforms.append(e)
                entities.add(e)
            x += 32
        y += 32
        x = 0

    total_level_width  = len(level[0])*32
    total_level_height = len(level)*32
    camera = Camera(complex_camera, total_level_width, total_level_height)
    entities.add(player)





    while 1:
        timer.tick(60)

        for e in pygame.event.get():
            if e.type == QUIT: raise SystemExit, "QUIT"
            if e.type == KEYDOWN and e.key == K_ESCAPE:
                raise SystemExit, "ESCAPE"
            if e.type == KEYDOWN and e.key == K_UP:
                up = True
            if e.type == KEYDOWN and e.key == K_DOWN:
                down = True
            if e.type == KEYDOWN and e.key == K_LEFT:
                left = True
            if e.type == KEYDOWN and e.key == K_RIGHT:
                right = True
            if e.type == KEYDOWN and e.key == K_SPACE:
                running = True

            if e.type == KEYUP and e.key == K_UP:
                up = False
            if e.type == KEYUP and e.key == K_DOWN:
                down = False
            if e.type == KEYUP and e.key == K_RIGHT:
                right = False
            if e.type == KEYUP and e.key == K_LEFT:
                left = False
            if e.type == KEYUP and e.key == K_SPACE:
                running = False



        for y in range(32):
            for x in range(32):
                screen.blit(bg, (x * 32, y * 32))

        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()

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

    l = min(0, l)                           
    l = max(-(camera.width-WIN_WIDTH), l)  
    t = max(-(camera.height-WIN_HEIGHT), t) 
    t = min(0, t)                           
    return Rect(l, t, w, h)

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.xvel = -10
        self.yvel = 50
        self.onGround = False
        self.image = pygame.Surface((32, 109)).convert_alpha()
        self.image.convert()

        self.image.blit(SPRITE, SPRITE.get_rect())



        self.rect = Rect(x, y, 60, 109.5)

    def update(self, up, down, left, right, running, platforms):

        if up:
            if self.onGround: self.yvel -= 10
        if down:
            pass
        if running:
            self.xvel += 5
        if left:
            self.xvel = -8
        if right:
            self.xvel = 8





        if not self.onGround:

            self.yvel += 0.3

            if self.yvel > 100: self.yvel = 100
        if not(left or right):
            self.xvel = 0
        self.rect.left += self.xvel

        self.collide(self.xvel, 0, platforms)

        self.rect.top += self.yvel

        self.onGround = False;

        self.collide(0, self.yvel, platforms)

    def collide(self, xvel, yvel, platforms):
        for p in platforms:
            if pygame.sprite.collide_rect(self, p):
                if isinstance(p, ExitBlock):
                    pygame.event.post(pygame.event.Event(QUIT))
                if xvel > 0:
                    self.rect.right = p.rect.left
                    self.onGround = True

                if xvel < 0:
                    self.rect.left = p.rect.right
                    self.onGround = True

                    self.xvel += 2
                if yvel > 0:
                    self.rect.bottom = p.rect.top
                    self.onGround = True
                    self.yvel = 0
                if yvel < 0:
                    self.rect.top = p.rect.bottom
                    self.yvel += 10






class Platform(Entity):
    def __init__(self, x, y):
        Entity.__init__(self)
        self.image = pygame.Surface((32, 32)).convert_alpha()
        self.image.convert()
        self.image.blit(BRICK, BRICK.get_rect())
        self.rect = Rect(x, y, 32, 32)

    def update(self):
        pass

class ExitBlock(Platform):
    def __init__(self, x, y):
        Platform.__init__(self, x, y)
        self.image.fill(Color("#0033FF"))

if __name__ == "__main__":
    main()

0 个答案:

没有答案