Pygame,角色移动速度 [英] Pygame, Character Movement Speed

查看:112
本文介绍了Pygame,角色移动速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是犹他大学的一名学生,正在 Pygame 中进行一个项目,但我遇到了一些小问题.我已经编写了整个游戏,它运行得很好,但我希望我的角色在走过某些地形时能减慢移动速度.例如,如果他/她走过一块沙地砖,我希望他/她的速度减半.由于我仍在学习,因此我无法自己解决这个问题.我的代码的链接如下.任何帮助将不胜感激!

I am a student at the University of Utah and am working on a project in Pygame that I am having minor trouble with. I have coded the entirety of the game, and it runs perfectly, but I would like my character to slow movement when walking over certain terrain. For example, if he/she walks over a sand tile than I would like for his/her speed to cut in half. I have not been able to figure this out on my own as I am still learning. The link to my code is below. Any help would be much appreciated!

我相信解决方案将包含在这些代码行中:

I believe the solution will come within these lines of code:

if keys[pygame.K_LEFT]:
    is_facing_left = True
    movement_x -= tile_rect.width
    mapx -= 1
if keys[pygame.K_RIGHT]:
    is_facing_left = False
    movement_x += tile_rect.width
    mapx += 1
if keys[pygame.K_UP]:
    movement_y -= tile_rect.height
    mapy -= 1
if keys[pygame.K_DOWN]:
    movement_y += tile_rect.height
    mapy += 1

if mapx < 0:
    mapx = 0
    movement_x = 0
if mapx > world.get_width()-1 - map_tile_width:
    mapx = world.get_width()-1 - map_tile_width
    movement_x = 0
if mapy < 0:
    mapy = 0
    movement_y = 0
if mapy > world.get_height()-1 - map_tile_height:
    mapy = world.get_height()-1 - map_tile_height
    movement_y = 0

如果您想查看完整代码,请在此处查看:

The full code is here if you'd like to see it:

https://github.com/DanPatWils/AdventureGame/blob/master/Almost

推荐答案

您有多行 mapx -= 1 形式.你可以在这个循环之外使用一个变量来跟踪你的速度,然后你可以使用 mapx -= current_speed 来移动你的角色.然后您可以根据不同的条件修改您的速度.

You have multiple lines of the form mapx -= 1. You could use a variable outside of this loop that tracks your speed, and then you can use mapx -= current_speed to move your character. You can then modify your speed according to different conditions.

你应该确保你的游戏的事件循环和地图大小足够大,这样你的角色就不会看起来像是在传送而不是高速运行.

You should ensure your game's event loop and the map size are large enough, so that it does not look like your character is teleporting rather than running at high speed.

这篇关于Pygame,角色移动速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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