我正在使用python和pygame开发一个蛇游戏,但是在检查蛇是否穿过食物时遇到了问题。有人能帮我吗?你知道吗
我试着把食物的位置设为10的倍数,因为我的蛇的宽度和高度也是10,窗户(宽度和高度)也是10的倍数。你知道吗
food_x = random.randrange(0, displayWidth-foodWidth, 10)
food_y = random.randrange(0, displayHeight-foodHeight, 10)
我希望这样做将使食物的位置,这样就不会有碰撞,但蛇和食物的直接重叠,这将使编码更容易。然而,也发生了碰撞。你知道吗
因此,假设您的snake数据结构是一组矩形,并且snake只“吃”头部矩形,那么确定碰撞例程非常简单。你知道吗
PyGame rect library具有矩形之间checking collisions的函数。你知道吗
所以假设
head_rect
是一个rect
和你的蛇头的坐标和大小,并且food_rect
是一个要检查的项目:或者如果在
food_list
中有一个food_rect
列表:使用PyGame的库矩形重叠函数要比创建自己的库简单得多。你知道吗
相关问题 更多 >
编程相关推荐