2016-12-05 196 views
2

我正在python和pygame的平臺遊戲。整個代碼可以在「https://github.com/C-Kimber/FBLA_Game」找到。我遇到的問題是玩家精靈和牆上精靈之間的碰撞,特別是角落。當玩家按下x移動鍵並跳躍時,玩家不會移動或卡住。這裏是衝突樣本:pygame sprite牆碰撞

def wallCollisions(self): 

    block_hit_list = pygame.sprite.spritecollide(self, self.walls, False) 

    for block in block_hit_list: 


     if self.rect.bottom >= block.rect.top and self.rect.bottom <= block.rect.top + 15: # Moving down; Hit the top side of the wall 
      if self.rect.right > block.rect.left: 
       self.rect.bottom = block.rect.top 
       self.yvel = 0 
       self.onGround = True 
       self.jumps = 1 
     elif self.rect.top <= block.rect.bottom and self.rect.top >= block.rect.bottom - 15: # Moving up; Hit the bottom side of the wall 
      self.rect.top = block.rect.bottom 
      self.yvel = 0 
     if self.rect.right >= block.rect.left and self.rect.right <= block.rect.left + 15: # Moving right; Hit the left side of the wall 
      if self.rect.bottom > block.rect.top+15: 
       self.rect.right = block.rect.left#+1 
       self.xvel = 0 
     elif self.rect.left <= block.rect.right and self.rect.left >= block.rect.right - 15: # Moving left; Hit the right side of the wall 
      self.rect.left = block.rect.right#-1 
      self.xvel = 0 = block.rect.right#-1 
      self.xvel = 0 

我已經包含了正在發生的事情和我想要的圖像。 enter image description here

我已經嘗試過其他方法,例如使用velocity來確定衝突的因素,但這是迄今爲止最好的工作方式。如果你能提供解決方案,將不勝感激。

+0

你在代碼中做出的一個假設是你可以獨立地對每個塊進行碰撞。但是黑色的碰撞屬性應該依賴於它旁邊的其他塊嗎?例如,你是否應該能夠在一個塊上(onGround = true)着陸另一個塊? – samgak

+1

看看[這個問題](https://stackoverflow.com/questions/14354171/add-scrolling-to-a-platformer-in-pygame/14357169#14357169)。雖然這個話題實際上是不同的,但它是您如何進行碰撞檢測的一個實例(查看碰撞函數)。這個想法是檢查碰撞兩次,一次是爲x,一次爲y軸。 – sloth

回答

0
elif self.rect.top <= block.rect.bottom and self.rect.top >= block.rect.bottom - 15: # Moving up; Hit the bottom side of the wall 
     self.rect.top = block.rect.bottom 
     self.yvel = 0 

是否應該將yvel設置爲正數以使其升高?

2

感謝您的用戶懶惰!他所聯繫的問題給了我一些非常需要的清晰度。它花了我一些,但我實現了它。我爲碰撞創建了一個函數。

def wallColl(self, xvel, yvel, colliders): 
    for collider in colliders: 
     if pygame.sprite.collide_rect(self, collider): 
      if xvel > 0: 
       self.rect.right = collider.rect.left 
       self.xvel = 0 
      if xvel < 0: 
       self.rect.left = collider.rect.right 
       self.xvel = 0 
      if yvel < 0: 
       self.rect.bottom = collider.rect.top 
       self.onGround = True 
       self.jumps = 3 
       self.yvel = 0 
      if yvel > 0: 
       self.yvel = 0 
       self.rect.top = collider.rect.bottom 

然後我在我的更新函數中調用它們。

def update(self): 
    self.rect.x += self.xvel 
    # self.walls is an array of sprites. 
    self.wallColl(self.xvel, 0, self.walls) 

    self.rect.y -= self.yvel 
    self.onGround = False 
    self.wallColl(0, self.yvel, self.walls) 

    self.wallCollisions() 
    if self.otherplayers != None: 
     self.playerCollisions() 

    # Gravity 
    if self.onGround == False: 
     self.yvel-=.0066*self.mass 

    self.boundries(highbound, lowbound, leftbound, rightbound) 
    self.down = False 

在我的遊戲中的實際使用使可用性接近完美。不是100%,但這不是一個完美的答案。

1

來處理衝突與牆壁最簡單的方法是首先移動沿x軸的球員RECT或精靈,檢查它是否與牆壁碰撞,然後設置其self.rect.right = wall.rect.left如果它移動到右邊或self.rect.left = wall.rect.right如果它的移動往左邊。之後,你對y軸也做同樣的事情。你必須單獨做這個動作,否則你不知道方向和如何重置矩形的位置。

import pygame as pg 
from pygame.math import Vector2 


class Player(pg.sprite.Sprite): 

    def __init__(self, x, y, walls): 
     super().__init__() 
     self.image = pg.Surface((30, 50)) 
     self.image.fill(pg.Color('dodgerblue1')) 
     self.rect = self.image.get_rect(center=(x, y)) 
     self.pos = Vector2(x, y) # Position vector. 
     self.vel = Vector2(0, 0) # Velocity vector. 
     self.walls = walls # A reference to the wall group. 

    def update(self): 
     self.pos += self.vel 
     self.wall_collisions() 

    def wall_collisions(self): 
     """Handle collisions with walls.""" 
     self.rect.centerx = self.pos.x 
     for wall in pg.sprite.spritecollide(self, self.walls, False): 
      if self.vel.x > 0: 
       self.rect.right = wall.rect.left 
      elif self.vel.x < 0: 
       self.rect.left = wall.rect.right 
      self.pos.x = self.rect.centerx 

     self.rect.centery = self.pos.y 
     for wall in pg.sprite.spritecollide(self, self.walls, False): 
      if self.vel.y > 0: 
       self.rect.bottom = wall.rect.top 
      elif self.vel.y < 0: 
       self.rect.top = wall.rect.bottom 
      self.pos.y = self.rect.centery 


class Wall(pg.sprite.Sprite): 

    def __init__(self, x, y, w, h): 
     super().__init__() 
     self.image = pg.Surface((w, h)) 
     self.image.fill(pg.Color('sienna1')) 
     self.rect = self.image.get_rect(topleft=(x, y)) 


def main(): 
    screen = pg.display.set_mode((640, 480)) 
    clock = pg.time.Clock() 

    all_sprites = pg.sprite.Group() 
    walls = pg.sprite.Group() 

    wall = Wall(100, 200, 300, 30) 
    wall2 = Wall(230, 70, 30, 300) 
    walls.add(wall, wall2) 
    all_sprites.add(wall, wall2) 

    player = Player(300, 300, walls) 
    all_sprites.add(player) 

    done = False 

    while not done: 
     for event in pg.event.get(): 
      if event.type == pg.QUIT: 
       done = True 

     keys = pg.key.get_pressed() 
     if keys[pg.K_w]: 
      player.vel.y = -3 
     elif keys[pg.K_s]: 
      player.vel.y = 3 
     else: 
      player.vel.y = 0 
     if keys[pg.K_a]: 
      player.vel.x = -3 
     elif keys[pg.K_d]: 
      player.vel.x = 3 
     else: 
      player.vel.x = 0 

     all_sprites.update() 
     screen.fill((30, 30, 30)) 
     all_sprites.draw(screen) 

     pg.display.flip() 
     clock.tick(30) 


if __name__ == '__main__': 
    pg.init() 
    main() 
    pg.quit() 
+0

精靈,精靈組和spritecollide的完整示例。 – skrx