如何更改代码,使播放器停在边缘而不是环绕? [英] How do i change the code so that the player stops at the edges rather than wrapping around?

查看:116
本文介绍了如何更改代码,使播放器停在边缘而不是环绕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WIDTH = 800
HEIGHT = 500

background = Actor("background")
player = Actor("player")
player.x = 200
player.y = 200

def draw():
screen.clear()
background.draw()
player.draw()

def update():
if keyboard.right:
    player.x = player.x + 4
if keyboard.left:
    player.x = player.x - 4
if keyboard.down:
    player.y = player.y + 4
if keyboard.up:
    player.y = player.y - 4

if player.x > WIDTH:
    player.x = 0
if player.x < 0:
    player.x = WIDTH
if player.y < 0:
    player.y = HEIGHT
if player.y > HEIGHT:
    player.y = 0

我想让播放器停在边缘而不是环绕并传送到另一侧.帮助将不胜感激.

I want to make the player stop at the edges instead of wrapping around and teleporting to the other side. Help would be very appreciated.

推荐答案

您的解决方法不正确:

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

这篇关于如何更改代码,使播放器停在边缘而不是环绕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