在这里感谢贺利坚老师对我这学期的帮助,嘿嘿。
/*
* 程序的版权和版本声明部分
* Copyright (c)2013, 在校学生
* All rightsreserved.
* 文件名称: 贪吃蛇.cpp
* 作 者:刘旺
* 完成日期:2014年7月4日
* 版本号: v1.0
*
* 输入描述:
* 问题描述:实现贪吃蛇
* 程序输出:
* 问题分析:
*/
#include <iostream>
#include <vector>
#include <windows.h>
#include <conio.h>
#include <cstdio>
#include <fstream>
#include <ctime>
using namespace std ;
//定义蛇类
class Snack{
public:
Snack *next ; //指向下一个蛇身的指针
Snack(){}
Snack(int x1, int y1):x(x1),y(y1){}
int getX() ; //获取x坐标
int getY() ; //获取y坐标
int getN() ; //获取蛇的长度
void setN(int n1) ; //设置蛇长度
void setX(int x1) ; //设置y坐标
void setY(int y1) ; //设置y坐标
private:
static int n ; //蛇长度
int x ; //x坐标
int y ; //y坐标
};
int Snack::n = 3 ;//初始化舍身
//定义获取x坐标函数
int Snack::getX()
{
return x ;
}
//定义获取y坐标函数
int Snack::getY()
{
return y ;
}
//定义获取蛇身函数
int Snack::getN()
{
return n ;
}
//定义设置x坐标函数
void Snack::setX(int x1)
{
x = x1 ;
}
//定义设置y坐标函数
void Snack::setY(int y1)
{
y = y1 ;
}
//定义设置蛇身长度函数
void Snack::setN(int n1)
{
n = n1 ;
}
//定义操控类
class Console{
public:
Console():head(NULL),speed(100),style(1),lis(true){a[0]=3;a[1]=3;a[2]=3;}
void snack_Add() ; //控制蛇的生长
void snack_body() ; //控制蛇身
void gotoxy(Snack *p) ; //控制蛇身的坐标
void gotoxy(int x1, int y1) ;
void console() ; //控制输入的按键
void snack_Run(int m) ; //控制蛇身移动
void snack_food() ; //控制蛇的食物出现
void save2() ;
void read2() ;
void del() ;
void setLis(bool b){lis=b;}
void setSpeed(int s){speed=s;}
int getSpeed(){return speed;}
void setstyle(int l){style=l;} ;
~Console() ; //释放动态内存
int a[3] ;
void setHead(Snack *he){head=he;}
private:
Snack *head ;//蛇头
Snack *food ;//蛇的食物
int speed ; //速度
int style ; //样式
bool lis ;
};
//定义食物坐标函数
void Console::snack_food()
{
int x ,y ;
srand(time(NULL)) ;
x = rand()%45+2 ;
y = rand()%15+1 ;
food = new Snack(x,y) ;
}
//定义控制蛇增长函数
void Console::snack_Add()
{
if(head==NULL)
{
Snack *p = new Snack(4,1) ;
Snack *p1 = new Snack(3,1) ;
Snack *p2 = new Snack(2,1) ;
head = p ;
p->next = p1 ;
p1->next = p2 ;
p2->next = NULL ;
}
else
{
int x = food->getX() ;
int y = food->getY() ;
Snack *head1 = new Snack(x,y) ;
head1->next = head ;
int n1 = head->getN()+1 ;
head = head1 ;
head->setN(n1) ;
snack_food() ;
}
}
//定义蛇的运动函数
void Console::snack_Run(int m)
{
Snack *p=head->next ;
Snack *p1 ;
int x1 = head->getX() ;
int y1 = head->getY() ;
int n = p->getN() ;
int j = 2 ;
while(j<n)
{
if((x1==p->getX())&&y1==p->getY())
{
exit(0) ;
}
p = p->next ;
j++;
}
if((head->getX()==food->getX())&&(head->getY()==food->getY()))
{
if(lis){cout << '\a';}
snack_Add() ;
}
p = head ;
Snack *head1;
head1 = new Snack(0,0) ;
*head1 = *head ;
int x,y ;
x = p->getX() ;
y = p->getY() ;
j = 1 ;
while(j<=n)
{
p = p->next ;
j++ ;
}
switch(m)
{
case 1: head1->setY(y-1) ;break ;
case 2: head1->setY(y+1) ;break ;
case 3: head1->setX(x-1) ;break ;
case 4: head1->setX(x+1) ;break ;
}
head1->next = head ;
head = head1 ;
}
//定义蛇的身体函数
void Console::snack_body()
{
Snack *p = head ;
int n = p->getN() ;
int j = 1 ;
gotoxy(73,5);
cout << n-3 ;
while(j<n)
{
if(p==head)
{
gotoxy(p) ;
if(style==1){
cout << '@' ;}
else{cout << '#';}
}
else
{
gotoxy(p) ;
if(style==1){
cout << '#' ;}
else{cout << '*';}
}
p = p->next ;
j++;
}
gotoxy(p) ;
cout << ' ' ;
gotoxy(food) ;
cout << '$' ;
}
//定义蛇身的坐标函数
void Console::gotoxy(Snack *p)
{
int x,y ;
x = p->getX() ;
y = p->getY() ;
HANDLE hout ;
COORD coord ;
if(x>64){cout<<"游戏结束! 请按Esc退出";save2();exit(0);}
if(y>24){cout<<"游戏结束! 请按Esc退出";save2();exit(0);}
if(x<1){cout<<"游戏结束! 请按Esc退出";save2();exit(0);}
if(y<1){cout<<"游戏结束! 请按Esc退出";save2();exit(0);}
coord.X= x ;
coord.Y= y ;
hout=GetStdHandle(STD_OUTPUT_HANDLE) ;
SetConsoleCursorPosition(hout,coord) ;
}
void Console::save2(){
int i = head->getN() ;
ofstream fout("fen.dat",ios_base::out) ;
if(i>a[0]){
a[0]=i ;
}else if(i>a[1]){
a[1] = i ;
}
else if(i>a[2]){
a[2] = i ;
}
fout.write((char*)&a[0],sizeof(a[0])) ;
fout.write((char*)&a[1],sizeof(a[1])) ;
fout.write((char*)&a[2],sizeof(a[2])) ;
}
void Console::read2()
{
ifstream fin("fen.dat",ios_base::in) ;
fin.read((char*)&a[0],sizeof(a[0])) ;
fin.read((char*)&a[1],sizeof(a[1])) ;
fin.read((char*)&a[2],sizeof(a[2])) ;
fin.close() ;
}
void Console::gotoxy(int x1, int y1)
{
HANDLE hout ;
COORD coord ;
coord.X= x1 ;
coord.Y= y1 ;
hout=GetStdHandle(STD_OUTPUT_HANDLE) ;
SetConsoleCursorPosition(hout,coord) ;
}
//定义判断输入键盘的函数
void Console::console()
{
char i ;
char j ;
while(1)
{
i = getch() ;
if((i=='s'&&j=='w')||(i=='w'&&j=='s')||(i=='a'&&j=='d')||(i=='d'&&j=='a'))
{
i = j ;
}
switch(i)
{
case 'w': while(!kbhit()){Sleep(speed);snack_Run(1);snack_body();} break ;
case 's': while(!kbhit()){Sleep(speed);snack_Run(2);snack_body();} break ;
case 'a': while(!kbhit()){Sleep(speed);snack_Run(3);snack_body();} break ;
case 'd': while(!kbhit()){Sleep(speed);snack_Run(4);snack_body();} break ;
case 27 : break ;
}
j = i ;
if(i==27){break;}
}
}
//定义释放动态内存的函数
Console::~Console()
{
int i=1 ;
int n1 = head->getN() ;
Snack *p =head,*p2 ;
for(i=1; i<=n1; i++)
{
p2 = p ;
p = p->next ;
delete p2 ;
}
}
void Console::del(){
int i=1 ;
int n1 = head->getN() ;
Snack *p =head,*p2 ;
for(i=1; i<=n1; i++)
{
p2 = p ;
p = p->next ;
delete p2 ;
}
head = NULL ;
}
class Game{
public:
Game():x(0),y(0),s(){s.read2();}
Game(int x1, int y1):x(x1),y(y1){}
void cheek() ; //边框
void gotoxy(int x1, int y1) ; //坐标
void welcome() ;
void console() ;
void console2() ;
void console3() ;
void console4() ;
void console5() ;
void console6() ;
void console7() ;
void col() ;
void nandu() ;
void seting() ;
void listener() ;
void paihang() ;
void style() ;
void welcome1() ;
void print() ;
private:
//Console con ;
int x ; //x坐标
int y ; //y坐标
Console s ;
};
void Game::print()
{
cout << " ╔═══════════╗ " ;
cout << " ║操作说明: ║ " ;
cout << " ║ 1.w,s上下移动 ║ " ;
cout << " ║ 2.回车代表确定 ║ " ;
cout << " ║ 3.ESC代表退出 ║ " ;
cout << " ║ 4.蛇的移动是w,s,a,d║ " ;
cout << " ╚═══════════╝ " ;
}
void Game::paihang()
{
cout << " ╔═══════════╗ " ;
cout << " ║排行榜: ║ " ;
cout << " ║ 1. ║ " ;
cout << " ║ 2. ║ " ;
cout << " ║ 3. ║ " ;
cout << " ╚═══════════╝ " ;
gotoxy(37,12);
cout << s.a[0]-3 << "分";
gotoxy(37,13);
cout << s.a[1]-3 << "分";
gotoxy(37,14);
cout << s.a[2]-3 << "分";
}
void Game::welcome(){
cout << "☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉" ;
cout << "☉ * * * * * * * * ☉" ;
cout << "☉ * * * ☉" ;
cout << "☉ * * * ☉" ;
cout << "☉ * * * * * * * * * * * * * * ☉" ;
cout << "☉ * * * * * * * * * * * * ☉" ;
cout << "☉ * * * * * * * * ☉" ;
cout << "☉ * * * * * * * * * * ☉" ;
cout << "☉ * * * * * * * * ☉" ;
cout << "☉ * * * * * * * * * * * * * * * * * ☉" ;
cout << "☉ ☉" ;
cout << "☉ ☉" ;
cout << "☉ ☉" ;
cout << "☉ ☉" ;
cout << "☉ ☉" ;
cout << "☉ ╔═════════════╗ ☉" ;
cout << "☉ ║ ║ ☉" ;
cout << "☉ ║ 1.开始游戏 ║ ☉" ;
cout << "☉ ║ 2.排行榜 ║ ☉" ;
cout << "☉ ║ 3.设置 ║ ☉" ;
cout << "☉ ║ ║ ☉" ;
cout << "☉ ╚═════════════╝ ☉" ;
cout << "☉ ☉" ;
cout << "☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉" ;
}
void Game::welcome1(){
cout << "☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉" ;
cout << "☉ * * * * * * * * ☉" ;
Sleep(1000) ;
cout << "☉ * * * ☉" ;
Sleep(1000) ;
cout << "☉ * * * ☉" ;
Sleep(1000) ;
cout << "☉ * * * * * * * * * * * * * * ☉" ;
Sleep(1000) ;
cout << "☉ * * * * * * * * * * * * ☉" ;
Sleep(1000) ;
cout << "☉ * * * * * * * * ☉" ;
Sleep(1000) ;
cout << "☉ * * * * * * * * * * ☉" ;
Sleep(1000) ;
cout << "☉ * * * * * * * * ☉" ;
Sleep(1000) ;
cout << "☉ * * * * * * * * * * * * * * * * * ☉" ;
cout << "☉ ☉" ;
cout << "☉ ☉" ;
cout << "☉ ☉" ;
cout << "☉ ☉" ;
cout << "☉ ☉" ;
cout << "☉ ╔═════════════╗ ☉" ;
cout << "☉ ║ ║ ☉" ;
cout << "☉ ║ 1.开始游戏 ║ ☉" ;
cout << "☉ ║ 2.排行榜 ║ ☉" ;
cout << "☉ ║ 3.设置 ║ ☉" ;
cout << "☉ ║ ║ ☉" ;
cout << "☉ ╚═════════════╝ ☉" ;
cout << "☉ ☉" ;
cout << "☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉☉" ;
}
void Game::col() {
cout << " ╔═══════════╗ " ;
cout << " ║游戏界面颜色: ║ " ;
cout << " ║ 1.蓝色 ║ " ;
cout << " ║ 2.绿色 ║ " ;
cout << " ║ 3.蓝绿 ║ " ;
cout << " ║ 4.红色 ║ " ;
cout << " ║ 5.紫色 ║ " ;
cout << " ╚═══════════╝ " ;
}
void Game::seting(){
cout << " ╔════════════════╗ " ;
cout << " ║ 1.游戏界面颜色 ║ " ;
cout << " ║ 2.游戏难度 ║ " ;
cout << " ║ 3.游戏声音 ║ " ;
cout << " ║ 4.蛇的样式 ║ " ;
cout << " ╚════════════════╝ " ;
}
void Game::nandu(){
cout << " ╔═══════════╗ " ;
cout << " ║游戏难度: ║ " ;
cout << " ║ 1.简单 ║ " ;
cout << " ║ 2.一般 ║ " ;
cout << " ║ 3.困难 ║ " ;
cout << " ╚═══════════╝ " ;
}
void Game::listener(){
cout << " ╔═══════════╗ " ;
cout << " ║游戏声音: ║ " ;
cout << " ║ 1.开 ║ " ;
cout << " ║ 2.关 ║ " ;
cout << " ╚═══════════╝ " ;
}
void Game::style(){
cout << " ╔═══════════╗ " ;
cout << " ║蛇的样式: ║ " ;
cout << " ║ 1.@# ║ " ;
cout << " ║ 2.#* ║ " ;
cout << " ╚═══════════╝ " ;
}
void Game::console(){
system("cls") ;
Snack *p = NULL ;
Game wel(32,17) ;
wel.welcome() ;
wel.gotoxy(32,17) ;
cout << "→" ;
int a =17 ;
char c ;
while(1)
{
c = getch() ;
switch(c)
{
case 'w':a-- ;if(a<17){a=17;}wel.gotoxy(32,a) ;cout << "→" ;wel.gotoxy(32,a+1) ;cout<<" ";break ;
case 's':a++ ;if(a>19){a=19;}wel.gotoxy(32,a) ;cout << "→" ;wel.gotoxy(32,a-1) ;cout<<" ";break ;
case 27 :exit(0) ;
case 13 :if(a==17){system("cls");cheek();s.snack_Add();s.snack_food();s.snack_body();s.console();system("cls") ;wel.welcome();wel.gotoxy(32,17) ;
cout << "→" ;break;}
else if(a==19){system("cls");wel.gotoxy(0,10);seting();console2();}
else{system("cls");wel.gotoxy(0,10);paihang();console7();}
}
}
}
void Game::console2()
{
system("cls") ;
Game g ;
g.gotoxy(0,10) ;
g.seting() ;
g.gotoxy(29,11) ;
cout << "→" ;
int a =11 ;
char c ;
while(1)
{
c = getch() ;
switch(c)
{
case 'w':a-- ;if(a<11){a=11;}g.gotoxy(29,a) ;cout << "→" ;g.gotoxy(29,a+1) ;cout<<" ";break ;
case 's':a++ ;if(a>14){a=14;}g.gotoxy(29,a) ;cout << "→" ;g.gotoxy(29,a-1) ;cout<<" ";break ;
case 27 :console() ;
case 13 :if(a==11){system("cls");g.gotoxy(0,7) ;col();console3();}
else if(a==12){system("cls");g.gotoxy(0,7);nandu();console4();}
else if(a==13){system("cls");g.gotoxy(0,7);listener();console5();}
else{system("cls");g.gotoxy(0,7);style();console6();}
}
}
}
void Game::console3(){
Game g ;
g.gotoxy(38,9) ;
cout << "→" ;
int a =9 ;
char c ;
while(1)
{
c = getch() ;
switch(c)
{
case 'w':a-- ;if(a<9){a=9;}g.gotoxy(38,a) ;cout << "→" ;g.gotoxy(38,a+1) ;cout<<" ";break ;
case 's':a++ ;if(a>13){a=13;}g.gotoxy(38,a) ;cout << "→" ;g.gotoxy(38,a-1) ;cout<<" ";break ;
case 27 :console2() ;
case 13 :if(a==9){system("color 1");}
else if(a==10){system("color 2");}
else if(a==11){system("color 3");}
else if(a==12){system("color 4");}
else{system("color 5");}
}
}
}
void Game::console4(){
Game g ;
g.gotoxy(35,9) ;
cout << "→" ;
int a =9 ;
char c ;
while(1)
{
c = getch() ;
switch(c)
{
case 'w':a-- ;if(a<9){a=9;}g.gotoxy(35,a) ;cout << "→" ;g.gotoxy(35,a+1) ;cout<<" ";break ;
case 's':a++ ;if(a>11){a=11;}g.gotoxy(35,a) ;cout << "→" ;g.gotoxy(35,a-1) ;cout<<" ";break ;
case 27 :console2() ;
case 13 :if(a==9){s.setSpeed(150);g.gotoxy(35,13);cout << "难度设为: 简单 按ESC退出";}
else if(a==10){s.setSpeed(100);g.gotoxy(35,13);cout << "难度设为: 一般 按ESC退出";}
else{s.setSpeed(50);g.gotoxy(35,13);cout << "难度设为: 困难 按ESC退出";}
}
}
}
void Game::console5()
{
Game g ;
g.gotoxy(35,9) ;
cout << "→" ;
int a =9 ;
char c ;
while(1)
{
c = getch() ;
switch(c)
{
case 'w':a-- ;if(a<9){a=9;}g.gotoxy(35,a) ;cout << "→" ;g.gotoxy(35,a+1) ;cout<<" ";break ;
case 's':a++ ;if(a>10){a=10;}g.gotoxy(35,a) ;cout << "→" ;g.gotoxy(35,a-1) ;cout<<" ";break ;
case 27 :console2() ;
case 13 :if(a==9){s.setLis(true);g.gotoxy(35,15);cout<<"声音已开启 按ESC退出" ;}
else{s.setLis(false);g.gotoxy(35,15);cout<<"声音已关闭 按ESC退出" ;}
}
}
}
void Game::console6()
{
Game g ;
g.gotoxy(35,9) ;
cout << "→" ;
int a =9 ;
char c ;
while(1)
{
c = getch() ;
switch(c)
{
case 'w':a-- ;if(a<9){a=9;}g.gotoxy(35,a) ;cout << "→" ;g.gotoxy(35,a+1) ;cout<<" ";break ;
case 's':a++ ;if(a>10){a=10;}g.gotoxy(35,a) ;cout << "→" ;g.gotoxy(35,a-1) ;cout<<" ";break ;
case 27 :console2() ;
case 13 :if(a==9){s.setstyle(1);g.gotoxy(35,13);cout <<"蛇的样式: 样式一 按ESC退出";}
else{s.setstyle(2);g.gotoxy(35,13);cout <<"蛇的样式: 样式二 按ESC退出";}
}
}
}
void Game::console7()
{
char c ;
while(1)
{
c = getch() ;
if(c==27){console();break;}
}
}
//定义边框
void Game::cheek()
{
cout << "╔" ;
cout << "═══════════════════════════════╦══════" ;
cout << "╗" ;
cout << "║ ║ ║" ;
cout << "║ ║ ║" ;
cout << "║ ║ 难度: ║" ;
cout << "║ ║ ║" ;
cout << "║ ║ 分数: ║" ;
cout << "║ ║ ║" ;
cout << "║ ║ ║" ;
cout << "║ ║ w键:上 ║" ;
cout << "║ ║ s键:下 ║" ;
cout << "║ ║ a键:左 ║" ;
cout << "║ ║ f键:右 ║" ;
cout << "║ ║ ║" ;
cout << "║ ║ ║" ;
cout << "║ ║ ║" ;
cout << "║ ║ 排行榜 ║" ;
cout << "║ ║ ║" ;
cout << "║ ║ 1. ║" ;
cout << "║ ║ 2. ║" ;
cout << "║ ║ 3. ║" ;
cout << "║ ║ ║" ;
cout << "║ ║ ║" ;
cout << "║ ║ ║" ;
cout << "║ ║ESC退出游戏 ║" ;
cout << "╚" ;
cout << "═══════════════════════════════╩══════" ;
cout << "╝" ;
gotoxy(69,17);
cout << s.a[0]-3<<"分" ;
gotoxy(69,18);
cout << s.a[1]-3<<"分" ;
gotoxy(69,19);
cout << s.a[2]-3<<"分" ;
gotoxy(73,3);
if(s.getSpeed()==100){cout << "一般";}
else if(s.getSpeed()==50){cout << "困难";}
else {cout << "简单" ;}
}
//定义边框内显示内容的坐标
void Game::gotoxy(int x1, int y1)
{
HANDLE hout ;
COORD coord ;
coord.X= x1 ;
coord.Y= y1 ;
hout=GetStdHandle(STD_OUTPUT_HANDLE) ;
SetConsoleCursorPosition(hout,coord) ;
}
//主函数
int main()
{
//system("color fc");
//system("mode con cols=80 lines=25") ;
system("title 贪吃蛇") ;
Game g ;
g.print() ;
Sleep(5000) ;
system("cls") ;
g.welcome1() ;
g.console() ;
return 0 ;
}
首先进入操作说明界面:
过几秒自动跳转到开始界面:
游戏界面:
排行榜:
剩下的都是设置界面: