Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cube drawing
#41
If I write (x - width), there's a part of the text outside from the left side of the window, I changed it to (x + width and then it's in the center, but not completely centered, example from the game and the part of the code:

1) x - width
https://gofile.io/d/AmugVb

def blit_text(surface, msg, pos, font, color = pygame.Color('dodgerblue')):
        x, y = pos
        m = msg.split('\n')
        for line in m:
            text = pygame.font.Font(None, 48).render(line, True, msg_color)
            width, height = text.get_size()
            surface.blit(text, (x - width/2, y - height/2))
            y += height + 2.
2) x + width
https://gofile.io/d/zgag83

def blit_text(surface, msg, pos, font, color = pygame.Color('dodgerblue')):
        x, y = pos
        m = msg.split('\n')
        for line in m:
            text = pygame.font.Font(None, 48).render(line, True, msg_color)
            width, height = text.get_size()
            surface.blit(text, (x + width/2, y - height/2))
            y += height + 2.
EDIT: sorry, maybe my bad beacuse of this:
if msg:
            text = pygame.font.Font(None, 48).render(msg, True, msg_color)
            pos = center.x - text.get_width() / 2, center.y * 2 - text.get_height() <-- "because of this line"
            blit_text(surface, msg, pos, None, color = pygame.Color('dodgerblue'))
But can I somehow make that part responsive for any text length?
Reply
#42
Use just what I had in my post. Don't thow away the /2.
width, height = text.get_size()
surface.blit(text, (x - width/2, y - height/2))
Reply
#43
Yeah I did, look at the code I posted.
Each photo (link) refers to each code.
But I got the logic now, I'll try to fix it, shouldn't be hard.
Thanks!
Reply
#44
In your example you passed a bad pos() argument to blit_text(). Notice that the two lines are nicely centered, just not in the place you wanted.
Reply
#45
I got that.

But is there any default way that I can center any text, because with this I wrote, it probably suits an unique length, when I write something else, it's not centered.

In my case, I should set a default pos() somehow.

EDIT: I got it probably ...
Reply
#46
I modified my screen_update to better handle multi-line text. Now it centers horizontally and vertically about a point. To get the starting y coordinate it splits the message into lines, converts the lines into text surfaces, and sums the height of those surfaces. Next it adds a line space between consecutive lines to get the height of the overall block of text. This information is used to compute a starting y position.
    def refresh_screen(msg=None, font=text_font, color=text_color):
        """Draw the cube and an optional message"""
        line_space = 2
        surface.fill(background)
        for square in cube.squares:
            square.draw(surface)
        if msg:
            lines = msg.split('\n')
            lines = [font.render(line, True, color) for line in lines]
            x = center.x
            y = center.y - (
                sum(line.get_height() for line in lines) +
                (len(lines) - 1) * line_space) / 2
            for line in lines:
                surface.blit(line, (x - line.get_width() / 2, y))
                y = y + line.get_height() + line_space
        pygame.display.flip()
It would be easy to modify this function to just draw some text centered over some x, y position.
Reply
#47
Okay, thanks!

Could you explain me a little bit deep how exactly the minimax algorithm works?
Didn't catch it the best way.
Reply
#48
Read these and let me know if you still have questions about minimax.

https://www.geeksforgeeks.org/minimax-al...a-pruning/
https://www.neverstopbuilding.com/blog/minimax

I am also doing the depth check, which is not part of the minimax algorithm. I need something like this because I'm not really solving all three boards simultaneously. To save time I am solving the boards one at a time and using the depth check to select which board is the best move.
Reply
#49
Great, thanks!

Could that algorithm work better if I switched diagonals with combinations (0, 2, 4, 6, 8) and (1, 3, 5, 7) as it's written in the code? I made that change because it's too easy to win with classic tic-tac-toe rules. This change makes the game more interesting.

Perhaps that the computer always tries to have 3 in a row vertical or horizontal so that I can't win it too easy if I change sides of the cube.

And, in the class Square, why do we need this area of triangle? What does that triangle represent?
Reply
#50
The program needs to know what "square" you clicked. The squares are not square once rotated, so you cannot use the pygame rect collision detection. Instead, the program needs polygon collision detection. The algorithm uses splits our four-sided polygons with vertices ABCD into two triangles (ABC and ACD) and tests if the click is inside one of the triangles. This is the code in the program
has_point(a, b, c, point) or has_point(a, c, d, point)
To test if the point is inside the traingle ABC, I make three triangles using the triangle vertices (ABC) and the click point (P). These are triangles ABP, BPC and CPA. If point P is inside triangle ABC, the area of these triangles will add up to the area of triangle ABC. If point p is outside the triangle, the sum of the ABP, BPC and CPA tringles will be greater than the area of ABC. That is this code in the program.
area(a, b, c) >= int(area(a, p, b) + area(b, p, c) + area(c, p, a))
I used the >= and int() function because you should never test if two float numbers are equal. I know that int(x) will always be <= x, so I round down the sum of the little triangle and test if it is less than or equal to the area of the big triangle.

I found determining which square was clicked the most challenging part of writing the game.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Drawing a net of a cube freethrownucleus 26 5,117 May-05-2023, 10:23 PM
Last Post: freethrownucleus
  2D-Cube-Tic-Tac-Toe freethrownucleus 0 1,159 Mar-10-2023, 07:07 PM
Last Post: freethrownucleus
  PyGlet Trouble Drawing Cube. Windspar 3 5,744 Jan-02-2018, 06:37 PM
Last Post: Windspar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020