贪吃蛇
来源:互联网 发布:人品差的演员知乎 编辑:程序博客网 时间:2024/04/30 13:05
#include <iostream>
#include <windows.h>
#include <stdlib.h>
#include<iomanip>
#include <conio.h>
#include <time.h> //使用当前时间做种子;
enum dir{up_key = 1,down_key,left_key,right_key}; //枚举类型enum dir;
//围墙;
using namespace std;
class Fence{
public:
void InitFence();
void OutputF();
char game[20][20];
}f; //定义对象;
//画框框;
void Fence::InitFence(){
for(int i=0; i<20; i++)
for(int j=0; j<20; j++){
if(i==0||i==19||j==0||j==19)
game[i][j]= '$';
else game[i][j]= ' ';
}
}
void Fence::OutputF(){
for(int i=0; i<20; i++){
for(int j=0; j<20; j++)
cout<<game[i][j]<<' ';
cout<<endl;
}
}
class Time
{
public:
Time(){
second=0,minute=0,hour=0;
}
void display();
void settime();
private:
int second;
int minute;
int hour;
}t;
void Time::settime()
{
second++;
if(60==second)
{
second=0;
minute+=1;
}
else if(60==minute)
{
minute=0;
hour+=1;
}
int i, j;
for (i = 0; i < 1000; i++)
for (j = 0; j < 1000; j++);
}
void Time::display()
{
cout<<"\t"<<right<<setfill('0')<<setw(2)<<hour
<<':'<<right<<setfill('0')<<setw(2)<<minute
<<':'
<<right<<setw(2)<<second<<setfill('0')<<"\n";
cout<<"\n";
}
//蛇结点;
class SnakeNode{
private:
int x,y;
SnakeNode *prior,*next;
public:
void add_head(int x,int y);
int get_x();
int get_y();
void delete_tail();
SnakeNode* next_return(){return next;}
void DestroyList(SnakeNode L);
}*head=NULL, *tail =NULL;
//插入头结点;
void SnakeNode::add_head(int x,int y){
SnakeNode *q=new SnakeNode;
q->x =x; q->y =y;
q->next =head;
q->prior =NULL;
if(head) head->prior =q;
head =q;
if(!tail) tail = head;
f.game[x][y]= '*'; //f对象可以在定义Fence类时定义; 且Fence类在SnakeNode类前定义;
}
int SnakeNode::get_x(){
return x;
}
int SnakeNode::get_y(){
return y;
}
void SnakeNode::delete_tail()
{
SnakeNode *p =tail;
f.game[tail->get_x()][tail->get_y()]= ' ';
if(tail==head)
tail= head= NULL;
else{
tail= tail->prior;
tail->next= NULL;
}
delete p;
}
class move{
public:
move(string n):Name(n){score = 0;}
dir point; //枚举变量point: 控制方向;
int food_x;
int food_y;
public:
char moving(string *n,int *s);
void change_point(char); //改变方向;
void get_food();
int score_return(int n){
cout<<" 积分:"<<score;cout<<" leavel:"<<n;
}
string Name;
int score;
};
char move::moving(string *n,int *s){
int a,b,k = 0;
char YN;
string swp;
int swp2;
SnakeNode *p = head,*q,*z;
a= head->get_x(); //取得头结点横坐标
b= head->get_y(); //头结点纵坐标
switch(point){
case 1: --a; break;
case 2: ++a; break;
case 3: --b; break;
case 4: ++b; break;
}
if(a==19||b==19||a==0||b==0)
{
cout<<"\t\t\t\tgame over!!!\n\n\n"<<endl;
cout<<"本次游戏所用时间为:";
t.display();
cout<<" 积分:"<<score;
exit(0);
}
if(a==food_x && b==food_y){ //吃food;
head->add_head(a,b);
score += 10;
get_food();
}
else{
head->add_head(a,b); //插入头结点;
head->delete_tail(); //删除尾结点;
}
if(a!=food_x && b!=food_y&&f.game[a][b]=='*')
{
cout<<"咬到自己"<<endl;
exit(0);
}
}
void help()
{
cout<<"\t\t\t----------------帮助-------------------"<<endl;
cout<<"\t\t\t1.使用'W','S,'A,'D控制上下左右 "<<endl;
cout<<"\t\t\t2.按任意方向键开始游戏,按'Q'键暂停游戏"<<endl;
cout<<"\t\t\t3.退出游戏请按'T'键!!! "<<endl;
cout<<"\t\t\t按任意键进入游戏。。。。。。。。。。。"<<endl;
while(!kbhit()){Sleep(1000);}
getch();
system("cls");
}
void move::change_point(char keydown){
switch(keydown){
case 'W':
case 'w': point= up_key; break;
case 'S':
case 's': point= down_key; break;
case 'A':
case 'a': point= left_key; break;
case 'D':
case 'd': point= right_key; break;
case 'Q':
case 'q': while(!kbhit()){Sleep(1000);};break;
case 'E':
case 'e': help();break;
case 'T':
case 't': cout<<"正在退出。。。。。。";Sleep(2000);exit(0);break;
}
}
void move::get_food(){
srand((unsigned int) time(NULL)); //做种子(程序运行时间);
food_x= rand()%17+1;
food_y= rand()%17+1;
f.game[food_x][food_y]= '#';
}
int leave(int n)
{
cout<<"\t\t\t\t游戏难度级别1——5"<<endl;
cout<<"\t\t\t\t当前难度"<<n<<endl;
cout<<"\t\t\t\t选择难度[ ]\b\b";
cin>>n;
system("cls");
return n;
}
int menu()
{ int k = 1;
cout<<"\t\t\t************************"<<endl;
cout<<"\t\t\t\t<1>开始游戏"<<endl;
cout<<"\t\t\t\t<2>选择难度"<<endl;
cout<<"\t\t\t\t<3>退出游戏"<<endl;
cout<<"\t\t\t\t输入你的选择[ ]\b\b";
cin>>k;
return k;
}
void game_play(int n,string *name,int *s)
{ string na;
system("cls");
system("color a4");
move m(na);
char k;
f.InitFence();
head->add_head(4,3);
head->add_head(4,4);
head->add_head(4,5);
m.get_food();
f.OutputF();
while (true){
char keydown = getch();
t.settime();t.display();
m.change_point(keydown);
while(!kbhit()){
system("cls");
k = m.moving(name,s);
f.OutputF();
m.score_return(n);
Sleep(200/n);
}
}
end:
system("cls");
}
int main(){
int n = 1,k = 1;
string name[3];
int score[3] = {0};
while(n)
{
k = menu();
system("cls");
switch(k){
case 1:help(),game_play(n,name,score);break;
case 2:n = leave(n);break;
case 3:cout<<"\t\t\t\t退出游戏中。。。。";Sleep(2000);exit(0);break;
}
}
return 0;
}
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- 贪吃蛇
- Android 学习总结
- 解决hibernate无法自动创建表
- 冒泡排序
- jz2440烧录(二)
- 思想上移,行动下移
- 贪吃蛇
- NYOJ-115-城市平乱(裸迪杰斯特拉)
- 并发队列ConcurrentLinkedQueue和阻塞队列LinkedBlockingQueue用法
- hdu 1176 免费馅饼
- CSS布局问题集锦
- UIDatePicker的使用
- cogs 1487. 麻球繁衍(概率DP)
- poj3581 Sequence (后缀数组)
- POI解析03版07版Excel表