當前位置:編程學習大全網 - 網絡軟體 - c語言小遊戲代碼

c語言小遊戲代碼

最基礎的貪吃蛇的代碼

#include<stdio.h>

#include<windows.h>//基本型態定義。支援型態定義函數。使用者界面函數 圖形裝置界面函數。

#include<conio.h> //用戶通過按鍵盤產生的對應操作 (控制臺)

#include<stdlib.h>

#include<time.h> //日期和時間頭文件

#define LEN 30

#define WID 25

int Snake[LEN][WID] = {0}; //數組的元素代表蛇的各個部位

char Sna_Hea_Dir = 'a';//記錄蛇頭的移動方向

int Sna_Hea_X, Sna_Hea_Y;//記錄蛇頭的位置

int Snake_Len = 3;//記錄蛇的長度

clock_t Now_Time;//記錄當前時間,以便自動移動

int Wait_Time ;//記錄自動移動的時間間隔

int Eat_Apple = 1;//吃到蘋果表示為1

int Level ;

int All_Score = -1;

int Apple_Num = -1;

HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); //獲取標準輸出的句柄 <windows.h>

//句柄 :標誌應用程序中的不同對象和同類對象中的不同的實例 方便操控,

void gotoxy(int x, int y)//設置光標位置

{

COORD pos = {x,y}; //定義壹個字符在控制臺屏幕上的坐標POS

SetConsoleCursorPosition(hConsole, pos); //定位光標位置的函數<windows.h>

}

void Hide_Cursor()//隱藏光標 固定函數

{

CONSOLE_CURSOR_INFO cursor_info = {1, 0};

SetConsoleCursorInfo(hConsole, &cursor_info);

}

void SetColor(int color)//設置顏色

{

SetConsoleTextAttribute(hConsole, color);

//是API設置字體顏色和背景色的函數 格式:SetConsoleTextAttribute(句柄,顏色);

}

void Print_Snake()//打印蛇頭和蛇的脖子和蛇尾

{

int iy, ix, color;

for(iy = 0; iy < WID; ++iy)

for(ix = 0; ix < LEN; ++ix)

{

if(Snake[ix][iy] == 1)//蛇頭

{

SetColor(0xf); //oxf代表分配的內存地址 setcolor:34行自定義設置顏色的函數

gotoxy(ix*2, iy);

printf("※");

}

if(Snake[ix][iy] == 2)//蛇的脖子

{

color = rand()%15 + 1; //rand()函數是產生隨機數的壹個隨機函數。C語言裏還有 srand()函數等。

//頭文件: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 < WID; ++iy)

for(ix = 0; ix < LEN; ++ix)

{

gotoxy(ix*2, iy);

if(Snake[ix][iy] == Snake_Len)

printf(" ");

}

}

void Rand_Apple()//隨機產生蘋果

{

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("Game Over");

Sleep(3000);

system("pause > nul");

exit(0);

}

void Move_Snake()//讓蛇動起來

{

int ix, iy;

for(ix = 0; ix < LEN; ++ix)//先標記蛇頭

for(iy = 0; iy < WID; ++iy)

if(Snake[ix][iy] == 1)

{

switch(Sna_Hea_Dir)//根據新的蛇頭方向標誌蛇頭

{

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] < 0)//吃到蘋果

{

++Snake_Len;

Eat_Apple = 1;

}

for(ix = 0; ix < LEN; ++ix)//處理蛇尾

for(iy = 0; iy < WID; ++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()//控制蛇的移動方向

{

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)//蛇到時間自動行走

{

Clear_Snake();

Move_Snake();

Print_Snake();

Now_Time = clock();

}

}

void Init()//初始化

{

system("title 貪吃毛毛蛇");

system("mode con: cols=80 lines=25");

Hide_Cursor();

gotoxy(61, 4);

printf("You Score:");

gotoxy(61, 6);

printf("You Level:");

gotoxy(61, 8);

printf("The Lenght:");

gotoxy(61, 10);

printf("The Speed:");

gotoxy(61, 12);

printf("Apple Num:");

int i;

for(i = 0; i < Snake_Len; ++i)//生成蛇

Snake[10+i][15] = i+1;

int iy, ix;//打印蛇

for(iy = 0; iy < WID; ++iy)

for(ix = 0; ix < LEN; ++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("%3d", All_Score);

gotoxy(73, 6);

printf("%3d", Level);

gotoxy(73, 8);

printf("%3d",Snake_Len);

gotoxy(73, 10);

printf("0.%3ds", Wait_Time/10);

gotoxy(73, 12);

printf("%d", Apple_Num);

}

void Lev_Sys()//等級系統

{

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));//設置隨機數的種子

Now_Time = clock();

int speed1=1000,speed2,a;

printf("\n");

printf("請輸入妳想要的速度\n");

scanf("%d",&speed2);

Level=1;

Wait_Time=speed1-speed2;

printf("請輸入妳想要的蘋果數\n");

scanf("%d",&a);

while(a--)

Rand_Apple();

while(1)

{

if(Eat_Apple)

{

++Apple_Num;

Rand_Apple();

Lev_Sys();

Pri_News();

}

Get_Input();

Sleep(10);

}

return 0;

}

  • 上一篇:什麽是智慧用電安全管理系統
  • 下一篇:2022愛眼護眼手抄報簡單又好看
  • copyright 2024編程學習大全網