vpython 贞测碰撞_Python碰撞检测

我已经尝试添加碰撞检测一段时间了,但似乎做不到。。在

我只是用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语句和其他注释掉的语句。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值