这是一个自作自受的问题,列表基本上包含了用户所处的位置。然而,它并不是一条长度为"x“的蛇,而是从用户开始的地方不停地往前走。
找到下面发布的代码,我认为问题主要是pop放置的位置,但我不能确定。如果您这样认为,只需按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()发布于 2011-08-09 09:08:53
您可以使用如下函数清除黑屏:
def clear(self, screen):
screen.fill((0,0,0))并在任何其他绘制发生之前调用它:
wormy.clear(screen)
wormy.worm_draw(screen)发布于 2011-08-09 06:51:34
我想你可能忘了解开正在弹出的那部分蠕虫了。
https://stackoverflow.com/questions/6989507
复制相似问题