#由于本人比较菜,不会太高级的模版,所以写出来的游戏不咋好玩
乒乓球小游戏代码 :
#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
using namespace std;
#define KEY_DOWN(VK_NONAME)((GetAsyncKeyState(VK_NONAME)&0x8000)?1:0)
int game_s[10][30];
int sss;
int show_gm(int q,int w,int e,int r){
for(int i=0;i<10;i++){
for(int h=0;h<31;h++){
if(q==i&&h==0){
cout<<"]";
}else{
if(w==i&&h==29){
cout<<"[";
}else{
if(e==i&&r==h){
cout<<"o";
}else{
if(h==30){
cout<<"|";
}else{
cout<<" ";
}
}
}
}
}
cout<<"\n";
}
cout<<"_______________________________"<<endl;
}
int plrvsplr(){
srand(time(0));
bool s;
system("cls");
int ax=0,bx=0,ballx=5,bally=15,asro=0,bsro=0;
cout<<"return to start\n\n_______________________________\n";
show_gm(ax,bx,ballx,bally);
while(1){
if(getch()==13) break;
}
system("cls");
show_gm(ax,bx,ballx,bally);
int lr=rand()%2;
if(lr==0) lr=-1;
int ss=rand()%5;
ss-=2;
bool z=1;
while(asro<11&&bsro<11){
z=!z;
cout<<asro<<":"<<bsro<<"\n_______________________________\n";
if(KEY_DOWN('Q')){
if(ax!=0){
ax--;
}
}
if(KEY_DOWN('A')){
if(ax!=9){
ax++;
}
}
if(KEY_DOWN('O')){
if(bx!=0){
bx--;
}
}
if(KEY_DOWN('L')){
if(bx!=9){
bx++;
}
}
if(lr==1&&bally+lr==29&&(ballx==bx)){
lr=-1;
ss=rand()%5-2;
}else{
if(lr==-1&&bally+lr==0&&(ballx==ax)){
lr=1;
ss=rand()%5-2;
}
}
if(bally==29||bally==0){
if(bally==29){
asro++;
lr=-1;
}else{
bsro++;
lr=1;
}
bally=15;
ballx=5;
}
if(z){
if((ss==1||ss==-1)){
ballx+=ss;
}else{
sss=rand()%2;
if(ss==2){
if(sss) ballx+=1;
}
if(ss==-2){
if(sss) ballx-=1;
}
}
}
s=!s;
if(ballx==0||ballx==9){
if(ballx==0){
ss=rand()%2+1;
}
if(ballx==9){
ss=0-rand()%2-1;
}
}
if(z) bally+=lr;
show_gm(ax,bx,ballx,bally);
Sleep(100);
system("cls");
}
cout<<asro<<":"<<bsro<<"!\n";
if(asro==3){
cout<<"A player win!";
}else{
cout<<"B player win!";
}
Sleep(1000);
return 0;
}
int plrvsrobot(){
srand(time(0));
bool s;
system("cls");
int ax=0,bx=0,ballx=5,bally=15,asro=0,bsro=0;
cout<<"return to start\n\n_______________________________\n";
show_gm(ax,bx,ballx,bally);
while(1){
if(getch()==13) break;
}
system("cls");
show_gm(ax,bx,ballx,bally);
int lr=rand()%2;
if(lr==0) lr=-1;
int ss=rand()%5;
ss-=2;
bool z=1;
while(asro<11&&bsro<11){
z=!z;
cout<<asro<<":"<<bsro<<"\n_______________________________\n";
if(KEY_DOWN('Q')){
if(ax!=0){
ax--;
}
}
if(KEY_DOWN('A')){
if(ax!=9){
ax++;
}
}
if(bx>ballx&&rand()%2){
if(bx!=0){
bx--;
}
}
if(bx<ballx&&rand()%2){
if(bx!=9){
bx++;
}
}
if(lr==1&&bally+lr==29&&(ballx==bx)){
lr=-1;
ss=rand()%5-2;
}else{
if(lr==-1&&bally+lr==0&&(ballx==ax)){
lr=1;
ss=rand()%5-2;
}
}
if(bally==29||bally==0){
if(bally==29){
asro++;
lr=-1;
}else{
bsro++;
lr=1;
}
bally=15;
ballx=5;
}
if(z){
if((ss==1||ss==-1)){
ballx+=ss;
}else{
sss=rand()%2;
if(ss==2){
if(sss) ballx+=1;
}
if(ss==-2){
if(sss) ballx-=1;
}
}
}
s=!s;
if(ballx==0||ballx==9){
if(ballx==0){
ss=rand()%2+1;
}
if(ballx==9){
ss=0-rand()%2-1;
}
}
if(z) bally+=lr;
show_gm(ax,bx,ballx,bally);
Sleep(100);
system("cls");
}
cout<<asro<<":"<<bsro<<"!\n";
if(asro==3){
cout<<"player win!";
}else{
cout<<"robot win!";
}
Sleep(1000);
return 0;
}
int stng(){
system("cls");
cout<<"1.game's color\n";
int y=getch();
if(y==49){
system("cls");
cout<<"color:\n0=black \n 8=gray \n 1=blue 9=light blue \n 2=green A=light green \n 3=light green B=light light green 4=red C=light red \n 5=purple D=light purple \n 6=yellow E=light yellow 7=white F=light white\n\ninput:backround_color1+word_color\n";
char dd[3];
char ddd[128];
strcpy(ddd,"color ");
cin>>dd;
strcat(ddd,dd);
system(ddd);
return 0;
}
}
int start_game(){
system("cls");
cout<<" Ping_Pong! \n1.play with robot\n2.two player\n3.setting\nbackspace:off game\n\n\n";
int a=getch();
if(a==50) plrvsplr();
if(a==8) exit(0);
if(a==49) plrvsrobot();
if(a==51) stng();
}
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 main(){
system("color 0E");
hide_cursor();
int n;
while(1) start_game();
}
效果 :
yi