gpt4 book ai didi

python - (Python贪吃蛇游戏)for循环不画蛇

转载 作者:行者123 更新时间:2023-12-02 02:40:27 26 4
gpt4 key购买 nike

这是一个小问题,一直在用 python 开发贪吃蛇游戏,它还没有完成,只有一件事我想知道。我最初在 move_snake 函数中有代码 pygame.draw.rect(SCREEN, GREEN, (x_pos[i], y_pos[i], WIDTH, HEIGHT))。当我那样画蛇时,代码画得很好,但是现在我正在尝试培养蛇,所以我将它移到了 grow_snake 函数的 for 循环中。它不会再画蛇了,但它仍然存在,显然我按下按键它最终会碰到边界并结束游戏。只是想知道为什么它不再画蛇了。谢谢你。我是编程新手,所以如果您想查看代码并给我一些改进方法的提示,我们将不胜感激。请记住,这是一项正在进行的工作,因此游戏中有许多明显的部分缺失,我将在稍后添加。

# Snake game

import pygame
import random


pygame.init()
pygame.display.set_caption("Snake Game and AI")

WIDTH = 24
HEIGHT = 24
SCREEN = pygame.display.set_mode((500, 500))
RED = (255, 0, 0)
BLACK = (0, 0, 0)
GREEN = (0, 128, 0)
WHITE = (255, 255, 255)
SPEED = 25
x_head = 251
y_head = 251
direction = None
apple_x = random.randrange(26, 476, 25)
apple_y = random.randrange(26, 476, 25)
length = 0
x_pos = [x_head]
y_pos = [y_head]


def grid():
for x in range(25, 500, 25):
pygame.draw.rect(SCREEN, WHITE, (x, 25, 1, 450))

for y in range(25, 500, 25):
pygame.draw.rect(SCREEN, WHITE, (25, y, 450, 1))


def press_key():
global direction
global keys
if keys[pygame.K_RIGHT] and direction != 'left':
direction = 'right'
if keys[pygame.K_LEFT] and direction != 'right':
direction = 'left'
if keys[pygame.K_UP] and direction != 'down':
direction = 'up'
if keys[pygame.K_DOWN] and direction != 'up':
direction = 'down'


def move_snake():
global x_head
global y_head
global SCREEN
global WIDTH
global HEIGHT
if direction == 'right':
x_head += SPEED
if direction == 'left':
x_head -= SPEED
if direction == 'up':
y_head -= SPEED
if direction == 'down':
y_head += SPEED


def eat_apple():
global apple_x
global apple_y
global length
if x_head == apple_x and y_head == apple_y:
apple_x = random.randrange(26, 476, 25)
apple_y = random.randrange(26, 476, 25)
if direction == 'right':
pass
if direction == 'left':
pass
if direction == 'up':
pass
if direction == 'down':
pass
length += 1
pygame.draw.rect(SCREEN, RED, (apple_x, apple_y, WIDTH, HEIGHT))


def grow_snake():
move_snake()
global x_pos
global y_pos
for i in range(length):
pygame.draw.rect(SCREEN, GREEN, (x_pos[i], y_pos[i], WIDTH, HEIGHT))


def game_over():
global is_running
if x_head < 26 or x_head > 456 or y_head < 26 or y_head > 456:
is_running = False


is_running = True
while is_running:
pygame.time.delay(150)
for event in pygame.event.get():
if event.type == pygame.QUIT:
is_running = False

keys = pygame.key.get_pressed()
press_key()
move_snake()
SCREEN.fill(BLACK)
grid()
grow_snake()
eat_apple()
game_over()
pygame.display.update()


pygame.quit()

最佳答案

您根本不需要x_heady_headlength。蛇的位置存储在 x_posy_pos 列表中,使用它:
(zip() 聚合列表的元素)

def grow_snake():
for x, y in zip(x_pos, y_pos):
pygame.draw.rect(SCREEN, GREEN, (x, y, WIDTH, HEIGHT))

