0
已解决
李泽屿
新手启示者
新手启示者
思源工作室 年底作品 贪吃蛇2.0 震撼来袭!
之前也发过预告,在这里
最后的成果是和预告有些差别的
此处鸣谢:
@张云瀚 提供了思源工作室专属函数
@彭志杰 在我开发贪吃蛇2.0的时候照理分部
@钱思源 吃瓜作用(bushi)
(啊就不鸣谢自己了,免得没人说脸皮厚)
咳咳。。。切入正题
本来说是20:00发布的,但是耐不住某些人在催,所以提前1~1.5小时发布
游戏代码:
#include <bits/stdc++.h>
#include <windows.h>
#include <stdio.h>
#include <conio.h>
using namespace std;
struct pos{
int x;
int y;
} food, head; /*食物蛇头坐标*/
const int mapX = 35;/*地图*/
const int mapY = 18;/*地图*/
int speed = 300;
int snake[mapX * mapY][2];/*蛇身子*/
int length;/*蛇身子长度*/
int direction;/*方向*/
void info();/*提示信息*/
void init();/*初始化*/
void getInput();/*获取键盘输入*/
void changeSnake();
bool alive();/*没有撞墙*/
void makeFood();/*制造食物*/
void gotoXY(short, short);/*移动光标到指定位置*/
void drawMap();/*画地图*/
void drawFood();/*画食物*/
void drawSnake();/*画蛇*/
void clearSnake();/*清除蛇*/
void upgrade();/*吃到食物,升级*/
bool getFood();
void run();
void gameOver();
void studio_jian_shao(){//工作室介绍
system("cls");
cout<<"本工作室拒绝抄袭,必是原创,制作不易,帮忙点个赞,拜托了"<<endl;
Sleep(1000);
cout<<"工作室介绍:\n总部室长:钱思源"<<endl;
Sleep(1000);
cout<<"总部副室长:李泽屿"<<endl;
Sleep(1000);
cout<<"总部成员:卢嘉懿,蒋祖轩,任熠同"<<endl;
Sleep(1000);
cout<<"分部室长:张云翰\n";
Sleep(1000);
cout<<"分部副室长:彭志杰\n";
Sleep(1000);
cout<<"分部成员:刘荣一,陈子明,王煜辰\n";
Sleep(1000);
cout<<"洛谷连接:https://www.luogu.com.cn/team/51866 团队号:51866";
Sleep(1800);
return ;
}
void game_start(){//思源工作室产品开头必须函数
char a;
cout<<"该游戏由思源游戏(钱思源团队)生产"<<endl;
Sleep(1800);
cout<<"感谢您支持我们工作室,谢谢"<<endl;
Sleep(1800);
system("cls");
cout<<"输入任意数字字符开始,输入7关于此工作室,输入8直接进入游戏,输入9关于此函数作者"<<endl;
cin>>a;
if(a>='0'&&a<='6'){
cout<<"加载时间较长,请耐心等待";
Sleep(1000);
system("cls");
for(int i=0;i<=100;i++){
if(i<=10)cout<<"正在安装中......"<<i<<"%";
else if(i<=30)cout<<"正在为您加载资源中......"<<i<<"%";
else if(i<=60)cout<<"正在解压游戏中......"<<i<<"%";
else if(i<=80)cout<<"正在检测游戏安全**中......"<<i<<"%";
else if(i<=99)cout<<"正在开启游戏中,请耐心等待......"<<i<<"%";
else cout<<"加载完成已完成 %100,感谢您支持我们!";
Sleep(50);
system("cls");
}
cout<<"为了更好的体验游戏,请您开启英文输入法";
Sleep(1000);
}else if(a=='8'){
for(int i=0;i<=100;i++){
if(i<=99)cout<<"正在开启游戏中,请耐心等待......"<<i<<"%";
else cout<<"开启完成 已完成 %100,感谢您支持我们!";
Sleep(125);
system("cls");
}
cout<<"为了更好的体验游戏,请您开启英文输入法";
Sleep(1000);
return ;
}else if(a=='9'){
cout<<"姓名:张云瀚"<<endl;
Sleep(1500);
cout<<"历史简介,前任WSX-HTC-365工作室(已解散)成员"<<endl;
Sleep(1500);
cout<<"现任思源工作室(钱思源团队)成员"<<endl;
Sleep(1500);
cout<<"酷町问答等级:新手光能"<<endl;
Sleep(1500);
cout<<"希望大家多多支持我们思源工作室,下面为您加载游戏"<<endl;
Sleep(1800);
cout<<"加载时间较长,请耐心等待"<<endl;
Sleep(1000);
system("cls");
for(int i=0;i<=100;i++){
if(i<=10)cout<<"正在安装中......"<<i<<"%";
else if(i<=30)cout<<"正在为您加载资源中......"<<i<<"%";
else if(i<=60)cout<<"正在解压游戏中......"<<i<<"%";
else if(i<=80)cout<<"正在检测游戏安全**中......"<<i<<"%";
else if(i<=99)cout<<"正在开启游戏中,请耐心等待......"<<i<<"%";
else cout<<"加载完成已完成 %100,感谢您支持我们!";
Sleep(50);
system("cls");
}
cout<<"为了更好的体验游戏,请您开启英文输入法";
Sleep(1500);
}else if(a=='7'){
studio_jian_shao();
return ;
}else{
cout<<"输入错误,请重新开始游戏";
return ;
}
cout<<"\n";//贪吃蛇2.0专属
return ;
}
void game_end(){//思源工作室产品结束必要
int x;
//system("cls");
//贪吃蛇专属,为了《蛇王争霸赛》,暂时取消此清屏
cout<<"感谢您体验我们的游戏,请为我们打分,满分10分";
cin>>x;
if(x>=0&&x<=3){
cout<<"谢谢您的评分,我们会改进游戏的。";
Sleep(1000);
return ;
}
else if(x<=6){
cout<<"谢谢您的评分,我们会继续加油的!";
Sleep(1000);
return ;
}
else{
cout<<"谢谢您的评分,我们会做出更好的游戏的!";
Sleep(1000);
return ;
}
}
int main()
{
cout<<"PS:思源工作室专属函数由张云翰开发,但与原版有所不同,数据同步:李泽屿\n为了照顾一些客户,将一些Sleep函数的等待时间减少了一半";
Sleep(5000);
system("cls");
game_start();
system("cls");
run();
game_end();
}
void run()
{
cout << "按任意键开始游戏..." << endl;
getch();
srand(int(time(NULL)));
info();/*提示信息*/
init();/*初始化*/
while (true) /*一步的操作*/
{
gotoXY(37, 3);
cout << "蛇的长度是:" << length;
/* for (int i = 0; i < length; i++)
{
gotoXY(37, 2 + i);
cout << snake[i][0] << " " << snake[i][1];
}
*/
getInput();/*获取键盘输入*/
clearSnake();/*清除蛇*/
changeSnake();/*改变蛇身*/
drawSnake();/*画出蛇*/
if (!alive()) /*撞墙*/
{
gameOver();/*结束游戏*/
break;
}
if (getFood()) /*到食物的地方*/
{
upgrade();/*吃到食物,升级*/
makeFood();/*制造食物*/
drawFood();/*画出新的食物*/
}
//getch();
Sleep(speed-length/10*50);
}
}
void info()
{
gotoXY(37,9);
cout << "|------------------------------------------------|";
gotoXY(37,10);
cout << "|wasd控制上下左右移动:W往上,A往下,S往下,D往右|";
gotoXY(37,11);
cout << "|空格键暂停 |";
gotoXY(37,12);
cout << "|------------------------------------------------|";
}
void init()
{
length = 1;
head.x = snake[0][0] = 7;
head.y = snake[0][1] = 7;
makeFood();
direction = 3;
drawMap();
drawFood();
drawSnake();
}
void getInput() /*获取输入,返回一个方向1234分别代表上左下右*/
{
char ch;
if (_kbhit())
{
ch = getch();
if ((ch == 'w' && direction == 3) ||
(ch == 'a' && direction == 4) ||
(ch == 's' && direction == 1) ||
(ch == 'd' && direction == 2))
{
return ;
}
switch (ch)
{
case 'w':
direction = 1;
break;
case 'a':
direction = 2;
break;
case 's':
direction = 3;
break;
case 'd':
direction = 4;
break;
case ' ':
{
gotoXY(37,13);
cout << "游戏暂停,按任意键继续游戏...";
getch();
}
break;
default:
break;
}
}
}
void changeSnake() /*根据方向更新蛇身*/
{
/*更新蛇头*/
switch (direction)
{
case 1:
head.y--;
break;
case 2:
head.x--;
break;
case 3:
head.y++;
break;
case 4:
head.x++;
break;
default:
break;
}
/*更新蛇身*/
for (int i = length; i > 0; i--)
{
snake[i][0] = snake[i - 1][0];
snake[i][1] = snake[i - 1][1];
}
snake[0][0] = head.x;
snake[0][1] = head.y;
}
bool getFood()
{
return (head.x == food.x && head.y == food.y) ? true : false;
}
bool alive()
{
if (head.x == 0 || head.x == (mapX + 1) || head.y == 0 || head.y == (mapY + 1))
{
return false;
}
/*for (int i = 1; i < length; i++)
{
if (head.x == snake[i][0] && head.y == snake[i][1])
{
return false;
}
}*/
return true;
}
void makeFood() /*产生食物坐标*/
{
food.x = rand() % mapX + 1;
food.y = rand() % mapY + 1;
/*食物位置不合法的情况:食物food与snake重合*/
for (int i = 0; i < length; i++)
{
if (food.x == snake[i][0] && food.y == snake[i][1])
{
makeFood();
break;
}
}
}
void upgrade()
{
length++;
snake[length - 1][0] = food.x;
snake[length - 1][1] = food.y;
}
void gotoXY(short x, short y)
{
COORD pos = {x, y};
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, pos);
}
void drawMap()/*画地图*/
{
gotoXY(0, 0);
for (int i = 0; i < 37; i++) cout << "-";
gotoXY(0, 19);
for (int i = 0; i < 37; i++) cout << "-";
for (int i = 0; i < 20; i++)
{
gotoXY(0, i);
cout << "|";
gotoXY(36, i);
cout << "|";
}
}
void drawFood()/*画食物*/
{
gotoXY(food.x, food.y);
cout << "$";
}
void drawSnake()/*画蛇*/
{
for (int i = 0; i < length; i++)
{
gotoXY(snake[i][0], snake[i][1]);
cout << "@";
}
}
void clearSnake()/*清除最后一位即可*/
{
gotoXY(snake[length - 1][0], snake[length - 1][1]);
cout << " ";
}
void gameOver()
{
system("cls");
cout << "Game Over\n蛇的长度:"<<length<<"\n";
}
0
1
0
0
0
0
0
沙宸安
高级启示者
高级启示者
根据代码比对结果,除去输出文字函数studio_jian_shao()、game_start()、game_end()以及main函数的一些输出内容,唯三的改动如下:
改动一:
cout << "Game Over";
cout << "Game Over\n蛇的长度:"<<length<<"\n";
改动二:
for (int i = 1; i < length; i++)
{
if (head.x == snake[i][0] && head.y == snake[i][1])
{
return false;
}
}
/*for (int i = 1; i < length; i++)
{
if (head.x == snake[i][0] && head.y == snake[i][1])
{
return false;
}
}*/
改动三:
void info()
{
gotoXY(37,9);
cout << "|--------------------|";
gotoXY(37,10);
cout << "|wasd控制上下左右移动|";
gotoXY(37,11);
cout << "|空格键暂停 |";
gotoXY(37,12);
cout << "|--------------------|";
}
void info()
{
gotoXY(37,9);
cout << "|------------------------------------------------|";
gotoXY(37,10);
cout << "|wasd控制上下左右移动:W往上,A往下,S往下,D往右|";
gotoXY(37,11);
cout << "|空格键暂停 |";
gotoXY(37,12);
cout << "|------------------------------------------------|";
}
0
0
0
0
0
0
0
0