在我在#sensors注释下创建代码后,我的游戏变得太慢了(参见下面的代码,它的for循环中有很多迭代)。我已经为人类制作了移动红点的控制装置,但游戏应该是由计算机自己玩的
我的问题是:
还是别的什么
这是我的代码:
import turtle
import math
#Set up screen
wn = turtle.Screen()
wn.bgcolor("lightyellow")
score = 0
#Draw border
mypen = turtle.Turtle()
mypen.penup()
mypen.setposition(-300, -300)
mypen.speed(0)
mypen.pendown()
mypen.pensize(3)
for side in range(4):
mypen.forward(600)
mypen.left(90)
mypen.hideturtle()
#Draw obstacle
myObstacle = turtle.Turtle()
myObstacle.penup()
myObstacle.setposition(-150, -150)
myObstacle.speed(0)
myObstacle.pendown()
myObstacle.pensize(3)
for side in range(4):
myObstacle.forward(300)
myObstacle.left(90)
myObstacle.hideturtle()
#Create player turtle
player = turtle.Turtle()
player.penup()
player.speed(0)
player.setposition(-200, -200)
player.color("red")
player.shape("circle")
#Set speed variable
speed = 1
#define functions
def turnleft():
player.left(30)
def turnright():
player.right(30)
def increasespeed():
global speed
speed += 1
def decreasespeed():
global speed
if speed > 1:
speed -= 1
#Set keyboard bindings
turtle.listen()
turtle.onkey(turnleft, "Left")
turtle.onkey(turnright, "Right")
turtle.onkey(increasespeed, "Up")
turtle.onkey(decreasespeed, "Down")
#bounderies
def merge(list1, list2):
merged_list = [(list1[i], list2[i]) for i in range(0, len(list1))]
return merged_list
bounderies = merge([-300] * 601, list(range(-300,301)))
bounderies.extend(merge([300] * 601, list(range(-300,301))))
bounderies.extend(merge(list(range(-300,301)), [-300] * 601))
bounderies.extend(merge(list(range(-300,301)), [300] * 601))
bounderies.extend(merge([-150] * 301, list(range(-150,151))))
bounderies.extend(merge([150] * 301, list(range(-150,151))))
bounderies.extend(merge(list(range(-150,151)), [-150] * 301))
bounderies.extend(merge(list(range(-150,151)), [150] * 301))
def scoreset():
global score
score += 1
scorestring = "Score: %s" %score
mypen.undo()
mypen.penup()
mypen.setposition(-340, 310)
mypen.pendown()
mypen.color("green")
mypen.write(scorestring, False, align = "left", font=("ariel", 16, "bold"))
#sensors
def forwardDistance():
forwardDistance = []
minForwDist = 0
tupleCoordinate = (0,0)
yCoordinate = 0
xCoordinate = 0
position = (int(player.xcor()), int(player.ycor()))
heading = player.heading()
sinus = math.sin(math.radians(heading))
cosinus = math.cos(math.radians(heading))
tangent = sinus / cosinus
for alpha in range(1000):
if (heading < 45 and heading >= 0) or (heading < 360 and heading >= 315):
xCoordinate = position[0] + alpha
yCoordinate = xCoordinate * tangent + (position[1] - position[0] * tangent)
tupleCoordinate = (int(xCoordinate), int(yCoordinate))
elif (heading < 315 and heading >= 225):
yCoordinate = position[1] - alpha
xCoordinate = (yCoordinate - (position[1] - position[0] * tangent)) / tangent
tupleCoordinate = (int(xCoordinate), int(yCoordinate))
elif (heading < 225 and heading >= 135):
xCoordinate = position[0] - alpha
yCoordinate = xCoordinate * tangent + (position[1] - position[0] * tangent)
tupleCoordinate = (int(xCoordinate), int(yCoordinate))
elif (heading < 135 and heading >= 45):
yCoordinate = position[1] + alpha
xCoordinate = (yCoordinate - (position[1] - position[0] * tangent)) / tangent
tupleCoordinate = (int(xCoordinate), int(yCoordinate))
if tupleCoordinate in bounderies:
forwardDistance.append(player.distance(tupleCoordinate))
minForwDist = min(forwardDistance)
#print("Forward distance: ", int(minForwDist))
return minForwDist
def leftDistance():
forwardDistance = []
minForwDist = 0
tupleCoordinate = (0,0)
yCoordinate = 0
xCoordinate = 0
position = (int(player.xcor()), int(player.ycor()))
if player.heading() + 90 >= 360:
heading = player.heading() + 90 - 360
else:
heading = player.heading() + 90
sinus = math.sin(math.radians(heading))
cosinus = math.cos(math.radians(heading))
tangent = sinus / cosinus
for alpha in range(1000):
if (heading < 45 and heading >= 0) or (heading < 360 and heading >= 315):
xCoordinate = position[0] + alpha
yCoordinate = xCoordinate * tangent + (position[1] - position[0] * tangent)
tupleCoordinate = (int(xCoordinate), int(yCoordinate))
elif (heading < 315 and heading >= 225):
yCoordinate = position[1] - alpha
xCoordinate = (yCoordinate - (position[1] - position[0] * tangent)) / tangent
tupleCoordinate = (int(xCoordinate), int(yCoordinate))
elif (heading < 225 and heading >= 135):
xCoordinate = position[0] - alpha
yCoordinate = xCoordinate * tangent + (position[1] - position[0] * tangent)
tupleCoordinate = (int(xCoordinate), int(yCoordinate))
elif (heading < 135 and heading >= 45):
yCoordinate = position[1] + alpha
xCoordinate = (yCoordinate - (position[1] - position[0] * tangent)) / tangent
tupleCoordinate = (int(xCoordinate), int(yCoordinate))
if tupleCoordinate in bounderies:
forwardDistance.append(player.distance(tupleCoordinate))
minForwDist = min(forwardDistance)
#print("Left distance: ", int(minForwDist))
return minForwDist
def leftForwardDistance():
forwardDistance = []
minForwDist = 0
tupleCoordinate = (0,0)
yCoordinate = 0
xCoordinate = 0
position = (int(player.xcor()), int(player.ycor()))
if player.heading() + 45 >= 360:
heading = player.heading() + 45 - 360
else:
heading = player.heading() + 45
sinus = math.sin(math.radians(heading))
cosinus = math.cos(math.radians(heading))
tangent = sinus / cosinus
for alpha in range(1000):
if (heading < 45 and heading >= 0) or (heading < 360 and heading >= 315):
xCoordinate = position[0] + alpha
yCoordinate = xCoordinate * tangent + (position[1] - position[0] * tangent)
tupleCoordinate = (int(xCoordinate), int(yCoordinate))
elif (heading < 315 and heading >= 225):
yCoordinate = position[1] - alpha
xCoordinate = (yCoordinate - (position[1] - position[0] * tangent)) / tangent
tupleCoordinate = (int(xCoordinate), int(yCoordinate))
elif (heading < 225 and heading >= 135):
xCoordinate = position[0] - alpha
yCoordinate = xCoordinate * tangent + (position[1] - position[0] * tangent)
tupleCoordinate = (int(xCoordinate), int(yCoordinate))
elif (heading < 135 and heading >= 45):
yCoordinate = position[1] + alpha
xCoordinate = (yCoordinate - (position[1] - position[0] * tangent)) / tangent
tupleCoordinate = (int(xCoordinate), int(yCoordinate))
if tupleCoordinate in bounderies:
forwardDistance.append(player.distance(tupleCoordinate))
minForwDist = min(forwardDistance)
#print("Left-forward distance: ", int(minForwDist))
return minForwDist
def rightDistance():
forwardDistance = []
minForwDist = 0
tupleCoordinate = (0,0)
yCoordinate = 0
xCoordinate = 0
position = (int(player.xcor()), int(player.ycor()))
if player.heading() < 90:
heading = 360 - (90 - player.heading())
else:
heading = player.heading() - 90
sinus = math.sin(math.radians(heading))
cosinus = math.cos(math.radians(heading))
tangent = sinus / cosinus
for alpha in range(1000):
if (heading < 45 and heading >= 0) or (heading < 360 and heading >= 315):
xCoordinate = position[0] + alpha
yCoordinate = xCoordinate * tangent + (position[1] - position[0] * tangent)
tupleCoordinate = (int(xCoordinate), int(yCoordinate))
elif (heading < 315 and heading >= 225):
yCoordinate = position[1] - alpha
xCoordinate = (yCoordinate - (position[1] - position[0] * tangent)) / tangent
tupleCoordinate = (int(xCoordinate), int(yCoordinate))
elif (heading < 225 and heading >= 135):
xCoordinate = position[0] - alpha
yCoordinate = xCoordinate * tangent + (position[1] - position[0] * tangent)
tupleCoordinate = (int(xCoordinate), int(yCoordinate))
elif (heading < 135 and heading >= 45):
yCoordinate = position[1] + alpha
xCoordinate = (yCoordinate - (position[1] - position[0] * tangent)) / tangent
tupleCoordinate = (int(xCoordinate), int(yCoordinate))
if tupleCoordinate in bounderies:
forwardDistance.append(player.distance(tupleCoordinate))
minForwDist = min(forwardDistance)
#print("Right distance: ", int(minForwDist))
return minForwDist
def rightForwardDistance():
forwardDistance = []
minForwDist = 0
tupleCoordinate = (0,0)
yCoordinate = 0
xCoordinate = 0
position = (int(player.xcor()), int(player.ycor()))
if player.heading() < 45:
heading = 360 - (45 - player.heading())
else:
heading = player.heading() - 45
sinus = math.sin(math.radians(heading))
cosinus = math.cos(math.radians(heading))
tangent = sinus / cosinus
for alpha in range(1000):
if (heading < 45 and heading >= 0) or (heading < 360 and heading >= 315):
xCoordinate = position[0] + alpha
yCoordinate = xCoordinate * tangent + (position[1] - position[0] * tangent)
tupleCoordinate = (int(xCoordinate), int(yCoordinate))
elif (heading < 315 and heading >= 225):
yCoordinate = position[1] - alpha
xCoordinate = (yCoordinate - (position[1] - position[0] * tangent)) / tangent
tupleCoordinate = (int(xCoordinate), int(yCoordinate))
elif (heading < 225 and heading >= 135):
xCoordinate = position[0] - alpha
yCoordinate = xCoordinate * tangent + (position[1] - position[0] * tangent)
tupleCoordinate = (int(xCoordinate), int(yCoordinate))
elif (heading < 135 and heading >= 45):
yCoordinate = position[1] + alpha
xCoordinate = (yCoordinate - (position[1] - position[0] * tangent)) / tangent
tupleCoordinate = (int(xCoordinate), int(yCoordinate))
if tupleCoordinate in bounderies:
forwardDistance.append(player.distance(tupleCoordinate))
minForwDist = min(forwardDistance)
#print("Right-forward distance: ", int(minForwDist))
return minForwDist
#finished sensors
while True:
rightForwardDistance()
rightDistance()
leftForwardDistance()
leftDistance()
forwardDistance()
sensors = {'left': leftDistance(), 'left forward': leftForwardDistance(), 'forward': forwardDistance(), 'right forward': rightForwardDistance(), 'right': rightDistance()}
changeDirectionTo = max(sensors, key=sensors.get)
player.forward(speed)
#change Direction To
if changeDirectionTo == 'left':
player.left(90)
elif changeDirectionTo == 'left forward':
player.left(45)
elif changeDirectionTo == 'right forward':
player.right(45)
elif changeDirectionTo == 'right':
player.right(90)
#when hitting the boundary
if (int(player.position()[0]),int(player.position()[1])) in bounderies:
scoreset()
if player.xcor() > 300 or player.xcor() < -300:
player.right(30)
if player.ycor() > 300 or player.ycor() < -300:
player.right(30)
if player.position() == myObstacle.position():
player.right(30)
if player.xcor() > -150 and player.xcor() < 150 and player.ycor() > -150 and player.ycor() < 150:
player.right(30)
你有很多地方,比如:
if something in bounderies: ...
问题是,
bounderies
是一个列表,所以查找是一个O(n)操作。由于最常见的情况是something not in bounderies
,它通常必须检查整个列表,以确定您的坐标不在其中添加一行:
变成了超级昂贵而且频繁的从O(n)到O(1)的查找,在我的计算机上使整个程序运行速度提高了约18倍
你还可以做很多其他的事情来加快速度,但这是一个超级简单有效的优化
您的代码有几个问题。首先是@KirkStrauser很好地解决的
bounderies
[sic]set问题。但是sensors
的问题比@LucasBelfanti所说的还要严重。与进行数学(即几何)并查找到目标的距离不同,您可以沿着向量的每个元素测试每个可能点。除了修复几何体之外,由于您一次只能沿一个向量查看,因此第一个截距应该是您想要的点,您可以突破传感器,避免接下来的500次左右的测试如果我们将其与
math.tan()
而不是math.sin()/math.cos()
相结合,并在角度上使用模运算,对于您的传感器之一,我们得到如下结果:我已经复制并运行了您的代码,请允许我首先回答以下问题:
代码进行大量处理的主要位置是
wile True
条件。在这里您可以调用10个函数:如果其中每一个都有一个范围为1000的for循环,那么删除/评论前5个将使游戏更快一些
除此之外,还可以通过不同的方式改进代码,例如:
该代码重复5次,可以移动到函数中以避免重复。 代码中还有未使用的变量,可以删除这些变量
在这些更改之后,代码的可读性会更高,速度也会更快:
相关问题 更多 >
编程相关推荐