Python 海龟游戏--倾斜功能

Python Turtle Game--Tilt function

所以我正在尝试制作一款类似于 Stick Hero 的游戏,其目标是估计正确的长度以便传递到下一建筑物。现在我有了它,所以路径的长度随着 space 条被按下而增长,然后下降以查看玩家是否做出了正确的长度。我的困境是,每次路径长度改变时,都必须移动它才能正确落下(触及第一座建筑物的边缘)。现在 drop_path() 函数确定长度改变了多少次然后正确定位路径

def drop_path():
    path_pos = -360
    path.tilt(90)
    if grow_count == 1:
        path.backward(forward_len)
        path_pos += 24
        path.setx(path_pos)
    elif grow_count == 2:
        path.backward(forward_len)
        path_pos += 27
        path.setx(path_pos)
    elif grow_count == 3:
        path.backward(forward_len)
        path_pos += 29
        path.setx(path_pos)
    elif grow_count == 4:
        path.backward(forward_len)
        path_pos += 32
        path.setx(path_pos)
    elif grow_count == 5:
        path.backward(forward_len)
        path_pos += 34
        path.setx(path_pos)

等我意识到这可能不是最好的方法,而这正是我需要帮助的

非常感谢帮助,如果您需要澄清,请询问

#-----import statements-----
import turtle as trtl
import random as rand

#-----game configuration----
wn  = trtl.Screen()
wn.setup(1000,500)
wn.bgpic("city_background.png")
painter = trtl.Turtle()
game_over = False
wn.addshape('hero_image.gif')

path_len = 2
#-----initialize turtle-----
building = trtl.Turtle()
building.penup()
building.color("mediumslateblue")
building.pencolor("mediumslateblue")
building.goto(-400,-150)
building.shape("square")
building.shapesize(11,4)

hero = trtl.Turtle()
hero.shape("circle")
hero.penup()
hero.goto(-400,-30)
path = trtl.Turtle()
path.setheading(90)

grow_count = 0
forward_len = 22

#-----game functions--------
def make_structure():
    new_building = trtl.Turtle()
    new_building = building.clone()
    new_building.forward(200)
    width = rand.randint(1,8)
    new_building.shapesize(11, width)

def make_path():
    path.shape("square")
    path.penup()
    path.goto(-360,-20)
    path.shapesize(.25,2)

def grow_path():
    global path_len, grow_count,forward_len
    grow_count += 1
    forward_len += 2.5
    path_len += .25
    path.forward(2.5)
    path.shapesize(.25, path_len)
    print(grow_count) 
   
def drop_path():
    path_pos = -360
    path.tilt(90)
    if grow_count == 1:
        path.backward(forward_len)
        path_pos += 24
        path.setx(path_pos)
    elif grow_count == 2:
        path.backward(forward_len)
        path_pos += 27
        path.setx(path_pos)
    elif grow_count == 3:
        path.backward(forward_len)
        path_pos += 29
        path.setx(path_pos)
    elif grow_count == 4:
        path.backward(forward_len)
        path_pos += 32
        path.setx(path_pos)
    elif grow_count == 5:
        path.backward(forward_len)
        path_pos += 34
        path.setx(path_pos)
    elif grow_count == 6:
        path.backward(forward_len)
        path_pos += 37
        path.setx(path_pos)
    elif grow_count == 7:
        path.backward(forward_len)
        path_pos += 39
        path.setx(path_pos)
    elif grow_count == 8:
        path.backward(forward_len)
        path_pos += 41
        path.setx(path_pos)
    elif grow_count == 9:
        path.backward(forward_len)
        path_pos += 43
        path.setx(path_pos)
    elif grow_count == 10:
        path.backward(forward_len)
        path_pos += 46
        path.setx(path_pos)
    elif grow_count == 11:
        path.backward(forward_len)
        path_pos += 48
        path.setx(path_pos)
    elif grow_count == 12:
        path.backward(forward_len)
        path_pos += 51
        path.setx(path_pos)
    elif grow_count == 13:
        path.backward(forward_len)
        path_pos += 53
        path.setx(path_pos)
    elif grow_count == 14:
        path.backward(forward_len)
        path_pos += 56
        path.setx(path_pos)
    
#-----events----------------

make_path()
make_structure()
wn.onkeypress(grow_path, "space")
wn.onkeypress(drop_path, "g")
wn.update()
wn.listen()
wn.mainloop()

既然你使用shapesize()来调整路径,我们也可以使用shapesize(),不带任何参数,来查询路径的当前形状。有了这些信息,我们就可以计算出放置倾斜路径的位置:

#-----import statements-----
from turtle import Screen, Turtle
from random import randint

#-----game functions--------
def make_structure():
    new_building = building.clone()
    new_building.forward(200)
    width = randint(1, 8)
    new_building.shapesize(11, width)

def make_path():
    path = Turtle()
    path.shape('square')
    path.penup()
    path.goto(-362.5, -20)
    path.setheading(90)
    path.shapesize(.25, 2)

    return path

path_len = 2

def grow_path():
    global path_len

    path_len += 0.25
    path.forward(2.5)
    path.shapesize(0.25, path_len)

    screen.update()

def drop_path():
    path.tilt(90)

    stretch_wid, stretch_len, _ = path.shapesize()

    path.backward(stretch_len * 10 + stretch_wid * 10)
    path.setx(path.xcor() + stretch_len * 10)

    screen.update()

#-----game configuration----

screen = Screen()
screen.setup(1000, 500)
screen.tracer(False)

#-----initialize turtle-----
building = Turtle()
building.shape('square')
building.shapesize(11, 4)
building.color('mediumslateblue')
building.penup()
building.goto(-400, -150)

path = make_path()

make_structure()

#-----events----------------

screen.onkeypress(grow_path, 'space')
screen.onkeypress(drop_path, 'g')
screen.listen()
screen.update()
screen.mainloop()

我已经简化了您的示例代码,删除了不影响我们试图解决的问题的所有内容。我还打开了 tracer(),并增加了 update() 调用的次数,使移动看起来更流畅。