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

Author Topic: [Very Beginner] Need Help With Drawing Things  (Read 3126 times)

0 Members and 2 Guests are viewing this topic.

han27

  • Newbie
  • *
  • Posts: 4
    • View Profile
[Very Beginner] Need Help With Drawing Things
« on: November 26, 2020, 07:41:36 am »
Hi!
I have a snake and ladder game project, I have to draw a snake / ladder of course, but I am confused about how I can set the snake or ladder so that the location can match the board tile, is there any way that i can set the start of the snake with Vector2f, and the other end with also Vector2f? sorry for my bad english, hope you understand and can fix my problem, Thanks!
« Last Edit: November 26, 2020, 09:04:54 am by eXpl0it3r »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: [Very Beginner] Need Help With Drawing Things
« Reply #1 on: November 26, 2020, 09:07:09 am »
You just apply some math. Calculate the position of the tile, then subtract half of the width of the snake or ladder. Best you take some pen and paper and figure out the math on paper, before trying to apply something in code.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

han27

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: [Very Beginner] Need Help With Drawing Things
« Reply #2 on: November 26, 2020, 09:23:44 am »
You just apply some math. Calculate the position of the tile, then subtract half of the width of the snake or ladder. Best you take some pen and paper and figure out the math on paper, before trying to apply something in code.

i have the location of the every tile, but i dont know how to set the beginning and the end of the snake in tiles, what type of shape should i use and how to set the positions?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: [Very Beginner] Need Help With Drawing Things
« Reply #3 on: November 26, 2020, 12:49:47 pm »
You first need to figure out how you want to draw a snake, since they can have different lengths or directions.
If you just use shape, you'd need to calculate the correct rotation for a start and end point yourself or you could also check out the LineShape someone wrote once.

But if you then have different textures for the snake or ladder, you'll have to come up with a way to add more sections of the given texture.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

han27

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: [Very Beginner] Need Help With Drawing Things
« Reply #4 on: November 26, 2020, 01:45:22 pm »
You first need to figure out how you want to draw a snake, since they can have different lengths or directions.
If you just use shape, you'd need to calculate the correct rotation for a start and end point yourself or you could also check out the LineShape someone wrote once.

But if you then have different textures for the snake or ladder, you'll have to come up with a way to add more sections of the given texture.

after searching answers on the internet, i think that problem is solved, here's what i've done to my code, but i have a new problem and i don't know what to do, here's the code

void Board::render(RenderTarget& target) //<- this is RenderWindow from the main game.cpp
{
        node* temp = head;
        while (temp) {
                if (temp->jump->location > temp->location) {
                        float distance = pow(temp->locx - temp->jump->locx, 2) + pow(temp->locy - temp->jump->locy, 2);
                        distance = pow(distance, 1 / 2);
                        float scaley = distance / laddersprite.getTexture()->getSize().y;
                        laddersprite.setScale(5, scaley);
                        laddersprite.setOrigin(laddertexture.getSize().x / 2, laddertexture.getSize().y);
                        float angle = atan2(temp->jump->locy - temp->locy, temp->jump->locx - temp->locx);
                        laddersprite.setRotation(angle);
                        laddersprite.setPosition(temp->locx, temp->locy);
                        target.draw(laddersprite);//<--it triggers breakpoint here
                }
                else if (temp->jump->location < temp->location) {

                }
                temp = temp->next;
        }
}
 
i'm pretty new with SFML and i never encountered problems like that, i used linked list for the game and jump is snake/ladder target tile(as a node), locx and locy is basically the position of the tile
« Last Edit: November 26, 2020, 01:49:12 pm by han27 »

han27

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: [Very Beginner] Need Help With Drawing Things
« Reply #5 on: November 26, 2020, 04:34:37 pm »
all the problem is solved, thankyou for helping! :D

 

anything