游戏幸存者学习VC源码

#include <ctime>

#include <easyx.h>

#include <list>

#include <stdio.h>

#include "header.h"

// 初始化

void InitGame();

// 绘制

void DrawGame();

// 更新

void UpdateGame();

// 定义武器的对象

struct FWeapon

{

// 旋转角度

float angle;

// 旋转速度

float speed;

// 半径

float radius;

// 位置

FVector location;

};

// 定义敌人对象

struct FEnemy

{

// 移动速度

float speed;

// 方向

int direction;

// 位置

FVector location;

// 动画当前帧

int current_anim;

};

// 背景图片

IMAGE bg_img;

// 玩家图片

IMAGE player_left[6];

IMAGE player_right[6];

// 定义玩家位置

FVector player_location;

// 定义玩家当前帧

int player_current_anim;

// 定义玩家当前方向

int player_direction;

// 定义玩家速度

float player_speed;

// 定义一个武器链表

std::list<FWeapon> weapon_list;

// 定义敌人图片

IMAGE enemy_left[6];

IMAGE enemy_right[6];

// 定义敌人链表

std::list<FEnemy> enemy_list;

FRectangle weapon_getBoundingBox(FWeapon weapon)

{

return FRectangle(weapon.location.x - weapon.radius, weapon.location.y - weapon.radius, weapon.radius * 2, weapon.radius * 2);

}

FRectangle enemy_getBoundingBox(FEnemy enemy)

{

return FRectangle(enemy.location.x, enemy.location.y, enemy_left[0].getwidth(), enemy_left->getheight());

}

int main()

{

// 初始化随机数种子

srand(time(NULL));

initgraph(1280, 720, EX_SHOWCONSOLE);

float FPS = 1000 / 60;

InitGame();

bool is_run = true;

while (true)

{

DWORD start_time = GetCurrentTime();

if (is_run == true)

{

DrawGame();

UpdateGame();

}

DWORD end_time = GetCurrentTime();

DWORD delay_time = end_time - start_time;

if (delay_time < FPS)

{

Sleep(FPS - delay_time);

}

}

return 0;

}

void InitGame()

{

// 加载背景图片

loadimage(&bg_img, TEXT("img/Background.png"));

// 加载玩家图片

for (int i = 0; i < 6; i++)

{

TCHAR player_left_path[MAX_PATH] = { 0 };

TCHAR player_right_path[MAX_PATH] = { 0 };

wsprintf(player_left_path, TEXT("img/player_left_%d.png"), i);

wsprintf(player_right_path, TEXT("img/player_right_%d.png"), i);

loadimage(&player_left[i], player_left_path);

loadimage(&player_right[i], player_right_path);

}

// 初始化玩家的位置

player_location = FVector(500, 500);

// 初始化玩家当前帧

player_current_anim = 0;

// 初始化玩家当前方向

player_direction = 0;// 0表示左 1表示右

// 初始化玩家速度

player_speed = 5;

// 初始化一个武器

FWeapon weapon;

weapon.angle = 0;

weapon.speed = 5;

weapon.radius = 20;

FVector player_center = player_location + FVector(player_left[0].getwidth() * 0.5, player_left[0].getheight() * 0.5);

weapon.location = rotatePoint(FVector(player_center.x, player_center.y - 100), player_center, weapon.angle);

weapon_list.push_back(weapon);

// 加载敌人图片

for (int i = 0; i < 6; i++)

{

TCHAR enemy_left_path[MAX_PATH] = { 0 };

TCHAR enemy_right_path[MAX_PATH] = { 0 };

wsprintf(enemy_left_path, TEXT("img/enemy_left_%d.png"), i);

wsprintf(enemy_right_path, TEXT("img/enemy_right_%d.png"), i);

loadimage(&enemy_left[i], enemy_left_path);

loadimage(&enemy_right[i], enemy_right_path);

}

// 初始化敌人

for (int i = 0; i < 10; i++)

{

FEnemy enemy;

enemy.speed = 2;

enemy.direction = 0;

enemy.location.x = rand() % 1280;

enemy.location.y = rand() % 720;

enemy.current_anim = 0;

enemy_list.push_back(enemy);

}

}

void DrawGame()

{

BeginBatchDraw();

// 输出背景图片

putimage(0, 0, &bg_img);

// 输出玩家图片

if (player_direction == 0)putimage_alpha(player_location.x, player_location.y, &player_left[player_current_anim]);

else putimage_alpha(player_location.x, player_location.y, &player_right[player_current_anim]);

// 输出武器

setfillcolor(RED);

for (FWeapon weapon : weapon_list)

fillcircle(weapon.location.x, weapon.location.y, weapon.radius);

// 显示敌人

for (FEnemy enemy : enemy_list)

{

if (enemy.direction == 0)putimage_alpha(enemy.location.x, enemy.location.y, &enemy_left[enemy.current_anim]);

else putimage_alpha(enemy.location.x, enemy.location.y, &enemy_right[enemy.current_anim]);

}

EndBatchDraw();

}

