游戏截图


demo实现了人物的子弹发射,跳跃和重力的判断,上下左右出界则会出现人物值越界弹出游戏
Windows10系统会出现光标乱闪情况
#include"pch.h"
#include <iostream>
#include <time.h>
#include <windows.h>
#include <conio.h>
using namespace std;
int main() {
//世界尺寸
const int ww = 100;
const int wh = 40;
//窗口尺寸
int cx = 0;
int cy = 0;
const int cw = 25;
const int ch = 20;
const int cs = cw * ch;
int map[cs];
int herox = 0;
int heroy = 38;
int zl = 1; //重力
int jp = 4; //跳跃力
int curjp = 0; //跳跃判定
int herofx = 6; //英雄方向
int herost = 5; //英雄状态
//子弹
int zdx = -1;
int zdy = -1;
int zdfx = 6;
int zdzt = -1;
int zdspeed = 2;
//地面
int dmx[3] = { 0, 29, 69 };
int dmy[3] = { 39, 39, 39 };
int dmw[3] = { 20, 30, 30 };
int dmh[3] = { 1, 1, 1 };
int dmlen = 3;
//平台
int ptx[5] = { 5, 34, 74, 5, 13 };
int pty[5] = { 8, 17, 30, 35, 35 };
int ptw[5] = { 20, 25, 25, 2, 2 };
int pth[5] = { 2, 2, 2, 4, 4 };
int ptlen = 5;
while (1)
{
//卷轴算法
cx = herox - cw / 2;
if (cx < 0)
{
cx = 0;
}
else if (cx + cw >= ww) {
cx = ww - cw;
}
cy = heroy - ch / 2;
if (cy < 0)cy = 0;
else if (cy + ch > wh)cy = wh - ch;
//清空画面
for (int i = 0; i < cs; i++)
{
map[i] = 0;
}
//标记地面
for (int i = 0; i < dmlen; i++)
{
int x = dmx[i] - cx;
int y = dmy[i] - cy;
int w = dmw[i];
int h = dmh[i];
if (x >= cw || x + w < 0 || y + h < 0 || y >= ch)continue;
if (x < 0)
{
w += x;
x = 0;
}
if (x + w >= cw)
{
w -= x + w - cw;
}
if (y < 0)
{
h += y;
y = 0;
}
if (y + h >= ch)
{
h -= h + y - ch;
}
int s = w * h;
int pos = x + y * cw;
for (int j = 0; j < s; j++)
{
map[pos] = 1;
pos++;
if (j % w == w - 1)
{
pos = pos + cw - w;
}
}
}
//放入平台
for (int i = 0; i < ptlen; i++)
{
int x = ptx[i] - cx;
int y = pty[i] - cy;
int w = ptw[i];
int h = pth[i];
if (x + w < 0 || x >= cw || y + h < 0 || y >= ch)continue;
if (x < 0) {
w += x;
x = 0;
}
if (x + w > cw)
{
w -= x + w - cw;
}
if (y < 0)
{
h += y;
y = 0;
}
if (y + h > ch)
{
h -= y + h - ch;
}
int pos = x + y * cw;
int s = w * h;
for (int j = 0; j < s; j++)
{
map[pos] = 1;
pos++;
if (j % w == w - 1)
{
pos = pos - w + cw;
}
}
}
//放入子弹
int pos = zdx - cx + (zdy - cy) * cw;
if (pos >= 0)
{
if (zdfx == 4)map[pos] = 4;
else map[pos] = 3;
}
//放入英雄
map[herox - cx + (heroy - cy)*cw] = 2;
//打印
system("cls");
for (int i = 0; i < cs; i++)
{
if (map[i] == 0)cout << " ";
else if (map[i] == 1)cout << "■";
else if (map[i] == 2)cout << "英";
else if (map[i] == 3) cout << "→";
else if (map[i] == 4) cout << "←";
if (i % cw == cw - 1) cout << endl;
}
//操作控制
if (GetAsyncKeyState('A'))
{
herox--;
herofx = 4;
for (int i = 0; i < ptlen; i++)
{
if (herox < ptx[i] || herox >= ptx[i] + ptw[i] || heroy < pty[i] ||
heroy >= pty[i] + pth[i])continue;
herox++;
break;
}
}
else if (GetAsyncKeyState('D'))
{
herox++;
herofx = 6;
for (int i = 0; i < ptlen; i++)
{
if (herox < ptx[i] || herox >= ptx[i] + ptw[i] || heroy < pty[i] ||
heroy >= pty[i] + pth[i])continue;
herox--;
break;
}
}
if (GetAsyncKeyState('J'))//跳跃
{
if (herost == 5)
{
herost = 8;
curjp = jp;
}
}
if (GetAsyncKeyState('K'))//发射
{
if (zdy == -1)
{
if (herofx == 4)
{
zdfx = 4;
zdx = herox - 1;
}
else if (herofx == 6)
{
zdfx = 6;
zdx = herox + 1;
}
zdy = heroy;
}
}
//跳跃判定
if (curjp)
{
for (int i = 0; i < curjp; i++)
{
heroy--;
for (int j = 0; j < ptlen; j++)//碰撞判定
{
if (herox < ptx[j] || herox >= ptx[j] + ptw[j] || heroy < pty[j] || heroy >= pty[j] + pth[j])
continue;
heroy++;
break;
}
}
curjp--;
}
else
{
herost = 8;
heroy += zl;
for (int i = 0; i < ptlen; i++)//碰撞判定
{
if (heroy < pty[i] || heroy >= pty[i] + pth[i] || herox < ptx[i] || herox >= ptx[i] + ptw[i])
continue;
heroy--;
herost = 5;
break;
}
if (herost == 8) {
for (int i = 0; i < dmlen; i++)//碰撞判定
{
if (heroy < dmy[i] || heroy >= dmy[i] + dmh[i] || herox < dmx[i] || herox >= dmx[i] + dmw[i])
continue;
heroy--;
herost = 5;
break;
}
}
}
//子弹运动
if (zdy != -1)
{
int fx = 1;
if (zdfx == 4)fx = -1;
for (int i = 0; i < zdspeed; i++)
{
zdx += fx;
if (zdx < cx || zdx >= cx + cw)
{
zdx = -1;
zdy = -1;
}
for (int j = 0; j < ptlen; j++)
{
if (zdy < pty[j] || zdy >= pty[j] + pth[j] || zdx < ptx[j] || zdx >= ptx[j] + ptw[j])
continue;
zdx = -1;
zdy = -1;
}
}
}
}
}
版权声明:本文为qq_39058599原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。