当前位置: 首页 > article >正文

Python——贪吃蛇

以下是一个简单的贪吃蛇游戏的Python代码示例:

import pygame
import time
import random

# 初始化 Pygame
pygame.init()

# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

# 设置屏幕尺寸
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("贪吃蛇游戏")

# 游戏帧率
clock = pygame.time.Clock()
snake_block_size = 20

# 字体设置
font_style = pygame.font.SysFont(None, 50)
score_font = pygame.font.SysFont(None, 35)

def your_score(score):
    value = score_font.render("Your Score: " + str(score), True, WHITE)
    screen.blit(value, [0, 0])

def our_snake(snake_block_size, snake_list):
    for x in snake_list:
        pygame.draw.rect(screen, GREEN, [x[0], x[1], snake_block_size, snake_block_size])

# 游戏主循环
def game_loop():
    game_over = False
    game_quit = False
    x1 = screen_width / 2
    y1 = screen_height / 2
    x1_change = 0
    y1_change = 0
    snake_list = []
    snake_length = 1
    foodx = round(random.randrange(0, screen_width - snake_block_size) / 20.0) * 20.0
    foody = round(random.randrange(0, screen_height - snake_block_size) / 20.0) * 20.0

    while not game_quit:
        while game_over == True:
            screen.fill(BLACK)
            message = font_style.render("Game Over! Press Q-Quit or C-Play Again", True, WHITE)
            screen.blit(message, [screen_width / 6, screen_height / 3])
            your_score(snake_length - 1)
            pygame.display.update()

            # 游戏结束等待用户输入
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    game_quit = True
                    game_over = False
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_q:
                        game_quit = True
                        game_over = False
                    if event.key == pygame.K_c:
                        game_loop()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                game_quit = True
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x1_change = -snake_block_size
                    y1_change = 0
                elif event.key == pygame.K_RIGHT:
                    x1_change = snake_block_size
                    y1_change = 0
                elif event.key == pygame.K_UP:
                    y1_change = -snake_block_size
                    x1_change = 0
                elif event.key == pygame.K_DOWN:
                    y1_change = snake_block_size
                    x1_change = 0

        if x1 >= screen_width or x1 < 0 or y1 >= screen_height or y1 < 0:
            game_over = True

        x1 += x1_change
        y1 += y1_change
        screen.fill(BLACK)
        pygame.draw.rect(screen, RED, [foodx, foody, snake_block_size, snake_block_size])
        snake_head = []
        snake_head.append(x1)
        snake_head.append(y1)
        snake_list.append(snake_head)
        if len(snake_list) > snake_length:
            del snake_list[0]

        for x in snake_list[:-1]:
            if x == snake_head:
                game_over = True

        our_snake(snake_block_size, snake_list)
        your_score(snake_length - 1)

        pygame.display.update()

        if x1 == foodx and y1 == foody:
            foodx = round(random.randrange(0, screen_width - snake_block_size) / 20.0) * 20.0
            foody = round(random.randrange(0, screen_height - snake_block_size) / 20.0) * 20.0
            snake_length += 1

        clock.tick(10)

    pygame.quit()
    quit()

game_loop()

这个代码使用Pygame库来创建一个简单的贪吃蛇游戏。在游戏中,玩家通过键盘控制贪吃蛇的移动方向,目标是吃到食物并尽可能多地增长长度。如果贪吃蛇碰到边界或自己的身体,游戏结束。游戏过程中会显示当前的分数。


http://www.kler.cn/a/301631.html

相关文章:

  • 【Android】EventBus事件总线用法浅析
  • 中信建投张青:以金融巨擘之姿,铸就公益慈善新篇章
  • Oracle Instant Client 23.5安装配置完整教程
  • 2024-11-16-机器学习方法:无监督学习(1) 聚类(上)
  • 利用Blackbox AI让编程更轻松
  • 电脑长期不用,开不了机怎样解决
  • JAVA基础:抽象类,接口,instanceof,类关系,克隆
  • 在深度学习计算机视觉的语义分割中,Boundary和Edge的区别是?
  • NX—UI界面生成的文件在VS上的设置
  • 【网络】DNS
  • 备忘录模式memento
  • C语言初识编译和链接
  • 01 Docker概念和部署
  • 【论文速读】| SEAS:大语言模型的自进化对抗性安全优化
  • Arm GIC-v3中断原理及验证(通过kvm-unit-tests)
  • Go语言练习——语法实践
  • Leetcode 3281. Maximize Score of Numbers in Ranges
  • 双端队列--java--黑马
  • Python 学习笔记(二)
  • Java后端开发(十六)-- JavaBean对象拷贝工具类:运用反射机制,实现对象的深拷贝
  • 指针与二维数组
  • 软考中级软件设计师-【计算机系统】必考题汇总
  • 【Java】解决项目启动时端口被占用
  • 【Linux取经之路】用户权限管理
  • 博客常见问题
  • WordPress上可以内容替换的插件