即时通讯试图使一个蛇AI,可以看到自己在哪里,所以即时通讯使用此代码来尝试做到这一点
我是python的新手
if new_direction == "Right" and new_head_position - (0, 20) in self.snake_positions:
if new_direction == "Right" and new_head_position + (0, 20) in self.snake_positions:
self.direction = "Right"
我希望代码仅在其他空间被蛇形部分占据时将self.direction设置为“Right”,但是我收到此错误
最佳答案
以下表达式将产生错误:new_head_position - (0, 20)
和new_head_position + (0, 20)
,因为Python不允许您在元组之间执行算术运算。
这是适合您的解决方法:
(new_head_position[0], new_head_position[1] - 20)
(new_head_position[0], new_head_position[1] + 20)
关于python - 如何修复TypeError : unsupported operand type(s) for -: 'tuple' and 'tuple' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58211289/