[英]PyGame: Collision Detection With Block
在平台游戲中,我正在生成地形,但沒有碰撞。 這是我嘗試用來檢查碰撞的代碼:
def player_move(self):
self.player.rect.x += self.player.velX
self.check_collision(self.player, self.player.velX, 0)
self.player.rect.y += self.player.velY
self.check_collision(self.player, 0, self.player.velY)
def check_collision(self, sprite, x_vel, y_vel):
# for every tile in Background.levelStructure, check for collision
for block in self.moon.get_surrounding_blocks(sprite):
if block is not None:
if pygame.sprite.collide_rect(sprite, block):
# we've collided! now we must move the collided sprite a step back
if x_vel < 0:
sprite.rect.x = block.rect.x + block.rect.w
if x_vel > 0:
sprite.rect.x = block.rect.x - sprite.rect.w
if y_vel < 0:
sprite.rect.y = block.rect.y + block.rect.h
然后,如果有幫助的話,我也有用於生成關卡的代碼:
import pygame, random
# Level
class Block(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('data/images/block.png')
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
def render(self, surface):
surface.blit(self.image, self.rect)
class Background():
def __init__(self):
self.bg_image = pygame.image.load('data/images/background.jpg')
# create a 2-dimensional array (etc. tile[x][y]) for holding the level structure
self.levelStructure = [[None for i in range(50)] for i in range(50)]
# generate level on initialization
self.generateLevel()
def generateLevel(self):
# fill self.levelStructure with tiles
for x in range(20):
for y in range(9):
if y < 11:
# make top space for the player to be
continue
tempBlock = Block(x*40, y*40)
# add block to both and self.levelStructure
self.levelStructure[x][y] = tempBlock
# generate some random terrain
for x in range(25):
# get a random height
height = random.randint(2, 5)
for y in range(height):
y += 15
tempBlock = Block(x*40, (y-height)*40)
self.levelStructure[x][y-height] = tempBlock
def get_surrounding_blocks(self, sprite):
# calculate the grid position of the sprite
sprite_x = sprite.rect.x // 40
sprite_y = sprite.rect.y // 40
# generate a list of surrounding sprites
blocks = []
for x in range(-2, 2+1):
for y in range(-2, 2+1):
blocks.append(self.levelStructure[sprite_x + x][sprite_y + y])
return blocks
def render(self, surface):
surface.blit(self.bg_image, (0, 0))
# also draw the blocks we created
for x in range(22):
for y in range(15):
block = self.levelStructure[x][y]
# check if there is a block, or if the grid is empty
if block is not None:
block.render(surface)
任何幫助將不勝感激,謝謝。
有幾個問題。
首先,您永遠不要告訴速度改變...您可能希望速度停止或在碰到障礙物時反轉。
其次,您要遍歷所有塊兩次,一次用於檢查X軸,一次用於Y。對於大型或復雜的地圖,這會使它慢兩倍。
我也建議不要在這種情況下使用collide_rect
函數來測試碰撞,除非您非常確定速度不能高於塊(或精靈)的總大小。 如果您以每幀71px的速度移動,但塊的大小為20px,而播放器的寬度僅為50px,則可以直接跳過一個塊而完全不碰它!
要特別注意的一件事是,先移動對象,然后檢查碰撞。 這可能會引起問題,因為您隨后必須將其移回以前的位置。
我建議您將結構更改為:
def player_move(self):
# create some local vars for ease of reading...
x = self.player.rect.x
x_next = x + self.player.velX
y = self.player.rect.y
y_next = y + self.player.vely
w = self.player.rect.w
h = self.player.rect.h
# has a collision happened?
hitX = False
hitY = False
for block in self.moon.get_surrounding_blocks(self.player):
# empty blocks are 'None' in this map system...
if not block:
continue
if self.player.velX > 0: # moving right
if x + w < block.x and x_next + w >= block.x:
# collision!
x_next = block.x - w
hitX = True
elif self.player.velX < 0: # moving left
if x > block.x + block.w and x_next > block.x + block.w:
# collision!
x_next = block.x + block.w
hitX = True
if self.player.velY...... # do the same for Y axis...
# now update rect to have the new collision fixed positions:
self.player.rect.x = x_next
self.player.rect.y = y_next
# and if there was a collision, fix the velocity.
if hitX:
self.velX = 0 # or 0 - velX to bounce instead of stop
if hitY:
self.velY = 0 # or 0 - velY to bounce instead of stop
希望這有助於朝正確的方向發展...
另一個想法是,幾年前當我編寫平台引擎時,我記得我在某個階段遇到了一些問題,播放器對象碰到了塊,然后停止,然后又重新開始。 最后,它是代碼中某處的一個錯誤。 可能值得添加一堆調試打印(最好使用'logging'包...),然后放慢幀速率,這樣您就可以確切地看到發生了什么。
祝好運! 編寫游戲很有趣,但有時也會很令人沮喪。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.