我的程序在一定时间后无故退出。它没有抛出任何错误,而且我唯一一次调用的要求是 done()
不符合要求。
(有时,对
done()
是在程序退出前满足的)。)
我的代码在下面。
import random
from turtle import *
import sys
sys.setrecursionlimit(10000)
l = 0
defense_x1 = -200
defense_x2 = -200
defense_x3 = -200
defense_x4 = -200
zombies = []
defenses = []
attacks = []
placement_options = [0, 1, 2, -1]
def new_zombie():
placement_level = random.choice(placement_options)
z = vector(200, placement_level*100)
print(placement_level)
zombies.append(z)
def new_defense():
global defense_x1
global defense_x2
global defense_x3
global defense_x4
i = input("select a number 1-4 on the keyboard. ")
if i == "1":
c = vector(defense_x1, 200)
goto(c.x, c.y)
dot(20, 'green')
defense_x1 += 21
elif i == "2":
c = vector(defense_x2, 100)
goto(c.x, c.y)
dot(20, 'green')
defense_x2 += 21
elif i == "3":
c = vector(defense_x3, 0)
goto(c.x, c.y)
dot(20, 'green')
defense_x3 += 21
elif i == "4":
c = vector(defense_x4, -100)
goto(c.x, c.y)
dot(20, 'green')
defense_x4 += 21
defenses.append(c)
def shooting():
for defense in defenses:
d = vector(defense.x, defense.y)
goto(d.x, d.y)
dot(5, 'blue')
attacks.append(d)
def contact():
for z in zombies:
for a in attacks:
if a.x >= z.x and a.y == z.y:
zombies.remove(z)
attacks.remove(a)
elif a.x > 200:
attacks.remove(a)
def drawing():
clear()
for zombie in zombies:
goto(zombie.x, zombie.y)
dot(20, 'red')
for defense in defenses:
goto(defense.x, defense.y)
dot(20, 'green')
for attack in attacks:
goto(attack.x, attack.y)
dot(5, 'blue')
update()
def movement():
global l
for zombie in zombies:
zombie.x -= 0.1
drawing()
contact()
for z in zombies:
if z.x < -200:
done()
for a in attacks:
if a.x > 200:
attacks.remove(a)
a.x += 0.5
l += 1
if l == 1300:
print("Firing Weapons!")
l = 0
shooting()
if random.randint(1, 350) == 2:
new_zombie()
if random.randint(1, 900) == 2:
print("You can build another defense module!")
new_defense()
movement()
def gameplay():
setup(420, 420, 370, 0)
hideturtle()
tracer(False)
up()
new_zombie()
movement()
gameplay()
而代码向量的使用是在下面这里, 如果你想看看。
import collections.abc
import math
import os
def path(filename):
filepath = os.path.realpath(__file__)
dirpath = os.path.dirname(filepath)
fullpath = os.path.join(dirpath, filename)
return fullpath
def line(a, b, x, y):
turtle.up()
turtle.goto(a, b)
turtle.down()
turtle.goto(x, y)
class vector(collections.abc.Sequence):
precision = 6
__slots__ = ('_x', '_y', '_hash')
def __init__(self, x, y):
self._hash = None
self._x = round(x, self.precision)
self._y = round(y, self.precision)
@property
def x(self):
return self._x
@x.setter
def x(self, value):
if self._hash is not None:
raise ValueError('Cannot set x after hashing')
self._x = round(value, self.precision)
@property
def y(self):
return self._y
@y.setter
def y(self, value):
if self._hash is not None:
raise ValueError('Cannot set y after hashing')
self._y = round(value, self.precision)
def __hash__(self):
#v.__hash__() -> hash(v)
if self._hash is None:
pair = (self.x, self.y)
self._hash = hash(pair)
return self._hash
def __len__(self):
return 2
def __getitem__(self, index):
if index == 0:
return self.x
elif index == 1:
return self.y
else:
raise IndexError
def copy(self):
type_self = type(self)
return type_self(self.x, self.y)
def __eq__(self, other):
if isinstance(other, vector):
return self.x == other.x and self.y == other.y
return NotImplemented
def __ne__(self, other):
if isinstance(other, vector):
return self.x != other.x and self.y != other.y
return NotImplemented
def __iadd__(self, other):
if self._hash is not None:
raise ValueError("Cannot add vector after hashing.")
elif isinstance(other, vector):
self.x += other.x
self.y += other.y
else:
self.x += other
self.y += other
return self
def __add__(self, other):
copy = self.copy()
return copy.__iadd__(other)
__radd__ = __add__
def move(self, other):
self.__iadd__(other)
def __isub__(self, other):
if self._hash is not None:
raise ValueError("Cannot subtract vector after hashing.")
elif isinstance(other, vector):
self.x -= other.x
self.y -= other.y
else:
self.x -= other
self.y -= other
return self
def __sub__(self, other):
copy = self.copy()
return copy.__isub__(other)
def __imul__(self, other):
if self._hash is not None:
raise ValueError("Cannot multiply vector after hashing.")
elif isinstance(other, vector):
self.x *= other.x
self.y *= other.y
else:
self.x *= other
self.y *= other
return self
def __mul__(self, other):
copy = self.copy()
return copy.__imul__(other)
__rmul__ = __mul__
def scale(self, other):
self.__imul__(other)
def __itruediv__(self, other):
if self._hash is not None:
raise ValueError("Cannot divide vector after hashing.")
elif isinstance(other, vector):
self.x /= other.x
self.y /= other.y
else:
self.x /= other
self.y /= other
return self
def __truediv__(self, other):
copy = self.copy()
return copy.__itruediv__(other)
def __neg__(self):
copy = self.copy()
copy.x = -copy.x
copy.y = -copy.y
return copy
def __abs__(self):
return (self.x**2+self.y**2)**0.5
def rotate(self, angle):
if self._hash is not None:
raise ValueError("Cannot rotate vector after hashing.")
radians= angle*math.pi/180.0
cosine = math.cos(radians)
sine = math.sin(radians)
x = self.x
y = self.y
self.x = x*cosine - y*sine
self.y = y*cosine + x*sine
def __repr__(self):
type_self = type(self)
name = type_self.__name__
return '{} ({!r},{!r})'.format(name, self.x, self.y)
任何和所有的帮助都是感激的,也欢迎对我可能没有正确使用的函数或代码的其他见解。谢谢您的时间
检查了一下,它在调用movement()1469次后闪电退出。当然,我用str(random.randint(1,4))代替了输入,因为没有等待,也没有用户输入。
这应该是递归调用造成的。经过以下操作后,工作正常。
while True:
movement()