我已经尝试添加碰撞检测一段时间了,但似乎做不到。。在
我只是用x,y坐标画地图:from setup import *
treeload = “Images/tree.jpg”
tree = pygame.image.load(treeload).convert_alpha()
class drawtree:
def init(self, x, width, step1, y, height, step2):
self.x = x
self.y = y
self.width = width
self.height = height
self.step1 = step1
self.step2 = step2
def draw(self):
for x in range(self.x, self.x+self.width, self.step1):
for y in range(self.y, self.y+self.height, self.step2):
window.blit(tree, (x,y))
t1 = drawtree(100, 100, 20, 0, 90, 30)
t2 = drawtree(400, 300, 20, 0, 90, 30)
t3 = drawtree(100, 270, 20, 450, 150, 30)
t4 = drawtree(400, 300, 20, 450, 150, 30)
trees = [t1, t2, t3 ,t4]
用这种方法我想出了一个检测方法:
^{pr2}$
我一直在尝试使用for循环遍历树来检测player(一个矩形)是否穿过树,但是我想不出任何东西。在
我试过了for i in trees:
collision = wall_detect(player.x, player.y, player.width, player.height, i.x, i.y, i.width, i.height)
player.update(collision)
在播放器更新(碰撞)如果collision=true,则将矩形更改为红色;如果为false,则将其保留为黑色。在
我试过使用for和if,例如:for i in trees:
if wall_detect(player.x, player.y, player.width, player.height, i.x, i.y, i.width, i.height) == wall_detect(player.x, player.y, player.width, player.height, t1.x, t1.y, t1.width, t1.height):
collision = wall_detect(player.x, player.y, player.width, player.height, t1.x, t1.y, t1.width, t1.height)
player.update(collision)
if wall_detect(player.x, player.y, player.width, player.height, i.x, i.y, i.width, i.height) == wall_detect(player.x, player.y, player.width, player.height, t2.x, t2.y, t2.width, t2.height):
collision = wall_detect(player.x, player.y, player.width, player.height, t2.x, t2.y, t2.width, t2.height)
player.update(collision)
等等。。但这不起作用,它只适用于带有1个if语句和其他注释掉的语句。在
Original: https://blog.csdn.net/weixin_42548893/article/details/113686501
Author: 任立龙
Title: vpython 贞测碰撞_Python碰撞检测
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/783787/
转载文章受原作者版权保护。转载请注明原作者出处!