DAY 30
1
Software Development

# 小遊戲

## 第一個向量問題: (解決)

2 for player

• `aimDir` ，也就是玩家與滑鼠之間的向量
• `aimDirNrom`，簡單說就是 A / |A| (A為向量)

2 for boss

• `bAimDir` ，也就是玩家與boss之間的向量
• `bAimDirNrom`，簡單說就是 A / |A| (A為向量)

## 第二個向量問題: (未解決)

``````#include<SFML/Graphics.hpp>
#include<iostream>

using namespace std;
using namespace sf;

/*
遊戲所需要的 class 宣告
*/

class Bullet
{

public:
Sprite shape;
Vector2f currVelocity;
float maxSpeed;

Bullet(Texture* texture);
~Bullet() {}
};

class Shield
{
public:
Sprite shape;

Shield(Texture* texture, Vector2f pos);
~Shield(){}

};

class Player
{
public:
Sprite shape;
Texture* texture;

int lvl = 1;
int exp = 0;

int HP;
int HPMax;

int MP;
int MPMax;

// for bullets
vector<Bullet> bullets;
Vector2f currVelocity;
float maxSpeed;

// for shield
vector<Shield> shield;

Player(Texture* texture);
~Player() {}

};

class Boss
{
public:
Sprite shape;

int HP;
int HPMax;

vector<Bullet> bullets;
Vector2f currVelocity;

Boss(Texture* texture);
~Boss() {}

};
``````

``````	#include"MyGame.h"
#include<iostream>
#include<SFML/Graphics.hpp>

using namespace std;
using namespace sf;

Bullet::Bullet(Texture* texture) : currVelocity(0.f, 0.f), maxSpeed(10.f)
{
this->shape.setTexture(*texture);
this->shape.setScale(Vector2f(0.03f, 0.03f));
this->shape.rotate(90);
}

Shield::Shield(Texture* texture, Vector2f pos)
{
this->shape.setTexture(*texture);
this->shape.setScale(0.1f, 0.1f);
this->shape.setPosition(pos);
}

Player::Player(Texture* texture)
{
this->HPMax = this->lvl * 103 + 53;
this->HP = this->HPMax;

this->MPMax = this->lvl * 103 + 53;
this->MP = this->MPMax;

this->texture = texture;
this->shape.setTexture(*texture);

this->shape.setScale(Vector2f(0.15f, 0.15f));
}

Boss::Boss(Texture* texture)
{
this->HPMax = 500;
this->HP = this->HPMax;

this->shape.setTexture(*texture);

this->shape.setScale(0.5f, 0.5f);

}
``````

``````#include<iostream>
#include<SFML\Graphics.hpp>
#include<SFML\Window.hpp>
#include<SFML\System.hpp>
#include<sstream>
#include<cstdlib>
#include<math.h>
#include<cmath>
#include<vector>
#include"MyGame.h"
using namespace std;
using namespace sf;

int main()

