当前位置:首页 » 编程语言 » c语言写窗口游戏
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言写窗口游戏

发布时间: 2022-11-03 19:14:52

⑴ 可以用c语言编写游戏吗

可以用C语言编写游戏的。

⑵ 如何用C语言编写一个窗体应用程序

要用C语言编写一个窗体应用程序需要调用系统或第三方提供的API函数,一般的步骤是:
定义窗口类
注册窗口类
创建窗口
显示、更新窗口
进行消息循环,不断处理窗口消息

⑶ 教你如何使用C语言编写简单小游戏

编写程序,实现如下表所示的5-魔方阵。
17

24

1

8

15

23

5

7

14

16

4

6

13

20

22

10

12

19

21

3

11

18

25

2

9

5-魔方阵
问题分析
所谓“n-魔方阵”,指的是使用1〜n2共n2个自然数排列成一个n×n的方阵,其中n为奇数;该方阵的每行、每列及对角线元素之和都相等,并为一个只与n有关的常数,该常数为n×(n2+1)/2。
例如5-魔方阵,其第一行、第一列及主对角线上各元素之和如下:
第一行元素之和:17+24+1+8+15=65
第一列元素之和:17+23+4+10+11=65
主对角线上元素之和:17+5+13+21+9=65

n×(n2+1)/2=5×(52+1)/2=65
可以验证,5-魔方阵中其余各行、各列及副对角线上的元素之和也都为65。
假定阵列的行列下标都从0开始,则魔方阵的生成方法为:在第0行中间置1,对从2开始的其余n2-1个数依次按下列规则存放:
(1)
假定当前数的下标为(i,j),则下一个数的放置位置为当前位置的右上方,即下标为(i-1,j+1)的位置。
(2)
如果当前数在第0行,即i-1小于0,则将下一个数放在最后一行的下一列上,即下标为(n-1,j+1)的位置。
(3)
如果当前数在最后一列上,即j+1大于n-1,则将下一个数放在上一行的第一列上,即下标为(i-1,0)的位置。
(4)
如果当前数是n的倍数,则将下一个数直接放在当前位置的正下方,即下标为(i+1,j)的位置。
算法设计
在设计算法时釆用了下面一些方法:
定义array()函数,array()函数的根据输入的n值,生成并显示一个魔方阵,当发现n不是奇数时,就加1使之成为奇数。
使用动态内存分配与释放函数malloc()与free(),在程序执行过程中动态分配与释放内存,这样做的好处是使代码具有通用性,同时提高内存的使用率。
在分配内存时还要注意,由于一个整型数要占用两个内存,因此,如果魔方阵中要存放的数有max个,则分配内存时要分配2*max个单元,从而有malloc(max+max)。在malloc()函数中使用max+max而不是2*max是考虑了程序运行的性能。
显然应该使用二维数组来表示魔方阵,但虽然数组是二维形式的,而由于内存是一维线性的,因此在存取数组元素时,要将双下标转换为单个索引编号。在程序中直接定义了指针变量来指向数组空间,即使用malloc()函数分配的内存。

⑷ 怎么用c语言写窗体程序

步骤:
1、注册窗口类;
2、创建窗体;
3、消息循环;
4、编写窗口消息处理函数。

代码:


