跳到正文

个人记录

贪吃蛇之笑傲江湖

lzh009约 8 分钟
原文
文章信息与版本

文章信息

发布
2023年6月2日
收录
2025年12月12日
更新
2026年11月13日
篇幅
约 8 分钟

版本

  1. v1当前

以下便是我的代码,大家请自便

贪吃蛇原版

C
#include<iostream>
#include<windows.h>
#include<time.h>
#include<stdlib.h>
#include<conio.h>
#include<bits/stdc++.h>
#define N 21
using namespace std;
char name;
int xue=1000,gong=10,my=5,money=500;
void color(double a){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
void spp(){
		color(10);cout<<"········\n";
		Sleep(500);system("cls");
}
void hy(){
	int system(const char *cmd);
        system("color F0");
	cout<<"欢迎来到\n"<<endl;color(8);
	Sleep(400);
	cout<<"笑傲江湖";color(0);cout<<endl;
	Sleep(1000);
	system("cls");
}
void mainn(){
    color(8);system("cls");
	cout<<"在许久许久以前,萧十一郎神秘的失踪了……"<<endl;
	Sleep(1500);system("cls");
	cout<<"古龙找了很久都没有找到……"<<endl;
	Sleep(1800);system("cls");
	cout<<"他于是痛饮三天三夜, 大醉归西……"<<endl;
	Sleep(2999);system("cls");
	cout<<"却不曾想到";
	Sleep(1500);system("cls");
	cout<<"   在金庸放下手中描绘江湖的妙笔之后"<<endl;
	Sleep(1800);system("cls");
	cout<<"一个不知名的小徒拾起了笔"<<endl;
	Sleep(1689);system("cls");
	cout<<"他大话武侠,将许多人如混到了一个世界里"<<endl;
	cout<<" 萧十一郎就是这样走的"<<endl;
	Sleep(1892);system("cls");
	cout<<"而今,几十载春秋已过\n"<<endl;
	cout<<"你能否重整武侠呢?"<<endl;
	Sleep(1689);system("cls");
	cout<<"有一个流浪汉经过\n"<<endl;
	Sleep(1500);system("cls");
	cout<<"画了一条贪吃蛇" ;
	Sleep(999);
}
void gotoxy(int x,int y){
COORD pos;
pos.X=2*x;
pos.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
void color(int a){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
void init(int apple[2])//初始化函数(初始化围墙、显示信息、苹果)
{
int i,j;//初始化围墙
int wall[N+2][N+2]={{0}};
for(i=1;i<=N;i++)
{
for(j=1;j<=N;j++)
wall[i][j]=1;
}
color(11);
for(i=0;i<N+2;i++)
{
for(j=0;j<N+2;j++)
{
if(wall[i][j])
cout<<"■";
else cout<<"□" ;
}
cout<<endl;
}
gotoxy(N+3,1);//显示信息
color(70);
cout<<"按 W S A D 移动方向"<<endl;
gotoxy(N+3,2);
color(70);
cout<<"按任意键暂停"<<endl;
gotoxy(N+3,3);
color(70);
cout<<"得分:"<<endl;
apple[0]=rand()%N+1;//苹果
apple[1]=rand()%N+1;
gotoxy(apple[0],apple[1]);
color(12);
cout<<"●"<<endl;
}
int kaishi()
{
    int system(const char *cmd);
        system("color 52");
int i,j;
int** snake=NULL;
int apple[2];
int score=0;
int tail[2];
int len=3;
char ch='p';
srand((unsigned)time(NULL));
init(apple);
snake=(int**)realloc(snake,sizeof(int*)*len);
for(i=0;i<len;i++)
snake[i]=(int*)malloc(sizeof(int)*2);
for(i=0;i<len;i++)
{
snake[i][0]=N/2;
snake[i][1]=N/2+i;
gotoxy(snake[i][0],snake[i][1]);
color(14);
cout<<"★"<<endl;
}
while(1)//进入消息循环
{
tail[0]=snake[len-1][0];
tail[1]=snake[len-1][1];
gotoxy(tail[0],tail[1]);
color(11);
cout<<"■"<<endl;
for(i=len-1;i>0;i--)
{
snake[i][0]=snake[i-1][0];
snake[i][1]=snake[i-1][1];
gotoxy(snake[i][0],snake[i][1]);
color(14);
cout<<"★"<<endl;
}
if(kbhit())
{
gotoxy(0,N+2);
ch=getche();
}
switch(ch)
{
case 'w':snake[0][1]--;break;
case 's':snake[0][1]++;break;
case 'a':snake[0][0]--;break;
case 'd':snake[0][0]++;break;
default: break;
}
gotoxy(snake[0][0],snake[0][1]);
color(14);
cout<<"★"<<endl;
Sleep(abs(200-0.5*score));
if(snake[0][0]==apple[0]&&snake[0][1]==apple[1])//吃掉苹果后蛇分数加1,蛇长加1
{
score++;
len++;
snake=(int**)realloc(snake,sizeof(int*)*len);
snake[len-1]=(int*)malloc(sizeof(int)*2);
apple[0]=rand()%N+1;
apple[1]=rand()%N+1;
gotoxy(apple[0],apple[1]);
color(12);
cout<<"●"<<endl;
gotoxy(N+5,3);
color(20);
cout<<score<<endl;
}
if(snake[0][1]==0||snake[0][1]==N||snake[0][0]==0||snake[0][0]==N)//撞到围墙后失败
{
gotoxy(N/2,N/2);
color(30);
cout<<"失败!!!"<<endl;
for(i=0;i<len;i++)
free(snake[i]);
Sleep(INFINITE);
exit(0);
}
}
return 0;
}
int main(){
	int system(const char *cmd);
        system("color F0");
	spp();
	system("title 笑傲江湖");
	if(MessageBox(NULL,"请首先阅读任何一条用户须知","系统提示",MB_OKCANCEL)==1)
	    MessageBox(NULL,"世界是如此美好,任何景观都不容错过,请勿沉迷游戏,否则有害身心健康;\n作业是那么可怕,我们为什么要写它呢?不如一把火将它烧了。","警告",MB_OKCANCEL);
	else return 0; 
	hy();
	mainn();
	int system(const char *cmd);
        system("color F0");
    system("cls"); 
    kaishi();
}
原文