Python 中的碰撞检测
Collision detection in Python
我目前正在开发一个用 iPython notbook 编写的迷宫游戏。我无权访问 pygame,因此正在从头开始进行碰撞检测。
到目前为止,我得到的代码能够让玩家四处移动,并且已经有一个网格可以确定游戏场地的大小。
from turtle import *
def line(x1, y1, x2, y2):
pu()
goto(x1,y1)
pd()
goto(x2,y2)
pu()
setup(600,600)
setworldcoordinates(-1,-1,11,11)
class Network:
tracer(30)
ht()
for n in range(0,11):
line(0,n,10,n)
line(n,0,n,10)
tracer(1)
head= heading()
st()
class Figur:
register_shape("figur.gif")
shape("figur.gif")
head = heading()
pu()
setpos(9.5,9.5)
def turtle_up():
if head != 90:
seth(90)
fd(1)
def turtle_down():
if head != 270:
seth(270)
fd(1)
def turtle_left():
if head != 180:
seth(180)
fd(1)
def turtle_right():
if head != 360:
seth(0)
fd(1)
onkey(turtle_up, "Up")
onkey(turtle_down, "Down")
onkey(turtle_left, "Left")
onkey(turtle_right, "Right")
listen()
class Walls:
def tortle():
tracer(30)
t1 = Turtle()
t1.color("green")
t1.left(180)
t1.fd(1)
t1.right(90)
t1.fd(11)
for i in range(1,4):
t1.right(90)
t1.fd(12)
for i in range(1,3):
t1.right(90)
t1.fd(1)
t1.left(90)
for i in range(1,5):
t1.fd(10)
t1.right(90)
Walls.tortle()
tracer(1)
update()
done()
目前墙还没有完工。我刚刚开始使用它们并尝试在比赛场地周围创建一堵墙,导致球洞区域被绿色覆盖。乌龟的图片是我自己画的,但我认为没有它应该也能用,只用普通的乌龟代替。
所以我的主要问题是:如何为我的乌龟创建碰撞检测以使其不能穿墙?
我想制作一个 set
的 Vec2D
个已经访问过的点,然后在每次移动时与它进行比较。这是一个简单的向前和向后函数:
def no_collision_forward(amount, walls):
movement = 1 if abs(amount) == amount else -1 # If amount is negative, moving down
for i in range(abs(amount)):
cur_pos = pos()
nex_pos = Vec2D(cur_pos[0], cur_pos[1] + movement)
if nex_pos in walls:
return # Can return whatever
else:
forward(movement)
def no_collision_backward(amount, walls):
return no_collision_forward(-amount, walls)
visited_locations
应该是一组 Vec2D
或元组。
这可能不是最有效的解决方案,但它确实有效!
我目前正在开发一个用 iPython notbook 编写的迷宫游戏。我无权访问 pygame,因此正在从头开始进行碰撞检测。
到目前为止,我得到的代码能够让玩家四处移动,并且已经有一个网格可以确定游戏场地的大小。
from turtle import *
def line(x1, y1, x2, y2):
pu()
goto(x1,y1)
pd()
goto(x2,y2)
pu()
setup(600,600)
setworldcoordinates(-1,-1,11,11)
class Network:
tracer(30)
ht()
for n in range(0,11):
line(0,n,10,n)
line(n,0,n,10)
tracer(1)
head= heading()
st()
class Figur:
register_shape("figur.gif")
shape("figur.gif")
head = heading()
pu()
setpos(9.5,9.5)
def turtle_up():
if head != 90:
seth(90)
fd(1)
def turtle_down():
if head != 270:
seth(270)
fd(1)
def turtle_left():
if head != 180:
seth(180)
fd(1)
def turtle_right():
if head != 360:
seth(0)
fd(1)
onkey(turtle_up, "Up")
onkey(turtle_down, "Down")
onkey(turtle_left, "Left")
onkey(turtle_right, "Right")
listen()
class Walls:
def tortle():
tracer(30)
t1 = Turtle()
t1.color("green")
t1.left(180)
t1.fd(1)
t1.right(90)
t1.fd(11)
for i in range(1,4):
t1.right(90)
t1.fd(12)
for i in range(1,3):
t1.right(90)
t1.fd(1)
t1.left(90)
for i in range(1,5):
t1.fd(10)
t1.right(90)
Walls.tortle()
tracer(1)
update()
done()
目前墙还没有完工。我刚刚开始使用它们并尝试在比赛场地周围创建一堵墙,导致球洞区域被绿色覆盖。乌龟的图片是我自己画的,但我认为没有它应该也能用,只用普通的乌龟代替。
所以我的主要问题是:如何为我的乌龟创建碰撞检测以使其不能穿墙?
我想制作一个 set
的 Vec2D
个已经访问过的点,然后在每次移动时与它进行比较。这是一个简单的向前和向后函数:
def no_collision_forward(amount, walls):
movement = 1 if abs(amount) == amount else -1 # If amount is negative, moving down
for i in range(abs(amount)):
cur_pos = pos()
nex_pos = Vec2D(cur_pos[0], cur_pos[1] + movement)
if nex_pos in walls:
return # Can return whatever
else:
forward(movement)
def no_collision_backward(amount, walls):
return no_collision_forward(-amount, walls)
visited_locations
应该是一组 Vec2D
或元组。
这可能不是最有效的解决方案,但它确实有效!