Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - klefmung

Pages: [1]
1
SFML projects / critique my pong code?
« on: June 09, 2018, 04:26:01 pm »
Hey guys, I am mostly self taught and have been improving my code by having it scrutinized on the internet. I've been doing a coding bootcamp for a few weeks now though, so I haven't had a chance to do any C++ or SFML for a while. Only ruby. This is my attempt to refresh my memory. This is the first one I needed any kind of physics for, so lets see what you think:

https://github.com/letsdothis64/Pong

2
Thanks for the feedback! I'm combing through my code and making changes for posterity now to practice those habits.

3
I already did this for my snake project, and received some great feedback to improve my habits, which is my primary goal right now. I've only been coding for a few months, and would like to begin making code I can show to other people and not be ashamed of myself. I guess that starts with asking people about my code, and the snake code is the only code anyone's ever seen from me. Let me know what I can do better! Screen shot attached.

Also, it's worth noting that I did my best to follow the tetris guideline

Exe with required files:
https://drive.google.com/open?id=1JP2AAomcjRNFxqLgJorH-V0_k5Xp1SsM

Source code on github:
https://github.com/letsdothis64/Tetris

4
I edited the one on github taking all the critique, if anyone feels like checking it out.
Thanks for all the help!

5
That's interesting. I think I can definitely point to the example of my code in snake::update() where I search through the food object's pellets manually, where maybe that could be a function called intersect in food that takes a vector as an argument and returns a bool, then handles the deleting of that pellet and generating another one internally.

6
Thanks! I figured there were some bugs hidden in my basic practice. I'll definitely apply all of that to my big project. And I think you've inspired me to do another mini project for code critiques that shows some other stuff I may be unaware I'm not doing the best way.

7
You should set this up in github so that I can look at it from my web browser.
https://github.com/letsdothis64/Snake
My first time using git or github. =P I took "learn how to use git" as a critique. Hopefully that works.

8
backstory:
Only been coding a total of 3-4 months. I've done and am doing things a lot more complex than this, but because of the part of the learning curve I'm on, I feel like I always have code I'm ashamed of somewhere in there. Basically, what I've done so far is make a console based pokemon battle simulator, which turned into an SFML based pokemon fan game. (with stick figures. I'm a bad artist) That'll get posted when I'm done overhauling the battle system. (which was  one of the first things I did in C++. It's hard to expand with more move support, and is my n00biest of code)

Why this?
I wanted to put together a couple hours worth of work showing how my general coding practices and SFML use look right now, since nobody has ever looked at or critiqued my code before. I didn't "cheat" by looking at other versions of snake code, so if I missed anything obvious, feel free to yell at me. I tried to (maybe over)comment it with what I was thinking when I wrote certain pieces of code.

Major critiques welcome!
Minor nitpicks welcome!
"Oh crap, I got distracted playing snake, here's my high score!" welcome!

Thank you so much for your critiques and comments.

oh whoops! Better post the link!

Game and source included.

https://drive.google.com/open?id=1wINgyIggyIDnOwC1l7TEoBPu8UpjzCvo

or just the source from github:
https://github.com/letsdothis64/Snake

edit:
added github link

9
Graphics / Re: setRepeated over a rectangle?
« on: November 02, 2017, 02:58:23 pm »
I get it now. It's working great. Thanks!

10
Graphics / setRepeated over a rectangle?
« on: November 02, 2017, 05:01:34 am »
Sorry, I'm pretty new at SFML (though I tried it once a few years ago and never got any traction). I am sure I am just misunderstanding this feature, but here goes. I tried to get the code down to the simplest version of the problem.

From the documentation, it seemed that if the shape was set to be larger than the texture or selected texture rect, and setRepeated was set to true, it would tile it repeatedly, but for me it just stretches. Here's some code that I believe outlines my problem along with the output screenshotted:

#include <SFML/Graphics.hpp>

