Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Duplicating sprite  (Read 2021 times)

0 Members and 1 Guest are viewing this topic.

Tweezy

  • Newbie
  • *
  • Posts: 43
    • View Profile
Duplicating sprite
« on: March 05, 2013, 05:58:05 pm »
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:

Code: [Select]
#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

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Duplicating sprite
« Reply #1 on: March 05, 2013, 06:02:54 pm »
We're not here to write you your game. ;D

Also we won't go over your code and try to figure out what your actual question is. Please read this and start over again. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Tweezy

  • Newbie
  • *
  • Posts: 43
    • View Profile
Re: Duplicating sprite
« Reply #2 on: March 05, 2013, 06:06:28 pm »
I don't want you to do it for me. Perhaps point me at something to look at. Thanks
« Last Edit: March 05, 2013, 06:08:07 pm by Tweezy »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Duplicating sprite
« Reply #3 on: March 05, 2013, 06:10:55 pm »
Perhaps point me at something to look at
I already pointed you towards the Read Before Posting thread. ;)

I've no idea what you understand under 'duplicating sprites' and I've also no idea what you've already achieved, so you need to give some proper context.

'Duplication' doesn't just happen, you have to do it. E.g. if you want to draw 2 sprite, then draw 2 sprites. If you have 6 sprites and what 12 sprites to be displayed, then draw the other 6 sprites as well. There's not 'magic' sprite.duplicate("Hello PC I'm gonna tell you now how I want things to happen, or well maybe you can read my mind? Duplicate please!") function. ;D
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/