Quantcast

Jump to content






C++ struggles

Posted by Rainforce, 07 January 2017 · 1156 views

So... I have to make a game in C++ for school, but it's so freaking complicated. I like programming, but I don't feel motivated to do anything at the moment.
I want to make asteroids in c++, but sfml isn't listening to me, haha. I have to have this game finished by the 26th of this month... I probably won't make it :/
Edit: deadline has been moved to the 28th.

 

UPDATE

 

So I managed to write some code, I decided to make an arkanoid/breakout clone instead. I do think I need some help though. I had zero c++/sfml experience before writing this, so bear with me haha.
The code itself is working, but I still need to do some stuff and I wouldn't feel confident showing this in the interview.

 

TO DO LIST:
- Add a score system (+100 points when block is hit)
- Play explosion animation when said block is hit
- Make code more organized (with classes?)

 

Code:

 

//////////////////////////////////////////////////////
// Headers
//////////////////////////////////////////////////////
#include <SFML/Graphics.hpp>
#include <SFML\Audio.hpp>
#include <iostream>
#include <time.h>
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
int main()
{
srand(time(0));
//Create the window
sf::RenderWindow window(sf::VideoMode(640, 550), "Explosive Arkanoid");
window.setFramerateLimit(60);
//Load the sound and music
sf::SoundBuffer boom;
if (!boom.loadFromFile("sound/explosion.wav"))
{
std::cout << "SFML error" << std::endl;
}
sf::Sound sound;
sound.setBuffer(boom);
sound.setVolume(10);
sf::SoundBuffer backgroundMusic;
if (backgroundMusic.loadFromFile("sound/Lightning.flac"))
{
std::cout << "SFML error" << std::endl;
}
sf::Sound music;
music.setBuffer(backgroundMusic);
music.setVolume(20);
music.play();
//Load Textures/sprites
sf::Texture redBlock, orangeBlock, yellowBlock, greenBlock, blueBlock;
sf::Texture tBackground;
sf::Texture tBall;
sf::Texture tPaddle;
redBlock.loadFromFile("images/redBlock.png");
orangeBlock.loadFromFile("images/orangeBlock.png");
yellowBlock.loadFromFile("images/yellowBlock.png");
greenBlock.loadFromFile("images/greenBlock.png");
blueBlock.loadFromFile("images/blueBlock.png");
tBackground.loadFromFile("images/background.png");
tBall.loadFromFile("images/ball.png");
tPaddle.loadFromFile("images/paddle.png");
sf::Sprite sBackground(tBackground), sBall(tBall), sPaddle(tPaddle);
sPaddle.setPosition(295, 440);
sf::Sprite block[1000];
// Placing the 5 colored blocks
//Red block
int n = 0;
for (int column = 0; column <= 50; column++)
for (int rows = 1; rows <= 1; rows++)
{
block[n].setTexture(redBlock);
block[n].setPosition(column * 64, rows * 0);
n++;
}

 

//Orange block
int orange = 10;
for (int column = 0; column <= 10; column++)
for (int rows = 1; rows <= 1; rows++)
{
block[orange].setTexture(orangeBlock);
block[orange].setPosition(column * 64, rows * 32);
orange++;
}

 

//Yellow block
int yellow = 20;
for (int column = 0; column <= 10; column++)
for (int rows = 1; rows <= 1; rows++)
{
block[yellow].setTexture(yellowBlock);
block[yellow].setPosition(column * 64, rows * 64);
yellow++;
}

 

//Green block
int green = 30;
for (int column = 0; column <= 10; column++)
for (int rows = 1; rows <= 1; rows++)
{
block[green].setTexture(greenBlock);
block[green].setPosition(column * 64, rows * 96);
green++;
}

 

//Blue block
int blue = 40;
for (int column = 0; column <= 10; column++)
for (int rows = 1; rows <= 1; rows++)
{
block[blue].setTexture(blueBlock);
block[blue].setPosition(column * 64, rows * 128);
blue++;
}

 

// Initializing the ball direction + position
float positionX = 320, positionY = 275;
float directionX = 2, directionY = 3;
while (window.isOpen())
{
//Handle events
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) window.close();
}
if (event.type == sf::Event::Resized)
{
std::cout << "new width: " << event.size.width << std::endl;
std::cout << "new height: " << event.size.height << std::endl;
}

 

// Ball collision with the blocks
positionX += directionX;
for (int i = 0; i<n; i++)
if (sf::FloatRect(positionX + 3, positionY + 3, 6, 9).intersects(block[i].getGlobalBounds()))
{
block[i].setPosition(-100, 0); directionX = -directionX;
sound.play();
}
positionY += directionY;
for (int i = 0; i<n; i++)
if (sf::FloatRect(positionX + 3, positionY + 3, 6, 9).intersects(block[i].getGlobalBounds()))
{
block[i].setPosition(-100, 0); directionY = -directionY;
sound.play();
}

 

// When the ball should bounce
if (positionX < 0) directionX = -directionX;
if (positionX > 640) directionX = -directionX;
if (positionY < 0) directionY = -directionY;
if (positionY > 550) positionX = 300, positionY = 275;
// Controls paddle
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) sPaddle.move(10, 0);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) sPaddle.move(-10, 0);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) sPaddle.move(10, 0);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) sPaddle.move(-10, 0);
// Ball speed + collision with paddle
if (sf::FloatRect(positionX, positionY, 12, 12).intersects(sPaddle.getGlobalBounds())) directionY = -(rand() % 5 + 5);
// set the ball position
sBall.setPosition(positionX, positionY);
// Clear + draw on window
window.clear();
window.draw(sBackground);
window.draw(sBall);
window.draw(sPaddle);
for (int i = 0; i<n; i++)
window.draw(block[i]);
window.display();
}
return 0;
}





Take it slow, and try not to panic. If you can take a couple of days to go over what you need to do, plan it out, take care of the basic/important stuff first, then that'd be a great start (and hopefully that'll give you some confidence/motivation). 19 days is a lot of time! :)

  • Report

Thanks! I'm sure I'll succeed :)

  • Report

March 2024

S M T W T F S
     12
3456789
10111213141516
17181920212223
2425262728 29 30
31      

Recent Entries

Recent Comments

Latest Visitors