Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cube drawing
#31
Thanks again. Yeah, I'm trying to learn, something I understand, something I don't for now.

Why did you remove cube.draw()? How come it become unnecessary? Is there any other simple way if you don't suggest me using time.wait() that I can have some time off between my and computer's move so I can follow the game easier like I've already had with cube.draw(surface) in the end of the loop?
Reply
#32
I wanted to put all the drawing code in one place. When the only thing drawn in a screen update was the cube, it made some sense to have Cube.draw() do the entire update. Now a screen update flood fill's the screen, draws the cube, and draws a message. I already felt a little guilty about having Cube.draw() doing the flood fill, but having it also draw a message, well it is just not good design.

I wrote a new screen_refresh() function that does all the things needed to refresh the screen. When I did that, the only thing Cube.draw() did was call Square.draw() for each of the squares. That isn't much to do, and Cube.draw() was only called from one place, so I incorporated the square drawing loop from Cube.draw() into screen_refresh(). You may have noticed that earlier I did the same thing with Cube.rotate() and Cube.move().

Now with a dedicated screen_refresh() function you have a logical place to put code if you wanted to add a score keeper or add an animation, or any kind of visual function that has nothing to do with cubes or squares or game play.

As written, there is a short delay between the player's move and the robot's move. If you want that delay to be longer you can use wait(), but that just seems silly. If you want the robot player to be slower, remove the alpha/beta pruning in minimax. Personally, I don't understand why you would want to make your player have to wait for the robot.
Reply
#33
Alright, thanks!

Referring to: "Personally, I don't understand why you would want to make your player have to wait for the robot.", I don't want my player to wait for the robot, I just want as little time as possible just to see robot playing after I make my move. For the best example, like any tic-tac-toe game you can play online, you have some time off between your and robot's move. That's why I was trying to accomplice.
Reply
#34
As I said, there is already some delay as the robot computes it's move. This is at least 10 milliseconds but can be longer if you have markers on multiple boards.

I don't play much tic-tac-toe online so I cannot say if any delay between drawing the player's mark and the robot's mark is computational time, or for show. The little experience I have writing web code makes me think it is computation time. Web programs are painfully slow. They get away with it because the things interacting with web pages, people, are even slower.
Reply
#35
Okay, thanks again.

Can I display text in multiple lines in pygame somehow so I can avoid writing a large function like this?
I want to explain the rules of the game.

def blit_text(surface, text, pos, font, color=pygame.Color('black')):
    words = [word.split(' ') for word in text.splitlines()]  # 2D array where each row is a list of words.
    space = font.size(' ')[0]  # The width of a space.
    max_width, max_height = surface.get_size()
    x, y = pos
    for line in words:
        for word in line:
            word_surface = font.render(word, 0, color)
            word_width, word_height = word_surface.get_size()
            if x + word_width >= max_width:
                x = pos[0]  # Reset the x.
                y += word_height  # Start on new row.
            surface.blit(word_surface, (x, y))
            x += word_width + space
        x = pos[0]  # Reset the x.
        y += word_height  # Start on new row.

Let's say in an easy way that I want to display "game over" like this:
Game
over!
Reply
#36
Instead of splitting on spaces you should splt on newline characters. Other than that it looks fine.
Reply
#37
Yeah I know, but I asked if there was any easier and much shorter way.
Reply
#38
If you split the message into lines you only have to change the y location for each line. Each line will be centered at the same x location. The words automatically get positioned correctly inside the lines. You should not draw them a word at a time.
def blit_text(surface, message, pos, font, color=pygame.Color('black')):
    x, y = pos
    for line in message.split('\n'):
        text = font.render(word, 0, color)
        width, height = text.get_size()
        surface.blit(text, (x - width/2, y - height/2))
        y += height + 2.
Reply
#39
And how can I display a text like this (every line to be centered)?
Game over!
Congratulations!
Reply
#40
Use the code from my last post. The lines are centered around x. You will have to give a good y starting point if you want them centered vertically on the screen.
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