Created
May 24, 2013 05:40
-
-
Save krisyr686/cd071447d9fb3750951a to your computer and use it in GitHub Desktop.
This is a very rough work in progress as I try to learn how to make a game.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SFML/Graphics.hpp> | |
#include <iostream> | |
#include <SFML/Audio.hpp> | |
int main() | |
{ | |
std::cout << "What is up?!\n"; | |
////CREATION | |
//render window | |
sf::RenderWindow window(sf::VideoMode(1000,800), "Little Bitty Game!"); | |
sf::View view; | |
view.reset(sf::FloatRect(0, 0, 1000, 800)); //(up moves left, up moves right) | |
window.setFramerateLimit(30); | |
window.setKeyRepeatEnabled(false); | |
//view | |
bool play = true; | |
sf::Event event; | |
//Textures | |
sf::Texture rstandtex; | |
if(rstandtex.loadFromFile("standing.png") == 0) | |
{return 1;} | |
sf::Texture lstandtex; | |
if(lstandtex.loadFromFile("standingleft.png") == 0) | |
{return 1;} | |
sf::Texture lwalktex; | |
if(lwalktex.loadFromFile("walkingleft.png") == 0) | |
{return 1;} | |
sf::Texture lwalktex2; | |
if(lwalktex2.loadFromFile("walkingleft2.png") == 0) | |
{return 1;} | |
sf::Texture ljumptex; | |
if(ljumptex.loadFromFile("walkingleft3.png") == 0) | |
{return 1;} | |
sf::Texture rwalktex; | |
if(rwalktex.loadFromFile("walking.png") == 0) | |
{return 1;} | |
sf::Texture rwalktex2; | |
if(rwalktex2.loadFromFile("walking2.png") == 0) | |
{return 1;} | |
sf::Texture rjumptex; | |
if(rjumptex.loadFromFile("walking3.png") == 0) | |
{return 1;} | |
sf::Texture rducktex; | |
if(rducktex.loadFromFile("duck.png") == 0) | |
{return 1;} | |
sf::Texture tex_background; | |
if(tex_background.loadFromFile("bg.png") == 0) | |
{return 1;} | |
sf::Texture flooring; | |
if(flooring.loadFromFile("brick.png") == 0) | |
{return 1;} | |
//Sounds | |
sf::SoundBuffer bouncing; | |
if(bouncing.loadFromFile("jump8.wav") == 0) | |
{return 1;} | |
sf::Sound bounce; | |
bounce.setBuffer(bouncing); | |
//Font | |
/*sf::Font font; | |
if(font.loadFromFile("Data/arial.ttf") == 0) | |
{ | |
return 1; | |
}*/ | |
//States | |
//Variables | |
bool lwalk= false; | |
bool rwalk = false; | |
bool rstand = false; | |
bool lstand = false; | |
bool rjump = false; | |
bool ljump = false; | |
bool rduck = false; | |
bool lduck = false; | |
bool jump = false; | |
bool fall = false; | |
float xVelocityShape1 = 0; | |
float yVelocityShape1 = 0; | |
float gravity = 1.75; | |
float max_velocity = 20; | |
//Shape | |
sf::RectangleShape shape1; | |
shape1.setSize(sf::Vector2f(100,100)); | |
shape1.setPosition(400, 650); | |
shape1.setTexture(&rstandtex); | |
float currentPosition = shape1.getPosition().y; | |
sf::RectangleShape ground; | |
ground.setSize(sf::Vector2f(10000,100)); | |
ground.setPosition(0,750); | |
ground.setTexture(&flooring); | |
sf::RectangleShape background; | |
background.setSize(sf::Vector2f(10000,8000)); | |
background.setPosition(0,0); | |
background.setTexture(&tex_background); | |
sf::RectangleShape blocks; | |
blocks.setSize(sf::Vector2f(200,100)); | |
blocks.setPosition(500,500); | |
blocks.setFillColor(sf::Color::Blue); | |
//Clock | |
sf::Clock deltaClock; | |
///****//// | |
//GAME LOOP | |
while(play == true) | |
{ | |
//EVENTS (ex. check if key has been pressed) | |
while(window.pollEvent(event)) //checking events that happened | |
{ | |
if(event.type == sf::Event::Closed) | |
{ | |
play = false; | |
} | |
if(event.type == sf::Event::KeyPressed) | |
{ | |
if(event.key.code == sf::Keyboard::Right) {rwalk = true;} | |
if(event.key.code == sf::Keyboard::Left) {lwalk = true;} | |
if(event.key.code == sf::Keyboard::Down) {rduck = true;} | |
if(event.key.code == sf::Keyboard::Up && !jump) {jump = true; bounce.play();} | |
} | |
if(event.type == sf::Event::KeyReleased) | |
{ | |
if(event.key.code == sf::Keyboard::Right) {rwalk = false;} | |
if(event.key.code == sf::Keyboard::Left) {lwalk = false;} | |
if(event.key.code == sf::Keyboard::Down) {rduck = false;} | |
if(event.key.code == sf::Keyboard::Up) {fall = true;} | |
} | |
} | |
////****//// | |
//LOGIC (ex. if left key was pressed, move player left) | |
//WALKRIGHT// | |
if(rwalk == true) | |
{ | |
xVelocityShape1 = 5; | |
int position = shape1.getPosition().x; | |
int ByTwo = position%2; | |
int ByThree = position%3; | |
int ByFour = position%4; | |
int ByFive = position%5; | |
int BySix = position%6; | |
int BySeven = position%7; | |
int ByEight = position%8; | |
int ByNine = position%9; | |
if(ByTwo == 0 && ByNine == 0) | |
{ | |
shape1.setTexture(&rwalktex); | |
} | |
else if(ByThree == 0 && ByEight == 0) | |
{ | |
shape1.setTexture(&rwalktex2); | |
} | |
else if(ByFour == 0 && BySeven == 0) | |
{ | |
shape1.setTexture(&rwalktex); | |
} | |
else if(ByFive == 0 && BySix == 0) | |
{ | |
shape1.setTexture(&rwalktex2); | |
} | |
else if(jump == true || fall == true) | |
{ | |
shape1.setTexture(&rjumptex); | |
} | |
else | |
{ | |
shape1.setTexture(&rstandtex); | |
} | |
} | |
//WALKLEFT// | |
if(lwalk == true){ | |
xVelocityShape1 = -5; | |
int position = shape1.getPosition().x; | |
int ByTwo = position%2; | |
int ByThree = position%3; | |
int ByFour = position%4; | |
int ByFive = position%5; | |
int BySix = position%6; | |
int BySeven = position%7; | |
int ByEight = position%8; | |
int ByNine = position%9; | |
if(ByTwo == 0 && ByNine == 0) | |
{ | |
shape1.setTexture(&lwalktex); | |
} | |
else if(ByThree == 0 && ByEight == 0) | |
{ | |
shape1.setTexture(&lwalktex2); | |
} | |
else if(ByFour == 0 && BySeven == 0) | |
{ | |
shape1.setTexture(&lwalktex); | |
} | |
else if(ByFive == 0 && BySix == 0) | |
{ | |
shape1.setTexture(&lwalktex2); | |
} | |
else if(jump == true || fall == true) | |
{ | |
shape1.setTexture(&ljumptex); | |
} | |
else | |
{ | |
shape1.setTexture(&lstandtex); | |
} | |
} | |
//STOP// | |
if(rwalk == false && lwalk == false && rduck == false && jump == false){ | |
shape1.setTexture(&rstandtex); | |
xVelocityShape1 = 0; | |
} | |
if(rwalk == true && lwalk == true) | |
{ | |
xVelocityShape1 = 0; | |
shape1.setTexture(&rstandtex); | |
} | |
//DUCK// | |
if(rduck == true) | |
{ | |
shape1.setTexture(&rducktex); | |
xVelocityShape1 = 0; | |
//shape1.setPosition(400, 350); | |
} | |
//JUMPING// | |
if (jump) | |
{ | |
yVelocityShape1 = -20; | |
} | |
while((fall==true) && (jump == true) && (yVelocityShape1 < max_velocity)) | |
{ | |
yVelocityShape1++; | |
} | |
if((shape1.getGlobalBounds().intersects(ground.getGlobalBounds()) && (jump == true))) | |
{ | |
yVelocityShape1 = 0; | |
float check = ground.getPosition().y; | |
float moveto = check - 100; | |
float xlocale = shape1.getPosition().x; | |
shape1.setPosition(xlocale, moveto); | |
jump = false; | |
fall = false; | |
std::cout << "Falling stopped " << yVelocityShape1 << std::endl; | |
} | |
if((shape1.getGlobalBounds().intersects(blocks.getGlobalBounds()))) | |
{ | |
yVelocityShape1 = 0; | |
float check = blocks.getPosition().y; | |
float moveto = check - 100; | |
float xlocale = shape1.getPosition().x; | |
shape1.setPosition(xlocale, moveto); | |
jump = false; | |
fall = false; | |
std::cout << "Falling stopped " << yVelocityShape1 << std::endl; | |
} | |
////****//// | |
//MOVEMENT// | |
shape1.move(xVelocityShape1, yVelocityShape1); | |
if(shape1.getPosition().x < 100) | |
{ | |
float rightwalk = shape1.getPosition().x; | |
float locale = rightwalk - 100; | |
view.reset(sf::FloatRect(locale, 0, 1000, 800)); | |
} | |
if(shape1.getPosition().x > 900) | |
{ | |
float leftwalk = shape1.getPosition().x; | |
float locale2 = leftwalk - 900; | |
view.reset(sf::FloatRect(locale2, 0, 1000, 800)); | |
} | |
////****//// | |
//RENDERING (display everything on the screen) | |
window.clear(); //wipes whole screen | |
window.setView(view); | |
window.draw(background); | |
window.draw(shape1); | |
window.draw(ground); | |
window.draw(blocks); | |
window.display(); //shows whatever we have drawn to screen | |
/////////// | |
} | |
//Clean up | |
window.close(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment