【问题标题】:(Python Snake game) For loop doesn't draw snake(Python Snake 游戏)For 循环不画蛇
【发布时间】:2020-01-29 00:26:04
【问题描述】:

这是一个小问题,一直在用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()

【问题讨论】:

  • 这里是否不需要使用 global 关键字,因为上面已经声明了变量?

标签: python python-3.x pygame


【解决方案1】:

您根本不需要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()

【讨论】:

    【解决方案2】:

    在您的代码中,您将length 初始化为0。以这个简化的代码为例,它清楚地显示了您的问题。

    length = 0
    for i in range(length):
        print(i)
    

    这个 sn-p 不产生任何输出,因为 range 给出了所有整数,但不包括 stop 参数。

    要解决您的问题,请将您的 range 更改为 range(length+1)

    Code Review 站点是获取有关工作代码的改进建议的合适场所。您将获得有关性能、评论、可读性和风格等方面的建议。

    请记住您可能会得到的建议是重构您的代码并将length 初始化为1。尽管0 可能对列表索引很方便,但它并不能准确地反映蛇的长度,有时会导致类似于您在这个问题中遇到的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-17
      • 2017-06-18
      • 1970-01-01
      • 2014-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多