基于c#控制台应用开发一个贪吃蛇小游戏

时间: 2023-08-02 10:16:59 浏览: 50
好的,下面是基于C#控制台应用的贪吃蛇小游戏的代码实现。 ```csharp using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleSnake { class Program { static void Main(string[] args) { Console.CursorVisible = false; //隐藏光标 Console.SetWindowSize(60, 30); //设置窗口大小 Console.SetBufferSize(60, 30); //设置缓冲区大小 //画边框 for (int i = 0; i < 60; i++) { Console.SetCursorPosition(i, 0); Console.Write("#"); Console.SetCursorPosition(i, 29); Console.Write("#"); } for (int i = 0; i < 30; i++) { Console.SetCursorPosition(0, i); Console.Write("#"); Console.SetCursorPosition(59, i); Console.Write("#"); } //初始化蛇 List<Point> snake = new List<Point>(); snake.Add(new Point(30, 15)); snake.Add(new Point(29, 15)); snake.Add(new Point(28, 15)); snake.Add(new Point(27, 15)); snake.Add(new Point(26, 15)); //初始化食物 Point food = new Point(RandomNumber(1, 58), RandomNumber(1, 28)); Console.SetCursorPosition(food.x, food.y); Console.Write("*"); //初始化方向 Direction direction = Direction.Right; //初始化分数 int score = 0; while (true) { //获取键盘输入 if (Console.KeyAvailable) { ConsoleKeyInfo key = Console.ReadKey(true); switch (key.Key) { case ConsoleKey.UpArrow: if (direction != Direction.Down) direction = Direction.Up; break; case ConsoleKey.DownArrow: if (direction != Direction.Up) direction = Direction.Down; break; case ConsoleKey.LeftArrow: if (direction != Direction.Right) direction = Direction.Left; break; case ConsoleKey.RightArrow: if (direction != Direction.Left) direction = Direction.Right; break; } } //移动蛇 Point head = snake[0]; Point newHead = new Point(head.x, head.y); switch (direction) { case Direction.Up: newHead.y--; break; case Direction.Down: newHead.y++; break; case Direction.Left: newHead.x--; break; case Direction.Right: newHead.x++; break; } snake.Insert(0, newHead); Console.SetCursorPosition(newHead.x, newHead.y); Console.Write("@"); if (newHead.x == food.x && newHead.y == food.y) //吃到食物 { food = new Point(RandomNumber(1, 58), RandomNumber(1, 28)); Console.SetCursorPosition(food.x, food.y); Console.Write("*"); score += 10; } else //没有吃到食物 { Point tail = snake[snake.Count - 1]; Console.SetCursorPosition(tail.x, tail.y); Console.Write(" "); snake.RemoveAt(snake.Count - 1); } //判断是否撞到边框或自己 if (newHead.x == 0 || newHead.x == 59 || newHead.y == 0 || newHead.y == 29) break; for (int i = 1; i < snake.Count; i++) { if (newHead.x == snake[i].x && newHead.y == snake[i].y) { Console.SetCursorPosition(0, 31); Console.Write("Game Over! Your score is: " + score); return; } } //延迟一段时间 System.Threading.Thread.Sleep(100); } } static int RandomNumber(int min, int max) //生成指定范围内的随机数 { Random random = new Random(); return random.Next(min, max + 1); } } enum Direction //方向枚举 { Up, Down, Left, Right } class Point //坐标类 { public int x; public int y; public Point(int x, int y) { this.x = x; this.y = y; } } } ``` 希望这个代码对你有帮助!

相关推荐

最新推荐

recommend-type

使用Python第三方库pygame写个贪吃蛇小游戏

今天看到几个关于pygame模块的博客和视频,感觉非常有趣,这里照猫画虎写了一个贪吃蛇小游戏,目前还有待完善,但是基本游戏功能已经实现,下面是代码: # 导入模块 import pygame import random # 初始化 pygame....
recommend-type

基于三层感知机实现手写数字识别-内含源码和说明书.zip

基于三层感知机实现手写数字识别-内含源码和说明书.zip
recommend-type

setuptools-40.7.0.zip

Python库是一组预先编写的代码模块,旨在帮助开发者实现特定的编程任务,无需从零开始编写代码。这些库可以包括各种功能,如数学运算、文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
recommend-type

搭建VGG16神经网络实现图像分类-内含源码和说明书.zip

搭建VGG16神经网络实现图像分类-内含源码和说明书.zip
recommend-type

setuptools-40.6.1.zip

Python库是一组预先编写的代码模块,旨在帮助开发者实现特定的编程任务,无需从零开始编写代码。这些库可以包括各种功能,如数学运算、文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

用 Python 画一个可以动的爱心

好的,我可以帮助您使用Python来绘制一个动态的爱心。您可以使用turtle库来实现。以下是实现代码: ```python import turtle import math # 设置画布和画笔 canvas = turtle.Screen() canvas.bgcolor("black") pencil = turtle.Turtle() pencil.speed(0) pencil.color("red", "pink") pencil.pensize(3) # 定义爱心函数 def draw_love(heart_size, x_offset=0, y_offset=0):
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。