现在我正在制作一个平台游戏,并且在碰撞检测方面遇到了很多麻烦。其中一个问题是,当角色在盒子上时,图片会不断地从跳跃变为站立。另一个是当角色在一个盒子上并击中另一个盒子时,它们立即被传送到底盒侧面。如果你对这个问题有任何见解,那就太好了。这是我目前的碰撞检测程序:
def update(self):
global playerRL, jump, playerUD, counter, inMotion, onGround, onBox, jumpSize, fallSpeed, jumpCounter, allBoxes, boxes, direction, gravity, bottomTouching, boxTouching
keys = pygame.key.get_pressed()
previousX = self.rect.left
previousY = self.rect.bottom
inMotion = False
bottomTouching = False
if counter > 20:
counter = 0
def collisionDetection():
global jump, playerRL, playerUD, counter, inMotion, onGround, onBox, jumpSize, fallSpeed, jumpCounter, allBoxes, boxes, direction, gravity, bottomTouching, boxTouching
collided = False
boxCounter = 0
for plat in boxes:
if self.rect.colliderect(plat.rect):
if self.rect.left < plat.rect.right and self.rect.right > plat.rect.left and self.rect.bottom > plat.rect.top and self.rect.bottom < plat.rect.bottom:
self.rect.bottom = plat.rect.top
playerUD = 0
jumpCounter = 0
gravity = 0
onGround = True
collided = True
boxCounter += 1
print 'hi'
if self.rect.right <= plat.rect.right and self.rect.left <= plat.rect.left and self.rect.bottom > plat.rect.top and self.rect.top < plat.rect.bottom:
self.rect.right = plat.rect.left
print 'right'
if self.rect.left >= plat.rect.left and self.rect.right >= plat.rect.right and self.rect.bottom > plat.rect.top and self.rect.top < plat.rect.bottom:
self.rect.left = plat.rect.right
if self.rect.left <= plat.rect.right and self.rect.right >= plat.rect.left and self.rect.top < plat.rect.bottom and self.rect.bottom > plat.rect.top:
self.rect.top = plat.rect.bottom
jumpCounter = 0
playerUD = 0
if self.rect.left < 0:
if playerRL < 0:
self.rect.left = 0
if self.rect.top < 0:
if playerUD < 0:
self.rect.top = 0
if self.rect.right >= 960:
if playerRL > 0:
self.rect.right = 960
if self.rect.bottom > 640:
self.rect.bottom = 640
playerUD = 0
if self.rect.bottom == 640:
onGround = True
else:
if collided == False:
onGround = False
keys = pygame.key.get_pressed()
if keys[K_d] or keys[K_RIGHT]:
direction = 'right'
inMotion = True
if rightTouch == False:
counter += 1
playerRL += 4
if keys[K_a] or keys[K_LEFT]:
direction = 'left'
inMotion = True
if leftTouch == False:
counter += 1
playerRL -= 4
if keys[K_w] or keys[K_UP]:
if onGround == True:
bottomTouching = False
onGround = False
jumpSize = 8
playerUD -= jumpSize
jumpCounter = 11
gravity = 1
if onGround == True:
if inMotion == True and counter > 10:
if direction == 'left':
self.image = backwardMovingPlayer
self.mask = pygame.mask.from_surface(self.image)
self.rect = self.image.get_rect()
self.rect.bottom = previousY
self.rect.left = previousX
else:
self.image = forwardMovingPlayer
self.mask = pygame.mask.from_surface(self.image)
self.rect = self.image.get_rect()
self.rect.bottom = previousY
self.rect.left = previousX
else:
if direction == 'left':
self.image = backwardStationaryPlayer
self.mask = pygame.mask.from_surface(self.image)
self.rect = self.image.get_rect()
self.rect.bottom = previousY
self.rect.left = previousX
else:
self.image = forwardStationaryPlayer
self.mask = pygame.mask.from_surface(self.image) #masks are used for cutouts
self.rect = self.image.get_rect()
self.rect.bottom = previousY
self.rect.left = previousX
else:
if direction == 'left':
self.image = backwardJumpingPlayer
self.mask = pygame.mask.from_surface(self.image) #masks are used for cutouts
self.rect = self.image.get_rect()
self.rect.bottom = previousY
self.rect.left = previousX
else:
self.image = forwardJumpingPlayer
self.mask = pygame.mask.from_surface(self.image) #masks are used for cutouts
self.rect = self.image.get_rect()
self.rect.bottom = previousY
self.rect.left = previousX
if onGround == False:
if jumpCounter > 0:
jumpCounter -= 1
playerUD -= jumpSize
else:
playerUD += gravity
gravity += 1
self.rect.left += playerRL
self.rect.bottom += playerUD
collisionDetection()
playerUD = 0
playerRL = 0
答案 0 :(得分:0)
Pygame内置有碰撞检测功能:
firstrect.colliderect(secondrect)
它返回一个布尔值。