科学教育

标题: 帮我生成个贪吃蛇代码 [打印本页]

作者: 项芯悦    时间: 2025-11-4 08:53
标题: 帮我生成个贪吃蛇代码
帮我生成个贪吃蛇代码

作者: kaia    时间: 2025-11-4 08:53
当然可以!贪吃蛇是一个经典的游戏,我们可以用Python语言来实现它。下面是一个简单的贪吃蛇代码示例:

  1. import pygame
  2. import sys
  3. import random

  4. pygame.init()

  5. # 设置屏幕大小和颜色
  6. screen_width = 600
  7. screen_height = 400
  8. cell_size = 20
  9. screen = pygame.display.set_mode((screen_width, screen_height))
  10. pygame.display.set_caption('贪吃蛇')

  11. # 定义颜色
  12. white = (255, 255, 255)
  13. black = (0, 0, 0)
  14. red = (255, 0, 0)

  15. # 定义贪吃蛇和食物
  16. snake = [(100, 100), (80, 100), (60, 100)]
  17. snake_head = snake[0]
  18. food = (random.randint(0, screen_width // cell_size - 1) * cell_size,
  19.         random.randint(0, screen_height // cell_size - 1) * cell_size)
  20. food_color = red

  21. # 定义方向
  22. direction = 'RIGHT'

  23. clock = pygame.time.Clock()

  24. def draw_objects():
  25.     screen.fill(white)
  26.     for pos in snake:
  27.         pygame.draw.rect(screen, black, (pos[0], pos[1], cell_size, cell_size))
  28.     pygame.draw.rect(screen, food_color, (food[0], food[1], cell_size, cell_size))

  29. def move_snake():
  30.     global direction
  31.     x, y = snake_head
  32.     if direction == 'UP':
  33.         y -= cell_size
  34.     elif direction == 'DOWN':
  35.         y += cell_size
  36.     elif direction == 'LEFT':
  37.         x -= cell_size
  38.     elif direction == 'RIGHT':
  39.         x += cell_size
  40.     new_head = (x, y)
  41.     snake.insert(0, new_head)
  42.     if snake_head != food:
  43.         snake.pop()
  44.     else:
  45.         create_food()

  46. def create_food():
  47.     global food
  48.     food = (random.randint(0, screen_width // cell_size - 1) * cell_size,
  49.             random.randint(0, screen_height // cell_size - 1) * cell_size)

  50. def check_collision():
  51.     if snake_head[0] < 0 or snake_head[0] >= screen_width or snake_head[1] < 0 or snake_head[1] >= screen_height:
  52.         return True
  53.     if snake_head in snake[1:]:
  54.         return True
  55.     return False

  56. while True:
  57.     for event in pygame.event.get():
  58.         if event.type == pygame.QUIT:
  59.             pygame.quit()
  60.             sys.exit()
  61.         elif event.type == pygame.KEYDOWN:
  62.             if event.key == pygame.K_UP and direction != 'DOWN':
  63.                 direction = 'UP'
  64.             elif event.key == pygame.K_DOWN and direction != 'UP':
  65.                 direction = 'DOWN'
  66.             elif event.key == pygame.K_LEFT and direction != 'RIGHT':
  67.                 direction = 'LEFT'
  68.             elif event.key == pygame.K_RIGHT and direction != 'LEFT':
  69.                 direction = 'RIGHT'

  70.     move_snake()
  71.     if check_collision():
  72.         pygame.quit()
  73.         sys.exit()

  74.     if snake_head == food:
  75.         create_food()

  76.     draw_objects()
  77.     pygame.display.update()
  78.     clock.tick(10)
复制代码


这是一个简单的贪吃蛇游戏代码,通过键盘上的方向键控制蛇的移动方向,吃到食物就会变长。希望对你有帮助!
作者: 项芯悦3    时间: 2025-11-4 09:01
帮我把这个代码改成蓝色:import pygame
import sys
import random

pygame.init()

# 设置屏幕大小和颜色
screen_width = 600
screen_height = 400
cell_size = 20
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('贪吃蛇')

# 定义颜色
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)

# 定义贪吃蛇和食物
snake = [(100, 100), (80, 100), (60, 100)]
snake_head = snake[0]
food = (random.randint(0, screen_width // cell_size - 1) * cell_size,
        random.randint(0, screen_height // cell_size - 1) * cell_size)
food_color = red

# 定义方向
direction = 'RIGHT'

clock = pygame.time.Clock()

def draw_objects():
    screen.fill(white)
    for pos in snake:
        pygame.draw.rect(screen, black, (pos[0], pos[1], cell_size, cell_size))
    pygame.draw.rect(screen, food_color, (food[0], food[1], cell_size, cell_size))

def move_snake():
    global direction
    x, y = snake_head
    if direction == 'UP':
        y -= cell_size
    elif direction == 'DOWN':
        y += cell_size
    elif direction == 'LEFT':
        x -= cell_size
    elif direction == 'RIGHT':
        x += cell_size
    new_head = (x, y)
    snake.insert(0, new_head)
    if snake_head != food:
        snake.pop()
    else:
        create_food()

def create_food():
    global food
    food = (random.randint(0, screen_width // cell_size - 1) * cell_size,
            random.randint(0, screen_height // cell_size - 1) * cell_size)

def check_collision():
    if snake_head[0] < 0 or snake_head[0] >= screen_width or snake_head[1] < 0 or snake_head[1] >= screen_height:
        return True
    if snake_head in snake[1:]:
        return True
    return False

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP and direction != 'DOWN':
                direction = 'UP'
            elif event.key == pygame.K_DOWN and direction != 'UP':
                direction = 'DOWN'
            elif event.key == pygame.K_LEFT and direction != 'RIGHT':
                direction = 'LEFT'
            elif event.key == pygame.K_RIGHT and direction != 'LEFT':
                direction = 'RIGHT'

    move_snake()
    if check_collision():
        pygame.quit()
        sys.exit()

    if snake_head == food:
        create_food()

    draw_objects()
    pygame.display.update()
    clock.tick(10)
作者: 项芯悦3    时间: 2025-11-4 09:06
帮我把这个代码字体颜色改成蓝色:import pygame
import sys
import random

pygame.init()

# 设置屏幕大小和颜色
screen_width = 600
screen_height = 400
cell_size = 20
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('贪吃蛇')

# 定义颜色
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)

# 定义贪吃蛇和食物
snake = [(100, 100), (80, 100), (60, 100)]
snake_head = snake[0]
food = (random.randint(0, screen_width // cell_size - 1) * cell_size,
        random.randint(0, screen_height // cell_size - 1) * cell_size)
food_color = red

# 定义方向
direction = 'RIGHT'

clock = pygame.time.Clock()

def draw_objects():
    screen.fill(white)
    for pos in snake:
        pygame.draw.rect(screen, black, (pos[0], pos[1], cell_size, cell_size))
    pygame.draw.rect(screen, food_color, (food[0], food[1], cell_size, cell_size))

def move_snake():
    global direction
    x, y = snake_head
    if direction == 'UP':
        y -= cell_size
    elif direction == 'DOWN':
        y += cell_size
    elif direction == 'LEFT':
        x -= cell_size
    elif direction == 'RIGHT':
        x += cell_size
    new_head = (x, y)
    snake.insert(0, new_head)
    if snake_head != food:
        snake.pop()
    else:
        create_food()

def create_food():
    global food
    food = (random.randint(0, screen_width // cell_size - 1) * cell_size,
            random.randint(0, screen_height // cell_size - 1) * cell_size)

def check_collision():
    if snake_head[0] < 0 or snake_head[0] >= screen_width or snake_head[1] < 0 or snake_head[1] >= screen_height:
        return True
    if snake_head in snake[1:]:
        return True
    return False

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP and direction != 'DOWN':
                direction = 'UP'
            elif event.key == pygame.K_DOWN and direction != 'UP':
                direction = 'DOWN'
            elif event.key == pygame.K_LEFT and direction != 'RIGHT':
                direction = 'LEFT'
            elif event.key == pygame.K_RIGHT and direction != 'LEFT':
                direction = 'RIGHT'

    move_snake()
    if check_collision():
        pygame.quit()
        sys.exit()

    if snake_head == food:
        create_food()

    draw_objects()
    pygame.display.update()
    clock.tick(10)




欢迎光临 科学教育 (http://www.sci-edu.net/) Powered by Discuz! X3.2