C++結(jié)構(gòu)體數(shù)組實(shí)現(xiàn)貪吃蛇
更新時間:2020年04月28日 09:24:39 作者:oyxy2019
這篇文章主要為大家詳細(xì)介紹了C++結(jié)構(gòu)體數(shù)組實(shí)現(xiàn)貪吃蛇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了C++結(jié)構(gòu)體數(shù)組實(shí)現(xiàn)貪吃蛇的具體代碼,供大家參考,具體內(nèi)容如下
代碼:
#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
using namespace std;
const int h=50,w=50,MaxLen=400;
void gotoxy(short y,short x)//光標(biāo)移動函數(shù)
{
COORD pos={x,y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
struct node
{
int x,y;
};
node s[MaxLen];
node food;
int dir,len;
int Map[h+5][w+5];
int Time[7]={0},level;//building。。。
void FoodCreate()
{
srand ((unsigned)time(NULL));//時間作隨機(jī)數(shù)種子,避免偽隨機(jī)
while(1)
{
food.x=rand()%h,food.y=rand()%w;
if(food.x>0&&food.y>0&&Map[food.x][food.y]==0)break;
}
gotoxy(food.x,food.y),printf("@");
gotoxy(h+1,0);
}
void init()
{
system("cls");
for(int i=0;i<=h;i++)//畫圖
{
for(int j=0;j<=w;j++)
{
if(i==0||j==0||i==h||j==w)Map[i][j]=1,printf("#");
else Map[i][j]=0,printf(" ");
}
printf("\n");
}
len=2; //初始化蛇
dir=0;
s[1].x=12,s[1].y=4;
s[len].x=12,s[len].y=3;
Map[s[1].x][s[1].y]=Map[s[len].x][s[len].y]=1;
gotoxy(s[1].x,s[1].y),printf("*");
gotoxy(s[len].x,s[len].y),printf("*");
gotoxy(h+1,0);
FoodCreate();
}
int move()
{
node next=s[1];
switch(dir)
{
case 0:next.y++;break;
case 1:next.x--;break;
case 2:next.y--;break;
case 3:next.x++;break;
}
if(Map[next.x][next.y])return 0; //下一步GG
if(next.x==food.x&&next.y==food.y) //下一步遇到食物
{
len++;
FoodCreate();
}
else //下一步是空白就將尾部覆蓋
{
gotoxy(s[len].x,s[len].y),printf(" ");
Map[s[len].x][s[len].y]=0;
}
gotoxy(next.x,next.y),printf("*");
gotoxy(h+1,0);
Map[next.x][next.y]=1;
for(int i=len;i>1;i--)s[i]=s[i-1];
s[1]=next;
Sleep(100); //Sleep放在最后比較順滑//速度在這兒調(diào)
return 1;
}
void GameOver()
{
for(int i=1;i<=3;i++)
{
gotoxy(s[1].x,s[1].y);
printf(" ");
Sleep(300);
gotoxy(s[1].x,s[1].y);
printf("*");
Sleep(300);
}
gotoxy(h+1,0);
printf("GameOver\n");
printf("Press any key to continue...");
}
void Welcome()
{
printf("為了您的游戲體驗,請先調(diào)整控制臺字體和布局(記得不要忘了默認(rèn)設(shè)置):\n");
printf("右鍵白色框->屬性->字體 選擇點(diǎn)陣字體并調(diào)整字體大小為8×8\n");
printf("再選擇布局設(shè)置窗口大小,推薦60×60\n\n");
printf("WASD控制方向\n");
printf("\n任意鍵進(jìn)入貪吃蛇皮...");
getch();
}
int main()
{
Welcome();
init();
while(1)
{
if(kbhit())
{
char ch=getch();
int temp=dir;
switch(ch)
{
case 'd':temp=0;break;
case 'w':temp=1;break;
case 'a':temp=2;break;
case 's':temp=3;break;
}
if((temp+dir)%2)dir=temp;//如果方向不沖突
}
if(move()==0)
{
GameOver();
getch();
init();
}
}
}
運(yùn)行效果:

關(guān)于C++小游戲的更多精彩內(nèi)容請點(diǎn)擊專題: 《C++經(jīng)典小游戲》 學(xué)習(xí)了解
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- C++ 情懷游戲貪吃蛇的實(shí)現(xiàn)流程詳解
- 利用C/C++實(shí)現(xiàn)貪吃蛇游戲
- C++入門指南之貪吃蛇游戲的實(shí)現(xiàn)
- C++實(shí)現(xiàn)簡易貪吃蛇游戲
- C++實(shí)現(xiàn)簡單貪吃蛇小游戲
- C++實(shí)現(xiàn)貪吃蛇游戲
- C++代碼實(shí)現(xiàn)貪吃蛇小游戲
- 基于easyx的C++實(shí)現(xiàn)貪吃蛇
- C++控制臺循環(huán)鏈表實(shí)現(xiàn)貪吃蛇
- C++控制臺實(shí)現(xiàn)貪吃蛇游戲
- C++通過類實(shí)現(xiàn)控制臺貪吃蛇
- c++實(shí)現(xiàn)超簡單的貪吃蛇游戲?qū)嵗榻B
相關(guān)文章
C++ string字符串的使用和簡單模擬實(shí)現(xiàn)
C語言中,字符串是以'\0'結(jié)尾的一些字符的集合,為了操作方便,C標(biāo)準(zhǔn)庫中提供了一些str系列的庫函數(shù),但是這些庫函數(shù)和字符串是分離的,本文給大家介紹了C++ string字符串的使用和簡單模擬實(shí)現(xiàn),感興趣的朋友可以參考下2024-06-06
C語言的可變參數(shù)函數(shù)實(shí)現(xiàn)詳解
某些情況下我們希望函數(shù)的參數(shù)個數(shù)可以根據(jù)需要確定,因此c語言引入可變參數(shù)函數(shù)。典型的可變參數(shù)函數(shù)的例子有printf()、scanf()等,下面我就開始講解2021-08-08

