Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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.
3 changes: 3 additions & 0 deletions globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
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 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.
Binary file added images/pollen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/raindrop2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 6 additions & 1 deletion obstacle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ void Obstacle::update(){
}

void Obstacle::draw() {
draw_bitmap(box, x, y, option_to_screen());
double center_x = this->x + this->width/2;
double center_y = this->y + this->height/2;
point_2d box_position = point_at(center_x,center_y);
circle scaled_box_circle = bitmap_cell_circle(box, box_position,1);
draw_bitmap(box, x, y, option_to_screen());
draw_circle(COLOR_RED,scaled_box_circle);
}

void Obstacle::CollisionUpdate(bool is_collision) {
Expand Down
4 changes: 2 additions & 2 deletions player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ Player::Player(float x, float y, float speed) {
this->x = x;
this->y = y;
this->speed = speed;
this->width = bitmap_width(bee); // Assuming 'bee' is the bitmap for the player
this->height = bitmap_height(bee);
this->width = bitmap_width(bee)*BEE_SCALE; // Assuming 'bee' is the bitmap for the player
this->height = bitmap_height(bee)*BEE_SCALE;
}

void Player::move_right() {
Expand Down
144 changes: 112 additions & 32 deletions program.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
// 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>
//skm g++ program.cpp player.cpp obstacle.cpp -o game.exe
#include "bullet_factory.h"
#include <vector>
#include <cmath>
//skm g++ program.cpp player.cpp obstacle.cpp bullet_factory.cpp -o game.exe

bitmap background = bitmap_named("images/Background.jpg");
bitmap bee = bitmap_named("images/Bee.png");
bitmap box = bitmap_named("images/box.png");
float player_posx = 480.0f;
float player_posy = 550.0f;
int RIGHT_BOUNDARY = 1020;
bitmap box = bitmap_named("images/raindrop2.png");
bitmap bullet = bitmap_named("images/pollen.png");
float player_posx = 550.0f;
float player_posy = 650.0f;
int RIGHT_BOUNDARY = 1200;
int LEFT_BOUNDARY = 0;
int GRAVITY = 3;
int spawn_interval = 60;// Spawn obstacles at a rate of 1 per second
float BEE_SCALE = 0.6;

template <typename T, typename U>
bool is_colliding(T& obj1, U& obj2) {
Expand Down Expand Up @@ -53,53 +58,128 @@ void handle_collision(T& subject, U& observer) {
}
}

void player_move(Player* player){
if (key_down(RIGHT_KEY) && player->get_x() <= RIGHT_BOUNDARY) {
player->move_right();
}
if (key_down(LEFT_KEY) && player->get_x() >= LEFT_BOUNDARY) {
player->move_left();
}
}
void Spawn_obstacle(std::vector<Obstacle>* obstacles,Player* player,int& spawn_timer){
spawn_timer++;
if (spawn_timer >= spawn_interval)
{
spawn_timer = 0;
int spawn_x = rand() % RIGHT_BOUNDARY; // Random x-coordinate between 0 and RIGHT_BOUNDARY
Obstacle newObstacle(spawn_x, 0,2);
obstacles->push_back(newObstacle);
player->attach(&newObstacle); // Attach player observer to new obstacle
}
}

void render(std::vector<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);

// Update and draw obstacles
for (Obstacle& obstacle : obstacles) {
obstacle.update();
obstacle.draw();
handle_collision(player, obstacle);
}
}

int main()
{
open_window("BeeFall", 1280, 960); //named window beefall and window size
hide_mouse(); // hide mouse while cursor is over game window
Player player(480.0f, 550.0f, 10.0f); // initialize player
std::vector<Obstacle> obstacles; // list of obstacle
std::vector<sprite> bullets;

// Check if bullet bitmap is loaded correctly
if (!bitmap_valid(bullet)) {
std::cout << "Error: Could not load bullet bitmap" << std::endl;
return 1;
}

// Spawn obstacles at a rate of 1 per second
int spawn_interval = 60; // 60 frames per second
int spawn_timer = 0;

while (!quit_requested())
{
process_events();
clear_screen();

if (key_down(RIGHT_KEY) && player.get_x() <= RIGHT_BOUNDARY) {
player.move_right();
player_move(&player);
// Shoot bullets when spacebar is pressed
if (key_typed(SPACE_KEY)) {
point_2d origin = point_at(player.get_x()-20+player.get_width()/2, player.get_y()-20+player.get_height()/2); // Adjust based on player sprite size
vector_2d direction = vector_to(0, -1); // Shoot upwards
int bulletCount = 5;
float spreadAngle = 45 * (std::atan(1) * 4 / 180); // 45 degrees in radians

std::vector<sprite> newBullets = BulletFactory::SprayProjectiles(ProjectileType::NORMAL, origin, direction, bulletCount, spreadAngle);
bullets.insert(bullets.end(), newBullets.begin(), newBullets.end());

std::cout << "Created " << newBullets.size() << " bullets" << std::endl;
}
if (key_down(LEFT_KEY) && player.get_x() >= LEFT_BOUNDARY) {
player.move_left();
}


// Spawn obstacle at a random x-coordinate
spawn_timer++;
if (spawn_timer >= spawn_interval)
{
spawn_timer = 0;
int spawn_x = rand() % RIGHT_BOUNDARY; // Random x-coordinate between 0 and RIGHT_BOUNDARY
Obstacle newObstacle(spawn_x, 0,2);
obstacles.push_back(newObstacle);
player.attach(&newObstacle); // Attach player observer to new obstacle
}
Spawn_obstacle(&obstacles,&player,spawn_timer);

render(obstacles,player);

// Update and draw bullets
for (auto it = bullets.begin(); it != bullets.end();) {
update_sprite(*it);

// Remove bullets that are off-screen
if (sprite_y(*it) < -50 || sprite_y(*it) > 1010 || sprite_x(*it) < -50 || sprite_x(*it) > 1330) {
free_sprite(*it);
it = bullets.erase(it);
} else {
draw_sprite(*it);

// Debug: Draw a red circle around each bullet
// After updating the bullet sprite in your main loop
point_2d bullet_pos = sprite_position(*it);

//redrawing the bitmap after every clear background and bee
draw_bitmap(background, 0 , 0 , option_to_screen());
draw_bitmap(bee, player.get_x(), player.get_y(), option_to_screen());
// Get the dimensions of the bullet bitmap
float bullet_width = bitmap_width(bullet); // Get the bitmap width
float bullet_height = bitmap_height(bullet); // Get the bitmap height
double center_x = bullet_pos.x+bullet_width / 2;
double center_y = bullet_pos.y+bullet_height / 2;

// Update and draw obstacles
for (Obstacle& obstacle : obstacles) {
obstacle.update();
obstacle.draw();
handle_collision(player, obstacle);
// Draw the red circle at the center of the bullet sprite
fill_circle(COLOR_RED, center_x, center_y, 5);

// Get the circle that encompasses the scaled bitmap
point_2d bullet_position = point_at(center_x,center_y);
circle scaled_bullet_circle = bitmap_cell_circle(bullet, bullet_position,1);

// Draw the circle for debugging
draw_circle(COLOR_RED,scaled_bullet_circle);

++it;
}
}

// Debug: Display bullet count
//draw_text("Bullets: " + std::to_string(bullets.size()), COLOR_WHITE, 10, 10);


refresh_screen(60);
}
}