简体   繁体   English

你如何使用dx?

[英]How do you use dx?

I have a slight issue with my code:我的代码有一个小问题:

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)

As you can see, when you run the code the "jump right" function works just fine.如您所见,当您运行代码时,“向右跳转”function 运行正常。 The jump left one?跳左一? not so much.没那么多。 I have tried solving this by trying tons of different possible combinations but none seem to work.我已经尝试通过尝试大量不同的可能组合来解决这个问题,但似乎都没有用。 All I want in the game is the jump right function but instead, it jumps left when the left arrow key is pressed.我在游戏中想要的只是向右跳 function 但相反,当按下左箭头键时它向左跳。 I was given a challenge to make a full game with only turtle but I don't know if i'll be able to continue anymore.我收到了一个挑战,要只用乌龟制作一个完整的游戏,但我不知道我是否还能继续下去。

Thanks so much in advance!非常感谢!

PS I'm using python 3 PS 我正在使用 python 3

dx and dy are the amount of change in position. If dx is 2, it means to move right 2 repeatedly for a certain amount of time. dxdy是position中的变化量,如果dx为2,表示在一定时间内重复右移2。 Also, dy is the amount of change in the up and down position. You implement it by adding or subtracting, not multiplying the amount of change.还有, dy是上下position的变化量,你通过加减来实现,而不是乘以变化量。 Multiplying will change the amount of change in position very abruptly, multiplying by a negative number means changing direction.乘法会很突然地改变position的变化量,乘以一个负数意味着改变方向。 Below is the modified code for your code.以下是您的代码的修改后的代码。

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)

Or if you want a faster moving and jumping,或者如果你想要更快的移动和跳跃,

def jump_right():
    cup.dx = 3
    cup.dy = 3


def jump_left():
    cup.dx = -3
    cup.dy = 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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