{
srand(time(NULL));

RenderWindow window(VideoMode(1680, 840), "The Final Game!");
window.setFramerateLimit(200);

/*===========================================================//
Init Text
//===========================================================*/
Font font;

/*===========================================================//
Init Textures
//===========================================================*/
Texture playerTex;

Texture humanBossTex;

Texture earthBossTex;

Texture bulletTex;

Texture bossBulletTex;

/*===========================================================//
UI init
//===========================================================*/

Text gameOverText;
gameOverText.setFont(font);
gameOverText.setCharacterSize(120);
gameOverText.setFillColor(Color::Red);
gameOverText.setPosition(100.f, window.getSize().y / 2);
gameOverText.setString("Game Over!!!!");

Text youWin;
youWin.setFont(font);
youWin.setCharacterSize(120);
youWin.setFillColor(Color::Red);
youWin.setPosition(100.f, window.getSize().y / 2);
youWin.setString("You Win!!!!");

/*===========================================================//
Player init
//===========================================================*/

int score = 0;

Player player(&playerTex);
int shootTimer = 20;

// for hp 顯示
RectangleShape hpBar;
hpBar.setFillColor(Color::Red);

// for mp 顯示
RectangleShape mpBar;
mpBar.setFillColor(Color::Blue);

/*===========================================================//
Boss init
//===========================================================*/
Boss weirdFace(&humanBossTex);
weirdFace.shape.setPosition(window.getSize().x / 2, window.getSize().y / 2);

int bShootTimer = 15;

Text wHpText;
wHpText.setFont(font);
wHpText.setCharacterSize(12);
wHpText.setFillColor(Color::White);

RectangleShape bHpBar;
bHpBar.setFillColor(Color::Red);

/*===========================================================//
Bullet init
//===========================================================*/
Bullet b1(&bulletTex);
Bullet b2(&bossBulletTex);
vector<Bullet> pBullets;
vector<Bullet> bBullets;

Vector2f playerCenter;
Vector2f mousePosWindow;
Vector2f aimDir;
Vector2f aimDirNorm;
Vector2f rotateOrigin = Vector2f(1.f, 1.f);

Vector2f bossCenter;
Vector2f bAimDir;
Vector2f bAimDirNorm;

//cout << aimDirNorm.x << " " << aimDirNorm.y << endl;

while (window.isOpen())
{
Event event;
while (window.pollEvent(event))
{

if (event.type == Event::Closed)
window.close();

if (Keyboard::isKeyPressed(Keyboard::Escape))
window.close();
}

if (player.HP >= 0 && weirdFace.HP >= 0)
{
/*===========================================================//
【Update】 player
//===========================================================*/
// movement

if (Keyboard::isKeyPressed(Keyboard::W))
player.shape.move(0.f, -3.f);
if (Keyboard::isKeyPressed(Keyboard::S))
player.shape.move(0.f, 3.f);
if (Keyboard::isKeyPressed(Keyboard::D))
player.shape.move(3.f, 0.f);
if (Keyboard::isKeyPressed(Keyboard::A))
player.shape.move(-3.f, 0.f);

// collision with window

if (player.shape.getPosition().x <= 0) // left
player.shape.setPosition(0.f, player.shape.getPosition().y);
if (player.shape.getPosition().x >= window.getSize().x - player.shape.getGlobalBounds().width) // right
player.shape.setPosition(window.getSize().x - player.shape.getGlobalBounds().width, player.shape.getPosition().y);
if (player.shape.getPosition().y <= 0) // Top
player.shape.setPosition(player.shape.getPosition().x, 0.f);
if (player.shape.getPosition().y >= window.getSize().y - player.shape.getGlobalBounds().height) // bottom
player.shape.setPosition(player.shape.getPosition().x, window.getSize().y - player.shape.getGlobalBounds().height);

// hp & mp Bars and Text set

hpBar.setPosition(player.shape.getPosition().x + 10, player.shape.getPosition().y - 25.f);
mpBar.setPosition(player.shape.getPosition().x + 9, player.shape.getPosition().y - 15.f);

// HP & MP set

/*===========================================================//
Update】 controls
//===========================================================*/

if (shootTimer < 20)
shootTimer++;

/*===========================================================//
【Update】 Bullet
//===========================================================*/
playerCenter = Vector2f(player.shape.getPosition().x + 100.f, player.shape.getPosition().y - 15.f);
mousePosWindow = Vector2f(Mouse::getPosition(window));
aimDir = mousePosWindow - playerCenter;
aimDirNorm = Vector2f(aimDir.x / sqrt(pow(aimDir.x, 2) + pow(aimDir.y, 2)), aimDir.y / sqrt(pow(aimDir.x, 2) + pow(aimDir.y, 2)));

if (Mouse::isButtonPressed(Mouse::Left) && shootTimer >= 20)
{
b1.shape.setPosition(playerCenter);
b1.currVelocity = aimDirNorm;
pBullets.push_back(Bullet(b1));

shootTimer = 0; // reset timer
}

for (size_t i = 0; i < pBullets.size(); i++)
{

pBullets[i].shape.move(pBullets[i].currVelocity);

//Out of bounds
if (pBullets[i].shape.getPosition().x < 0 || pBullets[i].shape.getPosition().x > window.getSize().x
|| pBullets[i].shape.getPosition().y < 0 || pBullets[i].shape.getPosition().y > window.getSize().y)
{
pBullets.erase(pBullets.begin() + i);

}

else
{
//Enemy collision

if (pBullets[i].shape.getGlobalBounds().intersects(weirdFace.shape.getGlobalBounds()))
{
if (weirdFace.HP < 1)
{

}

else
weirdFace.HP -= 5.3;

pBullets.erase(pBullets.begin() + i);

}
}
}

/*===========================================================//
【Update】 Enemy
//===========================================================*/

// Enemy bullets
Vector2f bossCenter = Vector2f(weirdFace.shape.getPosition().x + 100.f, weirdFace.shape.getPosition().y + 100.f);
bAimDir = playerCenter - bossCenter;
Vector2f bAimDirNorm = Vector2f(bAimDir.x / sqrt(pow(bAimDir.x, 2) + pow(bAimDir.y, 2)), bAimDir.y / sqrt(pow(bAimDir.x, 2) + pow(bAimDir.y, 2)));

if (bShootTimer >= 15)
{
b2.shape.setPosition(bossCenter);
b2.currVelocity = bAimDirNorm;
bBullets.push_back(Bullet(b2));

bShootTimer = 0; // reset timer
weirdFace.shape.move(bAirDirNorm * 10.f);
}

else
bShootTimer++;

for (size_t i = 0; i < bBullets.size(); i++)
{

bBullets[i].shape.move(bBullets[i].currVelocity);

//Out of bounds
if (bBullets[i].shape.getPosition().x < 0 || bBullets[i].shape.getPosition().x > window.getSize().x
|| bBullets[i].shape.getPosition().y < 0 || bBullets[i].shape.getPosition().y > window.getSize().y)
{
bBullets.erase(bBullets.begin() + i);

}

else
{
//Enemy collision

if (bBullets[i].shape.getGlobalBounds().intersects(player.shape.getGlobalBounds()))
{
player.HP -= 0.7;
bBullets.erase(bBullets.begin() + i);

}
}
}

// for enemy collision with player
if (weirdFace.shape.getGlobalBounds().intersects(player.shape.getGlobalBounds()))
{
player.HP -= 1.5;
player.shape.move(bAirDirNorm * 150.f);
}

bHpBar.setPosition(weirdFace.shape.getPosition().x + 5.f, weirdFace.shape.getPosition().y - 25.f);

/*===========================================================//
【Update】 UI
//===========================================================*/
hpBar.setSize(Vector2f((float)player.HP * 0.45f, 10.f));
mpBar.setSize(Vector2f((float)player.MP * 0.45f, 10.f));

bHpBar.setSize(Vector2f((float)weirdFace.HP * 0.45f, 10.f));

}

/*===========================================================//
【Draw】
//===========================================================*/
window.clear();
/*===========================================================//
【Draw】  player
//===========================================================*/
window.draw(player.shape);
window.draw(hpBar);
window.draw(mpBar);

/*===========================================================//
【Draw】 bullets
//===========================================================*/
for (size_t i = 0; i < pBullets.size(); i++)
{
window.draw(pBullets[i].shape);
}

for (size_t i = 0; i < bBullets.size(); i++)
{
window.draw(bBullets[i].shape);
}

/*===========================================================//
【Draw】 Boss
//===========================================================*/
window.draw(weirdFace.shape);
window.draw(bHpBar);

/*===========================================================//
【Draw】 UI
//===========================================================*/
if (player.HP <= 0)
{
window.draw(gameOverText);
}

if (weirdFace.HP <= 0)
{
window.draw(youWin);
}

window.draw(player.shape);
window.display();

}
}
``````

# 夥伴

【團隊成員】

Name 標題 Tags
ALu NFT 網站與 MetaMask 連動的 Owners 登入系統 BlockChain,Modern Web
Cooksuhr 1995到2021，php到react網站開發歷程 Modern Web
ExcitedMail 杰哥的考研紀錄 Computer Science
foodchain 三十天內用C++寫出一個小遊戲 Software Development
momojn C++ 三十天學習紀錄 Software Development
chen_yanlong 學密碼學也猜不到你的手機密碼 Cryptography
yywoli 從資料庫到資料分析視覺化 Data Analysis

### 1 則留言

1
juck30808
iT邦新手 3 級 ‧ 2021-10-12 18:27:25

foodchain iT邦新手 5 級 ‧ 2021-10-12 20:28:20 檢舉