c语言贪吃蛇代码,帮我写一个贪吃蛇的小程序
一、贪吃蛇c语言代码最短
#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
usingnamespacestd;
voidgotoxy(intx,inty){COORDpos={x,y};SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);}//光标定位
classFood{//食物类
private:intm_x;intm_y;
public:
voidrandfood(){//随机产生一个食物
srand((int)time(NULL));//利用时间添加随机数种子,需要ctime头文件
L1:{m_x=rand()%(85)+2;//2~86
m_y=rand()%(25)+2;//2~26
if(m_x%2)gotoL1;//如果食物的x坐标不是偶数则重新确定食物的坐标
gotoxy(m_x,m_y);//在确认好的位置输出食物
cout<<"★";}}
intgetFoodm_x(){returnm_x;}//返回食物的x坐标
intgetFoodm_y(){returnm_y;}};//返回食物的y坐标
classSnake{
private:
structSnakecoor{intx;inty;};//定义一个蛇的坐标机构
vector<Snakecoor>snakecoor;//将坐标存入vector容器中
//判断并改变前进方向的函数
voiddegdir(Snakecoor&nexthead){//定义新的蛇头变量
staticcharkey='d';//静态变量防止改变移动方向后重新改回来
if(_kbhit()){
chartemp=_getch();//定义一个临时变量储存键盘输入的值
switch(temp){//如果临时变量的值为wasd中的一个,则赋值给key
default:break;//default是缺省情况,只有任何条件都不匹配的情况下才会执行必须写在前面!不然蛇无法转向
case'w':case'a':case's':case'd':
//如果temp的方向和key的方向不相反则赋值因为两次移动方向不能相反将蛇设置为初始向右走
if(key=='w'&&temp!='s'||key=='s'&&temp!='w'||key=='a'&&temp!='d'||key=='d'&&temp!='a')key=temp;}}
switch(key){//根据key的值来确定蛇的移动方向
case'd':nexthead.x=snakecoor.front().x+2;nexthead.y=snakecoor.front().y;break;
//新的蛇头的头部等于容器内第一个数据(旧蛇头)x坐标+2因为蛇头占两个坐标,移动一次加2
case'a':nexthead.x=snakecoor.front().x-2;nexthead.y=snakecoor.front().y;break;
case'w':nexthead.x=snakecoor.front().x;nexthead.y=snakecoor.front().y-1;break;
//因为控制台的x长度是y的一半,所以用两个x做蛇头,需要的坐标是二倍
case's':nexthead.x=snakecoor.front().x;nexthead.y=snakecoor.front().y+1;}}
//游戏结束时设计一个界面输出“游戏结束”以及分数
voidfinmatt(constintscore){
system("cls");gotoxy(40,14);//清屏然后输出
cout<<"游戏结束";gotoxy(40,16);
cout<<"得分:"<<score;gotoxy(0,26);
exit(0);}//exit为C++的退出函数exit(0)表示程序正常退出,非0表示非正常退出
voidfinishgame(constintscore){//游戏结束
if(snakecoor[0].x>=88||snakecoor[0].x<0||snakecoor[0].y>=28||snakecoor[0].y<0)finmatt(score);//撞墙
for(inti=1;i<snakecoor.size();i++)if(snakecoor[0].x==snakecoor[i].x&&snakecoor[0].y==snakecoor[i].y)finmatt(score
二、c语言贪吃蛇代码及解析
以下是一个使用C语言编写的简单贪吃蛇游戏,包括初始化游戏界面、绘制蛇和食物、移动蛇和检测碰撞等功能。
```c
#include<stdio.h>
#include<conio.h>
#include<windows.h>
//定义常量
constintwidth=20;
constintheight=20;
constintmax_length=5;
constintblock_size=20;
constchardirection[]="RDLU";
constintfood_x=10;
constintfood_y=10;
constintsnake_speed=100;
//定义结构体,存储蛇的身体坐标和方向
structSnake{
intx,y;
intlength;
chardirection;
};
//定义结构体,存储食物的位置和状态(是否被吃掉)
structFood{
intx,y;
};
//定义全局变量,存储蛇和食物的信息
structSnakesnake;
structFoodfood;
intscore;
//初始化游戏界面和蛇的状态(位置和长度)
voidinit(){
//初始化窗口大小和标题栏
SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE),width*block_size,height*block_size);
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE),&buffer_info);
SetConsoleWindowInfo(GetStdHandle(STD_OUTPUT_HANDLE),TRUE,NULL,NULL,buffer_info.dwMaximumWindowSize);
printf("SnakeGame!
");
fflush(stdout);
//初始化蛇的位置和长度为3个方块,方向为左移符('L')
snake.x=height/2;
snake.y=height/2;
snake.length=3;
snake.direction='L';
//随机生成一个食物的位置和状态(是否被吃掉)
srand((unsigned)time(NULL));
food.x=(rand()%(width*block_size))+food_x;
food.y=(rand()%(height*block_size))+food_y;
}
//在屏幕上绘制蛇和食物的图像
voiddraw(){
RECTrect;
inti;
//根据蛇的位置和方向计算出每个方块的坐标和颜色值(RGB)
i=snake.length;
intcolorR=(snake.direction&'R')=='R'?155:155-(snake.length-i)*20;
intcolorG=(snake.direction&'G')=='G'?180:180-(snake.length-i)*20;
intcolorB=(snake.direction&'B')=='B'?25:25-(snake.length-i)*20;
intcolorD=(snake.direction&'D')=='D'?0:0-(snake.length-i)*20;
intcolorE=(snake.direction&'E')=='E'?7:7-(snake.length-i)*20;
intcolorF=(snake.direction&'F')=='F'?145:145-(snake.length-i)*20;
intcolorY=(snake.direction&'Y')=='Y'?11:11-(snake.length-i)*20;
intcolorX=(snake.direction&'X')=='X'?191:191-(snake.length-i)*20;
intcolorN=(snake.direction&'N')=='N'?165:165-(snake.length-i)*20;
intcolorM=(snake.direction&'M')=='M'?135:135-(snake.length-i)*20;
三、电脑贪吃蛇代码怎么运行
关于这个问题,电脑贪吃蛇代码需要使用编程软件打开运行。常见的编程软件有Python、Java、C++等。以下以Python为例,介绍如何运行电脑贪吃蛇代码:
1.安装Python编程环境。可从Python官网下载安装包,根据安装向导进行安装。
2.打开Python编辑器。可使用IDLE、PyCharm等编辑器打开。
3.复制贪吃蛇代码。可从网上搜索电脑贪吃蛇代码,复制到编辑器中。
4.运行代码。在编辑器中选择“运行”或按下F5键,即可运行代码。
5.玩游戏。代码运行后,会出现贪吃蛇游戏界面,按照提示操作,即可开始游戏。
需要注意的是,不同编程语言的贪吃蛇代码可能有所不同,具体操作方法也会有所差异。