#include<windows.h>
#include<tchar.h>
LRESULTCALLBACKWindowProc(HWNDhwnd,UINTmsg,WPARAMwParam,LPARAMlParam);
intWINAPI_tWinMain(HINSTANCEhInstance,HINSTANCEhPrevInstance,LPWSTRszCmdLine,intnCmdShow)
{
WNDCLASSwc;
wc.style=CS_HREDRAW|CS_VREDRAW;
wc.lpfnWndProc=WindowProc;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hInstance=hInstance;
wc.hIcon=NULL;
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=(HBRUSH)COLOR_WINDOW;//(HBRUSH)GetStockObject();
wc.lpszMenuName=NULL;
wc.lpszClassName=_T("MyWindowClass");
if(!RegisterClass(&wc))
{
MessageBox(NULL,_T("无法注册窗口类"),_T("错误"),MB_OK);
return0;
}
HWNDnewWindow=CreateWindow(
_T("MyWindowClass"),
_T("我的第一个winapi程序"),
WS_OVERLAPPEDWINDOW,
0,
0,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
);
if(NULL==newWindow)
{
MessageBox(NULL,_T("无法创建窗体"),_T("错误"),MB_OK);
return0;
}
ShowWindow(newWindow,nCmdShow);
UpdateWindow(newWindow);
MSGmsg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
LRESULTCALLBACKWindowProc(HWNDhwnd,UINTuMsg,WPARAMwParam,LPARAMlParam)
{
switch(uMsg)
{
caseWM_DESTROY:
{
PostQuitMessage(0);
break;
}
default:
returnDefWindowProc(hwnd,uMsg,wParam,lParam);
}
return0;
}

就是一个只有标题栏、关闭按钮、最小化按钮、最大化/还原按钮、显示区域的窗体。

⑸ 如何用C语言编写控制台小游戏

//C语言实例:推箱子小游戏
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
//行和列
#defineROW10
#defineCOL11
/*,system("pause")orinputloop*/
/**
*

*
*/
//地图
charmap[ROW][COL]={
"##########",//0
"#####",//1
"#####",//2
"##AX###",//3
"#####",//4
"######",//5
"###",//6
"#####",//7
"###",//8
"##########"//9
//A:人,X:箱子
};
//打印地图
voidshowMap();
//接收小人的方向
charenterDirection();

//小人向上移动的方法
voidmoveToUp();
//小人向下移动的方法
voidmoveToDown();
//小人向右移动的方法
voidmoveToRight();
//小人向左移动的方法
voidmoveToLeft();

//当前小人的坐标
intcurrentPersonRow=3;
intcurrentPersonCol=2;
//当前箱子的坐标
intcurrentBoxRow=3;
intcurrentBoxCol=3;intmain(intargc,char*argv[]){
//system("clear");
printf("点击回车键开始游戏^_^ ");
//1代表运行0停止
intflag=1;
while(flag==1){
//显示地图
showMap();
//接收小人的方向
chardir=enterDirection();
switch(dir){
//小人向上移动
case'w':
case'W':
moveToUp();
break;

//小人向下移动
case's':
case'S':
moveToDown();
break;
//小人向右移动
case'd':
case'D':
moveToRight();
break;
//小人向左移动
case'a':
case'A':
moveToLeft();
break;
//停止运行
case'q':
case'Q':
printf("你的智商真低!T_T ");
flag=0;
break;
}
showMap();
if(currentBoxRow==8&&currentBoxCol==9){
printf("你的智商真高^_^!!!");
flag=0;
}

}

}
/*
方法的实现
*/


//打印地图
voidshowMap(){
inti;
for(i=0;i<ROW;i++){
printf("%s ",map[i]);
}
printf(" ");
printf("W:上,S:下,A:左,D:右。Q:退出");
printf(" ");
}
//接收小人的方向
charenterDirection(){
//清除SCANF中的缓冲区
rewind(stdin);
chardir;
dir=getch();
//scanf("%c",&dir);
returndir;
}
//小人向上移动的方法
voidmoveToUp(){
//小人的下一个坐标
intnextPersonCol=currentPersonCol;
intnextPersonRow=currentPersonRow-1;
//箱子的下一个坐标
intnextBoxRow=currentBoxRow-1;
intnextBoxCol=currentBoxCol;

//如果小人的下一个坐标是路
if(map[nextPersonRow][nextPersonCol]==''){
map[nextPersonRow][nextPersonCol]='A';
map[currentPersonRow][currentPersonCol]='';
currentPersonRow=nextPersonRow;
currentPersonCol=nextPersonCol;
}
//如果小人的下一个坐标是墙
if(map[nextPersonRow][nextPersonCol]=='#'){
//什么也不做
}
//如果小人的下一个坐标是箱子
if(map[nextPersonRow][nextPersonCol]=='X'){
if(map[nextBoxRow][nextBoxCol]==''){

map[nextPersonRow][nextPersonCol]='A';
map[currentPersonRow][currentPersonCol]='';

map[nextBoxRow][nextBoxCol]='X';
map[currentBoxRow][currentBoxCol]='A';


currentPersonRow=nextPersonRow;
currentPersonCol=nextPersonCol;
currentBoxRow=nextBoxRow;
currentBoxCol=nextBoxCol;
}
}
}
//小人向下移动的方法
voidmoveToDown(){
//小人的下一个坐标
intnextPersonCol=currentPersonCol;
intnextPersonRow=currentPersonRow+1;
//箱子的下一个坐标
intnextBoxRow=currentBoxRow+1;
intnextBoxCol=currentBoxCol;

//如果小人的下一个坐标是路
if(map[nextPersonRow][nextPersonCol]==''){
map[nextPersonRow][nextPersonCol]='A';
map[currentPersonRow][currentPersonCol]='';
currentPersonRow=nextPersonRow;
currentPersonCol=nextPersonCol;
}
//如果小人的下一个坐标是墙
if(map[nextPersonRow][nextPersonCol]=='#'){
//什么也不做
}
//如果小人的下一个坐标是箱子
if(map[nextPersonRow][nextPersonCol]=='X'){
if(map[nextBoxRow][nextBoxCol]==''){

map[nextPersonRow][nextPersonCol]='A';
map[currentPersonRow][currentPersonCol]='';

map[nextBoxRow][nextBoxCol]='X';
map[currentBoxRow][currentBoxCol]='A';

currentPersonRow=nextPersonRow;
currentPersonCol=nextPersonCol;
currentBoxRow=nextBoxRow;
currentBoxCol=nextBoxCol;
}
}
}
//小人向右移动的方法
voidmoveToRight(){
//小人的下一个坐标
intnextPersonCol=currentPersonCol+1;
intnextPersonRow=currentPersonRow;
//箱子的下一个坐标
intnextBoxRow=currentBoxRow;
intnextBoxCol=currentBoxCol+1;

//如果小人的下一个坐标是路
if(map[nextPersonRow][nextPersonCol]==''){
map[nextPersonRow][nextPersonCol]='A';
map[currentPersonRow][currentPersonCol]='';
currentPersonRow=nextPersonRow;
currentPersonCol=nextPersonCol;
}
//如果小人的下一个坐标是墙
if(map[nextPersonRow][nextPersonCol]=='#'){
//什么也不做
}
//如果小人的下一个坐标是箱子
if(map[nextPersonRow][nextPersonCol]=='X'){
if(map[nextBoxRow][nextBoxCol]==''){

map[nextPersonRow][nextPersonCol]='A';
map[currentPersonRow][currentPersonCol]='';

map[nextBoxRow][nextBoxCol]='X';
map[currentBoxRow][currentBoxCol]='A';

currentPersonRow=nextPersonRow;
currentPersonCol=nextPersonCol;
currentBoxRow=nextBoxRow;
currentBoxCol=nextBoxCol;
}
}
}
//小人向左移动的方法
voidmoveToLeft(){
//小人的下一个坐标
intnextPersonCol=currentPersonCol-1;
intnextPersonRow=currentPersonRow;
//箱子的下一个坐标
intnextBoxRow=currentBoxRow;
intnextBoxCol=currentBoxCol-1;

//如果小人的下一个坐标是路
if(map[nextPersonRow][nextPersonCol]==''){
map[nextPersonRow][nextPersonCol]='A';
map[currentPersonRow][currentPersonCol]='';
currentPersonRow=nextPersonRow;
currentPersonCol=nextPersonCol;
}
//如果小人的下一个坐标是墙
if(map[nextPersonRow][nextPersonCol]=='#'){
//什么也不做
}
//如果小人的下一个坐标是箱子
if(map[nextPersonRow][nextPersonCol]=='X'){
if(map[nextBoxRow][nextBoxCol]==''){
map[nextPersonRow][nextPersonCol]='A';
map[currentPersonRow][currentPersonCol]='';

map[nextBoxRow][nextBoxCol]='X';
map[currentBoxRow][currentBoxCol]='A';

currentPersonRow=nextPersonRow;
currentPersonCol=nextPersonCol;
currentBoxRow=nextBoxRow;
currentBoxCol=nextBoxCol;
}
}
}

⑹ C语怎么写窗口

C语言在windows当然可以写窗口的,早期的窗口很多是用C而非C++写的只是现在很少有人这样做了(因为有MFC,VCL,QT)以下是一个EX:#include<windows.h>/**/LRESULTCALLBACKWndProc(HWNDhwnd,UINTMessage,WPARAMwParam,LPARAMlParam){switch(Message){/*Upondestruction,tellthemainthreadtostop*/caseWM_DESTROY:{PostQuitMessage(0);break;}/*Allothermessages(alotofthem)*/default:returnDefWindowProc(hwnd,Message,wParam,lParam);}return0;}/*The'main'functionofWin32GUIprograms:thisiswhereexecutionstarts*/intWINAPIWinMain(HINSTANCEhInstance,HINSTANCEhPrevInstance,LPSTRlpCmdLine,intnCmdShow){WNDCLASSEXwc;/*Apropertiesstructofourwindow*/HWNDhwnd;/*A'HANDLE',hencetheH,orapointertoourwindow*/MSGMsg;/**//**/memset(&wc,0,sizeof(wc));wc.cbSize=sizeof(WNDCLASSEX);wc.lpfnWndProc=WndProc;/**/wc.hInstance=hInstance;wc.hCursor=LoadCursor(NULL,IDC_ARROW);/*White,COLOR_WINDOWisjusta#defineforasystemcolor,tryCtrl+Clickingit*/wc.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);wc.lpszClassName="WindowClass";wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);/*Loadastandardicon*/wc.hIconSm=LoadIcon(NULL,IDI_APPLICATION);/*usethename"A"tousetheprojecticon*/if(!RegisterClassEx(&wc)){MessageBox(NULL,"WindowRegistrationFailed!","Error!",MB_ICONEXCLAMATION|MB_OK);return0;}hwnd=CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Caption",WS_VISIBLE|WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,/*x*/CW_USEDEFAULT,/*y*/640,/*width*/480,/*height*/NULL,NULL,hInstance,NULL);if(hwnd==NULL){MessageBox(NULL,"WindowCreationFailed!","Error!",MB_ICONEXCLAMATION|MB_OK);return0;}/*.,*/while(GetMessage(&Msg,NULL,0,0)>0){/*Ifnoerrorisreceived...*/TranslateMessage(&Msg);/**/DispatchMessage(&Msg);/*SendittoWndProc*/}returnMsg.wParam;}

⑺ 如何用C语言编写小游戏

这种小游戏其实就是一个大型的while循环。
初始化之后开始游戏,掉入while循环,在while循环里面的每一回合,得到玩家的鼠标和键盘输入,通过调用函数更新画面,输出画面,直至玩家选择退出游戏,结束while循环,释放储存空间,退出游戏。
想编小游戏的话,可以看看清华大学出版社的《C语言课程设计与游戏开发实践教程》,基于easyX开发小游戏。

⑻ 怎样用c语言编写一个界面好看的游戏 例如 俄罗斯方块

别说做游戏了,做一个正规的窗口都困难得要死!!!

自己看吧。这就是用C语言做的最最简单的一个窗口:

#include<tchar.h>
#include<windows.h>
/*这个函数由Windows内部函数DispatchMessage()调用*/
LRESULTCALLBACKWindowProcere(HWNDhWnd,UINTmessage,WPARAMwParam,LPARAMlParam)
{
HDChdc;
PAINTSTRUCTps;
RECTrect;
switch(message)/*处理信息*/
{
caseWM_DESTROY:
PostQuitMessage(0);/*发送WM_QUIT到消息队列*/
break;
caseWM_PAINT:
hdc=BeginPaint(hWnd,&ps);
GetClientRect(hWnd,&rect);
DrawText(hdc,TEXT("Hello,WindowsNT!"),-1,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
EndPaint(hWnd,&ps);
break;
default:/*不处理的消息*/
returnDefWindowProc(hWnd,message,wParam,lParam);
}
return0;
}
/*下面是主函数*/
intWINAPI_tWinMain(HINSTANCEhThisInstance,HINSTANCEhPrevInstance,LPTSTRlpCmdLine,intnFunsterStil)
{
HWNDhWnd;/*这是窗口的句柄*/
MSGmessages;/*应用程序的消息保存在这里*/
WNDCLASSEXwincl;/*窗口类的数据结构*/
TCHARszClassName[]=TEXT("WindowsApp");/*窗口类的类名*/
/*窗口结构*/
wincl.hInstance=hThisInstance;
wincl.lpszClassName=szClassName;
wincl.lpfnWndProc=WindowProcere;/*这个函数由Windows操作系统调用*/
wincl.style=CS_DBLCLKS;/*获取双击指令*/
wincl.cbSize=sizeof(WNDCLASSEX);
/*使用默认图标和鼠标指针*/
wincl.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wincl.hIconSm=LoadIcon(NULL,IDI_APPLICATION);
wincl.hCursor=LoadCursor(NULL,IDC_ARROW);
wincl.lpszMenuName=NULL;/*没有菜单*/
wincl.cbClsExtra=0;/*窗口类后面没有多余的字节*/
wincl.cbWndExtra=0;/*结构或者窗口实例*/
/*使用窗口的默认颜色作为窗口的背景色*/
wincl.hbrBackground=(HBRUSH)COLOR_BACKGROUND;
/*注册窗口类。如果注册失败,那么就退出程序*/
if(!RegisterClassEx(&wincl))
return0;
/*窗口类已被注册。创建它的程序*/
hWnd=CreateWindowEx(
0,
szClassName,/*类名*/
TEXT("WindowsApp"),/*窗口标题栏的文字*/
WS_OVERLAPPEDWINDOW,/*默认窗口*/
CW_USEDEFAULT,/*窗口左上角的位置*/
CW_USEDEFAULT,/*窗口右下角的位置*/
544,/*窗口宽度(以“像素”位单位)*/
375,/*窗口高度(以“像素”位单位)*/
HWND_DESKTOP,/*窗口是桌面的子窗口*/
NULL,/*该窗口无菜单*/
hThisInstance,/*程序实例的句柄*/
NULL/*没有窗口创建的数据*/
);
/*显示窗口*/
ShowWindow(hWnd,nFunsterStil);
/*重绘窗口*/
UpdateWindow(hWnd);
/*运行消息循环。循环到GetMessage()函数返回0*/
while(GetMessage(&messages,NULL,0,0))
{
/*把虚拟信息翻译成字符信息*/
TranslateMessage(&messages);
/*发送信息到窗口过程*/
DispatchMessage(&messages);
}
/*返回PostQuitMessage()函数的返回值*/
returnmessages.wParam;
}

⑼ 用C语言编写的小游戏代码是什么

/*贪吃蛇*/

#include<stdio.h>

#include<time.h>

#include<conio.h>

#include<stdlib.h>

int head=3 ,tail=0;

int main()

{

int i,j,k=0;

int zuobiao[2][80];

long start;

int direction=77;

int gamespeed;

int timeover;

int change(char qipan[20][80],

int zuobiao[2][80],

char direction);

zuobiao[0][tail]=1;

zuobiao[1][tail]=1;

zuobiao[0][1]=1;

zuobiao[1][1]=2;zuobiao[0

[2]=1;

zuobiao[1][2]=3;

zuobiao[0][head]=1;

zuobiao[1][head]=4;

/*处理棋盘*/

char qipan[20][80];

//定义棋盘

for(i=0;i<20;i++)

for(j=0;j<80;j++)

qipan[i][j]=' ';//初始化棋盘

for(i=0;i<80;i++)

qipan[0][i]='_';

for(i=0;i<20;i++)

qipan[i][0]='|';

for(i=0;i<20;i++)

qipan[i][79]='|';

for(i=0;i<80;i++)

C语言是一门通用计算机编程语言,应用广泛。C语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。

⑽ 怎样用C语言编写一个小游戏

“贪吃蛇”C代码:

#include <stdio.h>

#include <stdlib.h>

#include <conio.h>

#include <time.h>

#include <Windows.h>

#define W 78 //游戏框的宽,x轴

#define H 26 //游戏框的高,y轴

int dir=3; //方向变量,初值3表示向“左”

int Flag=0; //吃了食物的标志(1是0否)

int score=0; //玩家得分

struct food{ int x; //食物的x坐标

int y; //食物的y坐标

}fod; //结构体fod有2个成员

struct snake{ int len; //身长

int speed; //速度

int x[100];

int y[100];

}snk; //结构体snk有4个成员

void gtxy( int x,int y) //控制光标移动的函数

{ COORD coord;

coord.X=x;

coord.Y=y;

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);

}

void gtxy( int x,int y); //以下声明要用到的几个自编函数

void csh( ); //初始化界面

void keymove( ); //按键操作移动蛇

void putFod( ); //投放食物

int Over( ); //游戏结束(1是0否)

void setColor(unsigned short p, unsigned short q); //设定显示颜色

int main( ) //主函数

{ csh( );

while(1)

{ Sleep(snk.speed);

keymove( );

putFod( );

if(Over( ))

{system(“cls”);

gtxy(W/2+1,H/2); printf(“游戏结束!T__T”);

gtxy(W/2+1,H/2+2); printf(“玩家总分:%d分”,score);

getch( );

break;

}

}

return 0;

}

void csh( ) //初始化界面

{ int i;

gtxy(0,0);

CONSOLE_CURSOR_INFO cursor_info={1,0}; //以下两行是隐藏光标的设置

SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);

for(i=0;i<=W;i=i+2) //横坐标要为偶数,因为这个要打印的字符占2个位置

{ setColor(2, 0); //设定打印颜色为绿字黑底

gtxy(i,0); printf("■"); //打印上边框

gtxy(i,H); printf("■"); //打印下边框

}

for(i=1;i<H;i++)

{ gtxy(0,i); printf("■"); //打印左边框

gtxy(W,i); printf("■"); //打印右边框

}

while(1)

{ srand((unsigned)time(NULL)); //初始化随机数发生器srand( )

fod.x=rand()%(W-4)+2; //随机函数rand( )产生一个从0到比”(W-4)”小1的数再加2

fod.y=rand()%(H-2)+1; //随机函数rand( )产生一个从0到比”(H-2)”小1的数再加1

if (fod.x%2==0) break; //fod.x是食物的横坐标,要是2的倍数(为偶数)

}

setColor(12, 0); //设定打印颜色为淡红字黑底

gtxy(fod.x,fod.y); printf("●"); //到食物坐标处打印初试食物

snk.len=3; //蛇身长

snk.speed=350; //刷新蛇的时间,即是移动速度

snk.x[0]=W/2+1; //蛇头横坐标要为偶数(因为W/2=39)

snk.y[0]=H/2; //蛇头纵坐标

setColor(9, 0); //设定打印颜色为淡蓝字黑底

gtxy(snk.x[0], snk.y[0]); printf("■"); //打印蛇头

for(i=1;i<snk.len;i++)

{ snk.x[i]=snk.x[i-1]+2; snk.y[i]=snk.y[i-1];

gtxy(snk.x[i],snk.y[i]); printf("■"); //打印蛇身

}

setColor(7, 0); //恢复默认的白字黑底

return;

}

void keymove( ) //按键操作移动蛇

{ int key;

if( kbhit( ) ) //如有按键输入才执行下面操作

{ key=getch( );

if (key==224) //值为224表示按下了方向键,下面要再次获取键值

{ key=getch( );

if(key==72&&dir!=2)dir=1; //72表示按下了向上方向键

if(key==80&&dir!=1)dir=2; //80为向下

if(key==75&&dir!=4)dir=3; //75为向左

if(key==77&&dir!=3)dir=4; //77为向右

}

if (key==32)

{ while(1) if((key=getch( ))==32) break; } //32为空格键,这儿用来暂停

}

if (Flag==0) //如没吃食物,才执行下面操作擦掉蛇尾

{ gtxy(snk.x[snk.len-1],snk.y[snk.len-1]); printf(" "); }

int i;

for (i = snk.len - 1; i > 0; i--) //从蛇尾起每节存储前一节坐标值(蛇头除外)

{ snk.x[i]=snk.x[i-1]; snk.y[i]=snk.y[i-1]; }

switch (dir) //判断蛇头该往哪个方向移动,并获取最新坐标值

{ case 1: snk.y[0]--; break; //dir=1要向上移动

case 2: snk.y[0]++; break; //dir=2要向下移动

case 3: snk.x[0]-=2; break; //dir=3要向左移动

case 4: snk.x[0]+=2; break; //dir=4要向右移动

}

setColor(9, 0);

gtxy(snk.x[0], snk.y[0]); printf("■"); //打印蛇头

if (snk.x[0] == fod.x && snk.y[0] == fod.y) //如吃到食物则执行以下操作

{ printf("07"); snk.len++; score += 100; snk.speed -= 5; Flag = 1; } //007是响铃

else Flag = 0; //没吃到食物Flag的值为0

if(snk.speed<150) snk.speed= snk.speed+5; //作弊码,不让速度无限加快

}

void putFod( ) //投放食物

{ if (Flag == 1) //如吃到食物才执行以下操作,生成另一个食物

{ while (1)

{ int i,n= 1;

srand((unsigned)time(NULL)); //初始化随机数发生器srand( )

fod.x = rand( ) % (W - 4) + 2; //产生在游戏框范围内的一个x坐标值

fod.y = rand( ) % (H - 2) + 1; //产生在游戏框范围内的一个y坐标值

for (i = 0; i < snk.len; i++) //随机生成的食物不能在蛇的身体上

{ if (fod.x == snk.x[i] &&fod.y == snk.y[i]) { n= 0; break;} }

if (n && fod.x % 2 == 0) break; //n不为0且横坐标为偶数,则食物坐标取值成功

}

setColor(12, 0);

gtxy(fod.x, fod.y); printf("●"); //光标到取得的坐标处打印食物

}

return;

}

int Over( ) //判断游戏是否结束的函数

{ int i;

setColor(7, 0);

gtxy(2,H+1); printf(“暂停键:space.”); //以下打印一些其它信息

gtxy(2,H+2); printf(“游戏得分:%d”,score);

if (snk.x[0] == 0 || snk.x[0] == W) return 1; //蛇头触碰左右边界

if (snk.y[0] == 0 || snk.y[0] == H) return 1; //蛇头触碰上下边界

for (i = 1; i < snk.len; i++)

{ if (snk.x[0] == snk.x[i] && snk.y[0] == snk.y[i]) return 1; } //蛇头触碰自身

return 0; //没碰到边界及自身时就返回0

}

void setColor(unsigned short ForeColor = 7, unsigned short BackGroundColor = 0)

{ HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);

SetConsoleTextAttribute( handle, ForeColor + BackGroundColor * 0x10 );

} //用来设定颜色的函数