当我使用list.pop()方法剪切列表中的最后一项时,为什么我的实例不会“弹出”?

时间:2011-08-08 22:43:52

标签: debugging pygame

这是一个pygame问题,该列表基本上保存了用户所处的位置。但是,它不是长度为“x”的蛇,而是从用户启动的任何地方继续运行。

找到下面发布的代码,我认为问题主要在于放置流行音乐的位置,但我无法确定。如果您这么认为,只需按CTRL + F“pop”

import pygame    
running = 1
screen = pygame.display.set_mode((640,480))
clock = pygame.time.Clock()

UP = (0,-1)
DOWN = (0,1)
LEFT = (-1,0)
RIGHT = (1,0)
#Creating a worm
class WormFromTheFuture:
    def __init__(self,initial_x,initial_y,wormlength):
        self.x = initial_x
        self.y = initial_y
        self.length = wormlength
        self.body = []
        self.dirx = 1
        self.diry = 1
    def worm_assignment (self, arbtry):
        self.dirx, self.diry = arbtry
    def worm_moves (self):
        self.x += self.dirx
        self.y += self.diry
    def worm_draw (self, surface):
        self.body.insert(0, (self.x,self.y))
        if len(self.body) > self.length: 
            self.body.pop()
        for x,y in self.body: 
            surface.set_at((x,y), (255,255,255))

wormy = WormFromTheFuture (320,240,10)
while running:
    wormy.worm_draw(screen)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = 0
            pygame.quit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP: wormy.worm_assignment (UP)
            if event.key == pygame.K_DOWN: wormy.worm_assignment(DOWN)
            if event.key == pygame.K_RIGHT: wormy.worm_assignment(RIGHT)
            if event.key == pygame.K_LEFT: wormy.worm_assignment (LEFT)  
    wormy.worm_moves()

    clock.tick(100)
    pygame.display.flip()

2 个答案:

答案 0 :(得分:3)

您可以使用以下功能清除黑屏:

def clear(self, screen):
    screen.fill((0,0,0))

在发生任何其他绘图之前调用它:

wormy.clear(screen)
wormy.worm_draw(screen)

答案 1 :(得分:2)

我认为你可能会忘记挖出正在弹出的蠕虫部分。