如果你想知道蛇的长度,你可以通过列表的长度得到它(例如 len(x_pos)。

要移动蛇,计算头部的新位置,将新位置添加到列表的头部并移除列表的尾部

def move_snake():
global SCREEN, WIDTH, HEIGHT, x_pos, y_pos

# compute new head
x_head, y_head = x_pos[0], y_pos[0]
if direction == 'right':
x_head += SPEED
if direction == 'left':
x_head -= SPEED
if direction == 'up':
y_head -= SPEED
if direction == 'down':
y_head += SPEED

# add new head at head of lists
x_pos = [x_head] + x_pos
y_pos = [y_head] + y_pos

# delete tail of snake
del x_pos[-1]
del y_pos[-1]

如果你吃一个苹果,在蛇的尾部处添加一个新元素:

def eat_apple():
global apple_x, apple_y, x_pos, y_pos
if x_pos[0] == apple_x and y_pos[0] == apple_y:
apple_x = random.randrange(26, 476, 25)
apple_y = random.randrange(26, 476, 25)
x_pos.append(x_pos[-1])
y_pos.append(y_pos[-1])
pygame.draw.rect(SCREEN, RED, (apple_x, apple_y, WIDTH, HEIGHT))

没有x_heady_headlength 的代码:

# Snake game

import pygame
import random


pygame.init()
pygame.display.set_caption("Snake Game and AI")

WIDTH = 24
HEIGHT = 24
SCREEN = pygame.display.set_mode((500, 500))
RED = (255, 0, 0)
BLACK = (0, 0, 0)
GREEN = (0, 128, 0)
WHITE = (255, 255, 255)
SPEED = 25
direction = None
apple_x = random.randrange(26, 476, 25)
apple_y = random.randrange(26, 476, 25)
x_pos = [251]
y_pos = [251]


def grid():
for x in range(25, 500, 25):
pygame.draw.rect(SCREEN, WHITE, (x, 25, 1, 450))

for y in range(25, 500, 25):
pygame.draw.rect(SCREEN, WHITE, (25, y, 450, 1))


def press_key():
global direction
global keys
if keys[pygame.K_RIGHT] and direction != 'left':
direction = 'right'
if keys[pygame.K_LEFT] and direction != 'right':
direction = 'left'
if keys[pygame.K_UP] and direction != 'down':
direction = 'up'
if keys[pygame.K_DOWN] and direction != 'up':
direction = 'down'


def move_snake():
global SCREEN, WIDTH, HEIGHT, x_pos, y_pos
x_head, y_head = x_pos[0], y_pos[0]
if direction == 'right':
x_head += SPEED
if direction == 'left':
x_head -= SPEED
if direction == 'up':
y_head -= SPEED
if direction == 'down':
y_head += SPEED
x_pos = [x_head] + x_pos
y_pos = [y_head] + y_pos
del x_pos[-1]
del y_pos[-1]


def eat_apple():
global apple_x, apple_y, x_pos, y_pos
if x_pos[0] == apple_x and y_pos[0] == apple_y:
apple_x = random.randrange(26, 476, 25)
apple_y = random.randrange(26, 476, 25)
x_pos.append(x_pos[-1])
y_pos.append(y_pos[-1])
pygame.draw.rect(SCREEN, RED, (apple_x, apple_y, WIDTH, HEIGHT))


def grow_snake():
for x, y in zip(x_pos, y_pos):
pygame.draw.rect(SCREEN, GREEN, (x, y, WIDTH, HEIGHT))


def game_over():
global is_running
if x_pos[0] < 26 or x_pos[0] > 456 or y_pos[0] < 26 or y_pos[0] > 456:
is_running = False


is_running = True
while is_running:
pygame.time.delay(150)
for event in pygame.event.get():
if event.type == pygame.QUIT:
is_running = False

keys = pygame.key.get_pressed()
press_key()
move_snake()
SCREEN.fill(BLACK)
grid()
grow_snake()
eat_apple()
game_over()
pygame.display.update()


pygame.quit()

关于python - (Python贪吃蛇游戏)for循环不画蛇,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59959029/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com