void UpdateGame()

{

// 玩家的动画

player_current_anim++;

if (player_current_anim >= 6)

player_current_anim = 0;

// 敌人的动画

for (FEnemy& enemy : enemy_list)

{

enemy.current_anim++;

if (enemy.current_anim >= 6)

enemy.current_anim = 0;

}

// 玩家的移动

if (GetAsyncKeyState('W'))player_location.y -= player_speed;

if (GetAsyncKeyState('S'))player_location.y += player_speed;

if (GetAsyncKeyState('A')) { player_location.x -= player_speed; player_direction = 0; }

if (GetAsyncKeyState('D')) { player_location.x += player_speed; player_direction = 1; }

FVector player_center = player_location + FVector(player_left[0].getwidth() * 0.5, player_left[0].getheight() * 0.5);

// 武器围绕主角旋转

for (FWeapon& weapon : weapon_list)

{

weapon.angle += weapon.speed;

weapon.location = rotatePoint(FVector(player_center.x, player_center.y - 100), player_center, weapon.angle);

}

// 敌人的移动

for (FEnemy& enemy : enemy_list)

{

FVector enemy_center = enemy.location + FVector(enemy_left[0].getwidth() * 0.5, enemy_left[0].getheight() * 0.5);

FVector enemy_move_direction = FVector::unitization(player_center - enemy_center);

if (enemy_move_direction.dot(FVector(1, 0)) > 0)enemy.direction = 1;

else enemy.direction = 0;

enemy.location += enemy_move_direction * enemy.speed;

}

// 攻击敌人

std::list<FEnemy>::iterator enemy_list_iterator, temp;

int weapon_num = weapon_list.size();

for (FWeapon weapon : weapon_list)

{

FRectangle weapon_BoundingBox = weapon_getBoundingBox(weapon);

for (enemy_list_iterator = enemy_list.begin(); enemy_list_iterator != enemy_list.end(); )

{

if (weapon_BoundingBox.isOverlay(enemy_getBoundingBox(*enemy_list_iterator)))

{

temp = enemy_list_iterator;

enemy_list_iterator++;

enemy_list.erase(temp);

if (weapon_num < 5)

weapon_num++;

}

else

{

enemy_list_iterator++;

}

}

}

int need_weapon_num = weapon_num - weapon_list.size();

bool is = false;

for (int i = 0; i < need_weapon_num; i++)

{

FWeapon weapon;

weapon.angle = 0;

weapon.speed = 5;

weapon.radius = 20;

weapon_list.push_back(weapon);

is = true;

}

if (is)

{

int j = 0;

for (FWeapon& weapon : weapon_list)

{

weapon.angle = j * (360 / weapon_list.size());

FVector player_center = player_location + FVector(player_left[0].getwidth() * 0.5, player_left[0].getheight() * 0.5);

weapon.location = rotatePoint(FVector(player_center.x, player_center.y - 100), player_center, weapon.angle);

j++;

}

}

// 补充敌人

int need_enemy_num = 10 - enemy_list.size();

for (int i = 0; i < need_enemy_num; i++)

{

FEnemy enemy;

enemy.speed = 2;

enemy.direction = 0;

enemy.location.x = (rand() % 2) ? (-rand() % 400) : (1280 + rand() % 400);

enemy.location.y = (rand() % 2) ? (-rand() % 200) : (720 + rand() % 200);

enemy.current_anim = 0;

enemy_list.push_back(enemy);

}

}

相关推荐
初晴~3 分钟前
【Redis分布式锁】高并发场景下秒杀业务的实现思路(集群模式)
java·数据库·redis·分布式·后端·spring·
yuanManGan12 分钟前
数据结构漫游记:静态链表的实现(CPP)
数据结构·链表
黑胡子大叔的小屋1 小时前
基于springboot的海洋知识服务平台的设计与实现
java·spring boot·毕业设计
ThisIsClark1 小时前
【后端面试总结】深入解析进程和线程的区别
java·jvm·面试
火星机器人life1 小时前
基于ceres优化的3d激光雷达开源算法
算法·3d
虽千万人 吾往矣1 小时前
golang LeetCode 热题 100(动态规划)-更新中
算法·leetcode·动态规划
雷神乐乐2 小时前
Spring学习(一)——Sping-XML
java·学习·spring
arnold662 小时前
华为OD E卷(100分)34-转盘寿司
算法·华为od
小林coding2 小时前
阿里云 Java 后端一面,什么难度?
java·后端·mysql·spring·阿里云
V+zmm101342 小时前
基于小程序宿舍报修系统的设计与实现ssm+论文源码调试讲解
java·小程序·毕业设计·mvc·ssm