- 相關(guān)推薦
C語言小游戲
最基礎(chǔ)的貪吃蛇的代碼
#include
#include//基本型態(tài)定義。支援型態(tài)定義函數(shù)。使用者界面函數(shù) 圖形裝置界面函數(shù)。
#include //用戶通過按鍵盤產(chǎn)生的對(duì)應(yīng)操作 (控制臺(tái)) #include
#include //日期和時(shí)間頭文件
#define LEN 30
#define WID 25
int Snake[LEN][WID] = {0}; //數(shù)組的元素代表蛇的各個(gè)部位 char Sna_Hea_Dir = 'a';//記錄蛇頭的移動(dòng)方向
int Sna_Hea_X, Sna_Hea_Y;//記錄蛇頭的位置
int Snake_Len = 3;//記錄蛇的長度
clock_t Now_Time;//記錄當(dāng)前時(shí)間,以便自動(dòng)移動(dòng)
int Wait_Time ;//記錄自動(dòng)移動(dòng)的時(shí)間間隔
int Eat_Apple = 1;//吃到蘋果表示為1
int Level ;
int All_Score = -1;
int Apple_Num = -1;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); //獲取標(biāo)準(zhǔn)輸出的句柄
//句柄 :標(biāo)志應(yīng)用程序中的不同對(duì)象和同類對(duì)象中的不同的實(shí)例 方便操控, void gotoxy(int x, int y)//設(shè)置光標(biāo)位置
{
COORD pos = {x,y}; //定義一個(gè)字符在控制臺(tái)屏幕上的坐標(biāo)POS
//定位光標(biāo)位置的函數(shù) SetConsoleCursorPosition(hConsole, pos);
}
void Hide_Cursor()//隱藏光標(biāo) 固定函數(shù)
{
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(hConsole, &cursor_info);
}
void SetColor(int color)//設(shè)置顏色
{
SetConsoleTextAttribute(hConsole, color);
//是API設(shè)置字體顏色和背景色的函數(shù) 格式:SetConsoleTextAttribute(句柄,顏色);
}
void Print_Snake()//打印蛇頭和蛇的脖子和蛇尾
{
int iy, ix, color;
for(iy = 0; iy
for(ix = 0; ix
{
if(Snake[ix][iy] == 1)//蛇頭
{
SetColor(0xf); //oxf代表分配的內(nèi)存地址 setcolor:34行自定義設(shè)置顏色的函數(shù)
gotoxy(ix*2, iy);
printf(
}
if(Snake[ix][iy] == 2)//蛇的脖子
{
color = rand()%15 + 1; //rand()函數(shù)是產(chǎn)生隨機(jī)數(shù)的一個(gè)隨機(jī)函數(shù)。C語言里還有 srand()函數(shù)等。
//頭文件:stdlib.h
if(color == 14)
color -= rand() % 13 + 1;
SetColor(color);
gotoxy(ix*2, iy);
printf(
}
if(Snake[ix][iy] == Snake_Len)
{
gotoxy(ix*2, iy);
SetColor(0xe);
printf(
} //變色
}
}
void Clear_Snake()//擦除貪吃蛇
{
int iy, ix;
for(iy = 0; iy
for(ix = 0; ix
{
gotoxy(ix*2, iy);
if(Snake[ix][iy] == Snake_Len)
printf(
}
}
void Rand_Apple()//隨機(jī)產(chǎn)生蘋果
{
int ix, iy;
do
{
ix = rand() % LEN;
iy = rand() % WID;
}while(Snake[ix][iy]);
Snake[ix][iy] = -1;
gotoxy(ix*2, iy);
printf(
Eat_Apple = 0;
}
void Game_Over()//蛇死掉了
{
gotoxy(30, 10);
printf(
Sleep(3000);
system(
exit(0);
}
void Move_Snake()//讓蛇動(dòng)起來
{
int ix, iy;
for(ix = 0; ix
for(iy = 0; iy
if(Snake[ix][iy] == 1)
{
switch(Sna_Hea_Dir)//根據(jù)新的蛇頭方向標(biāo)志蛇頭 {
case 'w':
if(iy == 0)
Game_Over();
else
Sna_Hea_Y = iy - 1;
Sna_Hea_X = ix;
break;
case 's':
if(iy == (WID -1))
Game_Over();
else
Sna_Hea_Y = iy + 1;
Sna_Hea_X = ix;
break;
case 'a':
if(ix == 0)
Game_Over();
else
Sna_Hea_X = ix - 1;
Sna_Hea_Y = iy;
break;
case 'd':
if(ix == (LEN - 1))
Game_Over();
else
Sna_Hea_X = ix + 1;
Sna_Hea_Y = iy;
break;
default:
break;
}
}
if(Snake[Sna_Hea_X][Sna_Hea_Y]!=1&&Snake[Sna_Hea_X][Sna_Hea_Y]!=0&&Snake[Sna_Hea_X][Sna_Hea_Y]!=-1)
Game_Over();
if(Snake[Sna_Hea_X][Sna_Hea_Y]
{
++Snake_Len;
Eat_Apple = 1;
}
for(ix = 0; ix
for(iy = 0; iy
{
if(Snake[ix][iy] > 0)
{
if(Snake[ix][iy] != Snake_Len)
Snake[ix][iy] += 1;
else
Snake[ix][iy] = 0;
}
}
Snake[Sna_Hea_X][Sna_Hea_Y] = 1;//處理蛇頭
}
void Get_Input()//控制蛇的移動(dòng)方向
{
if(kbhit())
{
switch(getch())
{
case 87:
Sna_Hea_Dir = 'w';
break;
case 83:
Sna_Hea_Dir = 's';
break;
case 65:
Sna_Hea_Dir = 'a';
break;
case 68:
Sna_Hea_Dir = 'd';
break;
default:
break;
}
}
if(clock() - Now_Time >= Wait_Time)//蛇到時(shí)間自動(dòng)行走 {
Clear_Snake();
Move_Snake();
Print_Snake();
Now_Time = clock();
}
}
void Init()//初始化
{
system(
system(
Hide_Cursor();
gotoxy(61, 4);
printf(
gotoxy(61, 6);
printf(
gotoxy(61, 8);
printf(
gotoxy(61, 10);
printf(
gotoxy(61, 12);
printf(
int i;
for(i = 0; i
Snake[10+i][15] = i+1;
int iy, ix;//打印蛇
for(iy = 0; iy
for(ix = 0; ix
{
if(Snake[ix][iy])
{
SetColor(Snake[ix][iy]); gotoxy(ix*2, iy);
printf(
}
}
}
void Pri_News()//打印信息
{
SetColor(0xe);
gotoxy(73,4);
All_Score += Level;
printf(
gotoxy(73, 6);
printf(
gotoxy(73, 8);
printf(
gotoxy(73, 10);
printf(
gotoxy(73, 12);
printf(
}
void Lev_Sys()//等級(jí)系統(tǒng)
{
if(((Apple_Num-1) / 10) == Level)
{
++Level;
if(Wait_Time > 50)
Wait_Time -= 50;
else
if(Wait_Time > 10)
Wait_Time -= 10;
else
Wait_Time -= 1;
}
}
int main(void)
{
Init();
srand((unsigned)time(NULL));//設(shè)置隨機(jī)數(shù)的種子 Now_Time = clock();
int speed1=1000,speed2,a;
printf(
printf(
scanf(
Level=1;
Wait_Time=speed1-speed2;
printf(
scanf(
while(a--)
Rand_Apple(); while(1)
{
if(Eat_Apple) {
++Apple_Num; Rand_Apple(); Lev_Sys(); Pri_News(); }
Get_Input(); Sleep(10); }
return 0;
}
【C語言小游戲】相關(guān)文章:
c語言心得04-24
C語言課程教案04-24
c語言讀書筆記03-23
C語言實(shí)習(xí)心得01-01
C語言基礎(chǔ)實(shí)驗(yàn)報(bào)告10-11
c語言實(shí)踐報(bào)告模型10-28
c語言學(xué)習(xí)心得11-05
c語言課程設(shè)計(jì)心得04-24
c語言心得體會(huì)04-30