Quantcast

Jump to content






C++ update

Posted by Rainforce, 23 January 2017 · 400 views

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;
}





Can I ask how you learned all of this?

 

I too am trying to learn how to code without classes.

  • Report
Google, tutorials by Hilze Vonck and the book " Beginning c++ through game programming". I do want to learn about classes though and make this code more ehm, beautiful?
  • Report

December 2017

S M T W T F S
     12
3456789
10111213141516
17 18 1920212223
24252627282930
31      

Recent Entries

Recent Comments

Latest Visitors