游戏幸存者学习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);

}

}

相关推荐
m0_571957581 小时前
Java | Leetcode Java题解之第543题二叉树的直径
java·leetcode·题解
魔道不误砍柴功3 小时前
Java 中如何巧妙应用 Function 让方法复用性更强
java·开发语言·python
NiNg_1_2343 小时前
SpringBoot整合SpringSecurity实现密码加密解密、登录认证退出功能
java·spring boot·后端
pianmian13 小时前
python数据结构基础(7)
数据结构·算法
闲晨3 小时前
C++ 继承:代码传承的魔法棒,开启奇幻编程之旅
java·c语言·开发语言·c++·经验分享
测开小菜鸟5 小时前
使用python向钉钉群聊发送消息
java·python·钉钉
好奇龙猫5 小时前
【学习AI-相关路程-mnist手写数字分类-win-硬件:windows-自我学习AI-实验步骤-全连接神经网络(BPnetwork)-操作流程(3) 】
人工智能·算法
P.H. Infinity5 小时前
【RabbitMQ】04-发送者可靠性
java·rabbitmq·java-rabbitmq
生命几十年3万天6 小时前
java的threadlocal为何内存泄漏
java
sp_fyf_20246 小时前
计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-11-01
人工智能·深度学习·神经网络·算法·机器学习·语言模型·数据挖掘