注意:本作未降低难度,建议小白直接copy源码玩。
首先,为照顾开了快速编辑模式的用户,本作为纯键盘输入。
讲解
头文件(别看很多,其实有些是重复的,有些是不必要的):
#include<algorithm>
#include<fstream>
#include<iostream>
#include<stdio.h>
#include<cstdio>
#include<iostream>
#include<windows.h>
#include<conio.h>
#include <time.h>
#include <stdlib.h>
#include<bits/stdc++.h>
#include<cstdio>
#include<algorithm>
#include <windows.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
定义变量:
int blue=100;这是蓝量
int HP=1000;这是血量
int hurt=100;这是伤害
int x=10,y=1; 这是玩家坐标
int Boss_HP=2000;BOSS血量
int Boss_hurt=200;BOSS伤害
int Boss_x=10,Boss_y=20;BOSS坐标
int monsters_HP=200;小怪血量
int monsters_hurt=20;小怪伤害
int monsters_x,monsters_y;小怪坐标
int hj=0;护甲
int fj=0;反甲
int money=0;金钱
取随机数
int x_what(int x) {//取随机数
srand((unsigned)time(NULL));
return( rand() % x);
}
原理:用srand()和rand()取随机数
商城
int sc(){
system("cls");
char cz;
cout<<"金钱"<<money<<endl;
cout<<"1反甲(1个100反),200R金钱"<<endl;
cout<<"2护甲(1个100护),100R金钱"<<endl;
cout<<"3屠龙宝刀(1个100攻击),100金钱"<<endl;
//商城界面
while(!kbhit()){
}
cz=getch();//按键选择购买物品
if(cz=='1'&&money>=200){
money-=200;
fj+=100;
}
if(cz=='2'&&money>=100){
money-=100;
hj+=100;
}
if(cz=='3'&&money>=100){
money-=100;
hurt+=100;
}
//判断购买物品
}
开始界面(无非就是输出):
int open(){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED| FOREGROUND_GREEN);
cout<<" 传奇(DEVC++阳光版)";
cout<<"by:黑客小C"<<endl;
cout<<"wasd移动,f攻击,e技能(输出加回血,100蓝一次),q商城(请开英文输入法并调成小写)"<<endl;
cout<<"规则:打小怪加攻击,打败BOSS胜利"<<endl;
cout<<"按任意键开始";
while(!kbhit()) {
}
system("cls");
monsters_x=x_what(18);
monsters_x++;
monsters_y=x+1;
}
游戏函数(输出界面和进行操作)
int out(){//输出游戏界面
system("cls");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);
cout<<"血量:"<<HP<<"蓝量"<<blue<<"攻击"<<hurt<<"金钱"<<money<<"反甲"<<fj<<"护甲"<<hj<<endl;
for(int i=0;i<=21;i++){
for(int j=0;j<=21;j++){
if(i==0||i==21){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED| FOREGROUND_GREEN);
cout<<"+";
}
else if(j==0||j==21){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED| FOREGROUND_GREEN);
cout<<"+";
}
else if(i==x&&j==y){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);
cout<<"8";
}
else if(i==monsters_x&&j==monsters_y){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);
cout<<"o";
}
else if(i==Boss_x&&j==Boss_y){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);
cout<<"M";
}
else{
cout<<" ";
}
}
cout<<endl;
}
}
//按键操作
int play(){
char cz;
if(kbhit())
{
cz=getch();
if(cz=='w'){
x--;
}
if(cz=='s'){
x++;
}
if(cz=='a'){
y--;
}
if(cz=='d'){
y++;
}
if(cz=='f'){
if(x==monsters_x&&y==monsters_y-1){
monsters_HP-=hurt;
}
if(x==Boss_x&&y==Boss_y-1){
Boss_HP-=hurt;
}
}
if(cz=='e'){
if(blue>=100){
blue-=100;
HP+=200;
if(x==monsters_x&&y==monsters_y-1){
monsters_HP-=(hurt*2);
}
if(x==Boss_x&&y==Boss_y-1){
Boss_HP-=(hurt*2);
}
}
}
if(cz=='q'){
sc();
}
}
}
main()函数:
int main(){
open();
while(true){
out();
play();
//后面的是判断死亡、受伤、胜利的。
if(Boss_HP<=0){
system("cls");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED| FOREGROUND_GREEN);
cout<<"游戏胜利";
Sleep(2000);
return 0;
}
if(HP<=0){
system("cls");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);
cout<<"你死了";
Sleep(2000);
return 0;
}
if(monsters_HP<=0){
monsters_HP=200;
money+=100;
hurt+=100;
monsters_x=x_what(18);
monsters_x++;
monsters_y=x+1;
}
if(x==monsters_x&&y==monsters_y-1){
HP-=monsters_hurt;
HP+=hj;
monsters_HP-=fj;
}
if(x==Boss_x&&y==Boss_y-1){
HP-=Boss_hurt;
HP+=hj;
Boss_HP-=fj;
}
blue+=10;
Sleep(100);
}
return 0;
}
源码
#include<algorithm>
#include<fstream>
#include<iostream>
#include<stdio.h>
#include<cstdio>
#include<iostream>
#include<windows.h>
#include<conio.h>
#include <time.h>
#include <stdlib.h>
#include<bits/stdc++.h>
#include<cstdio>
#include<algorithm>
#include <windows.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int blue=100;
int HP=1000;
int hurt=100;
int x=10,y=1;
int Boss_HP=2000;
int Boss_hurt=200;
int Boss_x=10,Boss_y=20;
int monsters_HP=200;
int monsters_hurt=20;
int monsters_x,monsters_y;
int hj=0;
int fj=0;
int money=0;
int x_what(int x) {//取随机数
srand((unsigned)time(NULL));
return( rand() % x);
}
int sc(){
system("cls");
char cz;
cout<<"金钱"<<money<<endl;
cout<<"1反甲(1个100反),200R金钱"<<endl;
cout<<"2护甲(1个100护),100R金钱"<<endl;
cout<<"3屠龙宝刀(1个100攻击),100金钱"<<endl;
while(!kbhit()){
}
cz=getch();
if(cz=='1'&&money>=200){
money-=200;
fj+=100;
}
if(cz=='2'&&money>=100){
money-=100;
hj+=100;
}
if(cz=='3'&&money>=100){
money-=100;
hurt+=100;
}
}
int open(){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED| FOREGROUND_GREEN);
cout<<" 传奇(DEVC++阳光版)";
cout<<"by:黑客小C"<<endl;
cout<<"wasd移动,f攻击,e技能(输出加回血,100蓝一次),q商城(请开英文输入法并调成小写)"<<endl;
cout<<"规则:打小怪加攻击,打败BOSS胜利"<<endl;
cout<<"按任意键开始";
while(!kbhit()) {
}
system("cls");
monsters_x=x_what(18);
monsters_x++;
monsters_y=x+1;
}
int out(){
system("cls");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);
cout<<"血量:"<<HP<<"蓝量"<<blue<<"攻击"<<hurt<<"金钱"<<money<<"反甲"<<fj<<"护甲"<<hj<<endl;
for(int i=0;i<=21;i++){
for(int j=0;j<=21;j++){
if(i==0||i==21){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED| FOREGROUND_GREEN);
cout<<"+";
}
else if(j==0||j==21){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED| FOREGROUND_GREEN);
cout<<"+";
}
else if(i==x&&j==y){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);
cout<<"8";
}
else if(i==monsters_x&&j==monsters_y){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);
cout<<"o";
}
else if(i==Boss_x&&j==Boss_y){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);
cout<<"M";
}
else{
cout<<" ";
}
}
cout<<endl;
}
}
int play(){
char cz;
if(kbhit())
{
cz=getch();
if(cz=='w'){
x--;
}
if(cz=='s'){
x++;
}
if(cz=='a'){
y--;
}
if(cz=='d'){
y++;
}
if(cz=='f'){
if(x==monsters_x&&y==monsters_y-1){
monsters_HP-=hurt;
}
if(x==Boss_x&&y==Boss_y-1){
Boss_HP-=hurt;
}
}
if(cz=='e'){
if(blue>=100){
blue-=100;
HP+=200;
if(x==monsters_x&&y==monsters_y-1){
monsters_HP-=(hurt*2);
}
if(x==Boss_x&&y==Boss_y-1){
Boss_HP-=(hurt*2);
}
}
}
if(cz=='q'){
sc();
}
}
}
int main(){
open();
while(true){
out();
play();
if(Boss_HP<=0){
system("cls");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED| FOREGROUND_GREEN);
cout<<"游戏胜利";
Sleep(2000);
return 0;
}
if(HP<=0){
system("cls");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);
cout<<"你死了";
Sleep(2000);
return 0;
}
if(monsters_HP<=0){
monsters_HP=200;
money+=100;
hurt+=100;
monsters_x=x_what(18);
monsters_x++;
monsters_y=x+1;
}
if(x==monsters_x&&y==monsters_y-1){
HP-=monsters_hurt;
HP+=hj;
monsters_HP-=fj;
}
if(x==Boss_x&&y==Boss_y-1){
HP-=Boss_hurt;
HP+=hj;
Boss_HP-=fj;
}
blue+=10;
Sleep(100);
}
return 0;
}