Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions bullet_factory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "bullet_factory.h"
#include "globals.h"
#include <cmath>
#include <iostream>

std::vector<sprite> BulletFactory::SprayProjectiles(ProjectileType type, point_2d origin, vector_2d direction, int count, float spread) {
std::vector<sprite> bullets;

float angleStep = spread / (count - 1);
float startAngle = -spread / 2;

for (int i = 0; i < count; i++) {
float angle = startAngle + i * angleStep;
vector_2d rotatedDirection = RotateVector(direction, angle);

sprite bullet = CreateBullet(type, origin, rotatedDirection);
if (bullet) {
bullets.push_back(bullet);
std::cout << "Created bullet at (" << sprite_x(bullet) << ", " << sprite_y(bullet) << ")" << std::endl;
}
}

return bullets;
}

sprite BulletFactory::CreateBullet(ProjectileType type, point_2d origin, vector_2d direction) {
if (!bitmap_valid(bullet)) {
std::cout << "Error: Could not load bullet bitmap in CreateBullet" << std::endl;
return nullptr;
}

sprite bullet_sprite = create_sprite(bullet);

// Scale the sprite
sprite_set_scale(bullet_sprite,0.2); // Scale by 50%

sprite_set_position(bullet_sprite, origin);

vector_2d velocity;
switch (type) {
case ProjectileType::NORMAL:
velocity = vector_multiply(direction, 5);
break;
case ProjectileType::FAST:
velocity = vector_multiply(direction, 10);
break;
case ProjectileType::EXPLOSIVE:
velocity = vector_multiply(direction, 3);
break;
}
sprite_set_velocity(bullet_sprite, velocity);

return bullet_sprite;
}

vector_2d BulletFactory::RotateVector(vector_2d vec, float angle) {
float cos_a = std::cos(angle);
float sin_a = std::sin(angle);
return vector_2d{
vec.x * cos_a - vec.y * sin_a,
vec.x * sin_a + vec.y * cos_a
};
}
22 changes: 22 additions & 0 deletions bullet_factory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef BULLET_FACTORY_H
#define BULLET_FACTORY_H

#include "splashkit.h"
#include <vector>

enum class ProjectileType {
NORMAL,
FAST,
EXPLOSIVE
};

class BulletFactory {
public:
static std::vector<sprite> SprayProjectiles(ProjectileType type, point_2d origin, vector_2d direction, int count, float spread);

private:
static sprite CreateBullet(ProjectileType type, point_2d origin, vector_2d direction);
static vector_2d RotateVector(vector_2d vec, float angle);
};

#endif // BULLET_FACTORY_H
Binary file modified game.exe
Binary file not shown.
4 changes: 3 additions & 1 deletion globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
extern bitmap background;
extern bitmap bee;
extern bitmap box;
extern bitmap bullet;
extern float player_posx;
extern float player_posy;
extern int RIGHT_BOUNDARY ;
extern int LEFT_BOUNDARY ;
extern int GRAVITY;
extern int spawn_interval;
extern int WINDOW_WIDTH ;
extern int WINDOW_HEIGHT ;
extern int spawn_interval;
extern float BEE_SCALE;
extern float BEE_SCALE;
Binary file added images/bullet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 18 additions & 31 deletions program.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
// import
#include "splashkit.h"
#include "globals.h" // <- added this import
//#include <graphics.h>
#include <cstdlib>
#include "player.h"
#include "obstacle.h"
#include "Observer.h"
#include "Subject.h"
#include <iostream>
#include <vector>
#include <memory>
//skm g++ program.cpp player.cpp obstacle.cpp -o game.exe
#include <cmath>

// Bitmap declarations
bitmap background = bitmap_named("images/Background.jpg");
bitmap bee = bitmap_named("images/Bee.png");
bitmap box = bitmap_named("images/box.png");
float player_posx = 550.0f;
float player_posy = 650.0f;
int RIGHT_BOUNDARY = 1200;
bitmap bullet = bitmap_named("images/bullet.png");

