我的代碼有一個小問題:
import turtle
wn = turtle.Screen()
wn.bgcolor('lightblue')
cup = turtle.Turtle()
cup.shape('square')
cup.shapesize(1.5, 1)
cup.speed(0)
cup.dy = 1
cup.dx = 2
cup.penup()
gravity = 0.1
def face_right():
cup.setheading(310)
def face_left():
cup.setheading(45)
def jump_right():
cup.dy *= -1
def jump_left():
cup.dx *= -1
cup.dy = gravity
def do_right():
jump_right()
face_right()
def do_left():
face_left()
jump_left()
wn.listen()
wn.onkeypress(do_right, 'Right')
wn.onkeypress(do_left, 'Left')
wn.listen()
while True:
wn.update()
cup.dy -= gravity
cup.sety(cup.ycor() cup.dy)
cup.setx(cup.xcor() cup.dx)
如您所見,當您運行代碼時,“右跳”功能運行良好。跳左一?沒那么多。我已經嘗試通過嘗試大量不同的可能組合來解決這個問題,但似乎沒有一個有效。我在游戲中想要的只是右跳功能,但是當按下左箭頭鍵時它會向左跳。我被要求用烏龜制作完整的游戲,但我不知道我是否能夠繼續下去。
提前非常感謝!
PS我正在使用python 3
uj5u.com熱心網友回復:
dx和dy是位置的變化量。如果dx為 2,則表示在一定時間內重復向右移動 2。此外,dy是上下位置的變化量。您通過加減來實作它,而不是乘以變化量。乘以會非常突然地改變位置的變化量,乘以負數意味著改變方向。以下是您的代碼的修改后的代碼。
import turtle
wn = turtle.Screen()
wn.bgcolor('lightblue')
cup = turtle.Turtle()
cup.shape('square')
cup.shapesize(1.5, 1)
cup.speed(0)
cup.dy = 1
cup.dx = 2
cup.penup()
gravity = 0.1
def face_right():
cup.setheading(310)
def face_left():
cup.setheading(45)
def jump_right():
cup.dx = 3
cup.dy = 3
def jump_left():
cup.dx = -3
cup.dy = 3
def do_right():
jump_right()
face_right()
def do_left():
face_left()
jump_left()
wn.onkeypress(do_right, 'Right')
wn.onkeypress(do_left, 'Left')
wn.listen()
while True:
wn.update()
cup.dy -= gravity
cup.sety(cup.ycor() cup.dy)
cup.setx(cup.xcor() cup.dx)
或者如果你想要更快的移動和跳躍,
def jump_right():
cup.dx = 3
cup.dy = 3
def jump_left():
cup.dx = -3
cup.dy = 3
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/432195.html
標籤:Python python-3.x 蟒蛇龟
