如何使用turtle模块创建一个环形空间?

huangapple go评论68阅读模式
英文:

How to make a toroidal space with the turtle module?

问题

这是你的代码:

import turtle as trtl
import keyboard
import math

screen = trtl.Screen()
screen.listen()
screen.setup(width=960, height=600)
screen.bgpic("C:/Users/alfon/OneDrive/Pictures/BGBW.png")

Point = trtl.Turtle()
trtl.register_shape("C:/Users/alfon/Downloads/BWCharacter.gif")
Point = trtl.Turtle("C:/Users/alfon/Downloads/BWCharacter.gif")
Point.penup()

def MH(Theta):    #MH stands more Move \[Towards\] Heading
    x, y = Point.position()
    dx = 5*math.cos(math.radians(Theta))
    dy = 5*math.sin(math.radians(Theta))
    x = x+dx
    y = y+dy
    Point.setposition(x, y)

while True:
    while keyboard.is_pressed("d") and not(keyboard.is_pressed("s") or keyboard.is_pressed("w")):
        MH(0)
    while keyboard.is_pressed("d") and keyboard.is_pressed("w"):
        MH(45)
    while keyboard.is_pressed("w") and not(keyboard.is_pressed("d") or keyboard.is_pressed("a")):
        MH(90)
    while keyboard.is_pressed("w") and keyboard.is_pressed("a"):
        MH(135)
    while keyboard.is_pressed("a") and not(keyboard.is_pressed("w") or keyboard.is_pressed("s")):
        MH(180)
    while keyboard.is_pressed("a") and keyboard.is_pressed("s"):
        MH(225)
    while keyboard.is_pressed("s") and not(keyboard.is_pressed("a") or keyboard.is_pressed("d")):
        MH(270)
    while keyboard.is_pressed("s") and keyboard.is_pressed("d"):
        MH(315)
    if Point.xcor() < -500:
        Point.hideturtle()
        Point.setpos(500, Point.ycor())
        Point.showturtle()
    if Point.xcor() > 500:
        Point.hideturtle()
        Point.setpos(-500, Point.ycor())
        Point.showturtle()
    if Point.ycor() < -320:
        Point.hideturtle()
        Point.setpos(320, Point.ycor())
        Point.showturtle()
    if Point.ycor() > 320:
        Point.hideturtle()
        Point.setpos(-320, Point.ycor())
        Point.showturtle()

如果你有其他问题或需要进一步的帮助,请告诉我。

英文:

I'm trying to make a program in which a turtle moves around the plane commanded by the user, I'm also trying to make it so that whenever it reaches an end of the screen, it gets teleported to the opposite end. This is my code:

import turtle as trtl
import keyboard
import math
screen = trtl.Screen()
screen.listen()
screen.setup(width=960, height=600)
screen.bgpic(&quot;C:/Users/alfon/OneDrive/Pictures/BGBW.png&quot;)
Point = trtl.Turtle()
trtl.register_shape(&quot;C:/Users/alfon/Downloads/BWCharacter.gif&quot;)
Point = trtl.Turtle(&quot;C:/Users/alfon/Downloads/BWCharacter.gif&quot;)
Point.penup()
def MH(Theta):    #MH stands more Move \[Towards\] Heading
x, y = Point.position()
dx = 5*math.cos(math.radians(Theta))
dy = 5*math.sin(math.radians(Theta))
x = x+dx
y = y+dy
Point.setposition(x, y)
while True:
while keyboard.is_pressed(&quot;d&quot;) and not(keyboard.is_pressed(&quot;s&quot;) or keyboard.is_pressed(&quot;w&quot;)):
MH(0)
while keyboard.is_pressed(&quot;d&quot;) and keyboard.is_pressed(&quot;w&quot;):
MH(45)
while keyboard.is_pressed(&quot;w&quot;) and not(keyboard.is_pressed(&quot;d&quot;) or keyboard.is_pressed(&quot;a&quot;)):
MH(90)
while keyboard.is_pressed(&quot;w&quot;) and keyboard.is_pressed(&quot;a&quot;):
MH(135)
while keyboard.is_pressed(&quot;a&quot;) and not(keyboard.is_pressed(&quot;w&quot;) or keyboard.is_pressed(&quot;s&quot;)):
MH(180)
while keyboard.is_pressed(&quot;a&quot;) and keyboard.is_pressed(&quot;s&quot;):
MH(225)
while keyboard.is_pressed(&quot;s&quot;) and not(keyboard.is_pressed(&quot;a&quot;) or keyboard.is_pressed(&quot;d&quot;)):
MH(270)
while keyboard.is_pressed(&quot;s&quot;) and keyboard.is_pressed(&quot;d&quot;):
MH(315)
if Point.xcor() \&lt; -500:
Point.hideturtle()
Point.setpos(500, Point.ycor())
Point.showturtle()
if Point.xcor() \&gt; 500:
Point.hideturtle()
Point.setpos(-500, Point.ycor())
Point.showturtle()
if Point.ycor() \&lt; -320:
Point.hideturtle()
Point.setpos(320, Point.ycor())
Point.showturtle()
if Point.ycor() \&gt; 320:
Point.hideturtle()
Point.setpos(-320, Point.ycor())
Point.showturtle()

The moving the turtle parts works fine, and it is also taken to the other side of the screen, however, this only happens once I release the key that I was moving the turtle with, I can see why it happens, but I don't know how to fix it. I'm looking for continuity in the way the turtle moves, this is, I don't want to have to stop when the turtle gets to the other side.

I tried making a boolean variable for OB (Out of Bounds) that would stop the while loops for movement when OB==True but that didn't work

答案1

得分: 1

你只需要使用取模运算符与屏幕尺寸一起使用,所以 x = (x + speed_x * dt) % SIZE,这个运算符会给出除以屏幕尺寸的余数,这正是你在环绕处理中所需要的。这是2D游戏开发中众所周知的技巧。

具体到你的代码中:

x = x + dx
y = y + dy

应该变成:

x = (x + dx) % WIDTH
y = (y + dy) % HEIGHT
英文:

You just need to use the modulo operator with the size of the screen so x = (x + speed_x * dt) % SIZE, this operator gives the remainder of the division with the screen size that is exactly what you need for a wrap around. This is a well-known trick in 2d game-development.


Specifically in your code:

x = x+dx
y = y+dy

should become:

x = (x+dx) % WIDTH
y = (y+dy) % HEIGHT

huangapple
  • 本文由 发表于 2023年5月14日 05:13:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76244902.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定