#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
using namespace std;
// Initializing Dimensions.
// resolutionX and resolutionY determine the rendering resolution.
// Don't edit unless required. Use functions on lines 43, 44, 45 for resizing the game window.
const int resolutionX = 960;
const int resolutionY = 960;
const int boxPixelsX = 32;
const int boxPixelsY = 32;
const int gameRows = resolutionX / boxPixelsX; // Total rows on grid
const int gameColumns = resolutionY / boxPixelsY; // Total columns on grid
// Initializing GameGrid.
int gameGrid[gameRows][gameColumns] = {};
// The following exist purely for readability.
const int x = 0;
const int y = 1;
const int exists = 2; //bool exists;//
const int direction = 3;
/////////////////////////////////////////////////////////////////////////////
// //
// Write your functions declarations here. Some have been written for you. //
// //
/////////////////////////////////////////////////////////////////////////////
void drawPlayer(sf::RenderWindow& window, float player[], sf::Sprite& playerSprite);
void movePlayer(float player[],float bullet[],float shroom[][2], int maxShrooms);
void moveBullet(float bullet[], sf::Clock& bulletClock);
void drawBullet(sf::RenderWindow& window, float bullet[], sf::Sprite& bulletSprite);
void drawShrooms(sf::RenderWindow& window, float shroom[][2], sf::Sprite& shroomSprite,int maxShrooms);
void initializeShrooms(float shroom[][2],int maxShrooms);
void initialize_centipede(float centipede[][4],int totalSegments);
void drawCentipede(sf::RenderWindow& window, float centipede[12][4], sf::Sprite& centipedeSprite,const int totalSegments);
void move_centipede(float centipede[][4], sf::Clock& bulletClock, int maxShrooms,float shroom[][2]); //remove from sf::render.. ////,int maxShrooms,float shroom[][2] //
void bullet_shroom(float bullet[],float shroom[][2]);
void shroom_centipede_collision(float centipede[][4],float shroom[][2],int totalSegments,int maxShrooms);
void splitCentipede(float centipede[][4], int totalSegments, int segmentHit);
// void player_shroom_collision(float player[];float shroom[][2] );
int main()
{
srand(time(0));
/*
//centipede stuff:
const int totalSegments = 12;
float centipede[totalSegments][2]; // 2D array to store x and y positions of each segment
// Initialize centipede positions (for example, starting from the top left)
const int startX = 100; // Adjust as needed
const int startY = 100; // Adjust as needed
const int segmentGap = 20; // Gap between segments
for (int i = 0; i < totalSegments; ++i) {
centipede[i][0] = startX + i * segmentGap; // x position
centipede[i][1] = startY; // y position (same for all segments in this example)
}
*/
// Declaring RenderWindow.
sf::RenderWindow window(sf::VideoMode(resolutionX, resolutionY), "Centipede", sf::Style::Close | sf::Style::Titlebar);
// Used to resize your window if it's too big or too small. Use according to your needs.
window.setSize(sf::Vector2u(640, 640)); // Recommended for 1366x768 (768p) displays.
//window.setSize(sf::Vector2u(1280, 1280)); // Recommended for 2560x1440 (1440p) displays.
// window.setSize(sf::Vector2u(1920, 1920)); // Recommended for 3840x2160 (4k) displays.
// Used to position your window on every launch. Use according to your needs.
window.setPosition(sf::Vector2i(100, 0));
// Initializing Background Music.
sf::Music bgMusic;
bgMusic.openFromFile("Centipede_Skeleton/Music/field_of_hopes.ogg");
bgMusic.play();
bgMusic.setVolume(50);
// Initializing Background.
sf::Texture backgroundTexture;
sf::Sprite backgroundSprite;
backgroundTexture.loadFromFile("Centipede_Skeleton/Textures/background.png");
backgroundSprite.setTexture(backgroundTexture);
backgroundSprite.setColor(sf::Color(255, 255, 255, 200)); // Reduces Opacity to 25%
// Initializing Player and Player Sprites.
float player[2] = {};
player[x] = (gameColumns / 2) * boxPixelsX;
player[y] = (gameColumns * 3 / 4) * boxPixelsY;
sf::Texture playerTexture;
sf::Sprite playerSprite;
playerTexture.loadFromFile("Centipede_Skeleton/Textures/player.png");
playerSprite.setTexture(playerTexture);
playerSprite.setTextureRect(sf::IntRect(0, 0, boxPixelsX, boxPixelsY));
sf::Clock playerClock;
// Initializing Bullet and Bullet Sprites.
float bullet[3] = {};
//bool bullet1[3];
bool request = false;
bullet[x] = player[x];
bullet[y] = player[y] - boxPixelsY;
bullet[exists] = false;
sf::Clock bulletClock;
sf::Texture bulletTexture;
sf::Sprite bulletSprite;
bulletTexture.loadFromFile("Centipede_Skeleton/Textures/bullet.png");
bulletSprite.setTexture(bulletTexture);
bulletSprite.setTextureRect(sf::IntRect(0, 0, boxPixelsX, boxPixelsY));
//initializing centipede
const int totalSegments = 12;
float centipede[100][4];
int segmentStrike = -1;
//centipede[x] = (gameColumns / 2) * boxPixelsX; //the position from where centipede will start its journey x-co-ordinate//
//centipede[y] = (gameColumns * 3 / 4) * boxPixelsY; //the position from where centipede will start its journey y-co-ordinate//
//centipede[1][exists] = false;
for(int i=0;i<totalSegments;i++){
centipede[i][exists] = true;
}
sf::Texture centipedeTexture;
sf::Sprite centipedeSprite;
centipedeTexture.loadFromFile("Centipede_Skeleton/Textures/c_body_left_walk.png");
centipedeSprite.setTexture(centipedeTexture);
centipedeSprite.setTextureRect(sf::IntRect(0, 0, boxPixelsX, boxPixelsY));
sf::Clock centipedeClock;
initialize_centipede(centipede,totalSegments);
//initializing shrooms:
const int maxShrooms = 18;
float shroom[25][2] = {};
sf::Texture shroomTexture;
sf::Sprite shroomSprite;
shroomTexture.loadFromFile("Centipede_Skeleton/Textures/mushroom.png");
shroomSprite.setTexture(shroomTexture);
shroomSprite.setTextureRect(sf::IntRect(0, 0, boxPixelsX, boxPixelsY));
for(int i=0;i<maxShrooms;i++){
shroom[i][exists] = true;
}
initializeShrooms(shroom,maxShrooms); //calling shroom's function to initialize position;
int segmentHit = -1; // shroom_centipede_collision(centipede,shroom,totalSegments,maxShrooms);
while(window.isOpen()) {
///////////////////////////////////////////////////////////////
// //
// Call Your Functions Here. Some have been written for you. //
// Be vary of the order you call them, SFML draws in order. //
// //
///////////////////////////////////////////////////////////////
window.draw(backgroundSprite);
drawPlayer(window, player, playerSprite);
movePlayer(player,bullet,shroom,maxShrooms); //movePlayer(player,bullet);
/*shootBullet(bullet,request);
if(request){
bullet[exists] = true;
request = false;
} */
if (bullet[exists] == true) {
// Initialize segmentHit to -1 indicating no segment was hit initially
// ... (existing code for game loop)
if (bullet[exists]) {
moveBullet(bullet, bulletClock);
drawBullet(window, bullet, bulletSprite);
bullet_shroom(bullet,shroom);
// Check for collision with centipede segments
for (int j = 0; j < totalSegments; j++) {
// ... (existing code for collision detection)
// Check if bullet hits the centipede segment
if (bulletX >= centipede[j][x] - centipedeCollisionRange &&
bulletX <= centipede[j][x] + centipedeCollisionRange &&
bulletY >= centipede[j][y] - centipedeCollisionRange &&
bulletY <= centipede[j][y] + centipedeCollisionRange &&
centipede[j][exists]) {
// Store the index of the hit segment
segmentHit = j;
// Split the centipede at the hit segment
splitCentipede(centipede, totalSegments, segmentHit);
bullet[exists] = false;
break; // Exit the loop after handling collision with one segment
}
}
}
// ... (rest of your game loop)
}
centipede_bullet_collision(centipede,bullet,totalSegments);
drawShrooms(window,shroom,shroomSprite,maxShrooms);
drawCentipede(window, centipede, centipedeSprite,totalSegments);
move_centipede(centipede,centipedeClock,maxShrooms,shroom); //,maxShrooms,shroom//
shroom_centipede_collision(centipede,shroom,totalSegments,maxShrooms);
sf::Event e;
while (window.pollEvent(e)) {
if (e.type == sf::Event::Closed) {
return 0;
}
}
window.display();
window.clear();
}
}
////////////////////////////////////////////////////////////////////////////
// //
// Write your functions definitions here. Some have been written for you. //
// //
////////////////////////////////////////////////////////////////////////////
void drawPlayer(sf::RenderWindow& window, float player[], sf::Sprite& playerSprite) {
playerSprite.setPosition(player[x], player[y]);
window.draw(playerSprite);
}
void drawBullet(sf::RenderWindow& window, float bullet[], sf::Sprite& bulletSprite) {
if(bullet[exists] == true){
bulletSprite.setPosition(bullet[x], bullet[y]);
window.draw(bulletSprite);
}
}
void moveBullet(float bullet[], sf::Clock& bulletClock) {
float bullet_speed = 10.0f;
if (bulletClock.getElapsedTime().asMilliseconds() < 10)
return;
bulletClock.restart();
bullet[y] += -32;
if (bullet[y] < -32)
{ bullet[exists] = false; }
}
void drawShrooms(sf::RenderWindow& window, float shroom[][2], sf::Sprite& shroomSprite,int maxShrooms){
for(int i=0;i<maxShrooms;i++){
if(shroom[i][exists]){
shroomSprite.setPosition(shroom[i][x],shroom[i][y]);
window.draw(shroomSprite);
}
}
}
void initializeShrooms(float shroom[][2],int maxShrooms){
for(int i=0;i<maxShrooms;i++){
shroom[i][x] = rand()%gameRows * boxPixelsX;
shroom[i][y] = rand()%gameColumns * boxPixelsY;
// shroom[i][exists] = true; }
}
}
void movePlayer(float player[],float bullet[],float shroom[][2], int maxShrooms) {
float movementSpeed = 5.0f;
int bottomLimit = resolutionY - (6 * boxPixelsY); // Calculate the bottom limit
bool collisionUp = false;
bool collisionDown = false;
bool collisionLeft = false;
bool collisionRight = false;
// Check for collision with mushrooms in each direction
for (int i = 0; i < maxShrooms; i++) {
if (shroom[i][exists]) {
// Check collision with each mushroom
// Define collision range around the mushrooms
float collisionRange = 16.0f; // Adjust this value as needed
// Check collision in each direction
if (player[x] + boxPixelsX > shroom[i][x] - collisionRange &&
player[x] < shroom[i][x] + boxPixelsX + collisionRange &&
player[y] + boxPixelsY > shroom[i][y] - collisionRange &&
player[y] < shroom[i][y] + boxPixelsY + collisionRange) {
// Collision occurred, set collision flags based on direction
if (player[y] > shroom[i][y]) {
collisionUp = true;
}
if (player[y] < shroom[i][y]) {
collisionDown = true;
}
if (player[x] > shroom[i][x]) {
collisionLeft = true;
}
if (player[x] < shroom[i][x]) {
collisionRight = true;
}
}
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W) && player[y] > bottomLimit && !collisionUp) {
player[y] -= movementSpeed + 3;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S) && player[y] < resolutionY - boxPixelsY && !collisionDown) {
player[y] += movementSpeed + 3;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D) && player[x] < resolutionX - boxPixelsX && !collisionRight) {
player[x] += movementSpeed + 3;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A) && player[x] > 0 && !collisionLeft) {
player[x] -= movementSpeed + 3;
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space) && bullet[exists]==false){
bullet[exists] = true;
bullet[x] = player[x];
bullet[y] = player[y] - boxPixelsY;
}
}
void initialize_centipede(float centipede[][4],int totalSegments){
for(int j=0;j<totalSegments;j++){
centipede[j][x] = boxPixelsX*j;
centipede[j][y] = boxPixelsY;
centipede[j][exists] = true;
centipede[j][direction] = 1; //1 for right and 0 for left;
}
}
void drawCentipede(sf::RenderWindow& window, float centipede[12][4], sf::Sprite& centipedeSprite,const int totalSegments) {
const int segmentWidth = boxPixelsX; // Width of each centipede segment
const int segmentHeight = boxPixelsY; // Height of each centipede segment
for (int i = 0; i < totalSegments; ++i) {
if(centipede[i][exists]){
centipedeSprite.setPosition(centipede[i][x], centipede[i][y]);
window.draw(centipedeSprite);
}
}
}
void move_centipede(float centipede[][4], sf::Clock& centipedeClock, int maxShrooms,float shroom[][2] ) { //, int maxShrooms,float shroom[][2] //
int totalSegments = 12;
if (centipedeClock.getElapsedTime().asMilliseconds() < 100)
return;
centipedeClock.restart();
bool reachedBottomRight = true;
bool reachedBottomLeft = true;
shroom_centipede_collision(centipede,shroom,totalSegments,maxShrooms);
for (int j = 0;j < totalSegments ; j++) {
if (centipede[j][direction] == 1 ) { // Moving right ////
if (centipede[j][x] < 928) {
centipede[j][x] += 32;
if (centipede[j][y] != 928) {
reachedBottomRight = false;
reachedBottomLeft = false;
}
} else {
centipede[j][direction] = 0; // Change direction to down
centipede[j][y] += 32; // Move down a row
}
} else { // Moving left
if (centipede[j][x] > 0) {
centipede[j][x] -= 32;
if (centipede[j][y] != 928) {
reachedBottomRight = false;
reachedBottomLeft = false;
}
} else {
centipede[j][direction] = 1; // Change direction to down
centipede[j][y] += 32; // Move down a row
}
}
}
for (int j = 0; j < totalSegments; j++){
if (centipede[j][y] == 928 && centipede[j][x] == 928){
reachedBottomRight = true;}
else{reachedBottomRight = false;}
if(centipede[j][y] == 928 && centipede[j][x] < 0){
reachedBottomLeft = true; }
else{reachedBottomLeft = false;}
if (reachedBottomRight || reachedBottomLeft) {
// Move to the 6th row above the bottom
{
centipede[j][y] = 928 - (7 * boxPixelsY);
}
}
}
/*mushroom_centipede collsion:
initializeShrooms(shroom,maxShrooms);
for(int i=0,j=0;i<maxShrooms && j<totalSegments;i++,j++){
if(shroom[i][x] == centipede[j][x] && shroom[i][y] == centipede[j][y]){
centipede[j][y] += 32;}
*/
}
void bullet_shroom(float bullet[], float shroom[][2]) {
float bulletX = bullet[x];
float bulletY = bullet[y];
for (int i = 0; i < 18; i++) {
float shroomX = shroom[i][x];
float shroomY = shroom[i][y];
// Define a range around the mushroom position for collision detection
float collisionRange = 16.0f; // Adjust this value as needed
// Check if bullet position is within the range of a mushroom
if (bulletX >= shroomX - collisionRange && bulletX <= shroomX + collisionRange &&
bulletY >= shroomY - collisionRange && bulletY <= shroomY + collisionRange) {
shroom[i][exists] = false;
bullet[exists] = false;
// break; // Exit loop after handling collision with one mushroom//
}
/* if(shroom[i][exists] == false){
bullet[exists] == true; } */
}
}
void shroom_centipede_collision(float centipede[][4],float shroom[][2],int totalSegments,int maxShrooms){
//initialize_centipede(centipede,totalSegments);
//initializeShrooms(shroom,maxShrooms);
for(int i=0;i<totalSegments;i++){ //still requiares changes
for(int j=0;j<maxShrooms;j++){
if(centipede[i][x] == shroom[j][x] && centipede[i][y] == shroom[j][y] && centipede[i][y] != 928 && shroom[i][x] != 0){
centipede[i][y] += 32;
centipede[i][direction] = !centipede[i][direction];
centipede[i][x] -=32;
}
else if(centipede[i][x] == shroom[j][x] && centipede[i][y] == shroom[j][y] && centipede[i][y] == 928 ){
centipede[i][y] -= 32;
centipede[i][direction] = centipede[i][direction];
//centipede[i][x] +=32;
}
}
}
}
/*void player_shroom_collision(float player[];float shroom[][2] ){
for(int i=0,i<maxShrooms;i++){
if(player[x] == shroom[i][x] && player[y] == shroom[i][y] ){
player[x
} */
/*for(int i=0,j=0;i<maxShrooms && j<totalSegments;i++,j++){
if(centipede[j][x] == shroom[i][x] && centipede[j][y] == shroom[j][y] ){
centipede[j][y] += 32;
}
}
} */
void splitCentipede(float centipede[][4], int totalSegments, int segmentHit) {
// Split the centipede at the hit segment
for (int k = segmentHit; k < totalSegments - 1; k++) {
centipede[k][x] = centipede[k + 1][x];
centipede[k][y] = centipede[k + 1][y];
centipede[k][exists] = centipede[k + 1][exists];
centipede[k][direction] = centipede[k + 1][direction];
}
// Mark the last segment as not existing
centipede[totalSegments - 1][exists] = false;
}