Python 蛇的身体没有正确地附在蛇身上

Python 蛇的身体没有正确地附在蛇身上,python,pygame,Python,Pygame,我终于想出了如何给我的蛇添加身体部位,但它们以一种不同寻常的方式添加。我已经为此奋斗了一段时间,终于成功了。但他们做得不对。看起来他们在后面附加了1个像素,而不是整个身体的长度。有人知道为什么吗 # Constants WIN_WIDTH = 500 WIN_HEIGHT = 600 HALF_WIN_WIDTH = WIN_WIDTH / 2 HALF_WIN_HEIGHT = WIN_HEIGHT / 2 FPS = 10 # Colors WHITE = (255, 255, 255)

我终于想出了如何给我的蛇添加身体部位,但它们以一种不同寻常的方式添加。我已经为此奋斗了一段时间,终于成功了。但他们做得不对。看起来他们在后面附加了1个像素,而不是整个身体的长度。有人知道为什么吗

# Constants
WIN_WIDTH = 500
WIN_HEIGHT = 600
HALF_WIN_WIDTH = WIN_WIDTH / 2
HALF_WIN_HEIGHT = WIN_HEIGHT / 2
FPS = 10

# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
DARK_GREEN = (0, 100, 0)
YELLOW = (255, 255, 0)

# Variables
screen = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
pygame.display.set_caption("Snake")
clock = pygame.time.Clock()
running = True


class Text:

    def __init__(self, x, y, size, font, color, text):
        self.x = x
        self.y = y
        self.size = size
        self.font = font
        self.color = color
        self.text = text

    def draw(self):
        self.my_font = pygame.font.SysFont(self.font, self.size)
        self.text_surface = self.my_font.render(self.text, True, self.color)
        screen.blit(self.text_surface, (self.x, self.y))


class Food:

    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.width = 25
        self.height = 25

    def draw(self):
        self.rect = (self.x, self.y, self.width, self.height)
        pygame.draw.rect(screen, BLUE, self.rect)

    def events(self):
        pass

    def update(self):
        pass


class Body:

    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.width = 25
        self.height = 25

    def draw(self):
        self.rect = pygame.Rect(self.x, self.y, self.width, self.height)
        pygame.draw.rect(screen, YELLOW, self.rect)


# Snake class
class Snake:

    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.width = 25
        self.height = 25
        self.direction = 1
        self.kill = False
        self.collide = False
        self.speed = 3
        self.score = 0
        self.bodies = []

    def draw(self):
        self.rect = pygame.Rect(self.x, self.y, self.width, self.height)
        pygame.draw.rect(screen, BLACK, self.rect)

    def events(self):
        # change direction on key press
        self.keys = pygame.key.get_pressed()

        if self.keys[pygame.K_UP] and self.direction != 3:
            self.direction = 1
        if self.keys[pygame.K_DOWN] and self.direction != 1:
            self.direction = 3
        if self.keys[pygame.K_LEFT] and self.direction != 2:
            self.direction = 4
        if self.keys[pygame.K_RIGHT] and self.direction != 4:
            self.direction = 2

        if self.rect.colliderect(food.rect):
            self.speed += 0.5
            food.x = random.randint(0, WIN_WIDTH)
            food.y = random.randint(0, WIN_HEIGHT)
            self.score += 5
            self.colliide = False
            self.bodies.append(Body(0, 0))

        # Move the end bodies first in reverse order
        for i in range(len(self.bodies)-1, 0, -1):
            x = snake.bodies[i-1].x
            y = snake.bodies[i-1].y
            snake.bodies[i].x = x
            snake.bodies[i].y = y
            snake.bodies[i].draw()

        # Move body 0 to where the head is
        if len(snake.bodies) > 0:
            x = snake.x
            y = snake.y
            snake.bodies[0].x = x
            snake.bodies[0].y = y
            snake.bodies[0].draw()


    def update(self):
        # move
        if self.direction == 1:
            self.y -= self.speed
        if self.direction == 2:
            self.x += self.speed
        if self.direction == 3:
            self.y += self.speed
        if self.direction == 4:
            self.x -= self.speed

        # if on edge of screen
        if self.rect.right > WIN_WIDTH:
            self.kill = True
        if self.x < 0:
            self.kill = True
        if self.y < 0:
            self.kill = True
        if self.rect.bottom > WIN_HEIGHT:
            self.kill = True


# Create the snake object
snake = Snake(HALF_WIN_WIDTH, HALF_WIN_HEIGHT)
food = Food(random.randint(0, WIN_WIDTH), random.randint(0, WIN_HEIGHT))

# Main Loop
while running:
    score_text = Text(220, 5, 40, 'arial', WHITE, f'Score: {snake.score}')
    # Draw
    screen.fill(DARK_GREEN)
    snake.draw()
    food.draw()
    score_text.draw()

    # Event handling
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    if snake.kill:
        running = False

    snake.events()

    # Update
    snake.update()
    food.update()
    clock.tick(60)
    pygame.display.update()
#常数
宽度=500
WIN_高度=600
一半宽度=宽度/2
半高=半高/2
FPS=10
#颜色
白色=(255,255,255)
黑色=(0,0,0)
红色=(255,0,0)
绿色=(0,255,0)
蓝色=(0,0255)
深绿色=(0,100,0)
黄色=(255,255,0)
#变数
screen=pygame.display.set_模式((赢得宽度,赢得高度))
pygame.display.set_标题(“蛇”)
clock=pygame.time.clock()
运行=真
类文本:
定义初始化(self、x、y、大小、字体、颜色、文本):
self.x=x
self.y=y
self.size=大小
self.font=font
self.color=颜色
self.text=文本
def牵引(自):
self.my_font=pygame.font.SysFont(self.font,self.size)
self.text\u surface=self.my\u font.render(self.text,True,self.color)
屏幕blit(self.text_表面,(self.x,self.y))
类别食物:
定义初始化(self,x,y):
self.x=x
self.y=y
自宽=25
自我高度=25
def牵引(自):
self.rect=(self.x、self.y、self.width、self.height)
pygame.draw.rect(屏幕,蓝色,self.rect)
def事件(自):
通过
def更新(自我):
通过
类别团体:
定义初始化(self,x,y):
self.x=x
self.y=y
自宽=25
自我高度=25
def牵引(自):
self.rect=pygame.rect(self.x、self.y、self.width、self.height)
pygame.draw.rect(屏幕,黄色,self.rect)
#蛇类
蛇类:
定义初始化(self,x,y):
self.x=x
self.y=y
自宽=25
自我高度=25
自我定向=1
self.kill=False
self.collide=False
自身速度=3
self.score=0
self.body=[]
def牵引(自):
self.rect=pygame.rect(self.x、self.y、self.width、self.height)
pygame.draw.rect(屏幕,黑色,self.rect)
def事件(自):
#按键改变方向
self.keys=pygame.key.get_pressed()
如果self.keys[pygame.K_UP]和self.direction!=三:
自我定向=1
如果self.keys[pygame.K_DOWN]和self.direction!=1:
自我定向=3
如果self.keys[pygame.K_LEFT]和self.direction!=2:
自我定向=4
如果self.key[pygame.K_RIGHT]和self.direction!=4:
自我定向=2
如果self.rect.collide rect(food.rect):
自身速度+=0.5
food.x=random.randint(0,WIN\u宽度)
food.y=random.randint(0,赢得高度)
自我评分+=5
self.collide=False
self.Body.append(Body(0,0))
#先按相反顺序移动末端实体
对于范围内的i(len(self.body)-1,0,-1):
x=snake.body[i-1].x
y=snake.body[i-1].y
snake.body[i].x=x
snake.body[i].y=y
snake.body[i].draw()
#将主体0移动到头部所在的位置
如果len(蛇体)>0:
x=snake.x
y=蛇
snake.body[0].x=x
snake.body[0].y=y
snake.bodies[0].draw()
def更新(自我):
#移动
如果self.direction==1:
self.y-=自速度
如果self.direction==2:
self.x+=自速度
如果self.direction==3:
self.y+=自速度
如果self.direction==4:
self.x-=自速度
#如果在屏幕边缘
如果self.rect.right>WIN_WIDTH:
self.kill=True
如果self.x<0:
self.kill=True
如果self.y<0:
self.kill=True
如果self.rect.bottom>WIN_HEIGHT:
self.kill=True
#创建snake对象
蛇=蛇(半宽半高)
食物=食物(random.randint(0,宽度),random.randint(0,高度))
#主回路
运行时:
score_text=text(220,5,40,'arial',WHITE,f'score:{snake.score}')
#画
屏幕填充(深绿色)
snake.draw()
食物
分数\文本绘制()
#事件处理
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
sys.exit()
如果是snake.kill:
运行=错误
snake.events()
#更新
snake.update()
食物(修订)
时钟滴答(60)
pygame.display.update()

多谢各位

您必须跟踪蛇遇到的位置。将列表属性
self.position
添加到类
Snake

类蛇:
定义初始化(self,x,y):
# [...]
self.positions=[(self.x,self.y)]
# [...]
当蛇移动时,将新位置添加到列表中:

类蛇:
# [...]
def更新(自我):
#移动
如果self.direction==1:
self.y-=自速度
如果self.direction==2:
self.x+=自速度
如果self.direction==3:
self.y+=自速度
如果self.direction==4:
self.x-=自速度
#添加ne位置
如果self.x!=self.positions[0][0]或self.y!=自身位置[0][1]:
self.positions.insert(0,(self.x,self.y))
沿
事件中存储的位置更新车身的
x
y
坐标。定义车身各部分之间的距离(例如35)。并使用方法
getPos
通过索引获取零件的位置:

类蛇:
# [...]
def事件(自):
#按键改变方向
self.keys=pygame.key.g