Search code examples
pythonturtle-graphicsgame-developmentpython-turtle

Making a sliding puzzle game using turtle (not tkinter or pygame)


So I'm currently trying to program a slidepuzzle game without importing tkinter or pygame. So far i've generated a board and populated it with working buttons (quit,load,reset) but I'm really lost on how to program the actual slide puzzle game with the images i've been provided.

This code generates the screen and buttons that makeup my board. clicking the load button (which i already have setup) allows the user to type in the puzzle they want to load and unscramble. The issue is that I don't know how to get all the images onto the board and Im not sure what direction i should go in to actually program the game elements (it's just a screen and buttons right now). I'm a newbie programmer so any help is really appreciated.

screen = turtle.Screen()
def generate_screen():
`os.chdir('Resources')  # Changes directory to allow access to .gifs in Resources
screen.setup(700, 700)
screen.title("Sliding Puzzle Game")
screen.tracer(0)
generate_scoreboard()
generate_leaderboard()
iconturtle = turtle.Turtle()
iconturtle.penup()
for file in os.listdir():
    screen.register_shape(file)
iconturtle.goto(280, -270)
iconturtle.shape('quitbutton.gif')
iconturtle.stamp()
iconturtle.goto(180, -270)
iconturtle.shape('loadbutton.gif')
iconturtle.stamp()
iconturtle.goto(80, -270)
iconturtle.shape('resetbutton.gif')
iconturtle.stamp()`

`def load_yoshi():
    os.chdir('Images\\yoshi')
    screen.tracer(1)
    screen.register_shape('yoshi_thumbnail.gif')
    t = turtle.Turtle()
    t.penup()
    t.shape('yoshi_thumbnail.gif')
    t.goto(250,290)
    t.stamp()
    screen.update()
    files = glob.glob('*.gif') # pulling out only .gif
    images = files
    print(images)
    for file in images:
        screen.register_shape(file)`

Solution

  • I've only seen turtle used to draw lines, not shapes, much less movable game pieces. I think pygame would definitely be better for this – OneCricketeer

    Below is an example slide game simplified from an earlier answer I wrote about creating numbered tiles using turtle:

    from turtle import Screen, Turtle
    from functools import partial
    from random import random
    
    SIZE = 4
    TILE_SIZE = 100
    OFFSETS = [(-1, 0), (0, -1), (1, 0), (0, 1)]
    
    CURSOR_SIZE = 20
    
    def slide(tile, row, col, x, y):
        tile.onclick(None)  # disable handler inside handler
    
        for dy, dx in OFFSETS:
            try:
                if row + dy >= 0 <= col + dx and matrix[row + dy][col + dx] is None:
                    matrix[row][col] = None
                    row, col = row + dy, col + dx
                    matrix[row][col] = tile
                    x, y = tile.position()
                    tile.setposition(x + dx * TILE_SIZE, y - dy * TILE_SIZE)
                    break
            except IndexError:
                pass
    
        tile.onclick(partial(slide, tile, row, col))
    
    screen = Screen()
    
    matrix = [[None for _ in range(SIZE)] for _ in range(SIZE)]
    
    offset = TILE_SIZE * 1.5
    
    for row in range(SIZE):
        for col in range(SIZE):
            if row == SIZE - 1 == col:
                break
    
            tile = Turtle('square', visible=False)
            tile.shapesize(TILE_SIZE / CURSOR_SIZE)
            tile.fillcolor(random(), random(), random())
            tile.penup()
            tile.goto(col * TILE_SIZE - offset, offset - row * TILE_SIZE)
            tile.onclick(partial(slide, tile, row, col))
            tile.showturtle()
    
            matrix[row][col] = tile
    
    screen.mainloop()
    

    Click on a tile next to the blank space to have it move into that space:

    enter image description here