C加加貪吃蛇
VS2013
自製
運行一下試試看
#include#include#include#includeusing namespace std;/*=============== all the structures ===============*/typedef struct Frame
{
COORD position[2];//位置
int flag;
}Frame;//幀/*=============== all the functions ===============*/void SetPos(COORD a)// set cursor 設置游標{
HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(out, a);
}/* set cursor設置游標*/void SetPos(int i, int j){
COORD pos = { i, j };
SetPos(pos);
}void HideCursor(){
CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}//把第y行,[x1, x2) 之間的坐標填充為 chvoid drawRow(int y, int x1, int x2, char ch){
SetPos(x1, y); for (int i = 0; i
}//在a, b 縱坐標相同的前提下,把坐標 [a, b] 之間填充為 chvoid drawRow(COORD a, COORD b, char ch){ if (a.Y == b.Y)
drawRow(a.Y, a.X, b.X, ch); else
{
SetPos(0, 25); cout
system("pause");
}
}//把第x列,[y1, y2] 之間的坐標填充為 chvoid drawCol(int x, int y1, int y2, char ch){ int y = y1; while (y != y2 + 1)
{
SetPos(x, y); cout
y++;
}
}//在a, b 橫坐標相同的前提下,把坐標 [a, b] 之間填充為 chvoid drawCol(COORD a, COORD b, char ch){ if (a.X == b.X)
drawCol(a.X, a.Y, b.Y, ch); else
{
SetPos(0, 25); cout
system("pause");
}
}//左上角坐標、右下角坐標、用row填充行、用col填充列void drawFrame(COORD a, COORD b, char row, char col){
drawRow(a.Y, a.X + 1, b.X - 1, row);
drawRow(b.Y, a.X + 1, b.X - 1, row);
drawCol(a.X, a.Y + 1, b.Y - 1, col);
drawCol(b.X, a.Y + 1, b.Y - 1, col);
}//點覆蓋void drawFrame(COORD a, char r){
SetPos(a.X, a.Y); cout
}void drawFrame(int x1, int y1, int x2, int y2, char row, char col){
COORD a = { x1, y1 };
COORD b = { x2, y2 };
drawFrame(a, b, row, col);
}void drawFrame(Frame frame, char row, char col){
COORD a = frame.position[0];
COORD b = frame.position[1];
drawFrame(a, b, row, col);
}void drawPlaying(){
drawFrame(0, 0, 48, 24, '=', '|');// draw map frame;
drawFrame(49, 0, 77, 24, '-', '|');// draw output frame
SetPos(52, 6); cout
SetPos(52, 8); cout
SetPos(52, 14); cout
SetPos(52, 16); cout
SetPos(52, 17); cout
SetPos(52, 19); cout
SetPos(52, 20); cout
}//在[a, b)之間產生一個隨機整數int random(int a, int b){ int c = (rand() % (a - b)) + a; return c;
}//在兩個坐標包括的矩形框內隨機產生一個坐標COORD random(COORD a, COORD b){ int x = random(a.X, b.X); int y = random(a.Y, b.Y);
COORD c = { x, y }; return c;
}//判斷是否撞到蛇身或者牆壁 點對點bool judgeCoordSnake(COORD spot1, COORD spot2){ if (spot1.X == spot2.X) if (spot1.Y == spot2.Y) return true; if (spot1.X == 0 || spot1.X == 48 || spot1.Y == 0 || spot1.Y == 24) return true; return false;
}//判斷是否撞到蛇身或者牆壁 頭對身子bool judgeCoordSnake(COORD spot1, COORD *snake1, COORD *snake2){ for (int i = 1; i
{ if (spot1.X == snake1[i].X) if (spot1.Y == snake1[i].Y) return false; if (spot1.X == snake2[i].X) if (spot1.Y == snake2[i].Y) return false; if (spot1.X == 0 || spot1.X == 48 || spot1.Y == 0 || spot1.Y == 24) return false;
} return true;
}bool judgeCoordSnakeFood(COORD spot1, COORD spot2, COORD spot3,int& a){ if (spot1.X == spot2.X) if (spot1.Y == spot2.Y)
{
a = 0; return true;
} if (spot1.X == spot3.X) if (spot1.Y == spot3.Y)
{
a = 1; return true;
} return false;
}//因為在判斷時頭部的問題所以單列出來
void printCoord(COORD a){ cout
}int drawMenu(){
SetPos(30, 1); cout
drawRow(3, 0, 79, '-');
drawRow(5, 0, 79, '-');
SetPos(28, 4); cout
SetPos(15, 11); cout
SetPos(15, 13); cout
drawRow(20, 0, 79, '-');
drawRow(22, 0, 79, '-');
SetPos(47, 11); cout
SetPos(51, 13); cout
SetPos(12, j); cout "; while (1)
{ if (_kbhit())
{ char x = _getch(); switch (x)
{ case 'w':
{ if (j == 13)
{
SetPos(12, j); cout
j = 11;
SetPos(51, 13); cout
SetPos(47, 11); cout
SetPos(51, 13); cout
} break;
} case 's':
{ if (j == 11)
{
SetPos(12, j); cout
j = 13;
SetPos(51, 13); cout
SetPos(47, 11); cout
SetPos(51, 13); cout
} break;
} case 'k':
{ if (j == 11)
return 1; else exit(0);
}
}
}
}
}/*
DWORD WINAPI MusicFun(LPVOID lpParamte)
{
//DWORD OBJ;
sndPlaySound(TEXT("bgm.wav"), SND_FILENAME|SND_ASYNC);
return 0;
}
*//*================== the Game Class ==================*/class Game
, snake1[1] = { 8, 16 }, snake1[2] = { 8, 17 };
snake2[0] = { 40, 15 }, snake2[1] = { 40, 16 }, snake2[2] = { 40, 17 };
length1 = 3, length2 = 3; for (int i = 3; i
{
snake1[i] = { 0, 0 };
snake2[i] = { 0, 0 };
}
}
};//畫第一條蛇void Game::drawSnake1(int direction)
{ for (int i = 0; i
{
SetPos(snake1[i]); if (i != 0) cout
{ if (direction==1)
{ cout
} else if(direction == 2)
{ cout
} else if (direction == 3)
{ cout
} else if (direction == 4)
{ cout ";
}
}
}
}void Game::drawSnake2(int direction)
{ for (int i = 0; i
{
SetPos(snake2[i]); if (i != 0) cout
{ if (direction == 1)
{ cout
} else if (direction == 2)
{ cout
} else if (direction == 3)
{ cout
} else if (direction == 4)
{ cout ";
}
}
}
}//把第一條蛇消失,行動時使用,只讓蛇頭蛇尾消失,這樣不會一閃一閃void Game::drawSnake1ToNull()
{
SetPos(snake1[0]); cout
SetPos(snake1[length1]); cout
}void Game::drawSnake2ToNull()
{ /*for (int i = length2; i
{
SetPos(snake2[i]);
cout
}*/
SetPos(snake2[0]); cout
SetPos(snake2[length2]); cout
}//初始食物void Game::initFood()
{
COORD a = { 2, 2 };
COORD b = { 24,12 };
COORD c = { 25, 2 };
COORD d = { 47,14 };
food[0] = random(a, b);
food[1] = random(c, d);
}//隨機食物,如果有牆需要修改。COORD Game::randomFood()
{
COORD food;
COORD a = { 2, 2 };
COORD b = { 47, 23 }; while (1)
{
food = random(a, b); if (judgeCoordSnake(food, snake1, snake2)) return food;
}
}//畫出食物
void Game::drawFood()
學習過程中遇到什麼問題或者想獲取學習資源的話,歡迎加入學習交流群
零基礎小白到大神之路,歡迎加裙 四八七八七五零零四,群里有免費C++課程,還有大量乾貨哦
※前端工程師如何增強自己的技能,12張知識圖譜讓你明白
※這些H5 js的表白特效你情人節上用場了嗎?
※從前端小白到極客,如何實現爆髮式成長-讀書篇 CSS性能
※從前端小白到極客,如何實現爆髮式成長-讀書篇
TAG:IT技術java交流 |