当然可以!下面是一个简单的Python版贪吃蛇游戏代码示例:

import pygame
import time
import random

pygame.init()

# 游戏窗口尺寸
width, height = 800, 600
window = pygame.display.set_mode((width, height))

# 颜色定义
black = pygame.Color(0, 0, 0)
white = pygame.Color(255, 255, 255)
red = pygame.Color(255, 0, 0)
green = pygame.Color(0, 255, 0)
blue = pygame.Color(0, 0, 255)

# 蛇的初始位置和移动速度
snake_position = [100, 50]
snake_body = [[100, 50], [90, 50], [80, 50]]
snake_speed = 10

# 食物的初始位置
food_position = [random.randrange(1, (width//10)) * 10, random.randrange(1, (height//10)) * 10]
food_spawn = True

# 控制蛇的移动方向
direction = 'RIGHT'
change_to = direction

# 游戏结束函数
def game_over():
    font_style = pygame.font.SysFont(None, 50)
    message = font_style.render('Game Over', True, red)
    window.blit(message, [width/2, height/2])
    pygame.display.update()
    time.sleep(2)
    pygame.quit()
    quit()

# 游戏主循环
while True:
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                change_to = 'UP'
            if event.key == pygame.K_DOWN:
                change_to = 'DOWN'
            if event.key == pygame.K_LEFT:
                change_to = 'LEFT'
            if event.key == pygame.K_RIGHT:
                change_to = 'RIGHT'

    # 确保蛇不会直接掉头
    if change_to == 'UP' and direction != 'DOWN':
        direction = 'UP'
    if change_to == 'DOWN' and direction != 'UP':
        direction = 'DOWN'
    if change_to == 'LEFT' and direction != 'RIGHT':
        direction = 'LEFT'
    if change_to == 'RIGHT' and direction != 'LEFT':
        direction = 'RIGHT'

    # 根据方向移动蛇头
    if direction == 'UP':
        snake_position[1] -= 10
    if direction == 'DOWN':
        snake_position[1] += 10
    if direction == 'LEFT':
        snake_position[0] -= 10
    if direction == 'RIGHT':
        snake_position[0] += 10

    # 更新蛇的身体
    snake_body.insert(0, list(snake_position))
    if snake_position[0] == food_position[0] and snake_position[1] == food_position[1]:
        food_spawn = False
    else:
        snake_body.pop()

    # 生成新的食物
    if not food_spawn:
        food_position = [random.randrange(1, (width // 10)) * 10, random.randrange(1, (height // 10)) * 10]
    food_spawn = True

    # 游戏结束判断
    if snake_position[0] < 0 or snake_position[0] > width - 10:
        game_over()
    if snake_position[1] < 0 or snake_position[1] > height - 10:
        game_over()
    if snake_position in snake_body[1:]:
        game_over()

    # 绘制游戏窗口
    window.fill(black)
    for pos in snake_body:
        pygame.draw.rect(window, green, pygame.Rect(pos[0], pos[1], 10, 10))
    pygame.draw.rect(window, white, pygame.Rect(food_position[0], food_position[1], 10, 10))

    pygame.display.update()

    # 控制游戏速度
    pygame.time.Clock().tick(snake_speed)

请确保已经安装 Pygame 库,可以使用 pip install pygame 命令进行安装。然后运行代码,即可开始游戏。使用上、下、左、右箭头键控制蛇的移动方向,吃到食物得分,碰到墙壁或自己的身体游戏结束

能不能写一个python版贪吃蛇

原文地址: http://www.cveoy.top/t/topic/hTnQ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录