如何在pygame中为我的角色增加加速度? - python

我是python和pygame的新手,我需要有关加速的帮助。我遵循了youtube上有关如何为平台游戏打基础的教程,并且我一直在使用它来创建像柯比游戏一样玩的游戏。我在Kirby游戏中注意到的微小细节之一是,当您向一个方向移动时,他如何打滑,然后迅速转向另一个方向,在过去的几天中,我一直在研究如何使其工作。我想出的解决方案是做到这一点,而不是每次按下按键时角色都会移动,一旦达到最大速度,角色就会加速,然后停止加速,然后再次按下则迅速减速并再次加速方向键。问题是,我不知道如何编程加速。谁能帮我这个?

这是我为游戏编写的代码(第一位用于碰撞,第二位用于实际移动玩家):

def move(rect, movement, tiles):
collide_types = {'top': False, 'bottom': False, 'right': False, 'left': False}
rect.x += movement[0]
hit_list = collide_test(rect, tiles)
for tile in hit_list:
    if movement[0] > 0:
        rect.right = tile.left
        collide_types['right'] = True
    if movement[0] < 0:
        rect.left = tile.right
        collide_types['left'] = True
rect.y += movement[1]
hit_list = collide_test(rect, tiles)
for tile in hit_list:
    if movement[1] > 0:
        rect.bottom = tile.top
        collide_types['bottom'] = True
    if movement[1] < 0:
        rect.top = tile.bottom
        collide_types['top'] = True

return rect, collide_types

第二位:

player_move = [0, 0]
if move_right:
    player_move[0] += 2.5
elif run:
    player_move[0] += -3
if move_left:
    player_move[0] -= 2
elif run:
    player_move[0] -= -3
player_move[1] += verticle_momentum
verticle_momentum += 0.4
if verticle_momentum > 12:
    verticle_momentum = 12
elif slow_fall == True:
    verticle_momentum = 1

if fly:
    verticle_momentum = -2
    slow_fall = True
    if verticle_momentum != 0:
        if ground_snd_timer == 0:
            ground_snd_timer = 20

参考方案

而不是直接更改按钮位置时字符的位置,而应该更改速度。
因此,例如,仅沿X轴移动:

acc = 0.02 # the rate of change for velocity
if move_right:
    xVel += acc 
if move_left:
    xVel -= acc 

# And now change your character position based on the current velocity
character.pose.x += xVel 

您还可以添加其他内容:做到这一点,当您不按任何键时就会失去动力,因此可以停下来。您可以通过减去速度或在其中添加一定的衰减因子(小于您的加速度常数,但是在进行游戏实验时必须对其进行调整)来实现。

R'relaimpo'软件包的Python端口 - python

我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。

如何用'-'解析字符串到节点js本地脚本? - python

我正在使用本地节点js脚本来处理字符串。我陷入了将'-'字符串解析为本地节点js脚本的问题。render.js:#! /usr/bin/env -S node -r esm let argv = require('yargs') .usage('$0 [string]') .argv; console.log(argv…

Python:传递记录器是个好主意吗? - python

我的Web服务器的API日志如下:started started succeeded failed 那是同时收到的两个请求。很难说哪一个成功或失败。为了彼此分离请求,我为每个请求创建了一个随机数,并将其用作记录器的名称logger = logging.getLogger(random_number) 日志变成[111] started [222] start…

Python-Excel导出 - python

我有以下代码:import pandas as pd import requests from bs4 import BeautifulSoup res = requests.get("https://www.bankier.pl/gielda/notowania/akcje") soup = BeautifulSoup(res.cont…

Python sqlite3数据库已锁定 - python

我在Windows上使用Python 3和sqlite3。我正在开发一个使用数据库存储联系人的小型应用程序。我注意到,如果应用程序被强制关闭(通过错误或通过任务管理器结束),则会收到sqlite3错误(sqlite3.OperationalError:数据库已锁定)。我想这是因为在应用程序关闭之前,我没有正确关闭数据库连接。我已经试过了: connectio…