c語言控制台游戲
㈠ c語言中怎麼寫打地鼠的游戲
在一個二維數組中,存放標記。
設置難度:分:
一次只顯示一個
一次只顯示二個
一次只顯示三個
對應產生2*N個隨機數,對應(x,y),設置為1,其它空的設置為0.
大概的游戲流程循環:
結束條件: 打到M個停止。
1,產生隨機數,且不相同
2,在對應的坐標中,畫出地鼠,
在N秒內,沒有擊中,地鼠消失;擊中,加分。
(擊中的坐標,要與用戶點的坐標想比較即可,在這個范圍就得分)
㈡ 求c語言控制面板貪吃蛇源代碼
//******友情提示:如想速度快點,請改小_sleep(500)函數中參數*****
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <time.h>
const int H = 8; //地圖的高
const int L = 16; //地圖的長
char GameMap[H][L]; //游戲地圖
int key; //按鍵保存
int sum = 1, over = 0; //蛇的長度, 游戲結束(自吃或碰牆)
int dx[4] = {0, 0, -1, 1}; //左、右、上、下的方向
int dy[4] = {-1, 1, 0, 0};
struct Snake //蛇的每個節點的數據類型
{
int x, y; //左邊位置
int now; //保存當前節點的方向, 0,1,2,3分別為左右上下
}Snake[H*L];
const char Shead = '@'; //蛇頭
const char Sbody = '#'; //蛇身
const char Sfood = '*'; //食物
const char Snode = '.'; //'.'在地圖上標示為空
void Initial(); //地圖的初始化
void Create_Food(); //在地圖上隨機產生食物
void Show(); //刷新顯示地圖
void Button(); //取出按鍵,並判斷方向
void Move(); //蛇的移動
void Check_Border(); //檢查蛇頭是否越界
void Check_Head(int x, int y); //檢查蛇頭移動後的位置情況
int main()
{
Initial();
Show();
return 0;
}
void Initial() //地圖的初始化
{
int i, j;
int hx, hy;
system("title 貪吃蛇"); //控制台的標題
memset(GameMap, '.', sizeof(GameMap)); //初始化地圖全部為空'.'
system("cls");
srand(time(0)); //隨機種子
hx = rand()%H; //產生蛇頭
hy = rand()%L;
GameMap[hx][hy] = Shead;
Snake[0].x = hx; Snake[0].y = hy;
Snake[0].now = -1;
Create_Food(); //隨機產生食物
for(i = 0; i < H; i++) //地圖顯示
{
for(j = 0; j < L; j++)
printf("%c", GameMap[i][j]);
printf("\n");
}
printf("\n小小C語言貪吃蛇\n");
printf("按任意方向鍵開始游戲\n");
getch(); //先接受一個按鍵,使蛇開始往該方向走
Button(); //取出按鍵,並判斷方向
}
void Create_Food() //在地圖上隨機產生食物
{
int fx, fy;
while(1)
{
fx = rand()%H;
fy = rand()%L;
if(GameMap[fx][fy] == '.') //不能出現在蛇所佔有的位置
{
GameMap[fx][fy] = Sfood;
break;
}
}
}
void Show() //刷新顯示地圖
{
int i, j;
while(1)
{
_sleep(500); //延遲半秒(1000為1s),即每半秒刷新一次地圖
Button(); //先判斷按鍵在移動
Move();
if(over) //自吃或碰牆即游戲結束
{
printf("\n**游戲結束**\n");
printf(" >_<\n");
getchar();
break;
}
system("cls"); //清空地圖再顯示刷新吼的地圖
for(i = 0; i < H; i++)
{
for(j = 0; j < L; j++)
printf("%c", GameMap[i][j]);
printf("\n");
}
printf("\n小小C語言貪吃蛇\n");
printf("按任意方向鍵開始游戲\n");
}
}
void Button() //取出按鍵,並判斷方向
{
if(kbhit() != 0) //檢查當前是否有鍵盤輸入,若有則返回一個非0值,否則返回0
{
while(kbhit() != 0) //可能存在多個按鍵,要全部取完,以最後一個為主
key = getch(); //將按鍵從控制台中取出並保存到key中
switch(key)
{ //左
case 75: Snake[0].now = 0;
break;
//右
case 77: Snake[0].now = 1;
break;
//上
case 72: Snake[0].now = 2;
break;
//下
case 80: Snake[0].now = 3;
break;
}
}
}
void Move() //蛇的移動
{
int i, x, y;
int t = sum; //保存當前蛇的長度
//記錄當前蛇頭的位置,並設置為空,蛇頭先移動
x = Snake[0].x; y = Snake[0].y; GameMap[x][y] = '.';
Snake[0].x = Snake[0].x + dx[ Snake[0].now ];
Snake[0].y = Snake[0].y + dy[ Snake[0].now ];
Check_Border(); //蛇頭是否越界
Check_Head(x, y); //蛇頭移動後的位置情況,參數為: 蛇頭的開始位置
if(sum == t) //未吃到食物即蛇身移動哦
for(i = 1; i < sum; i++) //要從蛇尾節點向前移動哦,前一個節點作為參照
{
if(i == 1) //尾節點設置為空再移動
GameMap[ Snake[i].x ][ Snake[i].y ] = '.';
if(i == sum-1) //為蛇頭後面的蛇身節點,特殊處理
{
Snake[i].x = x;
Snake[i].y = y;
Snake[i].now = Snake[0].now;
}
else //其他蛇身即走到前一個蛇身位置
{
Snake[i].x = Snake[i+1].x;
Snake[i].y = Snake[i+1].y;
Snake[i].now = Snake[i+1].now;
}
GameMap[ Snake[i].x ][ Snake[i].y ] = '#'; //移動後要置為'#'蛇身
}
}
void Check_Border() //檢查蛇頭是否越界
{
if(Snake[0].x < 0 || Snake[0].x >= H
|| Snake[0].y < 0 || Snake[0].y >= L)
over = 1;
}
void Check_Head(int x, int y) //檢查蛇頭移動後的位置情況
{
if(GameMap[ Snake[0].x ][ Snake[0].y ] == '.') //為空
GameMap[ Snake[0].x ][ Snake[0].y ] = '@';
else
if(GameMap[ Snake[0].x ][ Snake[0].y ] == '*') //為食物
{
GameMap[ Snake[0].x ][ Snake[0].y ] = '@';
Snake[sum].x = x; //新增加的蛇身為蛇頭後面的那個
Snake[sum].y = y;
Snake[sum].now = Snake[0].now;
GameMap[ Snake[sum].x ][ Snake[sum].y ] = '#';
sum++;
Create_Food(); //食物吃完了馬上再產生一個食物
}
else
over = 1;
}
㈢ c璇璦瀹炵幇鎺у埗鍙伴炴満澶ф垬娓告垙錛屾湁鍔炴硶浣跨敤涓涓鍑芥暟瀹炵幇鎸変竴涓嬬┖鏍煎彂涓棰楀瓙寮癸紵
緇欏瓙寮逛篃寮勪釜緇撴瀯浣擄紝瀛樺偍pos錛宻peed錛宭ength絳変俊鎮錛屽紕涓鏇存柊(鏄劇ず)瀛愬脊鐨勫嚱鏁幫紝鍦ㄤ富寰鐜閲屼笉鏂璋冪敤榪欎釜鍑芥暟
㈣ #高手往這看#用c語言編寫俄羅斯方塊代碼,要能在codeblocks上運行的。
main.c裡面
#include <stdio.h>
#include <stdlib.h>
#include "fangkuai.h"
#include <time.h>
int main()
{
Manager manager;
Control control;
initGame(&manager,&control);
do {
printPrompting();
printPoolBorder();
runGame(&manager,&control);
if(ifPlayAgain()){
SetConsoleTextAttribute(Output,0x7);
system("cls");
startGame(&manager,&control);
}
else{
break;
}
}
while(1);
gotoxyFull(0,0);
CloseHandle(Output);
return 0;
}
.h裡面
#ifndef FANGKUAI_H_INCLUDED
#define FANGKUAI_H_INCLUDED
#include <stdio.h> //標准輸入輸出
#include <string.h> //字元數組
#include <stdlib.h> //標准庫
#include <time.h> //日期和時間
#include <conio.h> //控制台輸入輸出
#include <windows.h> // windows控制台
#include <stdbool.h> //標准布爾函數
//定義句柄,結構體,數組;函數聲明
//定義方塊數組,[7][4],7種方塊,每種4個狀態
static const unsigned int TetrisTable[7][4]={
{0x4444,0x0f00,0x2222,0x00f0},
{0x04e0,0x4640,0x0720,0x0262},
{0x0446,0x0e80,0x6220,0x0170},
{0x0622,0x02e0,0x4460,0x0740},
{0x0630,0x0264,0x0c60,0x2640},
{0x0462,0x06c0,0x4620,0x0360},
{0x0660,0x0660,0x0660,0x0660},
};
typedef struct TetrisManger{
unsigned int pool[28];
int x;int y;
int type[3];
int orientation[3];
unsigned score;
unsigned erasedCount[4];
unsigned erasedTotal;
unsigned tetrisCount[7];
unsigned tetrisTotal;
bool dead;
}Manager;
static const unsigned int initTetrisPool[28]={
0xc003,0xc003,0xc003,0xc003,0xc003,0xc003,0xc003,
0xc003,0xc003,0xc003,0xc003,0xc003,0xc003,0xc003,
0xc003,0xc003,0xc003,0xc003,0xc003,0xc003,0xc003,
0xc003,0xc003,0xc003,0xc003,0xc003,0xffff,0xffff
};
typedef struct TetresControl{
bool pause;
bool clockwise;
int direction;
int color[28][16];
}Control;
HANDLE Output;
void initGame(Manager *manager,Control *control);
void gotoxyFull(short x,short y);
void printPrompting();
void printPoolBorder();
void printScore(const Manager *manager);
void printNextTetres(const Manager *manager);
void startGame(Manager *manager,Control *control);
void initTetris(Manager *manager);
void insertTetris(Manager *manager);
void setPoolColor(const Manager *manager,Control *control);
void printCurrentTetris(const Manager *manager,const Control *control);
void printTetrisPool(const Manager *manager,const Control *control);
bool checkCollision(const Manager *manager);
void removeTetris(Manager *manager);
void moveDownTetris(Manager *manager,Control *control);
void runGame(Manager *manager,Control *control);
void horzMoveTetris(Manager *manager,Control *control);
void keydownControl(Manager *manager,Control *control,int key);
void rotateTetris(Manager *manager,Control *control);
void dropTetris(Manager *manager,Control *control);
bool checkErasing(Manager *manager,Control *control);
bool ifPlayAgain();
#endif // FANGKUAI_H_INCLUDED
.c裡面
#include "fangkuai.h"
#include <stdio.h>
#include <windows.h>
void initGame(Manager *manager,Control *control)//初始化游戲
{
SetConsoleTitle("俄羅斯方塊"); //設置窗口標題
Output=GetStdHandle(STD_OUTPUT_HANDLE); //獲取標准輸出句柄
CONSOLE_CURSOR_INFO INFO; //定義游標屬性結構體變數
INFO.dwSize=1; //設置游標高度值
INFO.bVisible=FALSE; //設置游標隱藏值
SetConsoleCursorInfo(Output,&INFO); //設置游標屬性
startGame(manager,control);
}
//全形定位游標
void gotoxyFull(short x,short y) //全形方式定位
{
static COORD cd; //定義結構體變數
cd.X=2*x; //結構體變數 X=2x
cd.Y=y;
SetConsoleCursorPosition(Output,cd);//設置游標位置
}
//顯示右下角按鍵提示信息
void printPrompting(){
SetConsoleTextAttribute(Output,0x0b);//設置顯示顏色為藍色光亮
gotoxyFull(26,10);
printf("■控制:");
gotoxyFull(27,12);
printf("□向左移動:← A 4");
gotoxyFull(27,13);
printf("□向右移動:→ D 6");
gotoxyFull(27,14);
printf("□向下移動:↓ S 2");
gotoxyFull(27,15);
printf("□順時針轉:↑ W 8");
gotoxyFull(27,16);
printf("□逆時針轉:0");
gotoxyFull(27,17);
printf("□直接落地:空格");
gotoxyFull(27,18);
printf("□暫停游戲:回車");
gotoxyFull(26,23);
printf("■BY YU");
}
//顯示游戲池白色邊框
void printPoolBorder(){
SetConsoleTextAttribute(Output,0xF0);//設置背景色為白色高亮
int y=1;
for (y=1;y<23;y++){
gotoxyFull(10,y); //(10,1) 定位到(10,22)
printf("%2s","");
gotoxyFull(23,y); //(23,1)定位到(23,22)
printf("%2s","");
}
gotoxyFull(10,23);//底部一行
printf("%28s",""); //14個字元,每個兩位,左邊、右邊白色,中間12
}
//顯示得分、消行數、方塊數
void printScore(const Manager *manager){
SetConsoleTextAttribute(Output,0x0E);//設置顏色為黃色高亮
gotoxyFull(2,2);
printf("■得分:%u",manager->score);
gotoxyFull(1,6);
printf("■消行總數:%u",manager->erasedTotal);
int i;
for (i=0;i<4;i++){
gotoxyFull(2,7+i);
printf("□消%d:%u",i+1,manager->erasedCount[i]);
}
gotoxyFull(1,15);
printf("■方塊總數:%u",manager->tetrisTotal);
static const char *tetrisName="ITLJZSO";
for (i=0;i<7;i++){
gotoxyFull(2,17+i);
printf("□%c形:%u",tetrisName[i],manager->tetrisCount[i]);
}
}
void printNextTetres(const Manager *manager)//顯示下一個,下下一個方塊
{
SetConsoleTextAttribute(Output,0x0f);//設置前景色為白色高亮
gotoxyFull(26,1);
printf("┌─────────┬─────────┐");
gotoxyFull(26,2);
printf("│%9s│%9s│","","");
gotoxyFull(26,3);
printf("│%9s│%9s│","","");
gotoxyFull(26,4);
printf("│%9s│%9s│","","");
gotoxyFull(26,5);
printf("│%9s│%9s│","","");
gotoxyFull(26,6);
printf("└─────────┴─────────┘");
//顯示下一個方塊
unsigned int tetris ;
int i;
tetris=TetrisTable[manager->type[1]][manager->orientation[1]];
SetConsoleTextAttribute(Output,manager->type[1]|8);
for (i=0;i<16;i++){
gotoxyFull(27+(i&3),2+(i>>2));
((tetris<<i)&0x8000) ? printf("■"):printf("%2s","");
}
tetris=TetrisTable[manager->type[2]][manager->type[2]];
SetConsoleTextAttribute(Output,0x08);
for(i=0;i<16;i++){
gotoxyFull(32+(i&3),2+(i>>2));
((tetris<<i)&0x8000)?printf("■"):printf("%2s","");
}
}
void startGame(Manager *manager,Control *control)//開始游戲
{
memset(manager,0,sizeof(Manager));
memcpy(manager->pool,initTetrisPool,sizeof(unsigned int [28]));//復制游戲池數據
srand((unsigned)time(NULL));//設置隨機數種子
manager->type[1]=rand()%7;//下一個方塊類型
manager->orientation[1]=rand()%4;//下一個方塊狀態
manager->type[2]=rand()%7;//下下一個方塊類型
manager->orientation[2]=rand()%4;//下下一個方塊狀態
memset(control,0,sizeof(Control));//初始化控制結構體為0
initTetris(manager); //初始化方塊
setPoolColor(manager,control);//初始化方塊,若沒有碰撞,插入方塊,需要設置顏色
}
void initTetris(Manager *manager)//出第一個方塊
{
unsigned int tetris;
manager->type[0]=manager->type[1];
manager->orientation[0]=manager->orientation[1];
manager->type[1]=manager->type[2];
manager->orientation[1]=manager->orientation[2];
manager->type[2]=rand()%7;
manager->orientation[2]=rand()%4;
tetris=TetrisTable[manager->type[0]][manager->orientation[0]];
manager->x=6;
manager->y=4;
if(checkCollision(manager)){
manager->dead=true;
}
else{
insertTetris(manager);
}
++manager->tetrisTotal;
++manager->tetrisCount[manager->type[0]];
printNextTetres(manager);
printScore(manager);
}
//插入方塊到游戲池
void insertTetris(Manager *manager){
unsigned int tetris=TetrisTable[manager->type[0]][manager->orientation[0]];//相對於y的位置
manager->pool[manager->y+0]|=(((tetris<<0x0)&0xF000)>>manager->x);//或等於
manager->pool[manager->y+1]|=(((tetris<<0x4)&0xF000)>>manager->x);
manager->pool[manager->y+2]|=(((tetris<<0x8)&0xF000)>>manager->x);
manager->pool[manager->y+3]|=(((tetris<<0xc)&0xF000)>>manager->x);
}
//設置游戲顏色
void setPoolColor(const Manager *manager, Control *control){
int i,x,y;
/* i 當前方塊值 0----15
x 設置顏色的列 x=manager->x+&3
y 設置顏色的行 y=manager->y+i>>2 */
unsigned int tetris;
tetris=TetrisTable[manager->type[0]][manager->orientation[0]];
for(i=0;i<16;i++){
y=manager->y+(i>>2);
if(y>25){
break;
}
x=manager->x+(i&3);
if((tetris<<i)&0x8000){
control->color[y][x]=(manager->type[0]|8);
}
}
}
//顯示當前方塊
void printCurrentTetris(const Manager *manager,const Control *control){
int x,y;
y=(manager->y>4)?(manager->y-1):4;
for (;y<26&&y<manager->y+4;y++){
x=(manager->x>2)?(manager->x-1):2;
for (;x<14&x<manager->x+5;x++) {
gotoxyFull(x+9,y-3);
if((manager->pool[y]<<x) & 0x8000){
SetConsoleTextAttribute(Output,control->color[y][x]);
printf("■");
}
else{
SetConsoleTextAttribute(Output,0);
printf("%2s","");
}
}
}
}
//顯示游戲池
void printTetrisPool(const Manager *manager,const Control *control){
int x,y;
for(y=4;y<26;y++)
{
gotoxyFull(11,y-3);
for(x=2;x<14;x++)
{
if((manager->pool[y]<<x) & 0x8000)
{
SetConsoleTextAttribute(Output,control->color[y][x]);
printf("■");
}
else {
SetConsoleTextAttribute(Output,0);
printf("%2s","");
}
}
}
}
//碰撞檢測
bool checkCollision(const Manager *manager){
unsigned int tetris;
tetris=TetrisTable[manager->type[0]][manager->orientation[0]];//准備插入的當前方塊
unsigned int dest;//游戲池中4行4列的值
dest=0;
dest|=(((manager->pool[manager->y+0]<<manager->x)&0xF000)>>0x0);
dest|=(((manager->pool[manager->y+1]<<manager->x)&0xF000)>>0x4);//左移x,右移4
dest|=(((manager->pool[manager->y+2]<<manager->x)&0xF000)>>0x8);
dest|=(((manager->pool[manager->y+3]<<manager->x)&0xF000)>>0xc);
return((dest&tetris)!=0);
}
//移除方塊
void removeTetris(Manager *manager){
unsigned int tetris;
tetris=TetrisTable[manager->type[0]][manager->orientation[0]];//准備插入的當前方塊,當前方塊值
manager->pool[manager->y+0]&=~(((tetris<<0x0)&0xF000)>>manager->x);//左移0保留 最高,其餘清零,右移x,游戲中方塊列位置,取反,原來&後面,存
manager->pool[manager->y+1]&=~(((tetris<<0x4)&0xF000)>>manager->x);
manager->pool[manager->y+2]&=~(((tetris<<0x8)&0xF000)>>manager->x);
manager->pool[manager->y+3]&=~(((tetris<<0xc)&0xF000)>>manager->x);
}
//向下移動方塊
void moveDownTetris(Manager *manager,Control *control){
int y=manager->y;
removeTetris(manager);
++manager->y;
if(checkCollision(manager)){
manager->y=y;
insertTetris(manager);
if(checkErasing(manager,control))
{
printTetrisPool(manager,control);
}
}
else{
insertTetris(manager);
setPoolColor(manager,control);
printCurrentTetris(manager,control);
}
}
//運行游戲
void runGame(Manager *manager,Control *control){
clock_t clockNow,clockLast;
clockLast=clock();
printTetrisPool(manager,control);
while (!manager->dead){
while(_kbhit()){
keydownControl(manager,control,getch());
}
if (!control->pause){
clockNow=clock();
if(clockNow-clockLast>0.45F*CLOCKS_PER_SEC){
clockLast=clockNow;
moveDownTetris(manager,control);
}
}
}
}
㈤ 誰有能在vc++6.0上運行的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;
}