So, I created this project for fun but had a question. Basically, the game involves a circle that cycles through the color spectrum, and you can use the arrow keys to "draw" with the circle. Essentially I just have no App.clear() unless the user presses a certain key.
However, when the stuff actually renders out, the trails that were left behind by the circle seem to glitch back and forth, instead of staying still as I would prefer. Is there any easy way to fix this, or will it require reworking the way I render the trails?
Here's the source for those interested. You may have an error with the instructions font if you decide to compile it, so make sure to remove the text bits from the code first.
Thanks in advance!
// sfml.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <SFML\Graphics.hpp>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
sf::RenderWindow window(sf::VideoMode(1600,900), "CubeDraw");
window.setFramerateLimit(300);
sf::CircleShape Circle(50);
sf::Font default;
default.loadFromFile("imagine_font.ttf");
sf::Text instructions;
instructions.setFont(default);
instructions.setCharacterSize(16);
Circle.setFillColor(sf::Color(255,255,255));
instructions.setString("C = Clear ,= Reduce Size /= Increase Size N = Rotate Left M = Rotate Right .= Reset Position");
instructions.setPosition(window.getSize().x /5,window.getSize().y -200);
bool phase1=false,phase2=false,phase3=false,phase4=false,phase5=false,phase6=false,phase7=false,phase8=false,XkeyPressed = false,YkeyPressed = false;
double acceleration = 5,velocityX = 0,velocityY = 0,max=20;
//RGB Spectrum, used later on
int r = 255;
int g = 0;
int b = 0;
bool a=Circle.getScale().x;
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
//Phases of color cycling. Probably more efficient ways of doing this, but not highest priority.
if(r == 255 && g == 0 && b == 0)
{
phase1 = true;
}
if(phase1 == true)
{
if(g<255)
{
g++;
}
if(g==255)
{
phase2 = true;
phase1 = false;
}
}
if(phase2 == true)
{
if(r>0)
{
r--;
}
if(g<255)
{
g++;
}
if(r==0 && g==255)
{
phase3 = true;
phase2 = false;
}
}
if(phase3 == true)
{
if(g>225)
{
g--;
}
if(b<255)
{
b++;
}
if(g==225 && b==255)
{
phase4 = true;
phase3 = false;
}
}
if(phase4 == true)
{
if(g>0)
{
g--;
}
if(g==0)
{
phase5 = true;
phase4 = false;
}
}
if(phase5 == true)
{
if(r<150)
{
r++;
}
if(r==150)
{
phase6 = true;
phase5 = false;
}
}
if(phase6 == true)
{
if(r<255)
{
r++;
}
if(b>175)
{
b--;
}
if(r==255 && b==175)
{
phase7 = true;
phase6 = false;
}
}
if(phase7 == true)
{
if(r<255)
{
r++;
}
if(b>0)
{
b--;
}
if(r==255 && b==0)
{
phase1 = true;
phase7 = false;
}
}
//Set color to whatever phase it is in the spectrum
Circle.setFillColor(sf::Color(r,g,b));
Circle.move(velocityX,velocityY);
//Output velocities to console for bug testing
cout << "X VEL " << velocityX << "Y VEL " << velocityY << endl;
system("cls");
//Physics stuff
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
YkeyPressed = true;
velocityY = velocityY-acceleration;
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
YkeyPressed = true;
velocityY = velocityY+acceleration;
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
XkeyPressed = true;
velocityX = velocityX-acceleration;
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
XkeyPressed = true;
velocityX = velocityX+acceleration;
}
if(!sf::Keyboard::isKeyPressed(sf::Keyboard::Up)&&!sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
YkeyPressed = false;
}
if(!sf::Keyboard::isKeyPressed(sf::Keyboard::Right)&&!sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
XkeyPressed = false;
}
//Setting max velocity
if (velocityX >= max)
{
velocityX = max;
}
if(velocityX <= -max)
{
velocityX = -max;
}
if(velocityY >= max)
{
velocityY = max;
}
if(velocityY <= -max)
{
velocityY = -max;
}
//While not pressing keys, slow down
if(XkeyPressed == false)
{
if(velocityX>0)
{
velocityX = velocityX - acceleration;
}
if(velocityX<0)
{
velocityX = velocityX + acceleration;
}
}
if(YkeyPressed == false)
{
if(velocityY>0)
{
velocityY = velocityY - acceleration;
}
if(velocityY<0)
{
velocityY = velocityY + acceleration;
}
}
//Keys to change game mechanics
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Slash))//Increase player size
{
sf::Vector2f scale = Circle.getScale();
Circle.setScale(scale.x*1.05,scale.y*1.05);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Comma))//Decrease player size
{
sf::Vector2f scale = Circle.getScale();
Circle.setScale(scale.x*0.95,scale.y*0.95);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Period))//Reset player position, rotation, and scale to default
{
Circle.setScale(1,1);
Circle.setPosition(0,0);
Circle.setRotation(0);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::C))//Clear screen
{
window.clear();
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::N))//Rotate player left
{
Circle.rotate(-5);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::M))//Rotate player right
{
Circle.rotate(5);
}
window.draw(Circle);
window.draw(instructions);
window.display();
}
return 0;
}