c语言贪吃蛇源代码怎么用?
//******友情提示:如想速度快点,请改小_sleep(500)函数中参数***** #include #include #include #include #include const int H = 8; //地图的高 const int L = 16; //地图的长 char GameMap[H][L]; //游戏地图 int key; //按键保存 int sum = 1, over = 0; //蛇的长度, 游戏结束(自吃或碰墙) int dx[4] = {0, 0, -1, 1}; //左、右、上、下的方向 int dy[4] = {-1, 1, 0, 0}; struct Snake //蛇的每个节点的数据类型 { int x, y; //左边位置 int now; //保存当前节点的方向, 0,1,2,3分别为左右上下 }Snake[H*L]; const char Shead = '@'; //蛇头 const char Sbody = '#'; //蛇身 const char Sfood = '*'; //食物 const char Snode = '.'; //'.'在地图上标示为空 void Initial(); //地图的初始化 void Create_Food(); //在地图上随机产生食物 void Show(); //刷新显示地图 void Button(); //取出按键,并判断方向 void Move(); //蛇的移动 void Check_Border(); //检查蛇头是否越界 void Check_Head(int x, int y); //检查蛇头移动后的位置情况 int main() { Initial(); Show(); return 0; } void Initial() //地图的初始化 { int i, j; int hx, hy; system("title 贪吃蛇"); //控制台的标题 memset(GameMap, '.', sizeof(GameMap)); //初始化地图全部为空'.' system("cls"); srand(time(0)); //随机种子 hx = rand()%H; //产生蛇头 hy = rand()%L; GameMap[hx][hy] = Shead; Snake[0].x = hx; Snake[0].y = hy; Snake[0].now = -1; Create_Food(); //随机产生食物 for(i = 0; i _= H || Snake[0].y = L) over = 1; } void Check_Head(int x, int y) //检查蛇头移动后的位置情况 { if(GameMap[ Snake[0].x ][ Snake[0].y ] == '.') //为空 GameMap[ Snake[0].x ][ Snake[0].y ] = '@'; else if(GameMap[ Snake[0].x ][ Snake[0].y ] == '*') //为食物 { GameMap[ Snake[0].x ][ Snake[0].y ] = '@'; Snake[sum].x = x; //新增加的蛇身为蛇头后面的那个 Snake[sum].y = y; Snake[sum].now = Snake[0].now; GameMap[ Snake[sum].x ][ Snake[sum].y ] = '#'; sum++; Create_Food(); //食物吃完了马上再产生一个食物 } else over = 1; }
我记得我写的时候是先把框架列好,先从主程序开始,用到哪些函数 我就先写上,当然实现先不实现,然后一步一步的完善。
主函数中进行的步骤无非是接收一个键(这个接收函数一定要用getch(),为啥你懂得,还要有判断当前是否有键按下的函数,这个是游戏必须的函数,名字我忘了),清屏,画出下一个状态。
下一个状态包括 整个地图,苹果的位置,蛇的的长度以及每段的位置。
还要加上是否吃上东东,是否撞到墙,是否喷到了自己,就这些。
C语言贪吃蛇源代码必须经过相应的C/C++编译器编译成EXE文件后才能运行。
由于我们通常使用的操作系统是Windows系统,而在该系统下最长用的C/C++编译器是VC++编译器,目前在大专院校常用的版本还是VC++6.0
下面就以VC++6.0来说明编译过程:
1.在VC++6.0中通过“File”菜单下的 “Open”子菜单打开贪吃蛇代码
2.在VC++6.0中通过“Build”菜单下的 “Compile xxxx.xx”子菜单编译贪吃蛇代码
3.在VC++6.0中通过“Build”菜单下的 “Execute xxxx.exe”子菜单运行贪吃蛇程序
附:在VC++6环境下可运行的C/C++贪吃蛇源代码(无版权,自己编写,欢迎任意修改拷贝)
/*
C/C++贪吃蛇游戏,zjlj,2015.3.16
*/
#define DEBUG 0 //当程序在调试阶段时 DEBUG为 1
#include<iostream>
#include<windows.h>
#include<time.h>
#include<conio.h>
using namespace std;
void readini(FILE **fphead, int *score, char *argv[]) //创建或打开一个和运行文件对应的ini文件,读取最高纪录
{
char filename[200],*pfilename;
int flag=-1,i;
strcpy(filename,argv[0]);
for(i=0;filename[i]!='\0';i++)
{
if ('.'==filename[i])flag=1;
}
if(1==flag)
{
filename[i-1]='i';
filename[i-2]='n';
filename[i-3]='i';
}
else
{
filename[i]='.';
filename[i+1]='i';
filename[i+2]='n';
filename[i+3]='i';
filename[i+4]='\0';
}
for(;filename[i]!='\\'&&i>=0;i--)pfilename=&filename[i];
if ( (*fphead=fopen(pfilename, "rb+"))==NULL)
{
if ( (*fphead=fopen(pfilename, "wb+"))==NULL)
{
printf("无法创建或打开\"%s\"文件
",pfilename);
system("pause");
exit(0);
}
}
else
{
fread(score,sizeof(int),1,*fphead);
}
}
void writeini(FILE **fphead, int *score, char *argv[]) //打开一个和运行文件对应的ini文件,写入最高纪录
{
char filename[200],*pfilename;
int flag=-1,i;
strcpy(filename,argv[0]);
for(i=0;filename[i]!='\0';i++)
{
if ('.'==filename[i])flag=1;
}
if(1==flag)
{
filename[i-1]='i';
filename[i-2]='n';
filename[i-3]='i';
}
else
{
filename[i]='.';
filename[i+1]='i';
filename[i+2]='n';
filename[i+3]='i';
filename[i+4]='\0';
}
for(;filename[i]!='\\'&&i>=0;i--)pfilename=&filename[i];
if ( (*fphead=fopen(pfilename, "wb+"))==NULL)
{
printf("无法写入\"%s\"文件,磁盘写保护!
",pfilename);
system("pause");
exit(0);
}
else
{
rewind(*fphead);
fwrite(score,sizeof(int),1,*fphead);
fclose(*fphead);
}
}
void gotoxy(int x,int y)//光标定位,光标定位函数SetConsoleCursorPosition是左上角位置是0,0然后向左向下延伸
{
COORD pos;
pos.X=2*y;
pos.Y=x;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
void color(int a)//颜色函数
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
void Refresh(int q[][22], int grade, int gamespeed, int length,int score) // 输出贪吃蛇棋盘
{
int i,j;
for(i=0;i<22;i++)
{
for(j=0;j<22;j++)
{
if(q[i][j]==0)//输出棋盘空白
{
gotoxy(i,j);
color(11);
cout<<"■";
}
if(q[i][j]==1||q[i][j]==2)//输出棋盘墙壁
{
gotoxy(i,j);
color(11);
cout<<"□";
}
if(q[i][j]==3)//输出蛇头
{
gotoxy(i,j);
color(14);
cout<<"★";
}
if(q[i][j]==4)//输出蛇身
{
gotoxy(i,j);
color(12);
cout<<"◆";
}
if(q[i][j]==5)//输出果子
{
gotoxy(i,j);
color(12);
cout<<"●";
}
}
if(i==0) cout << "***********************";
if(i==1) cout << "等级为:" << grade;//显示等级
if(i==3) cout << "自动前进时间";
if(i==4) cout << "间隔为:" << gamespeed << "ms";//显示时间
if(i==6) cout << "历史最高分为:" << score << "分";
if(i==7) cout << "你现在得分为:" << (length+(grade-1)*8)*10 << "分";
if(i==8) cout << "**********************";
if(i==9) cout << "游戏说明:";
if(i==10) cout << "(1)用小键盘方向键控制";
if(i==11) cout << "蛇头运动方向;";
if(i==12) cout << "(2)蛇每吃一个果子蛇身";
if(i==13) cout << "增加一节;";
if(i==14) cout << "(3)蛇咬到自己或碰到墙";
if(i==15) cout << "壁游戏结束。";
if(i==18) cout << "**********************";
if(i==19) cout << "C/C++语言作业:";
if(i==20) cout << "zjlj,2015.03.16 ";
}
}
int main(int argc, char *argv[]){
int tcsQipan[22][22]; // 贪吃蛇棋盘是一个二维数组(如22*22,包括墙壁)
int i,j,score,directiontemp;
FILE *fpini;//*fpini 信息文件
readini(&fpini, &score, argv);//读取ini文件的最高纪录
if (score<0)//最高成绩小于零设置为零,初建文件会是负数
score=0;
while(1)
{
for(i=1;i<=20;i++)
for(j=1;j<=20;j++)
tcsQipan[i][j]=0; //贪吃蛇棋盘相应坐标标上中间空白部分的标志0
for(i=0;i<=21;i++)
tcsQipan[0][i] = tcsQipan[21][i] = 1; //贪吃蛇棋盘相应坐标标上上下墙壁的标志1
for(i=1;i<=20;i++)
tcsQipan[i][0] = tcsQipan[i][21] = 2; //贪吃蛇棋盘相应坐标标上左右墙壁的标志2
int tcsZuobiao[2][500]; //蛇的坐标数组
for(i=0; i<4; i++)
{
tcsZuobiao[0][i] = 1;//蛇身和蛇头的x坐标
tcsZuobiao[1][i] = i + 1;//蛇身和蛇头的y坐标
}
int head = 3,tail = 0;//标示蛇头和蛇尾的数组偏移量
for(i=1;i<=3;i++)
tcsQipan[1][i]=4; //蛇身
tcsQipan[1][4]=3; //蛇头
int x1, y1; // 随机出果子
srand(time(0));//设置随机种子
do
{
x1=rand()%20+1;
y1=rand()%20+1;
}
while(tcsQipan[x1][y1]!=0);//如果不是在空白处重新出果子
tcsQipan[x1][y1]=5;//贪吃蛇棋盘相应坐标标上果子的标志5
color(12);
cout<<"
贪吃蛇游戏即将开始 !"<<endl;//准备开始
long start,starttemp;
int grade = 1, length = 4; //设置初始等级和蛇的初始长度
int gamespeed = 500; //设置初始前进时间间隔
for(i=3;i>=0;i--)
{
start=clock();
while(clock()-start<=1000);
system("cls");
if(i>0)
cout << "
进入倒计时:" << i << endl; //倒计时显示
else
Refresh(tcsQipan,grade,gamespeed,length,score); //初始棋盘显示
}
int timeover=1,otherkey=1;//初始化超时时间和按键判断参数
char direction = 77; // 设置初始情况下,向右运动
int x=tcsZuobiao[0][head],y=tcsZuobiao[1][head];//保存蛇头坐标到x,y变量
while(1)//运行一局游戏
{
start = clock();
while((timeover=((starttemp=clock())-start<=gamespeed))&&!kbhit());//如果有键按下或时间超过自动前进时间间隔则终止循环
if(direction==72||direction==80||direction==75 ||direction==77)
directiontemp=direction;//保留上一次方向按键
//starttemp=gamespeed+start-starttemp;//保留停留时间
if(timeover)
{
#if (DEBUG==1)
direction = getch();//调试代码
#else
if((direction =getch())==-32)
direction = getch();
#endif
}
#if (DEBUG==1)//调试代码
start=clock();
while(clock()-start<=2000);
gotoxy(24,4);
cout << "按键ASCII代码"<<(int)direction<<" "<<endl;
#endif
if(!(direction==72||direction==80||direction==75 ||direction==77))
{
otherkey=0;// 按键非方向键,otherkey设置为0
}
else
{
otherkey=1;// 按键为方向键,otherkey设置为1
}
if(direction==72 && directiontemp==80)//忽略反方向按键
{
direction=32;
otherkey=0;
//start = clock();
//while(clock()-start<=starttemp);
}
else if(direction==80 && directiontemp==72)
{
direction=32;//设置按键为非方向键
otherkey=0;// 按键为非方向键,otherkey设置为0
// start = clock();
//while(clock()-start<=starttemp);//补偿等待时间
}
else if(direction==75 && directiontemp==77)
{
direction=32;
otherkey=0;
//start = clock();
//while(clock()-start<=starttemp);
}
else if(direction==77 && directiontemp==75)
{
direction=32;
otherkey=0;
//start = clock();
//while(clock()-start<=starttemp);
}
switch(direction)//判断方向键
{
case 72: x= tcsZuobiao[0][head]-1; y= tcsZuobiao[1][head];break; // 向上
case 80: x= tcsZuobiao[0][head]+1; y= tcsZuobiao[1][head];break; // 向下
case 75: x= tcsZuobiao[0][head]; y= tcsZuobiao[1][head]-1;break; // 向左
case 77: x= tcsZuobiao[0][head]; y= tcsZuobiao[1][head]+1;break; // 向右
default: break;
}
if(x==0 || x==21 ||y==0 || y==21) // 蛇头碰到墙壁,结束本局游戏
{
gotoxy(22,12);
cout << "游戏已结束!" << endl;
if(score>=(length+(grade-1)*8)*10)//判断是否破记录
{
gotoxy(10,7);
color(12);
cout << "闯关失败 加油耶!" << endl;
fclose(fpini);//关闭ini文件
}
else
{
gotoxy(10,7);
color(12);
cout << "恭喜您打破记录" << endl;
score=(length+(grade-1)*8)*10;
writeini(&fpini, &score, argv);//写入ini文件的最高纪录
}
gotoxy(23,12);
cout << "按回车键重新开始,按ESC退出游戏" << endl;//显示的提示
break;//退出该局游戏
}
if(tcsQipan[x][y]!=0&&!(x==x1&&y==y1)&&tcsQipan[x][y]!=3) // 蛇头碰到蛇身,结束本局游戏
{
gotoxy(22,12);
cout << "游戏已结束!" << endl;
if(score>=(length+(grade-1)*8)*10)//判断是否破记录
{
gotoxy(10,7);
color(12);
cout << "闯关失败 加油耶!" << endl;
fclose(fpini);//关闭ini文件
}
else
{
gotoxy(10,7);
color(12);
cout << "恭喜您打破记录" << endl;
score=(length+(grade-1)*8)*10;
writeini(&fpini, &score, argv);//写入ini文件的最高纪录
}
gotoxy(23,12);
cout << "按回车键重新开始,按ESC退出游戏" << endl;//显示的提示
break;//退出该局游戏
}
/*
游戏运行时的核心算法开始
*/
if(x==x1 && y==y1) // 吃果子,长度加1
{
length ++;
if(length>=8)//长度大于等于8重新计算长度,等级加1
{
length -= 8;//重新计算长度
grade ++;//等级加1
if(gamespeed>50)//控制最快速度为50
gamespeed = 550 - grade * 50; // 改变自动前进时间间隔
}
tcsQipan[x][y]= 3;//贪吃蛇棋盘相应坐标现在蛇头标志改为蛇头标志3
tcsQipan[tcsZuobiao[0][head]][tcsZuobiao[1][head]] = 4;//贪吃蛇棋盘相应坐标原来蛇头标志改为蛇身标志4
head = (head+1)%400;//防止数组越界
tcsZuobiao[0][head] = x;//蛇头的x坐标
tcsZuobiao[1][head] = y;//蛇头的y坐标
do//随机出果子
{
x1=rand()%20+1;
y1=rand()%20+1;
}
while(tcsQipan[x1][y1]!=0);//如果不是在空白处重新出果子
tcsQipan[x1][y1]=5;//贪吃蛇棋盘相应坐标标上果子的标志5
gotoxy(22,12);
cout << "游戏进行中!" << endl;
Refresh(tcsQipan,grade,gamespeed,length,score);
}
else // 不吃果子
{
if(otherkey)
{
tcsQipan [tcsZuobiao[0][tail]][tcsZuobiao[1][tail]]=0;
tail=(tail+1)%400;//防止数组越界
tcsQipan [tcsZuobiao[0][head]][tcsZuobiao[1][head]]=4;
head=(head+1)%400;//防止数组越界
tcsZuobiao[0][head]=x;//蛇头的x坐标
tcsZuobiao[1][head]=y;//蛇头的y坐标
tcsQipan[tcsZuobiao[0][head]][tcsZuobiao[1][head]]=3;
gotoxy(22,12);
cout << "游戏进行中!" << endl;
Refresh(tcsQipan,grade,gamespeed,length,score);
}
else
{
gotoxy(22,12);
cout << "游戏暂停中!" << endl;
}
}
/*
游戏运行时的核心算法结束
*/
}
while(1)
{
while(!kbhit());
if((direction =getch())==13)//按回车键开始下一局
break;
if(direction ==27)//按ESC退出游戏
exit(0);
}
system("cls");//清除屏幕重新开始
}
return 0;
}
放入编辑器变成exe文件。运行。
k,oyb965f
C语言课程设计,贪吃蛇应该怎么做?
贪吃蛇游戏是一个经典小游戏,一条蛇在封闭围墙里,围墙里随机出现一个食物,通过按键盘四个光标键控制蛇向上下左右四个方向移动,蛇头撞倒食物,则食物被吃掉,蛇身体长一节,同时记10分,接着又出现食物,等待蛇来吃,如果蛇在移动中撞到墙或身体交叉蛇头撞倒自己身体游戏结束。 2.2程序整体设计说明 一个游戏要有开始部分...
用类的c++小游戏源代码?
以下是贪吃蛇源代码:include include include include include define N 21 void gotoxy(int x,int y)\/\/位置函数 { COORD pos;pos.X=2*x;
跪求c++贪吃蛇源代码!!!高手救命啊!!!可以用的话追加5分。复制别人的不...
\/*贪吃蛇游戏代码*\/ include <graphics.h> include <stdlib.h> include include <dos.h> define MAX_JOINTS 200 define LEFT 0x4b00 define RIGHT 0x4d00 define DOWN 0x5000 define UP 0x4800 define ESC 0x011b define MV_RIGHT 1 define MV_LEFT 2 define MV_UP ...
求VS得贪吃蛇源代码!谢谢大神 还有个问题在VS中写#include<Windows.h>...
2012-12-06 求在VC++6.0中运行的贪吃蛇代码 312 2011-06-13 用C++编写的小游戏源代码 109 2015-07-06 谁有用c++写的贪吃蛇源代码分享一个给我吧, 好人一生平安! 2 2016-01-19 求C语言大神贪吃蛇开始界面程序代码C++程序 2 2012-04-07 如何用VS2010新建一个类?用来写C++文件的标准格式建立... 54...
c语言小游戏代码
贪吃蛇的源代码#include<stdio.h>#include<stdlib.h>#include#include<conio.h>typedef struct snake{ int a; int b; struct snake *u; struct snake *n;}snake,*snake1;typedef struct food{ int a; int b;}food;void main(){ char c,c0 = 'd'; int i,j,k,n=1,t,at; snake p,q; snake...
vs如何使用别人的c#代码,比如说贪吃蛇?
如果您想使用别人编写的 C# 代码,比如贪吃蛇游戏,可以按照以下步骤进行:获取源代码:首先需要获取贪吃蛇游戏的源代码。通常情况下,您可以在GitHub等开源代码库中找到现成的贪吃蛇游戏源码。导入代码:将贪吃蛇游戏的源码导入到 Visual Studio 中。可以通过“文件”->“打开”->“项目\/解决方案”来打开已有...
C语言编写贪吃蛇需要用哪些函数??
include <graphics.h> include <stdlib.h> include <dos.h> \/*引用的库函数*\/ define LEFT 0x4b00 define RIGHT 0x4d00 define DOWN 0x5000 define UP 0x4800 define ESC 0x011b\/*宏定义键名*\/ define N 200 int i,key;int level;\/*游戏等级*\/ int score=0;\/*得分*\/ int gamespeed;...
如何用Python写一个贪吃蛇AI
如果对源代码感兴趣,请戳以下的链接: Code goes here 另外,本文的贪吃蛇程序使用了curses模块, 类Unix系统都默认安装的,使用Windows的童鞋需要安装一下这个模块, 送上地址: 需要curses请戳我 以上的代码仍然可以继续改进(现在加注释不到300行,优化一下可以更少), 也可用pygame或是pyglet库把界面做得更加漂亮,Enjoy...
求大神给一份JAVA小游戏,贪吃蛇或者俄罗斯方块源代码。my eclipse能直接...
import javax.swing.*;import java.awt.*;import java.awt.event.*;class Game extends JFrame implements KeyListener,Runnable { Thread Down10 = new Thread(this);GameCanvas test = new GameCanvas();JLabel LabelTetris = new JLabel("Tetris Game");JLabel ScorePrint1 = new JLabel("...
怎么改c语言贪吃蛇源代码才能让贪吃蛇吃到自己尾巴后不死?
if snake[head].pos_x == snake[tail].pos_x && snake[head].pos_y == snake[tail].pos_y 代表头尾相碰了 那么修改这个body中的实现代码 原来肯定是显示游戏结束 清除上下文 挂了一类的,现在改成你想要的
衡勉亚硫: 简单说呢,你需要安装C的开发环境(需要注意你的代码是哪个C:C++、C#、Turb C、MS C等),然后只需要启动C的主程序,将你的代码Copy/Post到里面,在菜单中找到“运行”之类的命令就可以进行编译、联接、与运行了.GoodLuck!
历下区19734074901: 贪吃蛇的c语言代码怎么编制成游戏? - ?
衡勉亚硫: C语言贪吃蛇源代码必须经过相应的C/C++编译器编译成EXE文件后才能运行.由于我们通常使用的操作系统是Windows系统,而在该系统下最长用的C/C++编译器是VC++编译器,目前在大专院校常用的版本还是VC++6.0 下面就以VC++6.0来说明编译过程:1.在VC++6.0中通过“File”菜单下的 “Open”子菜单打开贪吃蛇代码
历下区19734074901: 跪求用c语言设计贪吃蛇软件的详解,急需!!!!!! - ?
衡勉亚硫: 贪吃蛇游戏c程序代码#define N 200#include <graphics.h>#include <stdlib.h>#include <dos.h>#define LEFT 0x4b00#define RIGHT 0x4d00#define DOWN 0x5000#define UP 0x4800#define ESC 0x011b int i,key; int score=0;/*得分*/ int ...
历下区19734074901: 怎么改c语言贪吃蛇源代码才能让贪吃蛇吃到自己尾巴后不死? - ?
衡勉亚硫: if snake[head].pos_x == snake[tail].pos_x && snake[head].pos_y == snake[tail].pos_y 代表头尾相碰了 那么修改这个body中的实现代码 原来肯定是显示游戏结束 清除上下文 挂了一类的,现在改成你想要的
历下区19734074901: 用C语言怎么做贪吃蛇游戏 - ?
衡勉亚硫: 代码:#define N 200 #include <graphics.h> #include <stdlib.h> #include <dos.h> #define LEFT 0x4b00 #define RIGHT 0x4d00 #define DOWN 0x5000 #define UP 0x4800 #define ESC 0x011b int i,key; int score=0;/*得分*/ int gamespeed=50000...
历下区19734074901: C语言的贪吃蛇源代码 - ?
衡勉亚硫: //******友情提示:如想速度快点,请改小_sleep(500)函数中参数***** #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <string.h> #include <time.h> const int H = 8; //地图的高 const int L = 16; //地图的长 char GameMap[H][L]; ...
历下区19734074901: 贪吃蛇游戏用C语言怎么编写?
衡勉亚硫: 在内存中定义一个数组,存储当前局面(如果棋盘小(<15)可以选用unsigned char类型,否则用int,蛇身就用1,2,3,4,5这样的数字序列表示,每走一格,根据当前方向将下一格数字变一下,把尾巴也处理一下,),每一秒刷新一下(向前走一格),每刷新一下,就重绘一遍当前局面(可以用prinrf函数,也可以用绘图函数), 总之方法很多很多,自己下来可以思索一下
历下区19734074901: 单片机贪吃蛇用C语言编程怎么实现蛇吃完食物后蛇头和蛇尾互换 - ?
衡勉亚硫: 蛇身应该是数组或链表吧,取它们的倒序,前进方向相反就行了
历下区19734074901: C语言做贪吃蛇做好了,就是不知道怎么用方向键控制方向,getch()放在里面就会等待我输入求好方法 - ?
衡勉亚硫: //用kbhit()函数 #include#include void main( void ) { int i = 1; int j; while(i++) { if(i==10000) { printf("1"); i=1;}if(kbhit()) { if(getch()==224) { j=getch(); if(j==72) printf("\nup"); if(j==80) printf("\ndown"); if(j==75) printf("\nleft"); if(j==77) printf("...
历下区19734074901: c语言编写贪吃蛇,如何在不输入的情况下使蛇移动? - ?
衡勉亚硫: do { 改变蛇的位置; }while(!_kbhit());就是说,在不按键的情况下,一直改变蛇的位置.记住包含_kbhit()的头文件