c++实现双人枪战

  • lgy2024
    了解作者
  • C/C++
    开发工具
  • 3.3KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 0
    下载次数
  • 2022-08-19 22:05
    上传日期
直接运行即可((((((((((((((((
game.zip
  • game.cpp
    12.8KB
内容介绍
#include <iostream> #include <cstdio> #include <windows.h> #include <conio.h> #include <ctime> #include <cstring> #define KEY_DOWN(vk_code) GetAsyncKeyState(vk_code) & 0x8000 ? 1 : 0 using namespace std; void HindCursor() { HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_CURSOR_INFO CursorInfo; GetConsoleCursorInfo(handle, &CursorInfo); CursorInfo.bVisible = false; SetConsoleCursorInfo(handle, &CursorInfo); } void gotoxy(int x, int y) { COORD pos; pos.X = y; pos.Y = x; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos); } void color(int a) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), a); } struct Button { int x, y, color; const char *name; int len; }; void GetPos(POINT &pt) { HWND hwnd = GetForegroundWindow(); GetCursorPos(&pt); ScreenToClient(hwnd, &pt); pt.y = pt.y / 16, pt.x = pt.x / 8; swap(pt.x, pt.y); } Button NewButton(int x, int y, int color, const char *name) { Button t; t.x = x, t.y = y, t.name = name; t.color = color; t.len = strlen(name); return t; } bool Preserve(Button A) { gotoxy(A.x, A.y), color(A.color), printf("%s", A.name); POINT pt; GetPos(pt); if (pt.x == A.x && (pt.y >= A.y&&pt.y <= A.y + A.len)) { color(112), gotoxy(A.x, A.y), printf("%s", A.name); if (KEY_DOWN(MOUSE_MOVED)) return 1; } return 0; } struct Sprite { int width, height, x, y, Color, direction; string * body; void Draw() { color(Color); for (int i = 0; i < height; i++) { gotoxy(x + i, y); for (int j = 0; j < width; j++) printf("%c", body[i][j]); } } void Clean() { for (int i = 0; i < height; i++) { gotoxy(x + i, y); for (int j = 0; j < width; j++) printf(" "); } } }; Sprite NewSprite(string s[], int w, int h, int _color, int nx, int ny) { Sprite newsprite; newsprite.x = nx; newsprite.y = ny; newsprite.body = s; newsprite.width = w; newsprite.height = h; newsprite.Color = _color; newsprite.direction = 1; return newsprite; } bool CheckCollision(Sprite a, Sprite b) { int ax1 = a.x, ax2 = a.x + a.height - 1, ay1 = a.y, ay2 = a.y + a.width - 1; int bx1 = b.x, bx2 = b.x + b.height - 1, by1 = b.y, by2 = b.y + b.width - 1; if ((ax2 >= bx1 && ay2 >= by1 && ax1 <= bx1 && ay1 <= by1) || (ax2 >= bx1 && ay1 <= by2 && ax1 <= bx1 && ay1 >= by1) || (bx2 >= ax1 && by2 >= ay1 && bx1 <= ax1 && by1 <= ay1) || (bx2 >= ax1 && by1 <= ay2 && bx1 <= ax1 && by1 >= ay1)) return 1; return 0; } bool CheckIn(Sprite a, Sprite b) { int ax1 = a.x, ax2 = a.x + a.height - 1, ay1 = a.y, ay2 = a.y + a.width - 1; int bx1 = b.x, bx2 = b.x + b.height - 1, by1 = b.y, by2 = b.y + b.width - 1; if (ax1 <= bx1 && ay1 <= by1 && ax2 >= bx2 && ay2 >= by2) return 1; return 0; } string grass[1] = { " " }; string soil[30]; string bullet[1] = { "▂" }; int SoilHieght[9] = { 2,2,2,10,20,7,7,7,7 }; Sprite Grass[9], Soil[9], Bullet[10005], bhp, rhp; bool flag[10005], gameover; int blueHP, redHP; Sprite letin(Sprite a) { a.x = max(a.x, 0); a.x = min(a.x, 27); a.y = max(a.y, 0); a.y = min(a.y, 117); return a; } int NowBackgroundColor(Sprite a) { for (int i = 0; i <= 8; i++) { if (CheckCollision(Soil[i], a)) return 96; if (a.height == 1 && CheckCollision(Grass[i], a)) return 32; } return 224; } int main() { HindCursor(); system("mode con cols=120 lines=30"); for (int i = 0; i < 30; i++) soil[i] = grass[0]; Grass[0] = NewSprite(grass, 12, 1, 32, 6, 52); Grass[1] = NewSprite(grass, 12, 1, 32, 11, 18); Grass[2] = NewSprite(grass, 12, 1, 32, 11, 91); Grass[3] = NewSprite(grass, 15, 1, 32, 18, 50); Grass[4] = NewSprite(grass, 53, 1, 32, 9, 34); Grass[5] = NewSprite(grass, 32, 1, 32, 14, 14); Grass[6] = NewSprite(grass, 38, 1, 32, 14, 70); Grass[7] = NewSprite(grass, 34, 1, 32, 22, 14); Grass[8] = NewSprite(grass, 41, 1, 32, 22, 67); color(224); for (int i = 0; i <= 30; i++) for (int j = 0; j < 119; j++) printf(" "); for (int i = 0; i <= 8; i++) { Soil[i] = NewSprite(soil, Grass[i].width, SoilHieght[i], 96, Grass[i].x + 1, Grass[i].y); Soil[i].Draw(); } for (int i = 0; i <= 8; i++) Grass[i].Draw(); string player[2] = { "●", "■" }; string HP[1] = { "■■■■■■■■■■" }; Sprite red = NewSprite(player, 2, 2, 4 | 224, 4, 54); Sprite blue = NewSprite(player, 2, 2, 1 | 224, 4, 57); bhp = NewSprite(HP, 20, 1, 1 | 224, 0, 3); rhp = NewSprite(HP, 20, 1, 4 | 224, 0, 97); int bluejump = 0, redjump = 0; bool bluejumping = false, redjumping = false; int tbluejump = 0, tredjump = 0; int bulleti = 0, tbluebullet = 0, tredbullet = 0; int bhps = 5, rhps = 5; blueHP = 50; redHP = 50; gameover = false; memset(flag, 0, sizeof(flag)); red.Draw(); blue.Draw(); bhp.Draw(); rhp.Draw(); color(0 | 224); gotoxy(0, 1); printf("%d", bhps); gotoxy(0, 118); printf("%d", rhps); while (1) { bool bluemove = false; int bnx = blue.x, bny = blue.y, rnx = red.x, rny = red.y; Sprite b = NewSprite(player, 2, 2, 1 | 224, bnx, bny); Sprite r = NewSprite(player, 2, 2, 4 | 224, rnx, rny); color(224); for (int i = 0; i < 3; i++) { if (CheckCollision(blue, Soil[0]) || CheckCollision(blue, Grass[0]) || CheckCollision(red, Soil[0]) || CheckCollision(red, Grass[0])) { gotoxy(6 + i, 51); printf(" "); gotoxy(6 + i, 64); printf(" "); } if (CheckCollision(blue, Soil[1]) || CheckCollision(blue, Grass[1]) || CheckCollision(red, Soil[1]) || CheckCollision(red, Grass[1])) { gotoxy(11 + i, 17); printf(" "); gotoxy(11 + i, 30); printf(" "); } if (CheckCollision(blue, Soil[2]) || CheckCollision(blue, Grass[2]) || CheckCollision(red, Soil[2]) || CheckCollision(red, Grass[2])) { gotoxy(11 + i, 90); printf(" "); gotoxy(11 + i, 103); printf(" "); } } for (int i = 0; i < 5; i++) if (CheckCollision(blue, Soil[4]) || CheckCollision(blue, Grass[4]) || CheckCollision(red, Soil[4]) || CheckCollision(red, Grass[4])) { gotoxy(9 + i, 33); printf(" "); gotoxy(9 + i, 87); printf(" "); } /* for (int i = 0; i < 7; i++) { if (CheckCollision(blue, Soil[5]) || CheckCollision(blue, Grass[5])) gotoxy(14 + i, 12); printf(" "); if (CheckCollision(blue, Soil[6]) || CheckCollision(blue, Grass[6])) gotoxy(14 + i, 108); printf(" "); if (CheckCollision(blue, Soil[7]) || CheckCollision(blue, Grass[7])) gotoxy(22 + i, 12); printf(" "); if (CheckCollision(blue, Soil[8]) || CheckCollision(blue, Soil[8])) gotoxy(22 + i, 108); printf(" "); }*/ blue.Draw(); red.Draw(); if (KEY_DOWN(VK_LEFT)) { int COLOR = NowBackgroundColor(b); blue.direction = -1; color(COLOR); blue.Clean(); blue.y--; blue.Color = 1 | COLOR; blue = letin(blue); blue.Draw(); red.Draw(); } if (KEY_DOWN(VK_RIGHT)) { int COLOR = NowBackgroundColor(b); blue.direction = 1; blue.Color = 1 | COLOR; color(COLOR); blue.Clean(); blue.y++; blue = letin(blue); blue.Draw(); red.Draw(); } if (KEY_DOWN(VK_UP)) if (bluejumping == false) { tbluejump = -1; bluejump = 0; bluejumping = true; } if (bluejumping) { int COLOR = NowBackgroundColor(blue); blue.Color = 1 | COLOR; color(COLOR); blue.Clean(); if (tbluejump != -2) { blue.x += tbluejump; bluejump += tbluejump; } for (int i = 0; i <= 8; i++) if (blue.x == Grass[i].x - 2 && blue.y >= Grass[i].y - 1 && blue.y + blue.width <= Grass[i].y + Grass[i].width + 1) { tbluejump = 0; bluejumping = false; break; } blue = letin(blue); blue.Draw(); red.Draw(); if (bluejump == -5) tbluejump = 1; } else { int j; for (j = 0; j <= 8; j++
评论
    相关推荐
    • C# 仿QQ农场游戏源代码
      C# 仿QQ农场游戏源代码,C#游戏源码,仿QQ农场源代码雏形,只实现了基本功能,编写过程中用到了C#多线程的原理和相关知识。实现了农场的播种、出芽、浇水、生长、开花、结果以及游戏统计、仓库管理等基础功能,对...
    • C#停车场游戏源代码
      摘要:C#源码,游戏开发,停车场游戏 C#停车场游戏源代码,仿QQ网的Flash停车场游戏,当然功能还有很多需要完善,仅提供一种编程思路吧。希望对初学者有所帮助。
    • 贪吃蛇游戏,C#游戏源代码
      摘要:C#源码,游戏开发,贪吃蛇 又一个C#贪吃蛇,没有抓到截图,源代码完整,请使用VS2008以上版本,学习C#游戏可以参选的一个小源码
    • C#智力问答游戏源代码
      摘要:C#源码,游戏开发,智力问答,IrisSkin2.dll C#漂亮界面的智力问答游戏源码下载,就像做单选题一样,选择答案后点击下一题,可知道上一题是否答对,也可统计成绩,本程序在界面方面使用了IrisSkin2.dll C#皮肤...
    • c#人机对战井字游戏源代码
      c#人机对战井字游戏源代码,可了解下棋盘的绘制、 电脑下棋的逻辑,判断输赢,修改所下棋子的颜色,修改该标签上的Owner的值为当前玩家;用到了C#自定义控件GameClass.dll,也包括有源码,生成GameClass.dll要拷贝到...
    • C# 连连看游戏源代码拳皇头像版
      内容索引:C#源码,游戏开发,连连看 C# 连连看游戏源代码拳皇头像版,带声音,编译环境VS2008。很好玩的游戏,这是C#版的源代码,虽然没有真正的连连看功能那么多,但已经实现了基础的游戏框架,有兴趣的朋友可以再此...
    • C#变色球小游戏源代码
      摘要:C#源码,游戏开发,C#游戏源码 C#编写的变色球小游戏源代码,通过游戏你可以学习下小球绘图、寻找鼠标坐标、得到小球绘图区的宽度和高度、找到标题绘图区高度、最大半径和摩擦力、滞留文字控制类集合、小球线程...
    • C#迷宫游戏源代码带音乐播放
      内容索引:C#源码,游戏开发,迷宫,C#游戏源码 c#2005写的迷宫游戏,源代码编译通过,只是游戏玩法说的不太清,编译后不知道怎么玩,玩的过程中还可以播放音乐,这都是些简单的游戏,希望对C#初学者有一定帮助。
    • android 游戏源码
      android 游戏源码 Android2.0游戏开发实战宝典源代码(第1章-第4章)
    • 网络五子棋游戏源码
      这个感觉很不错的一个网络版本的五子棋游戏源码,包含服务器端、游戏大厅之客户端。具有游戏大厅功能,支持多人游戏,可以实时显示玩家得分,支持互相发送信息聊天功能。可以自定义玩家昵称,玩家速度等。玩游戏时...