1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
#include <SFML/Graphics.hpp>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
int health=100,score=0,x=200,y=200,enemy;
void asteroid();
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600),"Game!!!");
sf::Texture ship;
if (!ship.loadFromFile("spaceship.jpg")){}
while(window.isOpen())
{
//guy
sf::Sprite guy;
guy.setTexture(ship);
guy.setPosition(x,y);
//guy
sf::Event event;
while(window.pollEvent(event))
{
//guy
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)&&x>0)
{
guy.setRotation(270);
x-=5;
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)&&x<380)
{
guy.setRotation(90);
x+=5;
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)&&y>0)
{
guy.setRotation(0);
y-=5;
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)&&y<280)
{
guy.setRotation(180);
y+=5;
}
guy.setOrigin(25,50);
guy.move(x,y);
//guy
enemy=rand()%100+1; //im having trouble with the asteroid/enemy
if(enemy==1){ //
asteroid(); //
} //
window.clear(sf::Color(80,80,80));
window.draw(guy);
window.display();
if(event.type==sf::Event::Closed){
window.close();
}
}
}
return 0;
}
void asteroid(){ // I want to generate enemies
sf::CircleShape smallasteroid(20);
smallasteroid.setFillColor(sf::Color(50,50,50));
int myx,myy;
srand(time(NULL));
int where=rand()%4+1;
srand(time(NULL));
if(where==1){
myx=0;
myy=rand()%280+1;
}
else if(where==2){
myx=380;
myy=rand()%280+1;
}
else if(where==3){
myy=0;
myx=rand()%380+1;
}
else if(where==4){
myy=280;
myx=rand()%380+1;
}
while(true){
srand(time(NULL));
myx+=rand()%2+(-2);
srand(time(NULL));
myy+=rand()%2+(-2);
}
}
|