I have been working on a simple C++ and SFML program recently, but have gotten stuck. Basically I want to do two things:
I want to generate a number of circles with random coordinates
I want to make those circles move randomly around the screen
I figured using a vector for the circles would be best so that I can use am int to determine the number of shapes.
The coordinates are generating correctly, but the shape is not drawing to the screen!
Here is my code:
#include "SFML/Graphics.hpp"
#include <stdlib.h>
#include <iostream>
int main()
{
srand(time(NULL));
const int WINDOW_X = 800;
const int WINDOW_Y = 480;
sf::RenderWindow window(sf::VideoMode(WINDOW_X, WINDOW_Y), "Epidemic Simulation", sf::Style::Close | sf::Style::Titlebar);
int numPeople = 2;
float size = 5;
int status = 0;
//Declare CircleShape
std::vector<sf::CircleShape> person(size);
//Change color depending on status
for (int i = 0; i < numPeople; i++)
{
if (status == 0)
person[i].setFillColor(sf::Color::Green);
else if (status == 1)
person[i].setFillColor(sf::Color::Red);
else if (status == 2)
person[i].setFillColor(sf::Color::Black);
else
{
std::cout << ("Error: Incorrect color value");
person[i].setFillColor(sf::Color::Green);
}
}
//Generate random coordinates for each circle
for (int i = 0; i < numPeople; i++)
{
int xPos = rand() % WINDOW_X - (size * 2);
int yPos = rand() % WINDOW_Y - (size * 2);
if (xPos < (size * 2))
xPos = 0;
if (yPos < (size * 2))
yPos = 0;
std::cout << ("X position: ") << xPos << (", Y position: ") << yPos << "\n";
}
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear(sf::Color::White);
//Draw circle
for (int i = 0; i < numPeople; i++)
window.draw(person[i]);
window.display();
}
return 0;
}
Any idea why the shape is not drawing? And how would I get the shape to move in a random direction?
It seems you just have to make a few changes.
The main ones (from the output of the diff command) are:
> // Give the circles definite radius value and segment count:
> for (int i = 0; i < numPeople; i++)
> {
> person[i].setRadius(size);
> person[i].setPointCount(100);
> }
and also:
// Generate random coordinates for each circle:
for (int i = 0; i < numPeople; i++)
{
int xPos = rand() % WINDOW_X - (size * 2);
int yPos = rand() % WINDOW_Y - (size * 2);
... <clip> ...
person[i].setPosition(xPos, yPos);
}
Side note:
The srand() and rand() functions are part of a legacy C API. They are not meant to be used in new C++ code. It is recommended to #include the <random> header and use the C++11 API. See paper for details: n3551.pdf.
Related
I am writing a bubble sort visualizer. But I can't seem to figure out how to swap the rectangles/bars.
#include<iostream>
#include<SFML/Graphics.hpp>
#include<cstdlib>
#include<ctime>
#include<chrono>
#include<thread>
int main()
{
srand(time(NULL));
sf::RenderWindow window(sf::VideoMode(798, 400), "Yoo", sf::Style::Default);
sf::RectangleShape *rec=new sf::RectangleShape[133];
for (int i = 0; i < 133; i++)
{
int num = rand() % 400;
rec[i].setFillColor(sf::Color::Red);
rec[i].setSize(sf::Vector2f(4.0f, static_cast<float>(num)));
rec[i].setPosition(sf::Vector2f(static_cast<float>(i * 6), 400-num));
}
sf::Event ev;
while (window.isOpen())
{
int i = rand() % 133;
int j = rand() % 133;
while (window.pollEvent(ev))
{
if (ev.type == sf::Event::Closed())
{
window.close();
}
}
window.clear(sf::Color::White);
rec[i].setFillColor(sf::Color::Blue);
rec[j].setFillColor(sf::Color::Green);
sf::Vector2f tmp = rec[i].getPosition();
sf::Vector2f size = rec[i].getSize();
rec[i].setPosition(rec[j].getPosition());
rec[i].setSize(rec[j].getSize());
rec[j].setSize(size);
rec[j].setPosition(tmp);
for (int i = 0; i < 133; i++)
{
window.draw(rec[i]);
}
rec[i].setFillColor(sf::Color::Red);
rec[j].setFillColor(sf::Color::Red);
window.display();
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
}
delete[] rec;
}
what is wrong with this code? I have tried normal swapping using std::swap to swap the rectangle array. It didn't work. So, I thought I should swap the position and size. It is not working either. But as shown in this picture, I can see the colors Blue and Green are interchanging correctly.
I tested your program. Your goal is to draw a green bar and a blue bar and swap them?
When you draw your rectangle, you swap them before displaying the window. So you don't see them swap. If you want, you have to draw your rectangles and display your window before swapping them.
window.clear(sf::Color::White);
rec[i].setFillColor(sf::Color::Blue);
rec[j].setFillColor(sf::Color::Green);
for (int i = 0; i < 133; i++)
{
window.draw(rec[i]);
}
window.display(); // display them before swap.
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
window.clear(sf::Color::White);
sf::Vector2f tmp = rec[i].getPosition();
sf::Vector2f size = rec[i].getSize();
rec[i].setPosition(rec[j].getPosition());
rec[i].setSize(rec[j].getSize());
rec[j].setSize(size);
rec[j].setPosition(tmp);
for (int i = 0; i < 133; i++)
{
window.draw(rec[i]);
}
rec[i].setFillColor(sf::Color::Red);
rec[j].setFillColor(sf::Color::Red);
window.display(); // display them again
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
something like this.
I'm trying to make a program that randomly generates a user-defined number of circles on the screen (using SFML), but when I choose a number of circles above 6, I get the Vector Subscript Out of Range error. Any number = to or below 6 works as expected.
I've tried looking up the error message, but none of the answers i've seen apply to my code. The code breaks at the first for loop, but if I comment out the first loop, it breaks at the second loop.
Here is my code:
#include "SFML/Graphics.hpp"
#include <stdlib.h>
#include <iostream>
int main()
{
srand(time(NULL));
const int WINDOW_X = 800;
const int WINDOW_Y = 480;
sf::RenderWindow window(sf::VideoMode(WINDOW_X, WINDOW_Y), "Epidemic Simulation", sf::Style::Close | sf::Style::Titlebar);
int numPeople = 10;
float size = 5;
int status = 0;
std::vector<sf::CircleShape> person(size);
for (int i = 0; i < numPeople; ++i)
{
if (status == 0)
person[i].setFillColor(sf::Color::Green);
else if (status == 1)
person[i].setFillColor(sf::Color::Red);
else if (status == 2)
person[i].setFillColor(sf::Color::Black);
else
{
std::cout << ("Error: Incorrect color value");
person[i].setFillColor(sf::Color::Green);
}
person[i].setFillColor(sf::Color::Green);
}
for (int i = 0; i < numPeople; ++i)
{
int xPos = rand() % WINDOW_X - (size * 2);
int yPos = rand() % WINDOW_Y - (size * 2);
if (xPos < (size * 2))
xPos = 0;
if (yPos < (size * 2))
yPos = 0;
std::cout << ("X position: ") << xPos << (", Y position: ") << yPos << "\n";
person[i].setPosition(xPos, yPos);
person[i].setRadius(size);
}
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear(sf::Color::White);
for (int i = 0; i < numPeople; ++i)
window.draw(person[i]);
window.display();
}
return 0;
}
Because you initiate person(size) with size = 5 and the loop is going up to numPeople = 10. You probably wanted to write:
std::vector<sf::CircleShape> person(numPeople);
I started Learning C++ yesterday And In that time i was rewriting my java "Falling Sand" Sandbox Game code in C++ using SFML (bit simplified since i don't know C++). but Performance in C++ was much worse in than java, what could be the reason for it, I Know this is very unfocused question, but my code is simple, i probably have a newbie mistakes which should be easy to correct.
#include <SFML/Graphics.hpp>
#include <iostream>
sf::Clock sclock;
const int WIDTH = 1440, HEIGHT = 960;
const char Blank = 0, Sand = 1, Water = 2;
const char* title = "Sandbox Simulation";
char map[WIDTH*HEIGHT];
sf::Vector2i mousePos;
int dist(int x1, int x2, int y1, int y2) {
return sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2));
}
int localBrushSize = 48;
short halfBrush = (short)floor(localBrushSize / 2);
char chosen = Sand;
void place() {
int randY = 0;
int randX = 0;
randX = randY = 1;
for (int y = mousePos.y - halfBrush; y <= mousePos.y + halfBrush; y += randY) {
for (int x = mousePos.x - halfBrush; x <= mousePos.x + halfBrush; x += randX) {
int I = x + y * WIDTH;
int distance = dist(mousePos.x, x, mousePos.y, y);
if (distance < halfBrush && I > 0) {
map[I] = chosen;
}
}
}
}
float Delta_Time() {
return sclock.restart().asSeconds();
}
int main() {
map[11111] = 2;
sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), title);
sf::Event evnt;
sf::RectangleShape pixel(sf::Vector2f(1.0f, 1.0f));
window.clear();
while (window.isOpen()) {
while (window.pollEvent(evnt)) {
switch (evnt.type) {
case sf::Event::Closed:
window.close();
break;
}
}
if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
mousePos = sf::Mouse::getPosition(window);
place();
}
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
int I = x + y * WIDTH;
switch (map[I]) {
case Sand:
pixel.setPosition(x, y);
pixel.setFillColor(sf::Color::Yellow);
window.draw(pixel);
break;
case Water:
pixel.setPosition(x, y);
pixel.setFillColor(sf::Color::Cyan);
window.draw(pixel);
break;
}
}
}
window.display();
}
return 0;
}
You might be able to make a cached / "framebuffer" like this
TOTALLY untested concept code. WIDTH/HEIGHT might be mixed up, endianess is not OK, etc.
sf::Image image;
sf:Image.create(WIDTH, HEIGHT, sf::Color(0, 0, 0));
sf::Sprite sprite;
std::array<sf::Uint8, WIDTH * HEIGHT * 4> pixels; // you can reuse this. The 4 is the size of RGBA
...
for(int y = 0; y < HEIGHT; y++) {
for(int x = 0; x < WIDTH; x++) {
int offset = (x + y * WIDTH) * 4;
pixels[offset] = sf::Color::Yellow.toInteger(); // in case BIG/Litte endian confusion you might have to do the below.
//pixels[offset + 0 ] = 255; // R?
//pixels[offset + 1 ] = 255; // G?
//pixels[offset + 2 ] = 255; // B?
//pixels[offset + 3 ] = 255; // A?
}
}
image.LoadFromPixels(WIDTH, HEIGHT, pixels);
sprite.SetImage(image);
window.Draw(sprite);
window.Display();
so recently I have been wanting to make a snake game in C++ using SFML in Microsoft Visual studio 2015 and I made one and I am actually pretty satisfied with my work but there is a problem, that I forgot to make a game over for it and it seems like I couldn't make it work and it really had me on edge. So I thought I could use stack overflow's help. I would really appreciate it if you guys would let me know how to make it work and please keep it simple obvious.
Here is my code:
// GraphicalLoopSnakeGame.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <ctime>
#include <SFML/Graphics.hpp>
using namespace sf;
int N = 30, M = 20;
int size = 16;
int w = size * N;
int h = size * M;
int dir, num = 4;
struct Snake {
int x, y;
} s[100];
struct Fruit {
int x, y;
} f;
void Tick() {
for(int i = num; i > 0; --i) {
s[i].x = s[i - 1].x;
s[i].y = s[i - 1].y;
}
if(dir == 0) s[0].y += 1;
if(dir == 1) s[0].x -= 1;
if(dir == 2) s[0].x += 1;
if(dir == 3) s[0].y -= 1;
if((s[0].x == f.x) && (s[0].y == f.y)) {
num++;
f.x = rand() % N;
f.y = rand() % M;
}
if(s[0].x > N) s[0].x = 0;
if(s[0].x < 0) s[0].x = N;
if(s[0].y > M) s[0].y = 0;
if(s[0].y < 0) s[0].y = M;
for(int i = 1; i < num; i++)
if(s[0].x == s[i].x && s[0].y == s[i].y) num = i;
}
int main() {
srand(time(0));
RenderWindow window(VideoMode(w, h), "Snake Game!");
Texture t1, t2, t3;
t1.loadFromFile("images/white.png");
t2.loadFromFile("images/red.png");
t3.loadFromFile("images/green.png");
Sprite sprite1(t1);
Sprite sprite2(t2);
Sprite sprite3(t3);
Clock clock;
float timer = 0, delay = 0.13;
f.x = 10;
f.y = 10;
while(window.isOpen()) {
float time = clock.getElapsedTime().asSeconds();
clock.restart();
timer += time;
Event e;
while(window.pollEvent(e)) {
if(e.type == Event::Closed) window.close();
}
if(Keyboard::isKeyPressed(Keyboard::Left)) dir = 1;
if(Keyboard::isKeyPressed(Keyboard::Right)) dir = 2;
if(Keyboard::isKeyPressed(Keyboard::Up)) dir = 3;
if(Keyboard::isKeyPressed(Keyboard::Down)) dir = 0;
if(Keyboard::isKeyPressed(Keyboard::W)) dir = 3;
if(Keyboard::isKeyPressed(Keyboard::D)) dir = 2;
if(Keyboard::isKeyPressed(Keyboard::A)) dir = 1;
if(Keyboard::isKeyPressed(Keyboard::S)) dir = 0;
if(timer > delay) {
timer = 0;
Tick();
}
////// draw ///////
window.clear();
for(int i = 0; i < N; i++)
for(int j = 0; j < M; j++) {
sprite1.setPosition(i * size, j * size);
window.draw(sprite1);
}
for(int i = 0; i < num; i++) {
sprite2.setPosition(s[i].x * size, s[i].y * size);
window.draw(sprite2);
}
sprite3.setPosition(f.x * size, f.y * size);
window.draw(sprite3);
window.display();
}
return 0;
}
In your Tick() function you can check whether the head bumps into anything after everything has moved in the given direction. If it does, let main() know about it somehow: for example, return a bool which expresses if the game is over. Let's say this bool is called over.
So, add if (over) { window.close(); } inside your while (window.isOpen()) loop (right after calling Tick()) to let main() reach return 0; and finish the program.
EDIT: Think about using std::deque for moving your snake using less code and time: you'd be able to just pop_back() the snake tile farthest from the head and push_front() the new tile where the head currently is (after a tick), simulating crawling one step forwards.
Anyway, after having moved your snake you can check each of its body tiles whether it has the same coordinates as its head. If it does, it means your snake crashed into its tail so the game is over.
// in Tick():
// ...other logic...
tiles.pop_back();
tiles.push_front(new_head_position);
for (/* each tile of your snake except its head */) {
if (tile.x == head.x && tile.y == head.y) {
return false; // game over
}
}
return true; // everything is fine
Everytime I run the code it says
Vector subscript OUT of range.
I've looked at other SFML vector Questions on "stackoverflow" and none of them have answer of my questions or worked. What I'm trying to do is:
vector<sf::Sprite> snake_part;
sf::Sprite sprite;
for (int i = 0; i <= 10; --i) {
snake_part.push_back(sprite);
cout << "Vector size : " << snake_part.size() << endl;
snake_part[i].setTexture(Snake_Part_Texture);
if (i > 0) {
x1[i] -= 10;
}
}
This is the Part of the code that doesn't work.
OUTPUT:
Vector size : 1
Vector size : 2
It's supposed to count to "10", but stops at "2" for some reason and Comes with the "Vector subscript out of range" error as well.
Full Code:
#include <SFML/Graphics.hpp>
#include <iostream>
#include <vector>
using namespace std;
int main() {
//=========================================================
// Coordinates
float y = 330; float x = 150;
float x1[10]; x1[0] = x;
float y1[10]; y1[0] = y;
// Texture
sf::Texture floorText;
if (!floorText.loadFromFile("Floor_Snake_SFML.png")) {
cout << "Floor_Snake_SFML.COULD_NOT_LOAD: [Error]" << endl;
}
sf::Texture Snake_Part_Texture;
if (!Snake_Part_Texture.loadFromFile("Snake_Part_Sprite.png")) {
cout << "Snake_Part_Texture.COULD_NOT_LOAD: [Error]" << endl;
}
// Sprite_floor
sf::Sprite floor;
floor.setTexture(floorText);
floor.setPosition(0, 320);
// vector <sprite> -- (snake)
vector<sf::Sprite> snake_part;
sf::Sprite sprite;
for (int i = 0; i <= 10; --i) {
snake_part.push_back(sprite);
cout << "Vector size : " << snake_part.size() << endl;
snake_part[i].setTexture(Snake_Part_Texture);
if (i > 0) {
x1[i] -= 10;
}
}
//=========================================================
sf::RenderWindow window(sf::VideoMode(640, 480), "<snake>");
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
switch (event.type) {
// Close Window
case sf::Event::Closed:
window.close();
break;
// Do [x] if KeyPressed-
case sf::Event::KeyPressed:
switch (event.key.code) {
// [A] -pressed-
case sf::Keyboard::A:
for (int j = 1; j <= 10; j++) {
x1[j] = x1[j - 1]; y1[j] = y1[j - 1];
}
x1[0] -= 10;
for (int k = 1; k <= 10; k++) {
snake_part[k].setPosition(x1[k], y1[k]);
}
break;
// [D] -pressed-
case sf::Keyboard::D:
break;
// [Space] -pressed-
case sf::Keyboard::Space:
break;
}
break;
}
}
window.clear(); // ===============================
window.draw(floor);
window.draw(snake_part[0]);
window.display(); // ===============================
}
return 0;
}
If you want a more detailed Error and synopsis of the Program, just comment, Also thanks in Advance, and if anyone know a better way instead of Vector to create 10 Sprites without having to do this 10 times:
sf::Sprite sprite;
sprite.setTexture(texture);
sprite.setTextureRect(sf::IntRect(0, 0, 10, 10));
float x = 320, y = 240;
sprite.setPosition(x, y);
then I would be REALLY grateful.
-Thanks in advance.
The error more than likely comes from this line:
for (int i = 0; i <= 10; --i)
I don't know what you meant to do with that line, but more than likely it was supposed to be ++i instead of --i.