#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[]);
void moveBullet(float bullet[], sf::Clock& bulletClock);
void drawBullet(sf::RenderWindow& window, float bullet[], sf::Sprite& bulletSprite);
void drawShrooms(sf::RenderWindow& window, int shroom[][2], sf::Sprite& shroomSprite,int maxShrooms);
void initializeShrooms(int shroom[][2],int maxShrooms);
void initialize_centipede(float centipede[12][4],int totalSegments);
void drawCentipede(sf::RenderWindow& window, float centipede[12][4], sf::Sprite& centipedeSprite,const int totalSegments);
void move_centipede(float centipede[12][4], sf::Clock& bulletClock);
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[totalSegments][4];
//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;
//initializing shrooms:
const int maxShrooms = 18;
int shroom[maxShrooms][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));
initializeShrooms(shroom,maxShrooms); //calling shroom's function to initialize position;
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);
/*shootBullet(bullet,request);
if(request){
bullet[exists] = true;
request = false;
} */
if (bullet[exists] == true) {
moveBullet(bullet, bulletClock);
drawBullet(window, bullet, bulletSprite);
}
drawShrooms(window,shroom,shroomSprite,maxShrooms);
//initialize_centipede(centipede,totalSegments);
drawCentipede(window, centipede, centipedeSprite,totalSegments);
move_centipede(centipede,centipedeClock);
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) {
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, int shroom[][2], sf::Sprite& shroomSprite,int maxShrooms){
for(int i=0;i<maxShrooms;i++){
shroomSprite.setPosition(shroom[i][x],shroom[i][y]);
window.draw(shroomSprite);
}
}
void initializeShrooms(int shroom[][2],int maxShrooms){
for(int i=0;i<maxShrooms;i++){
shroom[i][x] = rand()%gameRows * boxPixelsX;
shroom[i][y] = rand()%gameColumns * boxPixelsY;
}
}
void movePlayer(float player[],float bullet[]) {
float movementSpeed = 5.0f;
int bottomLimit = resolutionY - (6 * boxPixelsY); // Calculate the bottom limit
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W) && player[y] > bottomLimit) {
player[y] -= movementSpeed + 3;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S) && player[y] < resolutionY - boxPixelsY) {
player[y] += movementSpeed + 3;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D) && player[x] < resolutionX - boxPixelsX) {
player[x] += movementSpeed + 3;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A) && player[x] > 0) {
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[12][4],int totalSegments){
for(int i=0;i<gameRows;i++){
for(int j=0;j<totalSegments;j++){
centipede[j][x] = boxPixelsX*j;
centipede[i][y] = boxPixelsY*i;
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) {
centipedeSprite.setPosition(centipede[i][x], centipede[i][y]);
window.draw(centipedeSprite);
}
}
void move_centipede(float centipede[12][4], sf::Clock& centipedeClock){
int totalSegments = 12;
//initialize_centipede(centipede,12);
if (centipedeClock.getElapsedTime().asMilliseconds() < 10)
return;
centipedeClock.restart();
for(int j=0;j<gameRows;j++){
centipede[12][j] += 32;
for(int i=0;i<12;i++){
if(centipede[i][x]<900){
//tsudo x-resolution = 900
centipede[i][x] += 32;
}
else{
centipede[i][x] -= 32;
}
}
}
}
/*if (centipede[x] < -32)
bullet[exists] = false;
}
*/
/* direction 1 = right;
direction 0 = left; */
/* TO DO centipede sprite,centepede movement,mushrooms alignment i-e above a particulr row number,*/