Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
DeadlyOre committed Jan 23, 2022
1 parent da15dd6 commit f64d91f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
6 changes: 4 additions & 2 deletions Test game/Bullet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
#include"utility.h"

void Bullet::update() {

x += direction.x * speed;
y += direction.x * speed;
}

void Bullet::render(sf::RenderWindow& window) {
bullet.setPosition(sf::Vector2f{ x, y });
bullet.setPosition(sf::Vector2f{ (float)x, (float)y });
window.draw(bullet);
bullet.setFillColor(sf::Color{ 255,0,0 });
}
17 changes: 17 additions & 0 deletions Test game/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@
#include <cstdlib>
#include <string>
#include <vector>
#include"utility.h"

void Player::render(sf::RenderWindow& window) {
body.setPosition(sf::Vector2f{ x, y });
window.draw(body);

for (int i = 0; i < bullets.size(); ++i) {
bullets[i].render(window);
}
}

void Player::handleInput(sf::Event event) {
Expand Down Expand Up @@ -41,6 +46,13 @@ void Player::handleInput(sf::Event event) {
if (event.key.code == sf::Keyboard::D) {
rightKey = false;
}

if (event.type == sf::Event::MouseButtonPressed) {
sf::Vector2f diff{ event.mouseButton.x - x, event.mouseButton.y - y };
Bullet b;
b.direction = sf::normalize(diff);
bullets.push_back(b);
}
}
}

Expand All @@ -63,4 +75,9 @@ void Player::update() {

y += velY;
x += velX;

for (int i = 0; i < bullets.size(); ++i)
{
bullets[i].update();
}
}
3 changes: 3 additions & 0 deletions Test game/Player.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once

#include<SFML/graphics.hpp>
#include<vector>
#include"Bullet.h"


class Player {
Expand All @@ -21,6 +23,7 @@ class Player {
bool rightKey = false;
bool mouseDown = false;

std::vector<Bullet> bullets;

void render(sf::RenderWindow& window);
void handleInput(sf::Event event);
Expand Down

0 comments on commit f64d91f

Please sign in to comment.