haha,闲的没事再写个游戏
----------------------------------
上个游戏:乒乓球小游戏
本来想用图形库(ege)写,但后来想想还是下次吧
————————————
正文:
上次我写的太简单,于是提高了下水平,写了个更简单的:
c++躲方块游戏:
#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
#include<thread>
using namespace std;
char key;
bool stop=0;
int x=9,y=10;//玩家坐标
int game[20][20];
int bld=5,time_=0;//血量,坚持时间
void color(string c);
int get_key() {//收取操作
while(1){
key=getch();
}
}
void die(){//没什么好说的
stop=1;
system("cls");
color("RED");
Sleep(100);
cout<<"y...";
Sleep(100);
cout<<"yo";
Sleep(100);
cout<<"u";
Sleep(100);
cout<<" ";
Sleep(100);
cout<<"d";
Sleep(100);
cout<<"i";
Sleep(100);
cout<<"e";
Sleep(100);
cout<<".";
color("COMMON");
Sleep(100);
cout<<"\n";
Sleep(100);
cout<<"y";
Sleep(100);
cout<<"o";
Sleep(100);
cout<<"u";
Sleep(100);
cout<<"r";
Sleep(100);
cout<<" ";
Sleep(100);
cout<<"s";
Sleep(100);
cout<<"c";
Sleep(100);
cout<<"o";
Sleep(100);
cout<<"r";
Sleep(100);
cout<<"e";
Sleep(100);
cout<<":";
Sleep(100);
cout<<time_;
Sleep(3000);
system("pause");
exit(0);
}
void check(){//检查玩家gameover了没有
while(1){
if(game[x][y]>1&&game[x][y]<5){
bld--;
Sleep(500);
if(bld<=0) die();
}
Sleep(10);
}
}
void t(){//计算时间
while(1){ time_++; Sleep(1000); }
}
void color(string c){
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
if(c=="BLUE") SetConsoleTextAttribute(hConsole,FOREGROUND_BLUE );
if(c=="RED")SetConsoleTextAttribute(hConsole,FOREGROUND_RED );
if(c=="COMMON")SetConsoleTextAttribute(hConsole,FOREGROUND_INTENSITY );
if(c=="GREEN")SetConsoleTextAttribute(hConsole,FOREGROUND_GREEN );
}
void show(){//显示游戏
system("cls");
color("RED");
cout<<"your life:";
for(int i=0;i<bld;i++) cout<<"◇";
color("COMMON");
cout<<"\n";
for(int i=0;i<20;i++){
for(int j=0;j<20;j++){
if(i==x&&j==y){
color("BLUE");
printf("■");
color("COMMON");
}
else{
if(game[i][j]==4){
color("GREEN");
printf("■");
color("COMMON");
}else{
if(game[i][j]==3){
color("RED");
printf("■");
color("COMMON");
}
else{
if(game[i][j]==2){
printf("■");
}else{
cout<<" ";
}
}
}
}
}
cout<<"|\n";
}
for(int i=0;i<20;i++) cout<<"--";
}
void hide_cursor(){//隐藏光标
HANDLE h_GAME=GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cursor_info;
GetConsoleCursorInfo(h_GAME,&cursor_info);
cursor_info.bVisible=false;
SetConsoleCursorInfo(h_GAME,&cursor_info);
}
int ply_change(){//移动玩家,暂停操作
while(1){
if(key==' '){
int tmp[2]={x,y};
x=-1;y=-1;
system("pause");
x=tmp[0]; y=tmp[1];
}
if(key=='w'&&x>0){
x--;
}
if(key=='s' &&x<19){
x++;
}
if(key=='a'&&y>0) y--;
if(key=='d'&&y<19) y++;
key='h';
Sleep(100);
}
}
int change(){//移动方块
while(1){
Sleep(300);
for(int i=19;i>=0;i--){
for(int j=19;j>=0;j--){
if(game[i][j]==2) {
game[i][j]=0;
if(j<19)game[i][j+1]=2;
}
if(game[i][j]==3) {
game[i][j]=0;
if(i<19)game[i+1][j]=3;
}
if(game[i][j]==4&&i<19){
game[i+1][j]=4;
game[i+2][j]=4;
game[i+3][j]=4;
}
if(game[i][j]==4&&i>=19){
for(int k=0;k<20;k++) game[k][j]=0;
}
if(i==0) if(rand()%30==0){
if(rand()%20>17) game[i][j]=4;
else game[i][j]=3;
}
if(j==0) if(rand()%30==0) game[i][j]=2;
}
}
}
}
int show_game(){
while(1){
show();
Sleep(50);
if(stop) break;
}
system("cls");
return 0;
}
void start(){//启动游戏
while(getch()!='s'){}
thread t7(t);
thread t2(get_key);
thread t3(ply_change);
thread t4(show_game);
thread t5(change);
thread t6(check);
t7.join();
t5.join();
t3.join();
t4.join();
t2.join();
t6.join();
return ;
}
//主函数
int main(){
hide_cursor();
srand(time(0));
int tmp=10;
while(tmp--) cout<<"■";
cout<<endl;
cout<<"press 'w','s','a','d' to move,' ' to stop,any key to continue\n";
tmp=20;
while(tmp--) cout<<"_";
cout<<"\n's' to start\n";
start();
}