Python 用pygame制作蛇。当蛇触碰库克岛时,很难让它变长

Python 用pygame制作蛇。当蛇触碰库克岛时,很难让它变长,python,pygame,Python,Pygame,我正试图在pygame中制作一个蛇游戏,但是一旦蛇碰到点,我就很难让蛇变长。当我触摸圆点时,游戏有时会冻结,偶尔会出现记忆错误 而且,当我成功地吃掉了圆点时,蛇不会再长了 任何帮助都将不胜感激。 谢谢大家! 我的代码: import pygame import sys import random pygame.init() width = 800 height = 800 snake_pos = [width/2, height/2] snake_list = [] color_red =

我正试图在pygame中制作一个蛇游戏,但是一旦蛇碰到点,我就很难让蛇变长。当我触摸圆点时,游戏有时会冻结,偶尔会出现记忆错误

而且,当我成功地吃掉了圆点时,蛇不会再长了

任何帮助都将不胜感激。 谢谢大家!

我的代码:

import pygame
import sys
import random

pygame.init()

width = 800
height = 800
snake_pos = [width/2, height/2]
snake_list = []
color_red = (255, 0, 0)
snake_size = 20
game_over = False
screen = pygame.display.set_mode((width, height))
cookie_pos = [random.randint(0, width), random.randint(0, height)]
cookie_size = 10
color_white = (255, 255, 255)
cookie = []
direction = ''

game_over = False

def draw_snake():
    for snake in snake_list:
        pygame.draw.rect(screen, color_red, (snake[0], snake[1], snake_size, snake_size))

    if not snake_list:
        snake_list.append([snake_pos[0], snake_pos[1]])

def create_snake(direction):
    length = len(snake_list)
    for snake in snake_list:
        if direction == 'left':
            snake_list.append([snake[0] + (length * snake_size), snake[1]])
        elif direction == 'right':
            snake_list.append([snake[0] - (length * snake_size), snake[1]])
        elif direction == 'top':
            snake_list.append([snake[0], snake[1] + (length * snake_size)])
        elif direction == 'bottom':
            snake_list.append([snake[0], snake[1] - (length * snake_size)])

def create_cookie():
    cookie.append([random.randint(0, width), random.randint(0, height)])
    draw_cookie()

def draw_cookie():
    for cookie_pos in cookie:
        pygame.draw.rect(screen, color_white, (cookie_pos[0], cookie_pos[1], cookie_size, cookie_size))

def check_cookie(direction):
    for snake_pos in snake_list:
        for cookie_pos in cookie:
            p_x = snake_pos[0]
            p_y = snake_pos[1]

            e_x = cookie_pos[0]
            e_y = cookie_pos[1]

            if e_x >= p_x and e_x < (p_x + snake_size) or p_x >= e_x and p_x < (e_x + cookie_size):
                if e_y >= p_y and e_y < (p_y + snake_size) or p_y >= e_y and p_y < (e_y + cookie_size):
                    cookie.pop(0)
                    create_cookie()
                    create_snake(direction)

    if not cookie:
        cookie.append([random.randint(0, width), random.randint(0, height)])

def update_snake():
    pass

def move_snake(direction):
    keys = pygame.key.get_pressed()
    for snake_pos in snake_list:
        if keys[pygame.K_LEFT]:
            direction = 'left'
            snake_pos[0] -= 0.2
        if keys[pygame.K_RIGHT]:
            direction = 'right'
            snake_pos[0] += 0.2
        if keys[pygame.K_UP]:
            direction = 'up'
            snake_pos[1] -= 0.2
        if keys[pygame.K_DOWN]:
            direction = 'down'
            snake_pos[1] += 0.2
    screen.fill((0,0,0))
    return direction

def main_game(direction):

    while not game_over:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

        draw_snake()
        check_cookie(direction)
        draw_cookie()
        pygame.display.update()
        direction = move_snake(direction)

