Unlimited generator

Pages: 12
I want to make a program in SFML to make a spaceship fly around and dodge asteroids.

This is my program so far.

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);
    }
}


How can I draw each one?
Last edited on
You can put the asteroids (CircleShapes) in a vector and iterate through it to draw them.
Also, it looks like you are calling clear, draw and display in your event loop, they should be in the main loop.
How do you do that?
I'm not exactly sure how to use a vector in this.
Hello? Anyone, please help.
You should look up the vector class. but anyways...
1
2
3
4
5
6
7
8
9
std::vector<sf::CircleShape> asteroids;

//add some asteroids
asteroids.push_back(someAsteroid);

for(auto const &ast : asteroids)
{
    window.draw(ast);
}

http://www.cplusplus.com/reference/vector/vector/
I get the idea, but can you do you do it in detail inside my previous code? Thanks.
I can but, it would be better if you practiced. Anyways what's up with getting all those seeds in your asteroid function? Also the loop on line 95 will never end. I also am very confused on what you are trying to do. It looks like you are trying to make an asteroid at an arbitrary location but then I don't get some of the other arbitrary information.
Sorry, I'm just not clear about the vector stuff right now. I can change other things later.
I would suggest you play around with vectors before just copy/pasting the code in there :P

http://www.cplusplus.com/reference/vector/vector/

You can use it the same as a normal array plus some.
I know how to use vectors, but not with those kinds of data types.
A vector is templated so it shouldn't matter what type of data if you know how to use a vector.
What do I put in the some asteroid part?
I got lots of errors.
What do I put in the some asteroid part?


This would be the sf::CircleShape you created.
This?
asteroids.push_back(sf::CircleShape);
The name of the sf::CircleShape you created was called smallasteroid. So you would push this into the std::vector. Then you can iterate through these to update/render them.
Sorry, I still don't get it.
I think you should read up on objects vs types. You are trying to push a type to the container instead of pushing the actual object.
http://www.cplusplus.com/doc/tutorial/variables/

int a; type = int object = a

Basically what you would do is something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void addAsteroid(std::vector<sf::CircleShape> &asteroids,
int const width, int const height)
{
    sf::CircleShape asteroid(10);
    asteroid.setPosition(rand() % width, rand() % height);
    asteroid.setFillColor(sf::Color::Grey);

    asteroids.push_back(asteroid);
}


...

for(sf::CircleShape const &asteroid : asteroids)
{
    window.draw(asteroids);
}
Last edited on
Oh, thanks. So what do I put into the asteroids.push_back();? asteroids.push_back(int);?asteroids.push_back(POINT);?
Pages: 12