简体   繁体   中英

How to list the coordinates of a turtle movement?

I'm a beginner in python, and I'm making a small maze game with turtle. The game uses relative pixels (probably isn't an official term) - where instead of giving a value in pixels to move, it uses your screen resolution to determine the amount it needs to move (in the code - lines 33-44).

My screen res is 1366 x 768 - the pixel measurements are made off this. I want to track all the coordinates the turtle moves through, and add them all to a list of blocked coords for another turtle.

How do I measure all the coords the turtle mak1 passes through? (Preferably without for loops for each movement) Thanks!

PS. my code probably isn't efficient - I'm not really good at this

for i in range (1):
    import turtle
    import random
    import math
    import pyautogui
    import keyboard
    import ctypes
    import time


#turtle def
for i in range(1):
    turtle.getscreen().bgcolor('black')
    mak1 = turtle.Turtle()
    mak2 = turtle.Turtle()
    wn = turtle.Screen()
    pacman = turtle.Turtle()
    mak1.color('white')
    mak2.color('white')
    pacman.color('yellow')
    mak1.hideturtle()
    mak1.speed(0)
    mak2.hideturtle()
    mak2.speed(0)
    mak1.width(10)
    mak2.width(10)
    pacman.shape('circle')
    pacman.shapesize(stretch_wid = 1.5, stretch_len = 1.5)


#GetAndSetScreenRes - line 33
for i in range(1):
    user32 = ctypes.windll.user32
    width = user32.GetSystemMetrics(0)
    height = user32.GetSystemMetrics(1)
    wn.setup(width, height)

#values
for i in range(1):
    pix50 = (width/26)
    pix100 = (width/13)
    pix200 = (width/(13/2))
    blocked_coords = [] # line 44


#make the maze - mak1
for i in range (1):
    mak1.penup()
    mak1.fd(pix100)
    mak1.pendown()
    mak1.lt(90)
    mak1.bk(pix100)
    #innersquare
    for i in range(4):
        if (i == 1) or (i == 3):
            mak1.fd(pix50)
            mak1.penup()
            mak1.fd(pix100)
            mak1.pendown()
            mak1.fd(pix50)
        else:
            mak1.fd(pix200)
        mak1.lt(90)
    mak1.fd(pix100)
    mak1.rt(90)
    #right mid
    for i in range(1):
        mak1.penup()
        mak1.rt(90)
        mak1.fd(pix100)
        mak1.lt(90)
        mak1.fd(pix100)
        mak1.pendown()
        mak1.fd(pix100)
        mak1.rt(90)
        for i in range(2):
            mak1.fd(pix200)
            mak1.lt(90)
        mak1.fd(pix100)
        mak1.penup()
        mak1.lt(90)
        mak1.fd(pix100)
        mak1.rt(90)
        mak1.pendown()
        mak1.fd(pix100)
    #toppie
    for i in range(1):
        mak1.penup()
        mak1.goto(0, 0)
        mak1.seth(90)
        mak1.fd(pix200)
        mak1.pendown()
        mak1.fd(pix50)
        mak1.lt(90)
        mak1.fd(pix50)
        mak1.bk(pix100)
        mak1.penup()
        mak1.fd(pix200)
        mak1.pendown()
        mak1.fd(pix50)
        mak1.lt(90)
        mak1.fd(pix50)
        mak1.rt(90)
        mak1.fd(50)
        mak1.penup()
        mak1.fd(pix200)
        mak1.pendown()
        mak1.fd(pix100)
        mak1.penup()
        mak1.bk(pix200)
        mak1.lt(90)
        mak1.pendown()
        mak1.fd(pix100)
        mak1.penup()
        mak1.goto(0, 0)
        mak1.seth(0)
    #cubey
    for i in range (1):
        mak1.fd (pix100)
        mak1.pendown()
        mak1.fd(pix200)
        mak1.lt(90)
        mak1.fd(pix100)
        mak1.penup()
        mak1.lt(90)
        mak1.fd(pix100)
        mak1.rt(90)
        mak1.pendown()
        mak1.fd(pix100)
        mak1.penup()
        mak1.bk(pix100)
        mak1.rt(90)
        mak1.fd(pix200)
        mak1.pendown()
        mak1.fd(pix200)
        mak1.lt(90)
        mak1.fd(pix100)
        mak1.penup()
        mak1.lt(90)
        mak1.fd(pix100)
        mak1.pendown()
        mak1.fd(200)
        mak1.penup()
        mak1.fd(pix100)
        mak1.rt(90)
        mak1.fd(pix100)
        mak1.rt(90)
        mak1.pendown()
        mak1.fd(pix200 + pix200)
        mak1.penup()
        mak1.bk(pix200 * 4 + pix100)
        mak1.pendown()
        mak1.bk(pix200 + pix100)
        mak1.penup()
        mak1.goto(0, 0)
        mak1.seth(90)

    
#blocked coords
#??


#end program
turtle.done()```

The best that turtle is going to give you for free is the coordinates of all the points your turtle starts/stops at using begin_poly() , end_poly() and get_poly() , something like:

mak1.begin_poly()

#make the maze - mak1
for i in range (1):
    mak1.penup()
    mak1.fd(pix100)

# ...
        mak1.penup()
        mak1.goto(0, 0)
        mak1.seth(90)

mak1.end_poly()

#blocked coords

blocked_coords = mak1.get_poly()

This will get you something like this to work with:

((0.00,0.00), (129.23,0.00), (129.23,-129.23), (129.23,129.23), (64.62,129.23),
 (-64.62,129.23), (-129.23,129.23), (-129.23,-129.23), (-64.62,-129.23), (64.62,-129.23),
 (129.23,-129.23), (129.23,-0.00), (129.23,-129.23), (258.46,-129.23), (387.69,-129.23),
 (387.69,-387.69), (646.15,-387.69), (646.15,-258.46), (516.92,-258.46), (516.92,-129.23),
 (0.00,0.00), (0.00,258.46), (0.00,323.08), (-64.62,323.08), (64.62,323.08),
 (-193.85,323.08), (-258.46,323.08), (-258.46,258.46), (-308.46,258.46), (-566.92,258.46),
 (-696.15,258.46), (-437.69,258.46), (-437.69,129.23), (0.00,0.00), (129.23,-0.00),
 (387.69,-0.00), (387.69,129.23), (258.46,129.23), (258.46,258.46), (258.46,129.23),
 (516.92,129.23), (775.38,129.23), (775.38,258.46), (646.15,258.46), (446.15,258.46),
 (316.92,258.46), (316.92,387.69), (833.85,387.69), (-329.23,387.69), (-716.92,387.69),
 (0.00,0.00))

But unless you're disciplined about penups and pendowns, this would likely be impossible to work with for your purpose.

Another approach would be to first define your mazes in ASCII (spaces for empty spaces, periods for wall segments, etc.) and then divide your space into a grid. Since your walls are already 10px thick, that might work though I'd go thicker, eg the thickness of your pacman, 30px. Then for each period in your ASCII maze definition, drop a new square-shaped turtle at that point.

It may seem more difficult to draw, but at runtime, you can simply use the turtle distance() method to determine if you've collided with a wall segment or not. Search SO for examples of this, they're out there.

Finally, you should be able to remove your Windows dependency of using:

user32 = ctypes.windll.user32
width = user32.GetSystemMetrics(0)
height = user32.GetSystemMetrics(1)
wn.setup(width, height)

by instead doing something within turtle itself:

wn = turtle.Screen()
wn.setup(1.0, 1.0)  # as much screen as I can get
width = wn.window_width()
height = wn.window_height()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM