1. 程式人生 > 實用技巧 >自制C++遊戲(Name:YBX AK IOI)

自制C++遊戲(Name:YBX AK IOI)

更新日誌

\([2020.8.1]\)

確定了遊戲大致框架,粗略地製作了開始遊戲部分和地圖中的商店(基礎物品),初級試煉場(萌新刷金幣),角色各種資訊(攻擊力、生命、武器等)和一些神奇地方的入口(還有一個彩蛋

程式碼長度:283

目前程式碼

#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
using namespace std;
string s[11],ps[3];
int coin=0,attack=1,nowhealth=100,health=100,holekey=0,boxkey=0,box=1;
int Woodensword=0,Ironsword=0,Leatherclothing=0,Ironclothing=0;
void init(){
   printf("Welcome to the YBX's game!\n\nPlease press 'S' to start:");
   char c=getch();
   while(c!='S'&&c!='s'){
   	system("cls");
   	printf("Welcome to the YBX's game!\n\nPlease press 'S' to start:");
   	c=getch();
   }
   system("cls");
}
void a(){
   printf("Pay Attention:\n\n@ means Your Position\n\n# means Obstacle\n\nT mean Trees ( and you can hide in it ) \n\npress W,A,S,D to Move\n\n");
   Sleep(3000);
   system("pause");
   system("cls");
}
void b(int ck,int x,int y);
void shop(int ck,int x,int y){
   system("cls");
   if(ck==1) printf("*You don't have enough coins to buy it\n\n");
   if(ck==2) printf("*Successful Purchase !\n\n");
   if(ck==3) printf("*You can't buy this Article more !\n\n");
   if(ck==4) printf("*Input Error\n\n");
   if(ck==5) printf("You already have something better than this !\n\n");
   printf("-------------------Your Condition-------------------\n\n");
   printf("You have %d coins\n\n",coin);
   printf("Your attack power : %d\n\n",attack);
   printf("Your health : %d / %d\n\n",nowhealth,health);
   printf("Your weapons : ");
   if(Ironsword) printf("Iron sword\n\n");
   else if(Woodensword) printf("Wooden sword\n\n");
   else printf("Nothing\n\n");
   printf("Your clothes : ");
   if(Ironclothing) printf("Iron clothing\n\n");
   else if(Leatherclothing) printf("Leather clothing\n\n");
   else printf("Nothing\n\n");
   printf("----------------------The Shop----------------------\n\n");
   printf("Wooden sword : 50 coins\n\n");
   printf("Iron sword : 500 coins\n\n");
   printf("Leather clothing : 100 coins\n\n");
   printf("Iron clothing : 1000 coins\n\n");
   printf("Life restored 100 : 10 coins\n\n");
   printf("Life restored 1000 : 90 coins\n\n");
   printf("What would you like to buy ?\n\n( If you don't want to buy anything , input exit , otherwise input the name of the commodity)\n\n");
   string ss;
   getline(cin,ss);
   if(ss=="exit") b(0,x,y-1);
   else if(ss=="Wooden sword"){
   	if(coin<50) shop(1,x,y);
   	else if(Ironsword) shop(5,x,y);
   	else if(Woodensword) shop(3,x,y);
   	else{
   		coin-=50;
   		Woodensword=1;
   		attack=20;
   		shop(2,x,y);
   	}
   }
   else if(ss=="Iron sword"){
   	if(coin<500) shop(1,x,y);
   	else if(Ironsword) shop(3,x,y);
   	else{
   		coin-=500;
   		Ironsword=1;
   		attack=100;
   		shop(2,x,y);
   	}
   }
   else if(ss=="Leather clothing"){
   	if(coin<100) shop(1,x,y);
   	else if(Ironclothing) shop(5,x,y);
   	else if(Leatherclothing) shop(3,x,y);
   	else{
   		coin-=100;
   		Leatherclothing=1;
   		nowhealth=500;
   		health=500;
   		shop(2,x,y);
   	}
   }
   else if(ss=="Iron clothing"){
   	if(coin<1000) shop(1,x,y);
   	else if(Ironclothing) shop(3,x,y);
   	else{
   		coin-=1000;
   		Ironclothing=1;
   		nowhealth=1500;
   		health=1500;
   		shop(2,x,y);
   	}
   }
   else if(ss=="Life restored 100"){
   	if(coin<10) shop(1,x,y);
   	else{
   		coin-=10;
   		nowhealth=min(nowhealth+100,health);
   		shop(2,x,y);
   	}
   }
   else if(ss=="Life restored 1000"){
   	if(coin<90) shop(1,x,y);
   	else{
   		coin-=90;
   		nowhealth=min(nowhealth+1000,health);
   		shop(2,x,y);
   	}
   }
   else shop(4,x,y);
}
void hole(){
   b(0,4,5);
}
void primary(int yl,int x,int h,int y){
   system("cls");
   printf("-------------------Your Condition-------------------\n\n");
   printf("You have %d coins\n\n",coin);
   printf("Your attack power : %d\n\n",attack);
   printf("Your health : %d / %d\n\n",nowhealth,health);
   printf("Your weapons : ");
   if(Ironsword) printf("Iron sword\n\n");
   else if(Woodensword) printf("Wooden sword\n\n");
   else printf("Nothing\n\n");
   printf("Your clothes : ");
   if(Ironclothing) printf("Iron clothing\n\n");
   else if(Leatherclothing) printf("Leather clothing\n\n");
   else printf("Nothing\n\n----------------------The  Map----------------------\n\n");
   printf("! : monster\n\nPress A,D to left or right ( D also means attack )\n\nMonster blood volume : %d\n",x<37?h:0);
   for(int i=0;i<3;i++){
   	for(int j=0;j<40;j++){
   		if(i==1&&j==x) printf("@");
   		else if(i==1&&j<=y) printf(" ");
   		else cout<<ps[i][j];
   	}
   	printf("\n");
   }
   if(x==39){
   	printf("\nYou win ! ( You get %d coins )\n\n",coin-yl);
   	Sleep(3000);
   	system("pause");
   	b(0,4,6);
   }
   if(nowhealth<=0){
   	printf("\nYou lose ! ( You can't get any coins )\n\n");
   	coin=yl;
   	Sleep(3000);
   	system("pause");
   	b(0,4,6);
   }
   char c=getch();
   if(c=='A'||c=='a'){
   	if(x>0) primary(yl,x-1,h,max(y,x-1));
   	else primary(yl,x,h,max(y,x));
   }
   else if(c=='D'||c=='d'){
   	if(ps[1][x+1]=='!'&&y<x+1){
   		nowhealth--;
   		if(h-attack<=0){
   			coin+=5;
   			primary(yl,x+1,5,max(y,x+1));
   		}
   		else primary(yl,x,h-attack,max(y,x));
   	}
   	else primary(yl,x+1,h,max(y,x+1));
   }
   else{
   	if(ps[1][x+1]=='!'){
   		nowhealth--;
   		primary(yl,x,h,max(y,x));
   	}
   	else primary(yl,x,h,max(y,x));
   }
}
void b(int ck,int x,int y){
   system("cls");
   printf("-------------------Your Condition-------------------\n\n");
   printf("You have %d coins\n\n",coin);
   printf("Your attack power : %d\n\n",attack);
   printf("Your health : %d / %d\n\n",nowhealth,health);
   printf("Your weapons : ");
   if(Ironsword) printf("Iron sword\n\n");
   else if(Woodensword) printf("Wooden sword\n\n");
   else printf("Nothing\n\n");
   printf("Your clothes : ");
   if(Ironclothing) printf("Iron clothing\n\n");
   else if(Leatherclothing) printf("Leather clothing\n\n");
   else printf("Nothing\n\n----------------------The  Map----------------------\n\n");
   for(int i=0;i<11;i++){
   	for(int j=0;j<40;j++){
   		if(i==x&&j==y){
   			if(s[i][j]!='T') printf("@");
   			else printf("T");
   		}
   		else cout<<s[i][j];
   	}
   	printf("\n");
   }
   printf("\n----------------------Message-----------------------\n\n");
   if(ck==1) printf("You don't have box's key\n\n");
   if(ck==2) printf("You found 5000 coins!!!\n\n");
   if(ck==3) printf("You don't have hole's key\n\n");
   if((x==9&&y==33)||(x==8&&y==32)||(x==7&&y==33)||(x==6&&y==34)){
   	printf("Do you want to buy anything ?\n\n(Yes:Y,No:N):");
   	char c=getch();
   	if(c=='Y'||c=='y') shop(0,x,y);
   	else if(c=='N'||c=='n') b(0,x,y-1);
   	else b(0,x,y);
   }
   else if(x==3&&y==37&&box){
   	printf("You found a strange box\n\nDo you want to open it ?\n\n(Yes:Y,No:N):");
   	char c=getch();
   	if(c=='Y'||c=='y'){
   		if(boxkey){
   			box=0;
   			b(2,x-1,y);
   		}
   		else b(1,x,y-1);
   	}
   	else if(c=='N'||c=='n') b(0,x,y-1);
   	else b(0,x,y);
   }
   else if(x==4&&y==4){
   	printf("Thers is a big hole\n\nDo you want to jump in ?\n\n(Yes:Y,No:N):");
   	char c=getch();
   	if(c=='Y'||c=='y'){
   		if(holekey) hole();
   		else b(3,4,5);
   	}
   	else if(c=='N'||c=='n') b(0,4,5);
   	else b(0,x,y);
   }
   else if(x==3&&y==6){
   	printf("Primary training ground\n\nWould you like to enter the Primary training ground ?\n\n(Yes:Y,No:N):");
   	char c=getch();
   	if(c=='Y'||c=='y') primary(coin,0,5,0);
   	else if(c=='N'||c=='n') b(0,4,6);
   	else b(0,x,y);
   }
   else printf("*Press W,A,S,D to Move");
   char c=getch();
   if(c=='W'||c=='w'){
   	if(s[x-1][y]==' '||s[x-1][y]=='T') b(0,x-1,y);
   	else b(0,x,y);
   }
   else if(c=='A'||c=='a'){
   	if(s[x][y-1]==' '||s[x][y-1]=='T') b(0,x,y-1);
   	else b(0,x,y);
   }
   else if(c=='S'||c=='s'){
   	if(s[x+1][y]==' '||s[x+1][y]=='T') b(0,x+1,y);
   	else b(0,x,y);
   }
   else if(c=='D'||c=='d'){
   	if(s[x][y+1]==' '||s[x][y+1]=='T') b(0,x,y+1);
   	else b(0,x,y);
   }
   else b(0,x,y);
}
int main(){
   init();
   s[0]="########################################";
   s[1]="# _ #11#??#??#         TT T T#?????#TTT#";
   s[2]="#{ }#11#??#??#           TTTT#?????#TTT#";
   s[3]="#{ }## ## ## #            T TT#   #TTTT#";
   s[4]="#                          T TT TTTT T #";
   s[5]="#####                       T TTTT TTTT#";
   s[6]="#                             T    ____#";
   s[7]="#                                 /shop#";
   s[8]="#                                /|  _ #";
   s[9]="#                                 | |.|#";
   s[10]="########################################";
   ps[0]="                                        ";
   ps[1]="              !   !  !    !  ! !  !  !  ";
   ps[2]="----------------------------------------";
   a();
   b(0,9,1);
}