// Game settings
float player_posx = 480.0f;
float player_posy = 550.0f;
int RIGHT_BOUNDARY = 1020;
int LEFT_BOUNDARY = 0;
int GRAVITY = 3;
int spawn_interval = 60;// Spawn obstacles at a rate of 1 per second
int spawn_interval = 60; // Spawn obstacles at a rate of 1 per second
int WINDOW_WIDTH = 1280;
int WINDOW_HEIGHT = 960;
float BEE_SCALE = 0.6;
Expand All @@ -37,7 +41,7 @@ void display_start_screen();
void player_move(Player* player);
void Spawn_obstacle(std::vector<std::unique_ptr<Obstacle>>& obstacles, Player* player, int& spawn_timer);
void render(std::vector<std::unique_ptr<Obstacle>>& obstacles, Player& player);
void check_game_over(std::vector<std::unique_ptr<Obstacle>>& obstacles,Player& player);
void check_game_over(std::vector<std::unique_ptr<Obstacle>>& obstacles, Player& player);
void display_game_over_screen();

void start_game() {
Expand All @@ -60,15 +64,13 @@ void display_timer() {
}

void display_start_screen() {

draw_text("Press SPACE to Start", COLOR_BLACK, "Arial", 200, 550, 200);
}

void check_game_over(std::vector<std::unique_ptr<Obstacle>>& obstacles,Player& player) {
void check_game_over(std::vector<std::unique_ptr<Obstacle>>& obstacles, Player& player) {
if (Player::get_HP() == 1) {
player.notify_all_observers();
}
else if (Player::get_HP() <= 0) {
} else if (Player::get_HP() <= 0) {
game_over = true;
game_started = false; // Stop the game
obstacles.clear();
Expand Down Expand Up @@ -132,22 +134,11 @@ void Spawn_obstacle(std::vector<std::unique_ptr<Obstacle>>& obstacles, Player* p
}
}


void render(std::vector<std::unique_ptr<Obstacle>>& obstacles, Player& player) {
// Redrawing the bitmap after every clear background and bee
double center_x = player.get_x()+(player.get_width()/2);
double center_y = player.get_y()+(player.get_height()/2);
draw_bitmap(background, 0, 0, option_to_screen());
drawing_options scale_options = option_scale_bmp(BEE_SCALE+0.1, BEE_SCALE+0.1); // Scale to 70% of original size
draw_bitmap(bee, player.get_x()-50, player.get_y()-50,scale_options);

// Get the circle that encompasses the scaled bitmap

point_2d bee_position = point_at(center_x,center_y);
circle scaled_bee_circle = bitmap_cell_circle(bee, bee_position,BEE_SCALE);

// Draw the circle for debugging
draw_circle(COLOR_RED,scaled_bee_circle);
drawing_options scale_options = option_scale_bmp(BEE_SCALE + 0.1, BEE_SCALE + 0.1); // Scale to 70% of original size
draw_bitmap(bee, player.get_x(), player.get_y(), scale_options);

// Update and draw obstacles
for (const auto& obstacle_ptr : obstacles) {
Expand All @@ -159,7 +150,6 @@ void render(std::vector<std::unique_ptr<Obstacle>>& obstacles, Player& player) {
}
}


int main() {
open_window("BeeFall", WINDOW_WIDTH, WINDOW_HEIGHT); // Named window beefall and window size
hide_mouse(); // Hide mouse while cursor is over the game window
Expand All @@ -178,21 +168,18 @@ int main() {

if (!game_started) {
draw_bitmap(background, 0, 0, option_to_screen());
drawing_options scale_options = option_scale_bmp(BEE_SCALE+0.1, BEE_SCALE+0.1); // Scale to 40% of original size
draw_bitmap(bee, player.get_x()-50, player.get_y()-50,scale_options);
drawing_options scale_options = option_scale_bmp(BEE_SCALE + 0.1, BEE_SCALE + 0.1); // Scale to 40% of original size
draw_bitmap(bee, player.get_x() - 50, player.get_y() - 50, scale_options);
display_start_screen();
if (key_down(SPACE_KEY)) {

start_game();
}
refresh_screen(60);
continue; // Skip the rest of the loop until the game starts
}

if (game_over) {

display_game_over_screen();

if (key_down(SPACE_KEY)) {
start_game(); // Restart game
}
Expand All @@ -211,7 +198,7 @@ int main() {
// Update game elements
update_timer();
display_timer();
check_game_over(obstacles,player);
check_game_over(obstacles, player);

refresh_screen(60);
}
Expand Down