贪吃蛇(C++版)


这个分文件了,包括food.cpp,food.h,snak.cpp,snak.h,main.c,main.h;

可执行文件下载链接:http://download.csdn.net/detail/txgc0/5304710

main.cpp:

 

#include "main.h"
int foodx,foody;
int main(){
	snak s;
	food f;
	srand((int )time(0));//种种子
	f.init();
	s.initsnak();
	s.runing();
	return 0;
}


food.cpp:

#include "main.h"

void food::init(){
	makefood();
	putfood();
	
}

void food::makefood(){
	
	foodx=rand()%MAXLINE;
	foody=rand()%MAXROW;
	if(foodx == 0){
		foodx=1;
	}
	if(foody == 0){
		foody=1;
	}
}
void food::putfood(){
	food::gotoxy(foodx,foody);
	cout<<"O";
}

void food::gotoxy(int x,int y){
	HANDLE h1; 
	COORD pos;
	pos.X=x;
	pos.Y=y;
	h1=GetStdHandle(STD_OUTPUT_HANDLE); 
	SetConsoleCursorPosition(h1,pos); 
}

int food::getx(){
	return foodx;
}
int food::gety(){
	return foody;
}


snak.cpp:

#include "main.h"


void snak::judgeEnd(){
	int end=0;
	if(level >25){
		printf("恭喜你,通关了!");
		level =0;
		Sleep(1000);
		exit(0);
	}
	for(i=lens-1;i>0;i--){
		if(X[0] == X[i] && Y[0] == Y[i]){
			end = 1;
		}
	}
	if(end == 1 || \
			X[0] >= (MAXLINE) || Y[0] >= (MAXROW) || X[0] <=0 ||Y[0] <=0){
		end=0;
		gotoxy(11,4);
		printf("game over\n");
		gotoxy(8,5);
		printf("play agin:(Y or N)");
		if(getchar() == 'Y'){
			system("cls");
			fd.init();
			initsnak();
			runing();
		}
		else exit(0);
	}
}

void snak::movesnak(){
	for(i=lens-1;i>0;i--){
			X[i]=X[i-1];
			Y[i]=Y[i-1];
	}
	if(dir == LEFT){
			X[0]--;
	}
	else if(dir == RIGHT){
		X[0]++;
	}
	else if(dir == UP){
		Y[0]--;
	}
	else if(dir == DOWN){
		Y[0]++;
	}
}

void snak::initsnak(){
	int i;
	X[0]=10;Y[0]=6;
	X[1]=9;Y[1]=6;
	dir = RIGHT; 
	totalscore=0;
	lens=2;score=0;level=0;
	gotoxy(0,0);
	for(i=0;i<MAXLINE;i++){
		gotoxy(i,0);
		cout<<"-";
		gotoxy(i,MAXROW);
		cout<<"-";
	}
	for(i=0;i<MAXROW;i++){
		gotoxy(MAXLINE,i);
		cout<<"|";
		gotoxy(0,i);
		cout<<"|";
	}
	gotoxy(MAXLINE+5,3);
	cout<<"方向键:w,s,a,d:分别是上、下、左、右;\n";
	gotoxy(MAXLINE+5,4);
	cout<<"规则说明:\n";
	gotoxy(MAXLINE+5,5);
	cout<<"	蛇头不能触碰四周墙壁和蛇身!否则游戏结束\n";
	gotoxy(MAXLINE+5,6);
	cout<<"暂时没有增加选关功能,\n";
	gotoxy(MAXLINE+5,7);
	cout<<"	是从最容易开始,难度慢慢增加!\n";
	gotoxy(MAXLINE+5,9);
	cout<<"快乐游戏,快乐学习\n";
}

void snak::gotoxy(int x,int y){
	HANDLE h1; 
	COORD pos;
	pos.X=x;
	pos.Y=y;
	h1=GetStdHandle(STD_OUTPUT_HANDLE); 
	SetConsoleCursorPosition(h1,pos); 
}
int snak::getdir(int *p){
	char c=getch();
	if(c == 'w' && *p != DOWN)
		return UP;
	else if(c == 's' && *p != UP)
		return DOWN;
	
	else if(c == 'a' && *p != RIGHT)
		return LEFT;
	else if(c == 'd' && *p != LEFT)
		return RIGHT;
	else if(c == 'p'){
		gotoxy(8,4);
		cout<<"!pause!";
		while(getch() != ' ');
		gotoxy(8,4);
		cout<<"       ";
		return *p;
	}
	return *p;
}
void snak::dispmenu(){
	gotoxy(10,20);
	cout<<"head(x,y)=("<<X[0]<<","<<Y[0]<<")";
	gotoxy(10,21);
	cout<<"totalscore:  "<<totalscore<<",  level: "<<(level+1);
	gotoxy(10,22);
	cout<<"the snak lens :"<<lens;
}
void snak::dispsnak(){
	for(i=0;i<lens;i++){
		gotoxy(X[i],Y[i]);
		cout<<"X";
	}
	Sleep(300-level*10);
	gotoxy(X[lens-1],Y[lens-1]);
	cout<<" ";
}
void snak::ifeatfood(){
	gotoxy(10,19);
	cout<<"food(x,y)=("<<foodx<<","<<foody<<")";
	if(X[0] == fd.getx() && Y[0] == fd.gety()){
		fd.makefood();
		fd.putfood();
	gotoxy(X[lens-1],Y[lens-1]);
	cout<<" ";
		lens++;
		score++;
		totalscore++;
		if(score >= 2){
			score = 0;
			level++;
		}
	}
}

void snak::runing(){
	while(1){
		if(kbhit()){
			dir=getdir(&dirlast);
		}
		dirlast=dir;
		
		dispsnak();
		judgeEnd();
		ifeatfood();
		movesnak();
		
		dispmenu();
		
	}
}


main.h:

#ifndef __MAIN_H__
#define __MAIN_H__

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <time.h>
#include <string.h>
#include <iostream>
#include <conio.h>
#include "food.h"
#include "snak.h"

extern int foodx;
extern int foody;

#define MAXLINE	32
#define MAXROW	16
#define LEFT	0
#define RIGHT	1
#define UP		2
#define DOWN	3

class food;
class snak;
using std::cout;
using std::endl;

#endif


food.h:

#ifndef __FOOD_H__
#define __FOOD_H__
#include "main.h"

class sank;
class food{
private:
	
public:
	//food(int x,int y);//构造函数
	void init();
	void makefood();//生成放食物的坐标,并保证有意义
	void putfood();//放置食物
	void gotoxy(int x,int y);
	int getx();
	int gety();
//	void eatfood();
};

#endif


snak.h:

#ifndef __SNAK_H__
#define __SNAK_H__
#include "main.h"

class food;
class snak{
private:
	int X[256],Y[256];
	int totalscore,score,lens,level,dir,i,dirlast;
	food fd;
public:
	void gotoxy(int x,int y);
	int getdir(int *p);
	void initsnak();
	void judgeEnd();
	void dispmenu();
	void runing();
	void dispsnak();
	void ifeatfood();
	void movesnak();
};

#endif


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值