问题在Snake游戏Pygame中延伸尾部

时间:2017-02-08 03:13:53

标签: python pygame

我正在Pygame中写一个基本的Snake游戏,但蛇的身体在吃了4块食物后没有延伸,在第一口之后也没有延伸。我在每次咬之后创建一个新的body对象,并将其存储在列表中。 x和y变量用于存储头部移动前的位置,而后者又用作蛇身体第一段的坐标。然后我尝试迭代此列表,使用前一个主体段的x和y坐标创建新对象以创建尾部效果。为什么停止做空?

import pygame
from pygame.locals import *
import random
import sys

pygame.init()

FPS = 30
fpsClock = pygame.time.Clock()

WIN_WIDTH = 680 #width of window
WIN_HEIGHT = 500 #height of window

DISPLAY = (WIN_WIDTH, WIN_HEIGHT) #variable for screen display
DEPTH = 32 #standard
FLAGS = 0 #standard
BLACK = (0, 0, 0) #black
RED = (255, 0, 0) #red
GOLD = (255, 215, 0)
IDK = (178, 154, 96)

screen = pygame.display.set_mode(DISPLAY, FLAGS, DEPTH)
pygame.display.set_caption('Snake')                                      

snake_parts = [1]
Score = 0
speed = 10
snakex = 15
snakey = 70
size = 20
Snakes = 1

# --- classes ---
class Snake(pygame.Rect):
    def __init__(self, x, y, screen, size, colour):
        pygame.Rect.__init__(self, x, y, size, 20)
        self.screen = screen
        self.colour = colour
        self.x = x
        self.y = y


    def draw(self, screen):
        pygame.draw.rect(self.screen, self.colour, self)

    def coordinates(self):
        return self.x, self.y

class Food(pygame.Rect):
    def __init__(self, x, y, screen):
        pygame.Rect.__init__(self, x, y, 20, 20)
        self.screen = screen

    def draw(self, screen):
        pygame.draw.rect(self.screen, GOLD, self)



# --- functions ---
def get_food_pos(WIN_WIDTH, WIN_HEIGHT):
    WIN_WIDTH = random.randint(1, WIN_WIDTH)
    WIN_HEIGHT = random.randint(1, WIN_HEIGHT)
    return WIN_WIDTH, WIN_HEIGHT

eaten = True
pressed_right = True
pressed_left = False
pressed_up = False
pressed_down = False
pygame.key.set_repeat(10,10)


while True:
    screen.fill(BLACK)

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

        elif event.type == pygame.KEYDOWN:          # check for key presses          
            if event.key == pygame.K_LEFT:        # left arrow turns left
                pressed_left = True
                pressed_right = False
                pressed_up = False
                pressed_down = False
            elif event.key == pygame.K_RIGHT:     # right arrow turns right
                pressed_right = True
                pressed_left = False
                pressed_up = False
                pressed_down = False
            elif event.key == pygame.K_UP:        # up arrow goes up
                pressed_up = True
                pressed_right = False
                pressed_left = False
                pressed_down = False
            elif event.key == pygame.K_DOWN:     # down arrow goes down
                pressed_down = True
                pressed_right = False
                pressed_up = False
                pressed_left = False

    x = snakex
    y = snakey

    if pressed_left:
        snakex -= speed
    elif pressed_right:
        snakex += speed
    elif pressed_up:
        snakey -= speed
    elif pressed_down:
        snakey += speed

    snake_parts[0] = Snake(snakex, snakey, screen, int(size), IDK)
    snake_parts[0].draw(screen)



    if eaten:
        foodx, foody = get_food_pos(WIN_WIDTH, WIN_HEIGHT)
        eaten = False

    my_food = Food(foodx, foody, screen)
    my_food.draw(screen)

    if snake_parts[0].colliderect(my_food):
        eaten = True
        screen.fill(BLACK)
        a_snake = Snake(snakex, snakey, screen, int(size), RED)
        snake_parts.append(a_snake)

    if len(snake_parts) >= 1:
        for i in range(1, len(snake_parts)-1):
            tempx, tempy = snake_parts[i].coordinates()
            snake_parts[i] = Snake(x, y, screen, int(size), RED)
            snake_parts[i].draw(screen)
            snake_parts[i+1] = Snake(tempx, tempy, screen, int(size), RED)
            x, y = tempx, tempy

1 个答案:

答案 0 :(得分:0)

想出来,问题出在这一行:

snake_parts[i+1] = Snake(tempx, tempy, screen, int(size), RED)

将其移除并且工作正常。