全部,
如果有帮助,我可以发布我的其余代码。我有一个 pygame 版本的 Asteroids,但我使用的是文本文件中单词的直方图。我正在创建一个字典 [hist],其中单词作为键,出现次数作为值。
当我创建我的单词实例时,我想使用单词和值。我尝试在循环中添加另一个循环for w in range(len(hist))
,现在进行递归,但是通过递归,我没有得到任何值,而在另一个循环中,我只得到每个 w 实例的最后一个键和值。
class Word(object):
def __init__(self, color, pos, radius, speed, heading, term, font, screen):
self.color = color
self.x = pos[0]
self.y = pos[1]
self.radius = radius
self.speed = speed
self.heading = heading
self.term = term
self.font = font
self.screen = screen
def draw(self, surface):
pygame.draw.circle(surface, self.color, (int(self.x),int(self.y)), self.radius)
hist_text = self.font.render(self.term, True, (0, 128, 0))
self.screen.blit(hist_text, (int(self.x) - hist_text.get_width() // 2, int(self.y) - hist_text.get_height() // 2))
def move(self):
self.x += self.speed * math.cos(self.heading)
self.y += self.speed * math.sin(self.heading)
def checkBounds(self):
""" wrap around screen """
if self.x > width:
self.x = 0
if self.x < 0:
self.x = width
if self.y > height:
self.y = 0
if self.y < 0:
self.y = height
def first(lst):
if lst == []:
return 0
else:
print(lst[0])
return first(lst[1:])
for ele in hist:
term_keys = hist.keys()
sizes = hist.values()
for w in range(len(hist)):
color = (220,0,0)
radius = 33
for ele in sizes:
radius = 3 * ele
font = pygame.font.Font(None, 33 * int(ele))
print radius
x = 0
y = 0
speed = random.randint(1,5)
heading = random.uniform(0, math.pi*2)
for ele in term_keys:
term = ele
screen = screen
w = Word(color, (x, y), radius, speed, heading, term, font, screen)
words.append(w)