Hi. I have to develop a space invaders game for a university project. I've got some sprites that go across the screen which will act as the space invaders. I need to duplicate them though. At the moment there is only one row, a perfect amount would be 6. This is my code:
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>
#include <string>
int spriteWalkSpeed = 10;
int up=-spriteWalkSpeed, down=spriteWalkSpeed, left=-spriteWalkSpeed, right=spriteWalkSpeed;
int xVelocity =0, yVelocity=0;
const int SC_WIDTH=800;
const int SC_HEIGHT= 600;
const float REFRESH_RATE =0.01f; //how often we draw the frame in seconds
const double delay=0.1;
const int SPRITEROWS=1; //number of ROWS OF SPRITES
const int SPRITECOLS=2;//number of COLS OF SPRITES
std::string gameOver = "Game Over";
int main()
{
// Create the main window
sf::RenderWindow App(sf::VideoMode(SC_WIDTH, SC_HEIGHT, 32), "Space Invaders!");
// Create a clock for measuring time elapsed
sf::Clock Clock;
//background texture
sf::Texture backGround;
backGround.loadFromFile("images/background.jpg");
sf::Sprite back;
back.setTexture(backGround);
//load the spaceman images
sf::Texture invaders;
invaders.loadFromFile("images/invaders.png");
int invadersWidth=invaders.getSize().x;
int invadersHeight=invaders.getSize().y;
int spaceWidth=invadersWidth/SPRITECOLS;
int spaceheight=invadersHeight/SPRITEROWS;
sf::Sprite invadersSprite(invaders);
sf::Sprite invadersSprite2(invaders);
sf::IntRect area(0,0,spaceWidth,spaceheight);
invadersSprite.setTextureRect(area);
// Start game loop
while (App.isOpen())
{
// Process events
sf::Event Event;
while (App.pollEvent(Event))
{
// Close window : exit
if (Event.type == sf::Event::Closed)
App.close();
// Escape key : exit
if ((Event.type == sf::Event::KeyPressed) && (Event.key.code == sf::Keyboard::Escape))
App.close();
// Resize event : adjust viewport
//if (Event.type == sf::Event::Resized) glViewport(0, 0, Event.size.width, Event.size.height);
}
if(Clock.getElapsedTime().asSeconds()>REFRESH_RATE)
{
//carry out updating tasks
static float spriteTimer=0.0; //keep track of sprite time
spriteTimer+=Clock.getElapsedTime().asSeconds();
static int count=0; //keep track of where the sub rect is
if(spriteTimer>delay)
{
invadersSprite.setTextureRect(area);
++count;
invadersSprite.move(xVelocity, yVelocity);
if(count==SPRITECOLS) //WE HAVE MOVED OFF THE RIGHT OF THE IMAGE
{
area.left=0; //reset texture rect at left
count=0; //reset count
}
else
{
area.left+=spaceWidth; //move texture rect right
}
spriteTimer=0; //we have made one move in the sprite tile - start timing for the next move
}
if (invadersSprite.getPosition().x >= 770)// When it hits the right hand side of the screen it will move back down to the left
{
xVelocity = left;
invadersSprite.move(0,down);
}
else if (invadersSprite.getPosition().x <= 0 ) // When it hits the left hand side of the screen it will move up to the right.
{
invadersSprite.move(0,down);
xVelocity = right;
}
/*if (invadersSprite.getPosition().x >= SC_HEIGHT)
{
App.draw(std::string->gameOver);
}*/
Clock.restart();
}
App.draw(back);
App.draw(invadersSprite);
// Finally, display the rendered frame on screen
App.display();
}
return EXIT_SUCCESS;
}
Thanks