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.


Topics - Ashmor

Pages: [1]
1
SFML projects / Tetris clone
« on: July 30, 2019, 06:02:30 pm »


Hey guys. Programmer-hobbyist here. I made a Tetris clone in C++/SFML in my spare time. Here's a link to GitHub in case someone's interested in the code.

https://github.com/Ashmor00/Tetris

I also made a Snake clone a while ago.

https://github.com/Ashmor00/Snake

Tell me what you think :)

2
General / What's wrong with my code? Angles won't work
« on: July 30, 2019, 09:05:12 am »
Hey guys, I'm new to programming and SFML. I'm trying to make something like a canon. It's gonna fire balls that will be flying in an arc. Sounds like a very simple task to accomplish, yet I cannot seem to figure out how angles work in SFML. For example, with ang_const = 0.13 Rad (7.44 Deg), my balls flies in a beautiful arc. However, when I change the value of ang_const to 0.14 Rad (8.021 Deg), the ball flies in the opposite direction! If I change the angle to 0.19 Rad (10.88 Deg), it flies downwards for whatever reason.

So here's my code:
#include <SFML/Graphics.hpp>
#include <math.h>
int WIDTH = 1024, HEIGHT = 704;

class Ball {
private:
    float radius = 16.00;
public:
    sf::CircleShape shape;
    Ball () {
        shape.setPosition(0 + radius*2, HEIGHT - radius*2);
        shape.setRadius(radius);
        shape.setFillColor(sf::Color::Cyan);
        shape.setOrigin(radius, radius);
    }
    void update() {
        if (x() - radius > WIDTH) {
            this->shape.setPosition(0 - radius, y());
        }
        if (x() + radius < 0) {
            this->shape.setPosition(WIDTH + radius, y());
        }
        if (y() - radius > HEIGHT) {
            this->shape.setPosition(x(), 0 - radius);
        }
        if (y() + radius < 0) {
            this->shape.setPosition(x(), HEIGHT + radius);
        }
    }
    float RadToDeg (float radian) {
        double pi = 3.14159;
        return radian * (180 / pi);
    }
    float x() { return shape.getPosition().x; }
    float y() { return shape.getPosition().y; }
    float getRadius() { return radius; }
};

int main()
{
    // Create the main window
    sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), "del");
   
    // Some variables
    float ang_const = 0.13;                  
    float velX_const = 3.5, velY_const = 3.5;
    float grav_const = -0.02;                
   
    float ang = ang_const;
    float velX = velX_const, velY = velY_const;
    float grav = grav_const;
   
    // Text
    int size_for_text = 64;
    sf::Font f;
    f.loadFromFile("Keyboard.ttf");
    sf::Text text1;
    text1.setFont(f);
    text1.setCharacterSize(27);
    text1.setFillColor(sf::Color::White);
    text1.setPosition(size_for_text, size_for_text);
   
    // Ball
    Ball ball;
   
    while (window.isOpen())
    {
        // Process events
        sf::Event event;
        while (window.pollEvent(event))
        {
            // Close window: exit
            if (event.type == sf::Event::Closed) {
                window.close();
            }
           
            // Escape pressed: exit
            if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) {
                window.close();
            }
           
            // Restart
            if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Space) {
                ang = ang_const;
                velX = velX_const, velY = velY_const;
                grav = grav_const;
                ball.shape.setPosition(0 + ball.getRadius()*2, HEIGHT - ball.getRadius()*2);
            }
        }
       
        // Ball movement
        ball.update();
       
        velY += grav;
        ball.shape.move(velX * cos(ball.RadToDeg(ang)),
                        velY * -sin(ball.RadToDeg(ang)));
       
        // Clear screen
        window.clear(sf::Color(0,0,80,100));
       
        // Draw ball
        window.draw(ball.shape);
       
        // Draw text
        text1.setString("ang " + std::to_string(ang));
        window.draw(text1);
       
        // Update the window
        window.display();
    }
   
    return EXIT_SUCCESS;
}
 

The main lines are these:
Variables:
float ang_const = 0.13;                
float velX_const = 3.5, velY_const = 3.5;
float grav_const = -0.02;              
 

Ball movement:
velY += grav;
ball.shape.move(velX * cos(ball.RadToDeg(ang)), velY * -sin(ball.RadToDeg(ang)));
 

Radians to Degrees function:
float RadToDeg (float radian) {
        double pi = 3.14159;
        return radian * (180 / pi);
}
 

Could someone explain what's wrong with my code and how angles work in SFML? I'd be appreciated for your help guys.

3
General / Mouse events
« on: July 16, 2019, 08:50:02 pm »
Hey guys. I'm new to programming and to SFML. I have a little problem. I have three square sprites, s1, s2, and s3. I want them to change color when i move the cursor toward them. For example, i touch the cursor to the s1 sprite and it changes its color to green. How can i accomplish it? Could you help me out please?

Pages: [1]
anything