Hello!我是love you 的小羊生煎(>-<)
通过我分享的实用技巧和策略,你将在你的领域脱颖而出,引领潮流!
无论你遇到什么挑战,我将一直在你身边,为你提供支持和鼓励!
2话不说上代码
贪吃蛇
#include <windows.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <cstring>
#include <cstdio>
#include <iostream>
#define N 22
using namespace std;
int gameover;
int x1, y1; // Ëæ»ú³öÃ×
int x,y;
long start;
//=======================================
//ÀàµÄʵÏÖÓëÓ¦ÓÃinitialize
//=======================================
//ÏÂÃ涨ÒåÌ°³ÔÉßµÄ×ø±êÀà
class snake_position
{
public:
int x,y; //x±íʾÐУ¬y±íʾÁÐ
snake_position(){};
void initialize(int &);//×ø±ê³õʼ»¯
};
snake_position position[(N-2)*(N-2)+1]; //¶¨ÒåÌ°³ÔÉß×ø±êÀàÊý×飬ÓÐ(N-2)*(N-2)¸ö×ø±ê
void snake_position::initialize(int &j)
{
x = 1;
y = j;
}
//ÏÂÃ涨ÒåÌ°³ÔÉßµÄÆåÅÌͼ
class snake_map
{
private:
char s[N][N];//¶¨ÒåÌ°³ÔÉßÆåÅÌ£¬°üÀ¨Ç½±Ú¡£
int grade, length;
int gamespeed; //Ç°½øʱ¼ä¼ä¸ô
char direction; // ³õʼÇé¿öÏ£¬ÏòÓÒÔ˶¯
int head,tail;
int score;
bool gameauto;
public:
snake_map(int h=4,int t=1,int l=4,char d=77,int s=0):length(l),direction(d),head(h),tail(t),score(s){}
void initialize(); //³õʼ»¯º¯Êý
void show_game();
int updata_game();
void setpoint();
void getgrade();
void display();
};
//¶¨Òå³õʼ»¯º¯Êý£¬½«Ì°³ÔÉßµÄÆåÅÌͼ½øÐгõʼ»¯
void snake_map::initialize()
{
int i,j;
for(i=1;i<=3;i++)
s[1][i] = '*';
s[1][4] = '#';
for(i=1;i<=N-2;i++)
for(j=1;j<=N-2;j++)
s[i][j]=' '; // ³õʼ»¯Ì°³ÔÉßÆåÅÌÖмä¿Õ°×²¿·Ö
for(i=0;i<=N-1;i++)
s[0][i] = s[N-1][i] = '-'; //³õʼ»¯Ì°³ÔÉßÆåÅÌÉÏÏÂǽ±Ú
for(i=1;i<=N-2;i++)
s[i][0] = s[i][N-1] = '|'; //³õʼ»¯Ì°³ÔÉßÆåÅÌ×óÓÒǽ±Ú
}
//============================================
//Êä³öÌ°³ÔÉßÆåÅÌÐÅÏ¢
void snake_map::show_game()
{
system("cls"); // ÇåÆÁ
int i,j;
cout << endl;
for(i=0;i<N;i++)
{
cout << '\t';
for(j=0;j<N;j++)
cout<<s[i][j]<<' '; // Êä³öÌ°³ÔÉßÆåÅÌ
if(i==2) cout << "\tµÈ¼¶£º" << grade;
if(i==6) cout << "\tËٶȣº" << gamespeed;
if(i==10) cout << "\tµÃ·Ö£º" << score << "·Ö" ;
if(i==14) cout << "\tÔÝÍ££º°´Ò»Ï¿ոñ¼ü" ;
if(i==18) cout << "\t¼ÌÐø£º°´Á½Ï¿ոñ¼ü" ;
cout<<endl;
}
}
//ÊäÈëÑ¡ÔñµÈ¼¶
void snake_map::getgrade()
{
cin>>grade;
while( grade>7 || grade<1 )
{
cout << "ÇëÊäÈëÊý×Ö1-7Ñ¡ÔñµÈ¼¶£¬ÊäÈëÆäËûÊý×ÖÎÞЧ" << endl;
cin >> grade;
}
switch(grade)
{
case 1: gamespeed = 1000;gameauto = 0;break;
case 2: gamespeed = 800;gameauto = 0;break;
case 3: gamespeed = 600;gameauto = 0;break;
case 4: gamespeed = 400;gameauto = 0;break;
case 5: gamespeed = 200;gameauto = 0;break;
case 6: gamespeed = 100;gameauto = 0;break;
case 7: grade = 1;gamespeed = 1000;gameauto = 1;break;
}
}
//Êä³öµÈ¼¶£¬µÃ·ÖÇé¿öÒÔ¼°³ÆºÅ
void snake_map::display()
{
cout << "\n\t\t\t\tµÈ¼¶£º" << grade;
cout << "\n\n\n\t\t\t\tËٶȣº" << gamespeed;
cout << "\n\n\n\t\t\t\tµÃ·Ö£º" << score << "·Ö" ;
}
//Ëæ»ú²úÉúÃ×
void snake_map::setpoint()
{
srand(time(0));
do
{
x1 = rand() % (N-2) + 1;
y1 = rand() % (N-2) + 1;
}while(s[x1][y1]!=' ');
s[x1][y1]='*';
}
char key;
int snake_map::updata_game()
{
gameover = 1;
key = direction;
start = clock();
while((gameover=(clock()-start<=gamespeed))&&!kbhit());
//Èç¹ûÓмü°´Ï»òʱ¼ä³¬¹ý×Ô¶¯Ç°½øʱ¼ä¼ä¸ôÔòÖÕֹѻ·
if(gameover)
{
getch();
key = getch();
}
if(key == ' ')
{
while(getch()!=' '){};//ÕâÀïʵÏÖµÄÊÇ°´¿Õ¸ñ¼üÔÝÍ££¬°´¿Õ¸ñ¼ü¼ÌÐøµÄ¹¦ÄÜ£¬µ«²»ÖªÎªºÎÔÒò£¬ÐèÒª°´Á½Ï¿ոñ²ÅÄܼÌÐø¡£ÕâÊǸöbug¡£
}
else
direction = key;
switch(direction)
{
case 72: x= position[head].x-1; y= position[head].y;break; // ÏòÉÏ
case 80: x= position[head].x+1; y= position[head].y;break; // ÏòÏÂ
case 75: x= position[head].x; y= position[head].y-1;break; // Ïò×ó
case 77: x= position[head].x; y= position[head].y+1; // ÏòÓÒ
}
if(!(direction==72||direction==80||direction==75 ||direction==77)) // °´¼ü·Ç·½Ïò¼ü
gameover = 0;
else if(x==0 || x==N-1 ||y==0 || y==N-1) // Åöµ½Ç½±Ú
gameover = 0;
else if(s[x][y]!=' '&&!(x==x1&&y==y1)) // ÉßÍ·Åöµ½ÉßÉí
gameover = 0;
else if(x==x1 && y==y1)
{ // ³ÔÃ×£¬³¤¶È¼Ó1
length ++;
if(length>=8 && gameauto)
{
length -= 8;
grade ++;
if(gamespeed>=200)
gamespeed -= 200; // ¸Ä±äÌ°³ÔÉßÇ°½øËÙ¶È
else
gamespeed = 100;
}
s[x][y]= '#'; //¸üÐÂÉßÍ·
s[position[head].x][position[head].y] = '*'; //³ÔÃ׺ó½«ÔÏÈÉßÍ·±äΪÉßÉí
head = (head+1) % ( (N-2)*(N-2) ); //È¡ÉßÍ·×ø±ê
position[head].x = x;
position[head].y = y;
show_game();
gameover = 1;
score += grade*20; //¼Ó·Ö
setpoint(); //²úÉúÃ×
}
else
{ // ²»³ÔÃ×
s[position[tail].x][position[tail].y]=' ';//½«ÉßβÖÿÕ
tail= (tail+1) % ( (N-2) * (N-2) );//¸üÐÂÉßβ×ø±ê
s[position[head].x][position[head].y]='*'; //½«ÉßÍ·¸üΪÉßÉí
head= (head+1) % ( (N-2) * (N-2) );
position[head].x = x;
position[head].y = y;
s[position[head].x][position[head].y]='#'; //¸üÐÂÉßÍ·
gameover = 1;
}
return gameover;
}
//====================================
//Ö÷º¯Êý²¿·Ö
//====================================
int main()
{
char ctn = 'y';
int nodead;
cout<<"\n\n\n\n\n\t\t\t »¶Ó½øÈëÌ°³ÔÉßÓÎÏ·!"<<endl;//»¶Ó½çÃæ;
cout<<"\n\n\n\t\t\t °´ÈÎÒâ¼üÂíÉÏ¿ªÊ¼¡£¡£¡£"<<endl;//×¼±¸¿ªÊ¼;;
getch();
while( ctn=='y' )
{
system("cls"); // ÇåÆÁ
snake_map snake;
snake.initialize();
cout << "\n\nÇëÊäÈëÊý×ÖÑ¡ÔñÓÎÏ·µÈ¼¶£º" << endl;
cout << "\n\n\n\t\t\t1.µÈ¼¶Ò»£ºËÙ¶È 1000 \n\n\t\t\t2.µÈ¼¶¶þ£ºËÙ¶È 800 \n\n\t\t\t3.µÈ¼¶Èý£ºËÙ¶È 600 ";
cout << "\n\n\t\t\t4.µÈ¼¶ËÄ£ºËÙ¶È 400 \n\n\t\t\t5.µÈ¼¶Î壺ËÙ¶È 200 \n\n\t\t\t6.µÈ¼¶Áù£ºËÙ¶È 100 \n\n\t\t\t7.×Ô¶¯Éý¼¶Ä£Ê½" << endl;
snake.getgrade();//»ñÈ¡µÈ¼¶
for(int i=1;i<=4;i++)
{
position[i].initialize(i);//³õʼ»¯×ø±ê
}
snake.setpoint(); // ²úÉúµÚÒ»¸öÃ×
do
{
snake.show_game();
nodead = snake.updata_game();
}while(nodead);
system("cls"); //ÇåÆÁ
cout << "\n\n\n\t\t\t\tGameover£¡\n\n"<<endl;
snake.display();//Êä³öµÈ¼¶/µÃ·ÖÇé¿ö
cout << "\n\n\n\t\t ÊÇ·ñÑ¡Ôñ¼ÌÐøÓÎÏ·£¿ÊäÈë y ¼ÌÐø£¬n Í˳ö" << endl;
cin >> ctn;
}
return 0;
}
点赞是给我最大的动力!!!
标签:head,cout,int,void,贪吃蛇,snake,position From: https://blog.csdn.net/2401_83259703/article/details/140633910