Hello!
I'm trying to create a simple western style shooting game with the help of this tutorial:
http://www.sfml-dev.org/tutorials/1.6/graphics-sprite.phpI have managed to make a game character that can be moved around.
However I haven't been able to implement the bullets or the ability to shoot.
I'm using Code::Blocks svn 8928 with a Debian Linux 2.6.32-5-amd64.
So far I have created a class called Projectile from which I create a bullet instance.
The problem is that I cant get this bullet (bulletInst) drawn to the screen.
Could someone please help me with this?
At below are the three files of the game, which are graphics-sprite.cpp, Projectile.h and Projectile.cpp
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> graphics-sprite.cpp >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#include <SFML/Graphics.hpp>
#include "Projectile.h"
int main()
{
// Create the main rendering window
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics");
// Create a sprite for the background
sf::Image BackgroundImage;
if (!BackgroundImage.LoadFromFile("images/background.jpg"))
return EXIT_FAILURE;
sf::Sprite Background(BackgroundImage);
// Load the sprite image from a file
sf::Image Image;
if (!Image.LoadFromFile("images/sprite.tga"))
return EXIT_FAILURE;
// Create the sprite
sf::Sprite Sprite(Image);
// *** Create a bullet instance from the Projectile class ***
Projectile bulletInst = Projectile(210.f, 110.f);
Sprite.Scale(0.3f, 0.3f);
Sprite.SetPosition(200.f, 100.f);
// Start game loop
while (App.IsOpened())
{
// Process events
sf::Event Event;
while (App.GetEvent(Event))
{
// Close window : exit
if (Event.Type == sf::Event::Closed)
App.Close();
}
// Get elapsed time
float ElapsedTime = App.GetFrameTime();
// Move the sprite
if (App.GetInput().IsKeyDown(sf::Key::Left)) Sprite.Move(-100 * ElapsedTime, 0);
if (App.GetInput().IsKeyDown(sf::Key::Right)) Sprite.Move( 100 * ElapsedTime, 0);
if (App.GetInput().IsKeyDown(sf::Key::Up)) Sprite.Move(0, -100 * ElapsedTime);
if (App.GetInput().IsKeyDown(sf::Key::Down)) Sprite.Move(0, 100 * ElapsedTime);
// Clear screen
App.Clear();
// Draw background
App.Draw(Background);
// Display sprite in our window
App.Draw(Sprite);
// *** Trying to display bullet but it does not show up! ***
App.Draw(bulletInst.bullet);
// Display window contents on screen
App.Display();
}
return EXIT_SUCCESS;
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Projectile.h >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#ifndef PROJECTILE_H
#define PROJECTILE_H
#include <SFML/Graphics.hpp>
class Projectile
{
public:
sf::Image bulletImage;
sf::Sprite bullet;
Projectile();
Projectile(float, float);
virtual ~Projectile();
void setPosition(float, float);
private:
float x_;
float y_;
};
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Projectile.cpp >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#include <iostream>
using std::cout;
using std::endl;
#include "Projectile.h"
#include <SFML/Graphics.hpp>
Projectile::Projectile()
{
cout << "The bullet is here" << endl;
}
Projectile::Projectile(float x, float y) : x_(x), y_(y)
{
if (!bulletImage.LoadFromFile("images/bullet.png"))
exit(0);
sf::Sprite bullet(bulletImage);
cout << "The bullet is here with a position (x, y): (" << x_ << ", " << y_ << ")"<< endl;
}
Projectile::~Projectile()
{
cout << "The bullet was deleted in a position (x, y): (" << x_ << ", " << y_ << ")"<< endl;
}
void Projectile::setPosition(float x, float y)
{
x_ = x;
y_ = y;
}
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< The end of code <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<