1
Graphics / Window clear? Trying to shoot a bullet
« on: July 06, 2014, 07:29:10 pm »
First I am new to SFML and C++ I used to program awhile ago so I am learning as is everybody and I know my code is not really great or formatted great so take it easy on me. My problem is this when I shoot my bullet instead of clearing for each new position of the image it just stretches across the screen and slows down my program while left button is clicked. I want it to do what a bullet should do just shoot across the screen. Maybe it's my programing or maybe I am missing something in SFML Thanks!!
#include <SFML/Graphics.hpp>
#include <iostream>
int windowX = 800;
int windowY = 600;
float x = 0;
float y = 0;
sf::RenderWindow window(sf::VideoMode(windowX, windowY), "My window");
int controller(){
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
// left key is pressed: move our character
if (x <= 0){
}else{
x = x-.2;
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
// left key is pressed: move our character
if (x >= windowX -50){
}else{
x = x+.2;
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
// left key is pressed: move our character
if (y >= windowY -40){
}else{
y = y+.2;
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
// left key is pressed: move our character
if (y <= 0){
}else{
y = y-.2;
}
}
return 0;
}
int main()
{
// create the window
// run the program as long as the window is open
while (window.isOpen())
{
// check all the window's events that were triggered since the last iteration of the loop
sf::Event event;
while (window.pollEvent(event))
{
// "close requested" event: we close the window
if (event.type == sf::Event::Closed)
window.close();
}
// clear the window with black color
window.clear(sf::Color::Blue);
sf::Texture texture;
sf::Sprite sprite;
sprite.setPosition (x,y);
if (!texture.loadFromFile("image.png"))
{
// error...
}
controller();
bool drawbullet = false;
float bulletx = 49;
sf::Texture bullet1;
sf::Sprite spritebullet1;
if (!bullet1.loadFromFile("bullet.png"))
{
}
if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
drawbullet = true;
}
if (drawbullet == true){
for (float i = 0;i <= windowX; i = i + 1){
spritebullet1.setTexture(bullet1);
spritebullet1.setPosition (i,0);
window.draw(spritebullet1);
}
}
sprite.setTexture(texture);
window.draw(sprite);
// end the current frame
window.display();
}
return 0;
}
#include <iostream>
int windowX = 800;
int windowY = 600;
float x = 0;
float y = 0;
sf::RenderWindow window(sf::VideoMode(windowX, windowY), "My window");
int controller(){
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
// left key is pressed: move our character
if (x <= 0){
}else{
x = x-.2;
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
// left key is pressed: move our character
if (x >= windowX -50){
}else{
x = x+.2;
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
// left key is pressed: move our character
if (y >= windowY -40){
}else{
y = y+.2;
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
// left key is pressed: move our character
if (y <= 0){
}else{
y = y-.2;
}
}
return 0;
}
int main()
{
// create the window
// run the program as long as the window is open
while (window.isOpen())
{
// check all the window's events that were triggered since the last iteration of the loop
sf::Event event;
while (window.pollEvent(event))
{
// "close requested" event: we close the window
if (event.type == sf::Event::Closed)
window.close();
}
// clear the window with black color
window.clear(sf::Color::Blue);
sf::Texture texture;
sf::Sprite sprite;
sprite.setPosition (x,y);
if (!texture.loadFromFile("image.png"))
{
// error...
}
controller();
bool drawbullet = false;
float bulletx = 49;
sf::Texture bullet1;
sf::Sprite spritebullet1;
if (!bullet1.loadFromFile("bullet.png"))
{
}
if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
drawbullet = true;
}
if (drawbullet == true){
for (float i = 0;i <= windowX; i = i + 1){
spritebullet1.setTexture(bullet1);
spritebullet1.setPosition (i,0);
window.draw(spritebullet1);
}
}
sprite.setTexture(texture);
window.draw(sprite);
// end the current frame
window.display();
}
return 0;
}