當前位置:首頁 » 編程語言 » 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 );

} //用來設定顏色的函數