为什么我的玩家精灵会掉进平台?

时间:2017-02-16 01:51:04

标签: python-3.x pygame tiled

它可以在各个方面正确碰撞,当我从地面跳到平台上时,玩家将落在它的顶部并正确碰撞。然而,当我站在平台上并且我跳起并试图再次降落在平台上时,我直接穿过。为什么会这样? 此外,如果我按住空格键,那么我的精灵不会通过盒子掉落并正常跳到它上面。

它看起来像什么:https://gyazo.com/6e0c9dfd15cdd99e72f14137fb5ef1cb 应该是什么样的:https://gyazo.com/bec5dc5a07d0d323b4d1cc8597eafb39

main.py

import pygame
from map import *
from char import *

pygame.init()

class game():
    def __init__(self):
        self.width = 1280
        self.height = 720
        self.gameRunning = True
        self.clock = pygame.time.Clock()
        self.FPS = 60
        self.screen = pygame.display.set_mode((self.width, self.height))
        self.loadMapData()

    def update(self):

        pygame.display.set_caption("{:.2f}".format(self.clock.get_fps()))
        self.screen.fill(white)
        self.screen.blit(self.map_img, (0,336))
        playerGroup.draw(self.screen)
        pygame.display.update()

    def gameLoop(self):

        self.clock.tick(self.FPS)
        self.event()
        player1.move()
        self.update()

    def event(self):

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.gameRunning = False

    def loadMapData(self):

        self.map = TiledMap()
        self.map_img = self.map.make_mapSurface()
        for tile_object in self.map.gameMap.objects:
            if tile_object.name == "Floor":
                Collision(tile_object.x, tile_object.y + 336, tile_object.width, tile_object.height)
            if tile_object.name == "Collision Box":
                Platform(tile_object.x, tile_object.y + 336, tile_object.width, tile_object.height)

g = game()
player1 = player()
while g.gameRunning == True:
    g.gameLoop()

map.py

import pygame
import pytmx
from char import *

pygame.init()

class TiledMap():
    def __init__(self):
        self.gameMap = pytmx.load_pygame("CollisionMap.tmx")
        self.mapwidth = self.gameMap.tilewidth * self.gameMap.width
        self.mapheight = self.gameMap.tileheight * self.gameMap.height

    def renderMap(self, surface):
        for layer in self.gameMap.visible_layers:
            if isinstance(layer, pytmx.TiledTileLayer):
                for x,y,gid in layer:
                    tile = self.gameMap.get_tile_image_by_gid(gid)
                    if tile:
                        surface.blit(tile, (x * self.gameMap.tilewidth, y * self.gameMap.tileheight))

    def make_mapSurface(self):

        mapSurface = pygame.Surface((self.mapwidth, self.mapheight), pygame.SRCALPHA)
        self.renderMap(mapSurface)
        return mapSurface

class Collision(pygame.sprite.Sprite):
    def __init__(self, x, y, width, height):
        self.groups = SolidGroup
        pygame.sprite.Sprite.__init__(self, self.groups)
        self.rect = pygame.Rect(x, y, width, height)

class Platform(pygame.sprite.Sprite):
    def __init__(self, x, y, width, height):
        self.groups = platformGroup
        pygame.sprite.Sprite.__init__(self, self.groups)
        self.rect = pygame.Rect(x, y, width, height)

char.py

import pygame

pygame.init()

black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
playerGroup = pygame.sprite.Group()
SolidGroup = pygame.sprite.Group()
platformGroup = pygame.sprite.Group()

class player(pygame.sprite.Sprite):
    def __init__(self):
        self.groups = playerGroup
        pygame.sprite.Sprite.__init__(self, self.groups)
        self.image = pygame.Surface((50, 50))
        self.image.fill(blue)
        self.rect = self.image.get_rect()
        self.rect.center = (1280/2, 720/2)
        self.position = pygame.math.Vector2(400, 300)
        self.acceleration = pygame.math.Vector2(0, 0)
        self.velocity = pygame.math.Vector2(0, 0)
        self.friction = -0.18
        self.falling = False
        self.up = False
        self.down = False
        self.right = False
        self.Left = False

    def checkFalling(self):
        self.rect.y += 1
        if not pygame.sprite.spritecollideany(self, SolidGroup, False):
            self.falling = True
            if self.velocity.y < 0:
                self.up = True
                self.down = False
            if self.velocity.y > 0:
                self.up = False
                self.down = True
        else:
            self.falling = False
            self.up = False
            self.down = False

        self.rect.y -= 1

    def move(self):
        self.checkFalling()
        if self.falling:
            self.acceleration = pygame.math.Vector2(0, 0.5)
        if not self.falling:
            self.acceleration = pygame.math.Vector2(0, 0)

        key = pygame.key.get_pressed()
        if key[pygame.K_RIGHT]:
            self.acceleration.x = 1
            self.right = True
        if key[pygame.K_LEFT]:
            self.acceleration.x = -1
            self.left = True
        if key[pygame.K_SPACE]:
            self.jump()

        self.acceleration.x += self.velocity.x * self.friction
        self.velocity += self.acceleration
        if abs(self.velocity.x) < 0.1:
            self.velocity.x = 0
        self.position += self.velocity + 0.5 * self.acceleration

        hitsPlatform = pygame.sprite.spritecollide(self, platformGroup, False)
        if hitsPlatform:
            if self.up:
                self.position.y -= self.velocity.y - 0.5 * self.acceleration.y
                self.velocity.y = 0

        self.rect.midbottom = self.position

        """
        Collision Code, not in function because of screen tearing and bouncy collisions
        """

        hits = pygame.sprite.spritecollide(self, SolidGroup, False)
        if hits:
            self.position.y = hits[0].rect.top
            self.velocity.y = 0

        hitsPlatform = pygame.sprite.spritecollide(self, platformGroup, False)
        if hitsPlatform:
            if self.down and self.position.y <= 340:
                self.position.y = hitsPlatform[0].rect.top
                self.velocity.y = 0
            if self.velocity.x > 0 and self.position.x + 64 < 239:
                self.position.x -= self.velocity.x - 0.5 * self.acceleration.x
            if self.velocity.x < 0 and self.position.x > 256:
                self.position.x -= self.velocity.x - 0.5 * self.acceleration.x

        self.rect.midbottom = self.position

    def jump(self):
        self.rect.y += 1
        hits = pygame.sprite.spritecollide(self, SolidGroup, False)
        hitsPlatform = pygame.sprite.spritecollide(self, platformGroup, False)
        self.rect.y -= 1
        if hits:
            self.velocity.y = -15
        if hitsPlatform:
            self.velocity.y = -15

0 个答案:

没有答案