科学教育

搜索
查看: 546|回复: 3
打印 上一主题 下一主题

帮我生成个贪吃蛇代码

[复制链接]

2

主题

3

帖子

23

积分

新手上路

Rank: 1

积分
23
跳转到指定楼层
楼主
发表于 2025-11-4 08:53:01 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
帮我生成个贪吃蛇代码
回复

使用道具 举报

0

主题

1145

帖子

1715

积分

版主

Rank: 7Rank: 7Rank: 7

积分
1715
沙发
发表于 2025-11-4 08:53:12 | 只看该作者
当然可以!贪吃蛇是一个经典的游戏,我们可以用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)
复制代码


这是一个简单的贪吃蛇游戏代码,通过键盘上的方向键控制蛇的移动方向,吃到食物就会变长。希望对你有帮助!
回复 支持 反对

使用道具 举报

0

主题

3

帖子

8

积分

新手上路

Rank: 1

积分
8
板凳
发表于 2025-11-4 09:01:00 | 只看该作者
帮我把这个代码改成蓝色: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)
回复 支持 反对

使用道具 举报

0

主题

3

帖子

8

积分

新手上路

Rank: 1

积分
8
地板
发表于 2025-11-4 09:06:07 | 只看该作者
帮我把这个代码字体颜色改成蓝色: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)
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表