int main()
{
    sf::Texture tex;
    if(!tex.loadFromFile("crossed lines.png"))
    {

    }
    tex.setRepeated(true);

    sf::RectangleShape rect1;
    sf::Vector2f rectsize1(50, 50);
    rect1.setSize(rectsize1);
    rect1.setTexture(&tex, false);
    rect1.setPosition(0, 0);

    sf::RectangleShape rect2;
    sf::Vector2f rectsize2(100, 100);
    rect2.setSize(rectsize2);
    rect2.setTexture(&tex, false);
    rect2.setPosition(100, 100);



    sf::RenderWindow window(sf::VideoMode(800, 600), "My window");

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear(sf::Color::White);

        window.draw(rect1);
        window.draw(rect2);

        window.display();
    }

    return 0;
}

 

The output is attached.

So I guess my question is, how can I get this to repeat over rect2 instead of just stretch it?

11
Oh man, I made some dumb mistakes involving the lack of equality operator. Sorry, I knew that, I just was thinking about other things. I know most of the basics (and am still trying to solidify them every day!) but they often just kind of fall out of my head. When I put in the sleeping, I had something else in mind. I went through and fixed some things. Now the vertical coordinates on the sheet work, but not the horizontal. That whole thing was a messy operation that I wasnt too terribly sure about. I looked over the clock section again. I still cant resolve an efficient way to handle that though. Any suggestions? Here is the updated product:
#include <iostream>
#include <SFML/Graphics.hpp>
using namespace std;

int main()
{
    cout<<"Blah";
sf::Image sLink;
sf::Sprite Link;
sLink.LoadFromFile("Link.png");
Link.SetImage(sLink);

    sf::RenderWindow GameWindow(sf::VideoMode(800, 600, 32), "SFML Graphics");

    while (GameWindow.IsOpened())
    {
        // Process events
        sf::Event Event;
        while (GameWindow.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                GameWindow.Close();
        }

    GameWindow.Clear();




const sf::Input& Input = GameWindow.GetInput();
bool         LeftKey    = Input.IsKeyDown(sf::Key::Left);
bool         RightKey     = Input.IsKeyDown(sf::Key::Right);
bool         UpKey    = Input.IsKeyDown(sf::Key::Up);
bool         DownKey    = Input.IsKeyDown(sf::Key::Down);

int Facing;
int AniFrame;
int Height=sLink.GetHeight();
int Width=sLink.GetWidth();
int FrameV;
int FrameH;
bool Moving;




//moving Link
if(RightKey)
{
    Link.Move(.025,0);
    Facing=3;
    Moving=true;
}
else if (LeftKey)
{
  Link.Move(-.025,0);
  Facing=1;
  Moving=true;
}
else if (DownKey)
{
    Link.Move(0,.025);
    Facing=4;
    Moving=true;
}
else if (UpKey)
{
    Link.Move(0,-.025);
    Facing=2;
    Moving=true;
}
else Moving=false;

//Setting vertical pixel count
if(Facing==3)
{
FrameV=2*(Height/4);
}
if(Facing==1)
{
    FrameV=0;
}
if(Facing==4)
{
    FrameV=3*(Height/4);
}
if(Facing==2)
{
    FrameV=Height/4;
}
int FrameVT=FrameV+(Height/4);




//Handling Animation
sf::Clock clock;
if(Moving=true)
{

    if(AniFrame<=4)
    {
        clock.Reset();
        if(clock.GetElapsedTime()==.1)
        {
            FrameH+=Width/4;
            clock.Reset();
        }
    }
    else FrameH=0;
}
else FrameH=0;
int FrameHT=FrameH+(Width/4);

if(FrameH==0)
{
    AniFrame=1;
}
else if(FrameH==Width/4)
{
    AniFrame=2;
}
else if(FrameH==2*(Width/4))
{
    AniFrame=3;
}
else if(FrameH==3*(Width/4))
{
    AniFrame=4;
}
else AniFrame=5;

Link.SetSubRect(sf::IntRect(FrameH,FrameV,FrameHT,FrameVT));


GameWindow.Draw(Link);


    GameWindow.Display();
    }




    return 0;
}
 
