如何使边界在海龟中发挥作用 - python
因此,我正在制作一款游戏,其中我需要移动一个玩家,该玩家目前只是一个正方形,但如果我继续按该键,它可以离开屏幕。我想在屏幕末尾停止播放器。 这是我的代码,这不是完整的游戏:
import turtle
sc=turtle.Screen()
sc.title("Math fighter")
sc.bgcolor("black")
sc.setup(width=1000, height=600)
player=turtle.Turtle()
player.speed(0)
player.shape("square")
player.color("white")
player.shapesize(stretch_wid=2, stretch_len=3)
player.penup()
player.goto(0, -250)
def playerleft():
x = player.xcor()
x -= 20
player.setx(x)
def playerright():
y = player.xcor()
y += 20
player.setx(y)
sc.listen()
sc.onkeypress(playerright, "Right")
sc.onkeypress(playerleft, "Left")
So, I was making a game in which I need to move a player which is just a square as of now but it can go out of the screen if I keep pressing the key. I want to stop the player at the end of the screen.
Here is my code this is not the complete game:
import turtle
sc=turtle.Screen()
sc.title("Math fighter")
sc.bgcolor("black")
sc.setup(width=1000, height=600)
player=turtle.Turtle()
player.speed(0)
player.shape("square")
player.color("white")
player.shapesize(stretch_wid=2, stretch_len=3)
player.penup()
player.goto(0, -250)
def playerleft():
x = player.xcor()
x -= 20
player.setx(x)
def playerright():
y = player.xcor()
y += 20
player.setx(y)
sc.listen()
sc.onkeypress(playerright, "Right")
sc.onkeypress(playerleft, "Left")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只需要在
setx()
之前使用if
检查位置并跳过它。就这样。You need only use
if
to check position beforesetx()
and skip it. That's all.