为了账号安全,请及时绑定邮箱和手机立即绑定

我如何更改代码以使播放器停在边缘而不是环绕?

我如何更改代码以使播放器停在边缘而不是环绕?

慕姐8265434 2023-01-04 16:32:19
WIDTH = 800HEIGHT = 500background = Actor("background")player = Actor("player")player.x = 200player.y = 200def draw():screen.clear()background.draw()player.draw()def update():if keyboard.right:    player.x = player.x + 4if keyboard.left:    player.x = player.x - 4if keyboard.down:    player.y = player.y + 4if keyboard.up:    player.y = player.y - 4if player.x > WIDTH:    player.x = 0if player.x < 0:    player.x = WIDTHif player.y < 0:    player.y = HEIGHTif player.y > HEIGHT:    player.y = 0我想让玩家停在边缘而不是环绕并传送到另一边。帮助将不胜感激。
查看完整描述

2 回答

?
梦里花落0921

TA贡献1772条经验 获得超6个赞

你把它弄错了:


if player.x > WIDTH:

    player.x = WIDTH

if player.x < 0:

    player.x = 0

if player.y < 0:

    player.y = 0

if player.y > HEIGHT:

    player.y = HEIGHT


查看完整回答
反对 回复 2023-01-04
?
犯罪嫌疑人X

TA贡献2080条经验 获得超4个赞

已经有了答案,但我认为这段代码会更有效率。


def update():

 if keyboard.right and player.x<=WIDTH-4:

  player.x = player.x + 4

 if keyboard.left and player.x>=4:

  player.x = player.x - 4

 if keyboard.down and player.y<=HEIGHT-4:

  player.y = player.y + 4

 if keyboard.up and player.y>=4:

  player.y = player.y - 4


查看完整回答
反对 回复 2023-01-04
  • 2 回答
  • 0 关注
  • 100 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信