pythonpython-3.xturtle-graphicsgraph-drawing

How to add an object into a square which is pre-defined in Python-3x?


I drove a board with 8x8 dimension by using turtle module. It looks like a chessboard,but it is not,since the black blocks are randomly distributed.

The aim is that I want to make an A-Star Algorithm. The algorithm part is done. However, I am stuck in the graphical interface part.

I was using turtle-graphics module, but as far as I can understand, it draws the shapes by using a pen, which is move whole specific path by drawing the lines. I was dealing with the functions of turtle module in order to understand what are they.

My question are:

  1. I want to put an object or image into a specific block. In fact, the block that I put into the object will be white.

  2. I also want that object to move from one block to another block like a chess game. (However, it is not).

  3. After one movement, is it possible to fill the previous block with a color ? (Actually, it is possible, I've done this part, but it was a bit time consuming. I mean, the program has to wait a bit to complete the pen movements in order to fill that block with a color.)

    import turtle
    import image
    Tess=turtle.Turtle()
    Tess.pensize(1)
    Tess.speed(-100)
    w=40
    
    def mysquare(who,thecolor,size):
        who.pendown()
        who.pencolor('black')
        who.fillcolor(thecolor)
        who.begin_fill()
        who.setheading(0)
        for i in range(4):
            who.forward(size)
            who.left(90)
        who.end_fill()
    
    for j in range(8):
      for i in range(8):
    #      print(i,j)
          if i==5 and j==7 or i==5 and j==6 or i==3 and j==5 or i==7 and j==5 or i==2 and j==4 or i==4 and j==4 or i==5 and j==4 or i==6 and j==4:
              scolor='black'
          else:
              scolor='white'
          Tess.penup()
          Tess.goto((i-4)*w,(j-4)*w)
          mysquare(Tess,scolor,w)
    

Solution

  • I believe the following exhibits the behaviors you describe. It sets up the board, moves the turtle to a white square and then randomly moves it to squares it hasn't already visited (avoiding black squares) until it runs out of valid moves:

    from turtle import Turtle, Screen
    from random import choice
    
    WIDTH = 40
    BOARD_SIZE = 8
    COLOR_EMPTY, COLOR_BLOCKED, COLOR_VISITED, COLOR_FAILED = 'white', 'black', 'green', 'red'
    
    blocked_squares = [(5, 7), (5, 6), (3, 5), (7, 5), (2, 4), (4, 4), (5, 4), (6, 4)]  # should generate randomly
    
    def mysquare(turtle, i, j, color, size):
        turtle.penup()
        turtle.goto((i - BOARD_SIZE//2) * size, (j - BOARD_SIZE//2) * size)
        turtle.pendown()
        turtle.fillcolor(color)
        turtle.setheading(0)
    
        turtle.begin_fill()
        for _ in range(4):
            turtle.forward(size)
            turtle.left(90)
        turtle.end_fill()
    
    def board_to_screen(position):
        y, x = position
        return ((x - BOARD_SIZE//2) * WIDTH + WIDTH / 2, (y - BOARD_SIZE//2) * WIDTH + WIDTH / 2)
    
    def move():
        global position
    
        positions = []
    
        for j_delta in (-1, 0, 1):
            if 0 <= position[0] + j_delta < BOARD_SIZE:
                for i_delta in (-1, 0, 1):
                    if 0 <= position[1] + i_delta < BOARD_SIZE:
                        if board[position[0] + j_delta][position[1] + i_delta]:
                            new_position = (position[0] + j_delta, position[1] + i_delta)
                            if new_position != position:
                                positions.append(new_position)
        j, i = position
    
        if positions:
            board[j][i] = False
            mysquare(tess, i, j, COLOR_VISITED, WIDTH)
            position = choice(positions)
            screen.ontimer(move, 1000)
        else:
            mysquare(tess, i, j, COLOR_FAILED, WIDTH)
    
        tess.penup()
        tess.goto(board_to_screen(position))
        screen.update()
    
    tess = Turtle(shape='turtle')
    tess.speed('fastest')
    
    screen = Screen()
    screen.tracer(0)
    
    board = []
    
    for j in range(BOARD_SIZE):
        board.append(list())
        for i in range(BOARD_SIZE):
            board[j].append(True)
            square_color = COLOR_EMPTY
            if (i, j) in blocked_squares:
                square_color = COLOR_BLOCKED
                board[j][-1] = False
            mysquare(tess, i, j, square_color, WIDTH)
    
    screen.update()
    
    position = (3, 3)
    tess.penup()
    tess.goto(board_to_screen(position))
    
    screen.ontimer(move, 1000)
    
    screen.exitonclick()
    

    This is probably better done with multiple turtles to avoid some of the moves and the turtle on the board can be a GIF image instead of a built-in turtle shape. The black squares should probably be generated randomly (but carefully) as should the initial starting position on the board of the turtle.

    enter image description here