Also, as far as SFML 2 goes, I looked over the tutorials. Is there no graphics package for it? Or is there just no tutorial written for it yet? As in, does one have to do the graphics in OpenGL? Thanks.

12
Hey. I just picked up C++ a couple weeks ago, and now I'm trying to learn the basics of SFML. I can put a shape or sprite image on the screen and move it at this point, so I tried to do some animating using a basic Link sprite as practice. I saw that you can take a rectangle out of a sheet, so I thought I'd give try and cycle through them. I set variables for the reference coordinates of each rectangle, and for some reason, it only ones to give me the second one over on the x axis and the second one down on the y axis.  I can even move it around. It is just that one section though, and it doesn't change. I imagine the reasons for the x axis on the source image and the y axis not changing are completely different. Thanks for looking! Heres the image and the code:

Link.png:


#include <iostream>
#include <SFML/Graphics.hpp>
using namespace std;

int main()
{
    cout<<"Blah";
sf::Image sLink;
sf::Sprite Link;
sLink.LoadFromFile("Link.png");
Link.SetImage(sLink);

    sf::RenderWindow GameWindow(sf::VideoMode(800, 600, 32), "SFML Graphics");

    while (GameWindow.IsOpened())
    {
        // Process events
        sf::Event Event;
        while (GameWindow.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                GameWindow.Close();
        }

    GameWindow.Clear();




const sf::Input& Input = GameWindow.GetInput();
bool         LeftKey    = Input.IsKeyDown(sf::Key::Left);
bool         RightKey     = Input.IsKeyDown(sf::Key::Right);
bool         UpKey    = Input.IsKeyDown(sf::Key::Up);
bool         DownKey    = Input.IsKeyDown(sf::Key::Down);

int Facing;
int AniFrame;
int Height=sLink.GetHeight();
int Width=sLink.GetWidth();
int FrameV;
int FrameH;
bool Moving;




//moving Link
if(RightKey)
{
    Link.Move(1,0);
    sf::Sleep(.025);
    Facing=3;
    Moving=true;
}
else if (LeftKey)
{
  Link.Move(-1,0);
  sf::Sleep(.025);
  Facing=1;
}
else if (DownKey)
{
    Link.Move(0,1);
    sf::Sleep(.025);
    Facing=4;
}
else if (UpKey)
{
    Link.Move(0,-1);
    sf::Sleep(.025);
    Facing=2;
}
else Moving=false;

//Setting vertical pixel count
if(Facing=3)
{
FrameV=2*(Height/4);
}
if(Facing=1)
{
    FrameV=0;
}
if(Facing=4)
{
    FrameV=3*(Height/4);
}
if(Facing=2)
{
    FrameV=Height/4;
}
int FrameVT=FrameV+(Height/4);




//Handling Animation
sf::Clock clock;
if(Moving=true)
{

    if(AniFrame<=4)
    {
        clock.Reset();
        if(clock.GetElapsedTime()==.1)
        {
            FrameH+=Width/4;
            clock.Reset();
        }
    }
    else FrameH=0;
}
else FrameH=0;
int FrameHT=FrameH+(Width/4);

if(FrameH=0)
{
    AniFrame=1;
}
else if(FrameH=Width/4)
{
    AniFrame=2;
}
else if(FrameH=2*(Width/4))
{
    AniFrame=3;
}
else if(FrameH=3*(Width/4))
{
    AniFrame=4;
}
else AniFrame=5;

Link.SetSubRect(sf::IntRect(FrameH,FrameV,FrameHT,FrameVT));


GameWindow.Draw(Link);


    GameWindow.Display();
    }




    return 0;
}
 

Pages: [1]
anything