C++单人小游戏

1.4.3

1.4.4链接

a v1.4.3说明

#include <bits/stdc++.h>
#include <windows.h>
#include <conio.h>
#include <ctime>
using namespace std;
int Rand(int f, int c) {return f + rand() % (c - f + 1);}void SetColor(int wordColor, int backColor) {WORD wColor = ((backColor & 0x0F) << 4) + (wordColor & 0x0F);SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor);}void SetPos(COORD a){HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);SetConsoleCursorPosition(out, a);}void SetPos(short i, short j){COORD pos = { i, j };SetPos(pos);}struct cursor{void hide(){CONSOLE_CURSOR_INFO cursor_info={1,0};SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);}void show(){HANDLE h_GAME=GetStdHandle(STD_OUTPUT_HANDLE);CONSOLE_CURSOR_INFO cursor_info;GetConsoleCursorInfo(h_GAME,&cursor_info);cursor_info.bVisible=true;SetConsoleCursorInfo(h_GAME,&cursor_info);}}c;const int txtn = 4;string texts[txtn] {"There is lots of grass here.  You can build a trap(Press T, metal -10).","There are lots of trees here. You can build a trap(Press T, metal -10).You can also get   wood here(Press W)","There is a cave here. It is   very dark. You can light it up(Press L,wood -2) and mine forsome metal(Press M)","Here is a shop.shield(Press 1, money -5, wood -10, metal -1, blk +10),dagger(Press 2,     money -1, wood -2, metal -10, dmg +10, spd +1), pickaxe(Press 3, wood -3, metal -3, getmetalspeed +4), axe(Press 4, wood -2, metal -3, getwoodspeed +4)"};clock_t startTime;int seed;int refreshEnCnt = 0;int pTime = 0;int dmgpt = 0;struct Map {int pmoney;int w, h;int g[50][50];int trap[50][50];int sx, sy; bool islight[50][50];struct{bool h;double hp;int dmg;int spd;int lastRefreshTime = -1;}en[50][50];int px, py;int php, pdmg, pspd, pblk;int pwood, pmetal;int pwpn;int pdfs;int pgmt; int pgwt; int getmetalspeed, getwoodspeed;Map() {pgwt = 0;pgmt = 0;getmetalspeed = 1, getwoodspeed = 1;memset(en, 0, sizeof(en));memset(g, 0, sizeof(g));memset(trap, 0, sizeof(trap));memset(islight, 0, sizeof(islight));w = 29, h = 29;pmoney = 0, pwood = 0, pmetal = 0;php = 100, pdmg = 5;sx = Rand(w/2-5, w/2+5), sy = Rand(h/2-5, h/2+5);px = w / 2, py = h / 2;pwpn = 0, pdfs = 0;for (int i = 0; i < w; i++) {for (int j = 0; j < h; j++) {int k = rand() % 10;if (k == 0) {g[i][j] = 2;} else if (k <= 2) {g[i][j] = 1;} else {g[i][j] = 0;}}}g[sx][sy] = 3;}};void Start();void START();void prints(Map);void refresh(int, Map&);void setMessage(Map &m, string msg, int color = 0x0);void setText(Map& m, int id);int main() {SetConsoleTitle("a");system("cls");system("color F0");Start();START();}void Start() {ifstream fin("seed.txt");if (fin) {fin >> seed;} else {seed = (int)(time(0))%10000;}srand(seed);clock_t start;start = clock();ifstream fin1("start.txt");if (fin1) {return;}c.hide();int chs = 1;while(1) {SetPos(0, 0);if (chs == 1) {SetColor(0x0, 0xF);printf("开始\n");SetColor(0xF, 0x0);printf("退出\n\n");SetColor(0x0, 0xF);printf("seed: %d", seed);}if (chs == 0) {SetColor(0xF, 0x0);printf("开始\n");SetColor(0x0, 0xF);printf("退出\n\n");SetColor(0x0, 0xF);printf("seed: %d", seed);}int k = getch();if (k == 224) {int kk = getch();if (kk == 72) {chs--;}if (kk == 80) {chs++;}if (chs > 1) {chs = 1;}if (chs < 0) {chs = 0;}}if (k == 13) {if (chs) {exit(0);}else {break;}}Sleep(50);}c.show();system("cls");system("color F0");}void START() {c.hide();Map m;startTime = clock();clock_t lastDmgTime = clock() - 114514;clock_t lastgetwood = clock() - 114514;clock_t lastgetmetal = clock() - 114514;while (1) {prints(m);if (kbhit()) {int k = getch();if (k == 27) {exit(0);}if (k == 100) {if (clock() - lastDmgTime > 1000.0 / m.pspd) {lastDmgTime = clock();int cdx[4] = {0, 0, 1, -1};int cdy[4] = {1, -1, 0, 0};for (int i = 0; i < 4; i++) {if (m.en[m.px+cdx[i]][m.py+cdy[i]].h == true) {m.en[m.px+cdx[i]][m.py+cdy[i]].hp -= m.pdmg;}}}}if (k == VK_SPACE && true == false) {int sptime = clock();while (getch() != VK_SPACE) {}pTime += clock() - sptime;dmgpt += clock() - sptime;}if (k == 'w') {if (m.g[m.px][m.py] == 1 && clock() - lastgetwood >= 700.0 / m.getwoodspeed) {m.pwood++;lastgetwood = clock();}}if (k == 'l') {if (m.g[m.px][m.py] == 2 && m.pwood >= 2 && !m.islight[m.px][m.py]) {m.pwood -=2;m.islight[m.px][m.py] = true;}}if (k == 'm') {if (m.islight[m.px][m.py] == true && m.g[m.px][m.py] == 2 && clock() - lastgetmetal >= 2000.0 / m.getmetalspeed) {m.pmetal++;lastgetmetal = clock();}}if (k == 't') {if (m.pmetal >= 10 && m.g[m.px][m.py] <= 1) {m.pmetal -= 10;m.trap[m.px][m.py] += 3;}}if (m.px == m.sx && m.py == m.sy) {if (k == 49) {if (m.pdfs < 1 && m.pmoney >= 5 && m.pwood >= 10 && m.pmetal >= 1) {m.pdfs = 1;m.pmoney -=5;m.pwood-= 10;m.pmetal -= 1;}}if (k == 50) {if (m.pwpn < 1 && m.pmoney >= 1 && m.pwood >= 2 && m.pmetal >= 10) {m.pwpn = 1;m.pmoney -=1;m.pwood-= 2;m.pmetal -= 10;}}if (k == 51) {if (m.pgmt < 1 && m.pwood >= 3 && m.pmetal >= 3) {m.pgmt = 1;m.pwood -= 3;m.pmetal-=3;}}if (k == 52) {if (m.pgwt < 1 && m.pwood >= 2 && m.pmetal >= 3) {m.pgwt = 1;m.pwood -= 2;m.pmetal-=3;}}}if (k == 224) {int lastx = m.px, lasty = m.py;int kk = getch();if (kk == 72) {m.px--;}if (kk == 80) {m.px++;}if (kk == 75) {m.py--;}if (kk == 77) {m.py++;}if (m.px < 0 || m.px >= m.h || m.py < 0 || m.py >= m.w) {m.px = lastx, m.py = lasty;}if (m.en[m.px][m.py].h == 1) {m.px = lastx, m.py = lasty;}setText(m, m.g[m.px][m.py]);}}int delay = 5;Sleep(delay);clock_t now = clock();refresh((now - startTime) - pTime, m);}}void prints(Map m) {SetColor(0x0, 0xF);SetPos(m.w*2+2, 0);int t = clock() - startTime - pTime;printf("Time = %3d.%3d   ", t/1000, t%1000);SetPos(m.w*2+2, 2);printf("hp = %3d; dmg = %2d; spd = %1d; blk = %3d    ", m.php, m.pdmg,  m.pspd, m.pblk);SetPos(m.w*2+2, 3);printf("money = %d, wood = %d, metal = %d     ", m.pmoney, m.pwood, m.pmetal);int colors[4] = {0xA, 0x2, 0x8, 0xF};for (int i = 0; i < m.h; i++) {SetPos(0, i);for (int j = 0; j <m.w; j++) {SetColor(0xF, colors[m.g[i][j]]);if (m.en[i][j].h) {if (m.g[i][j] != 0) {SetColor(0xF, colors[m.g[i][j]]);} else SetColor(0x0, colors[m.g[i][j]]);if (m.en[i][j].hp > 99){m.en[i][j].h = 0;} else if (m.en[i][j].hp >= 10){cout << (int)m.en[i][j].hp;} else {cout << " " << (int)m.en[i][j].hp;}continue;}if (m.trap[i][j] != 0) {if (m.g[i][j] != 0) {SetColor(0xF, colors[m.g[i][j]]);} else SetColor(0x0, colors[m.g[i][j]]);printf("T");} else if (m.sx == i && m.sy == j) {SetColor(0x0, colors[m.g[i][j]]);printf("S");} else {printf(" ");}if (i == m.px && j == m.py) {if (m.g[i][j] != 0 && m.g[i][j] != 3) {SetColor(0xF, colors[m.g[i][j]]);} else SetColor(0x0, colors[m.g[i][j]]);printf("y");} else {printf(" ");}}}}bool hEn = false;void refresh(int t, Map &m) {int dmgs[2] = {0, 10};int spds[2] = {0, 1};m.pdmg = 5 + dmgs[m.pwpn];m.pspd = 1 + spds[m.pwpn];int blks[2] = {0, 10};m.pblk = 0 + blks[m.pdfs];int gmts[2] = {0, 4};m.getmetalspeed = 1 + gmts[m.pgmt];int gwts[2] = {0, 4};m.getwoodspeed = 1 + gwts[m.pgwt];SetPos(m.w*2+2, 4);SetColor(0x0, 0xF);if (m.php <= 0) {SetColor(0x0, 0xF);SetPos(m.w-3, m.h/2);printf("GAME OVER!");SetPos(m.w-5, m.h/2 + 1);printf("Time = %3d.%3d", t/1000, t%1000);while(getch()!=27);exit(0);}for (int i = 0; i < m.h; i++) {for (int j = 0; j < m.w; j++) {if (m.en[i][j].hp < 1 && m.en[i][j].h != 0) {m.en[i][j].h = 0;if (rand()%3 == 1) {int ccf = 1 + rand()%3;if (m.pmoney < 5){m.pmoney += ccf;int N = 1;string cdx[N] = {""};string cdy[N] {" money dropped from enemy pocket",};int randNum = rand()%N;setMessage(m, cdx[randNum] + char(ccf+'0') + cdy[randNum]);}}}}}if (t > refreshEnCnt * 1000) {int cdx[4] = {0, 0, 1, -1};int cdy[4] = {1, -1, 0, 0};for (int i = 0; i < 4; i++) {if (m.en[m.px+cdx[i]][m.py+cdy[i]].h == true) {m.php -= m.en[m.px+cdx[i]][m.py+cdy[i]].dmg - m.pblk;}}m.php += 1;refreshEnCnt++;for (int i = 0; i < m.h; i++) {for (int j = 0; j < m.w; j++) {if (m.en[i][j].h == 1) {if (m.en[i][j].lastRefreshTime == refreshEnCnt) {continue;}if (abs(m.px - i) + abs(m.py - j) == 1) {continue;}if (abs(m.px - i) + refreshEnCnt%2 > abs(m.py - j)) {if (m.px > i) {swap(m.en[i][j], m.en[i+1][j]);m.en[i+1][j].lastRefreshTime = refreshEnCnt;if (i+1 == m.sx && j == m.sy) {m.en[i+1][j].h = 0;}if (m.trap[i+1][j] != 0) {m.en[i+1][j].h = 0;m.trap[i+1][j]--;}} else{swap(m.en[i][j], m.en[i-1][j]);m.en[i-1][j].lastRefreshTime = refreshEnCnt;if (i-1 == m.sx && j == m.sy) {m.en[i-1][j].h = 0;}if (m.trap[i-1][j] != 0) {m.en[i-1][j].h = 0;m.trap[i-1][j]--;}}} else {if (m.py > j) {swap(m.en[i][j], m.en[i][j+1]);m.en[i][j+1].lastRefreshTime = refreshEnCnt;if (i == m.sx && j+1 == m.sy) {m.en[i][j+1].h = 0;}if (m.trap[i][j+1] != 0) {m.en[i][j+1].h = 0;m.trap[i][j+1]--;}} else{swap(m.en[i][j], m.en[i][j-1]);m.en[i][j-1].lastRefreshTime = refreshEnCnt;if (i == m.sx && j-1 == m.sy) {m.en[i][j-1].h = 0;}if (m.trap[i][j-1] != 0) {m.en[i][j-1].h = 0;m.trap[i][j-1]--;}}}}}}}int k = 1000.0 / (1.0 * t / 1000);if (t % k == rand() % k) {SetPos (0, 30);int i, j;switch (rand()%4) {case 0:i = 0, j = rand() % m.w;break;case 1:i = m.h-1, j = rand() % m.w;break;case 2:i = rand() % m.h, j = 0;break;case 3:i = rand() % m.h, j = m.w - 1;break;}if (i != m.px || j != m.py) {if (!hEn) {setMessage(m, "The first enemy appeared!", 0x4);hEn = 1;}m.en[i][j].h = 1;m.en[i][j].hp = t > 99000 ? 99 * (1.0 * (rand()%4) / 10 + 0.6) : t / 1000 * (1.0 * (rand()%4) / 10 + 0.6);m.en[i][j].spd = 1;m.en[i][j].dmg = t > 30000 ? 30 * (1.0 * (rand()%4) / 10 + 0.6) : t / 1000 * (1.0 * (rand()%4) / 10 + 0.6);}}}void setMessage(Map &m, string msg, int color) {SetPos(m.w*2+2, 4);SetColor(0xF, color);int i = 0;for (;i < msg.length(); i++) {printf("%c", msg[i]);}SetColor(0xF, 0xF);for (;i < 40; i++) {printf(" ");}}void setText(Map& m, int id) {SetPos(m.w*2+2, 6);SetColor(0x0, 0xF);int i = 0;const int mxl = 30;for (;i < texts[id].length(); i++) {if (i%mxl == 0) {SetPos(m.w*2+2, 6 + i/mxl);}printf("%c", texts[id][i]);}SetColor(0xF, 0xF);for (;i < 500; i++) {if (i%mxl == 0) {SetPos(m.w*2+2, 6 + i/mxl);}printf(" ");}}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值