貪吃蛇,c,c++
來源:程序員人生 發布時間:2015-01-09 08:20:34 閱讀次數:2648次
學了c++半個學期了,自己動手做了1個游戲。(有甚么不好的地方,歡迎指出<img alt="大笑" src="http://static.blog.csdn.net/xheditor/xheditor_emot/default/laugh.gif" />)
#include <windows.h>
#include <conio.h>
#include <ctime>
#include <iostream>
#include <cmath>
#include <mmsystem.h>
#include<stdio.h>
#include <stdlib.h>
# pragma comment (lib, "Winmm.lib")//導入Winmm.lib庫
using namespace std;
int node; //蛇的節數
int life; //生命,0活著,1死亡
int Q;//控制食品色彩
int fa,fb;//食品坐標
int q;//控制上下左右
int ti=0,ti1=0;//time
int nd=0;//難度
char k;
int a[4][2]={{⑴,0},{1,0},{0,⑴},{0,1}}; // 上下左右
struct food
{
int f1;
int f2;
int need;
}f;
struct she
{
int x;
int y;
}s[1000],t;
HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE); //光標定位
COORD coord;
void hide() //隱藏光標
{
CONSOLE_CURSOR_INFO cursor_info={1,0};
SetConsoleCursorInfo(hout, &cursor_info);
}
void gotoxy(int x, int y)
{
CONSOLE_SCREEN_BUFFER_INFO cs;
HANDLE hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsoleOut, &cs);
cs.dwCursorPosition.X = y;
cs.dwCursorPosition.Y = x;
SetConsoleCursorPosition(hConsoleOut,
cs.dwCursorPosition);
}
void color(int a)//色彩函數
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
void weiqiang(void)//圍圈
{
color(14);
int a=25,b=20,c=25,d=20;
while(a--)
{
cout << '-';
}
cout << endl;
while(b--)
{
cout << '|';
cout << endl;
}
while(d--)
{
gotoxy(20-d,24);
cout << '|';
cout << endl;
}
while(c--)
{
cout << '-';
}
cout << endl;
gotoxy(1,1);
}
void food1(void)//食品
{
f.f1=5;
f.f2=5;
int i;
srand((unsigned)time(NULL));
while (1)
{
loop:f.f1=rand()%19;//隨機數
f.f2=rand()%24;
for (i=0;i<node;i++)
{
if (f.f1==s[i].x||f.f2==s[i].y)
{
goto loop;
}
}
if (f.f1==0||f.f2==0)//避免超越框框
{
continue;
}
else
{
break;
}
}
if(f.need==0)
{
fa=f.f1;fb=f.f2;//f.f1,f.f2賦值給全局變量
gotoxy(f.f1,f.f2);
while (1)
{
Q=rand()%16;
if (Q>9)
{
break;
}
}
color(Q);
cout << "*";
}
}
int died(void)//判斷死亡
{
int i,flag=0;
for (i=1;i<node;i++)
{
if (s[0].x==s[i].x&&s[0].y==s[i].y)
{
flag=1;
}
}
if ((s[0].x==0||s[0].x==21||s[0].y==0||s[0].y==24)||flag==1)
{
return 0;
}
else
{
return 1;
}
}
void tyd(char c)
{
if (abs(c-k)!=3&&abs(c-k)!=4&&abs(c-k)!=0)//控制蛇頭走身體后面
{
if(c=='a')
{
q=2;
}
else if (c=='s')
{
q=1;
}
else if (c=='w')
{
q=0;
}
else if (c=='d')
{
q=3;
}
}
s[0].x+=a[q][0];
s[0].y+=a[q][1];
}
void yd(void)//移動方向
{
char c='d';
int i,a=200;
s[0].x=1;
s[0].y=1;
node=2;
do
{
ti+=1;//必須將其重新歸0,不然游戲會出錯
ti1+=ti;
if (ti>1000)
{
ti=0;
}
if (ti%150==0)
{
if (a>25)
{
nd++;
a-=25;
}
}
food1();
t.x=s[node⑴].x;//保存最后1個*
t.y=s[node⑴].y;
for (i=node⑴;i>0;i--)//身體移動
{
s[i].x=s[i⑴].x;
s[i].y=s[i⑴].y;
}
for (i=1;i<node;i++)
{
color(13);
gotoxy(s[i].x,s[i].y);
cout << "*" ;
}
gotoxy(t.x,t.y);//消去最后那個*
cout << " ";
if (_kbhit())//接受鍵盤輸入的上下左右,并以此改變方向
{
k=c;
c=_getch();
}
tyd(c);//移動
gotoxy(s[0].x,s[0].y);//頭移動
cout << "#";
if (s[0].x==fa&&s[0].y==fb)//控制食品每次只能有1個
{
f.need=0;
node++;
}
else
{
f.need=1;
}
Sleep(a);
if(died()==0)//判斷是不是
{
system("cls");//清屏
break;
}
color(12);
gotoxy(22,0);
cout << "食品統計: " << node⑵ ;
gotoxy(1,27);
color(11);
cout << "游戲難度: " << nd;
}while(1);
}
void jieshu(void)
{
color(12);
cout << "花費時間:" << ti1/60 << " s" << endl;
cout << " o(
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
------分隔線----------------------------
------分隔線----------------------------