I'm using sfml to draw the images.
If I try to draw 10 circles on random positions on the grid:
for (int i{ 0 }; i < 10; ++i)
{
grid[getRandomNumber(grid.size())] = -1;
}
//later
else if (grid[currentIndex] == -1)
{
Food food{ sf::Vector2f(x * PIXEL_WIDTH, y * PIXEL_WIDTH) }; // Draws circle
window.draw(food.getFood());
}
I get only less than what is expected. It is the same for whenever I change the number of circles I want in the for loop.
I draw the grid like so:
//Food.cpp
#include "Food.h"
Food::Food(sf::Vector2f pos)
{
m_food.setPosition(pos);
m_food.setFillColor(sf::Color::Red);
m_food.setRadius(800 / 64);
}
sf::CircleShape Food::getFood() { return m_food; }
// Main.cpp
int main()
{
sf::RenderWindow window{ sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "Snake" };
sf::Event event{};
std::vector<int> grid(PIXEL_WIDTH * PIXEL_HEIGHT);
Snake snake{ sf::Vector2f(PIXEL_WIDTH, PIXEL_HEIGHT) };
for (int i{ 0 }; i < 10; ++i)
{
grid[getRandomNumber(grid.size())] = -1;
}
while (window.isOpen())
{
while (window.pollEvent(event))
{
switch (event.type)
{
case sf::Event::Closed:
window.close();
}
}
window.clear(sf::Color::Black);
sf::VertexArray tile;
tile.setPrimitiveType(sf::LinesStrip);
tile.resize(PIXEL_WIDTH * PIXEL_HEIGHT * 4);
for (int x{ 0 }; x < (SCREEN_WIDTH / PIXEL_WIDTH); ++x)
{
for (int y{ 0 }; y < (SCREEN_HEIGHT / PIXEL_HEIGHT); ++y)
{
int currentIndex{ x * PIXEL_WIDTH + y };
if (grid[currentIndex] == 0)
{
sf::Vertex* quad = &tile[(currentIndex) * 4];
quad[0].position = sf::Vector2f(x * PIXEL_WIDTH, y * PIXEL_HEIGHT);
quad[1].position = sf::Vector2f((x + 1) * PIXEL_WIDTH, y * PIXEL_HEIGHT);
quad[2].position = sf::Vector2f((x + 1) * PIXEL_WIDTH, (y + 1) * PIXEL_HEIGHT);
quad[3].position = sf::Vector2f(x * PIXEL_WIDTH, (y + 1) * PIXEL_HEIGHT);
window.draw(quad, 4, tile.getPrimitiveType());
}
else if (grid[currentIndex] == -1)
{
Food food{ sf::Vector2f(x * PIXEL_WIDTH, y * PIXEL_WIDTH) };
window.draw(food.getFood());
}
}
}
window.display();
}
return 0;
}
I'm not sure as to why it does not produce 10 circles on the grid. I already set the values of those positions to -1 then I look for any values with -1 and draw them in their respective position.
Aucun commentaire:
Enregistrer un commentaire