回覆列表
  • 1 # 賣女孩的柴火

    最基礎的貪吃蛇的程式碼#include<stdio.h>#include<windows.h>//基本型態定義。支援型態定義函式。使用者介面函式 圖形裝置介面函式。#include<conio.h> //使用者透過按鍵盤產生的對應操作 (控制檯)#include<stdlib.h>#include<time.h> //日期和時間標頭檔案#define LEN 30#define WID 25int 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;//吃到蘋果表示為1int 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}; //定義一個字元在控制檯螢幕上的座標POSSetConsoleCursorPosition(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.hif(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();elseSna_Hea_Y = iy - 1;Sna_Hea_X = ix;break;case "s":if(iy == (WID -1))Game_Over();elseSna_Hea_Y = iy + 1;Sna_Hea_X = ix;break;case "a":if(ix == 0)Game_Over();elseSna_Hea_X = ix - 1;Sna_Hea_Y = iy;break;case "d":if(ix == (LEN - 1))Game_Over();elseSna_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;elseSnake[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;elseif(Wait_Time > 10)Wait_Time -= 10;elseWait_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;}

  • 中秋節和大豐收的關聯?
  • 秋天的景色是什麼顏色,有哪些水果成熟了?有哪些花兒盛開了?