gpt4 book ai didi

python - 蛇和苹果不对齐

转载 作者:太空宇宙 更新时间:2023-11-03 14:39:24 27 4
gpt4 key购买 nike

我正在学习如何使用 Python 3.6 的 PyGame 模块,在获取知识的过程中,我决定创建一个贪吃蛇游戏。我的游戏代码如下,除了蛇和苹果没有对齐之外,一切似乎都工作正常,如果它们没有对齐,吃苹果就非常困难。 (如果有帮助的话,它们仅相差几个像素)

# Imports
import pygame
import random

# Colors
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)

# Setup
pygame_init = pygame.init()
dis_width = 1000
dis_height = 600
window = pygame.display.set_mode((dis_width, dis_height))
pygame.display.set_caption("Slither")
window.fill(white)
fps = 10
clock = pygame.time.Clock()
size = 25
apple = [[0, 0]]
snake = [[0, 0]]
x_step, y_step = 0, 0
apples_ate = 0

# Game Loop
main_menu = True
play_game = False
death_screen = False
while True:
# Main Menu
while main_menu:
# Setup
snake = [[(dis_width - 200) / 2, dis_height / 2, size]]
apple = [[random.randrange(0, dis_width - 200 - size), random.randrange(0, dis_height - size), size]]

# Message
x_step, y_step = 0, 0
window.fill(black, rect=[0, 0, dis_width, dis_height])
font = pygame.font.SysFont(None, 50)
window.blit(font.render("Press 'Space' To Continue", True, green), [100, 200])
pygame.display.update()

# Event Handler
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
main_menu = False
play_game = True

# Play Menu
while play_game:

# Event Handler
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
y_step = -size
x_step = 0
elif event.key == pygame.K_DOWN:
y_step = size
x_step = 0
elif event.key == pygame.K_LEFT:
x_step = -size
y_step = 0
elif event.key == pygame.K_RIGHT:
x_step = size
y_step = 0

# Boundries
if snake[0][0] + x_step < 0 or snake[0][0] + x_step > dis_width - size - 200:
play_game = False
death_screen = True
elif snake[0][1] + y_step < 0 or snake[0][1] + y_step > dis_height - size:
play_game = False
death_screen = True
else:
snake[0][0] += x_step
snake[0][1] += y_step

# Apple Test
if apple[0][0] == snake[0][0]:
apple = [[random.randrange(0, dis_width - 200 - size),
random.randrange(0, dis_height - size), size]]
apples_ate += 1

# Draws
window.fill(white, rect=[dis_width - 200, 0, 200, dis_height])
window.fill(black, rect=[0, 0, dis_width - 200, dis_height])
font = pygame.font.SysFont(None, 50)
window.blit(font.render("SCORE:", True, black), [dis_width - 200, 0])
window.blit(font.render(str(apples_ate), True, black), [dis_width - 200, 50])
pygame.draw.rect(window, red, [apple[0][0], apple[0][1], size, size])
pygame.draw.rect(window, green, [snake[0][0], snake[0][1], size, size])
pygame.display.update()
clock.tick(fps)

# Death Screen
while death_screen:
# Message
window.fill(black, rect=[0, 0, dis_width - 200, dis_height])
font = pygame.font.SysFont(None, 50)
window.blit(font.render("You died.", True, red), [100, 100])
window.blit(font.render("Press 'A' To Play Again.", True, red), [100, 200])
window.blit(font.render("Press 'Q' To Quit", True, red), [100, 300])
pygame.display.update()

# Event Handler
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
main_menu = True
death_screen = False
elif event.key == pygame.K_q:
pygame.quit()
quit()

最佳答案

您的错误位于 apple = [[random.randrange(0, dis_width - 200 - size), random.randrange(0, dis_height - size), size]]。苹果位置可以是 (0,800) 内的任何值。所以它可以是12 835 125,而你的蛇位于(400,300)即(dis_width/2 -200,dis_height)。并且您将 x_step 指定为 +/-25,size 这是蛇的运动。
因此,您的蛇位置会减少或增加 25。0,25,50,.....,375,400,425,.....,750,775。如果你的苹果位置是 125 或 275 [125 % 25 = 0] 就没有问题。
但是如果你的苹果位置是 122 Boom... if apple[0][0] == Snake[0][0]: 你的蛇永远不能吃他最喜欢的东西水果。解决方案是在 randrange 函数中添加步骤 apple = [[random.randrange(0, dis_width - 200 - size,size), random.randrange(0, dis_height - size,size), size]]这将在范围内生成随机数0,25,50,.....,725,750,775(我不知道如果生成解决的话它也会生成800)。

还有其他小错误if apple[0][0] == Snake[0][0]:你也必须匹配蛇位置的x和y,编辑它如果 apple[0][0] == Snake[0][0] 且 apple[0][1] == Snake[0][1]: .

关于python - 蛇和苹果不对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46651589/

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