2017-02-06 1 views
0

나는이 비디오를 따라 시도 : 난 내 프로그램을 실행할 때 내 캐릭터가 계속 유지하기위한 것입니다, 그러나 https://www.youtube.com/watch?v=pN9pBx5ln40&index=20&list=PLsk-HSGFjnaH5yghzu7PcOzm9NhsW0Urw스프라이트가 튀는 이유는 무엇입니까?

을하지만 어떤 이유로이 그것을 모습입니다

반사 : https://gyazo.com/015b20c0e4ad64ff3905cbef5865eedc

의 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): 

     self.screen.fill(white) 
     self.screen.blit(self.map_img, (0,528)) 
     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 + 528, 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) 
        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) 

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

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 

    def move(self): 
     self.acceleration = pygame.math.Vector2(0, 0.5) 
     key = pygame.key.get_pressed() 
     if key[pygame.K_RIGHT]: 
      self.acceleration.x = 1 
     if key[pygame.K_LEFT]: 
      self.acceleration.x = -1 

     self.acceleration.x += self.velocity.x * self.friction 
     self.velocity += self.acceleration 
     self.position += self.velocity + 0.5 * self.acceleration 

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

     self.rect.midbottom = self.position 

답변

0

문제는이 같은 칠 때 스프라이트가 플랫폼의 상단에 설정되는 것을 :

self.position.y = hits[0].rect.top 

하지만 그건 단지 약간 너무 높은 그것을 배치되고, 그 다음 쉬운 수정 등, 다시 떨어질 있도록 : 내가 추가 할 때이 그러나 수신 거부 스프라이트 양식을 중지 할 것을 알고

self.position.y = hits[0].rect.top + 1 
+0

'self.velocity.y = -13은'jumpi를 추가 이것이 지상에 닿았을 때 점프하는 것을 멈추십시오. – CustomerSupport

관련 문제