main_game(direction)
导入pygame
导入系统
随机输入
pygame.init()
宽度=800
高度=800
snake_pos=[宽度/2,高度/2]
snake_list=[]
颜色\红色=(255,0,0)
蛇的大小=20
游戏结束=错误
screen=pygame.display.set_模式((宽度、高度))
cookie_pos=[random.randint(0,宽度),random.randint(0,高度)]
cookie_size=10
颜色\白色=(255、255、255)
cookie=[]
方向=“”
游戏结束=错误
def draw_snake():
对于snake\u列表中的snake:
pygame.draw.rect(屏幕,红色,(蛇[0],蛇[1],蛇大小,蛇大小))
如果不是,请列出:
snake_list.append([snake_pos[0],snake_pos[1]]))
def创建_蛇(方向):
长度=长度(蛇形列表)
对于snake\u列表中的snake:
如果方向==“左”:
snake_list.append([snake[0]+(长度*snake_大小),snake[1])
elif方向==‘右’:
snake_list.append([snake[0]-(长度*snake_大小),snake[1])
elif方向==“顶部”:
snake_list.append([snake[0],snake[1]+(长度*snake_大小)])
elif方向==“底部”:
snake_list.append([snake[0],snake[1]-(长度*snake_大小)])
def create_cookie():
cookie.append([random.randint(0,宽度),random.randint(0,高度)])
画
def draw_cookie():
对于cookie中的cookie\u pos:
pygame.draw.rect(屏幕,颜色为白色,(cookie\u位置[0],cookie\u位置[1],cookie\u大小,cookie\u大小))
def检查(方向):
对于snake_列表中的snake_位置:
对于cookie中的cookie\u pos:
p_x=蛇_位置[0]
p_y=蛇位置[1]
e_x=cookie_位置[0]
e_y=cookie_pos[1]
如果e_x>=p_x和e_x<(p_x+蛇_大小)或p_x>=e_x和p_x<(e_x+饼干_大小):
如果e_y>=p_y和e_y<(p_y+蛇大小)或p_y>=e_y和p_y<(e_y+饼干大小):
cookie.pop(0)
创建_cookie()
创建_snake(方向)
如果不是cookie:
cookie.append([random.randint(0,宽度),random.randint(0,高度)])
def更新_snake():
通过
def移动_蛇(方向):
keys=pygame.key.get_pressed()
对于snake_列表中的snake_位置:
如果键[pygame.K_左]:
方向=‘左’
snake_位置[0]-=0.2
如果键[pygame.K_RIGHT]:
方向=‘右’
snake_pos[0]+=0.2
如果键[pygame.K_UP]:
方向=‘向上’
snake_位置[1]-=0.2
如果键[pygame.K_DOWN]:
方向=‘向下’
snake_pos[1]+=0.2
屏幕填充((0,0,0))
返回方向
def main_游戏(方向):
虽然游戏尚未结束:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
sys.exit()
画蛇
检查cookie(方向)
画
pygame.display.update()
方向=移动(方向)
主游戏(方向)

您的
创建snake
有两个问题:

def create_snake(方向):
长度=长度(蛇形列表)
对于snake\u列表中的snake:
如果方向==“左”:
new_snake=[snake[0]+(长度*snake_大小),snake[1]]
elif方向==‘右’:
new_snake=[snake[0]-(长度*snake_大小),snake[1]]
elif方向==“向上”:
新的蛇=[snake[0],snake[1]+(长度*蛇大小)]
elif方向==“向下”:
new_snake=[snake[0],snake[1]-(长度*snake_大小)]
snake\u list.append(新的snake)
您的方向检查有缺陷:您将方向设置为左/右/上/下,但随后检查方向是否为左/右/上/下

更重要的是,您在遍历列表时将其追加,这将创建一个无限循环。通过创建一个tmp变量,
new\u snake
,然后附加它,可以解决这个问题。(这可能仍然不是您想要的——您试图渲染蛇的方式似乎有问题,我鼓励您重新思考)


祝你好运

如果要写入全局变量,则必须使用。
使用此选项在事件循环中设置
游戏结束
。进一步注意,应在主回路中清除显示:

def main_游戏(方向):
全球游戏结束
虽然游戏尚未结束:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
游戏结束=正确
#更新snake和Cookie
方向=移动(方向)
检查cookie(方向)
#画场景
屏幕填充(0)
画蛇
画
pygame.display.update()
蛇的示教部分大小为
snake\u size=20
。但是蛇每帧移动
0.2
。因此,无法在上一帧的位置上绘制蛇的第二部分,因为到上一帧位置的距离为
0.2
。这几乎是相同的位置,并将导致几乎完全覆盖零件。
蛇的第二部分的正确位置是头部在100(=20/0.2)帧前的位置

在列表中跟踪蛇的所有位置:

def move_snake(方向):
keys=pygame.key.get_pressed()
如果键[pygame.K_左]:
方向=‘左’
如果键[pygame.K_RIGHT]:
方向=‘右’
如果键[pygame.K_UP]:
方向=‘向上’
如果键[pygame.K_DOWN]:
方向=‘向下’
如有需要,请列出: