【问题标题】:Making jump look realistic?让跳跃看起来逼真?
【发布时间】:2015-02-15 11:48:39
【问题描述】:

这是我的程序源代码。必要时略读:

import pygame, sys
import time
pygame.init()
screen = pygame.display.set_mode([1500,750])
background = pygame.Surface(screen.get_size())
background.fill([255, 255, 255])

class Attacker(pygame.sprite.Sprite):
    def __init__(self, image_file, location):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(image_file)
        self.rect = self.image.get_rect()
        self.x_pos, self.y_pos = location
        self.rect.x, self.rect.y = location
        self.jumping = False
        self.falling = False
        self.direction = 1


    def jump(self):
        if self.y_pos > CEILING:
            self.x_pos += 1*self.direction
            self.y_pos -= 5
        else:
            self.jumping = False
            self.falling = True

        self.update_pos()

    def fall(self):
        if self.y_pos < FLOOR:
            self.x_pos += 1*self.direction
            self.y_pos += 5
        else:
            self.falling = False

        self.update_pos()

    def InvisWalls(self):
        if self.rect.left < 0:
            self.direction = -self.direction
        if self.rect.right > screen.get_width():
            self.direction = -self.direction

        self.update_pos()

    def update_pos(self):
        self.rect.x = self.x_pos
        self.rect.y = self.y_pos

FLOOR = 700
CEILING = 10

my_ball = Attacker('wackyball.bmp',[300, FLOOR])
delay = 100
interval = 50
pygame.key.set_repeat(delay, interval)



running = True
while running:
    if my_ball.jumping:
        my_ball.jump()
    if my_ball.falling:
        my_ball.fall()
    else:
        my_ball.InvisWalls()

    screen.blit(my_ball.image, my_ball.rect)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                my_ball.y_pos -= 25
            elif event.key == pygame.K_DOWN:
                my_ball.y_pos += 25
            elif event.key == pygame.K_RIGHT:
                my_ball.direction = 1
                my_ball.x_pos += 25
            elif event.key == pygame.K_LEFT:
                my_ball.direction = -1
                my_ball.x_pos -= 25
            elif event.key == pygame.K_SPACE:
                if not my_ball.falling:
                    my_ball.jumping = True
                my_ball.update_pos()
        screen.blit(background, (0, 0))
        screen.blit(my_ball.image, my_ball.rect)
        pygame.display.flip()

pygame.quit()

我的问题是,当我的球跳跃时,它会直线上升,也会直线下降。我试图让球在曲线上跳跃的尝试只是失败了,只是让跳跃更快了。怎样才能让球像真球一样在连续的曲线上上下运动?

【问题讨论】:

    标签: python-2.7 pygame pycharm


    【解决方案1】:

    我在制作基本平台游戏时遇到了类似的问题。我通过使用垂直速度变量(不跳跃时为零)来解决它。当玩家跳跃时,我将速度设置为最大值,然后在每次刷新屏幕时减小它(当它低于零时,它开始下降)。当我检测到玩家已经着陆时,我将其设置回零。

    注意:如果您愿意,可以设置最大向下速度检查,以模拟对象的“终端速度”。如果您想要一些非常酷的东西,您也可以研究“重力井”风格的设计,但这更高级一些。

    【讨论】:

    • 你能告诉我一些我可以添加/修复到我当前代码的代码吗?
    • 不幸的是我很久以前就制作了游戏,并且源文件已经丢失/损坏。从内存当按下跳转时,我将向上速度设置为最大值并将 FALLING var 设置为 TRUE。然后每个循环我都会降低速度并移动玩家那么多像素,然后检查碰撞(将玩家向后移动直到不再有碰撞)。一旦它降落 FALLIING = FALSE。应该不会太难。如果遇到困难,您还可以在 youtube 上找到一些不错的“如何制作平台游戏”教程。
    • 好的!谢谢你的提示!
    【解决方案2】:

    您的球沿直线移动,因为它具有恒定的y 速度。为了让它像真正的球一样弯曲,你需要在你的模型中加入一些物理特性。即根据球的重力加速度改变y-velocity。必要的方程是:v_y = v_y(0) - gt 其中 v_y(0) 是时间 t=0 时的初始 y 速度,g 是重力加速度(地球上为 10 ms^-2)。

    在不对代码进行重大更改的情况下,更改 self.y_pos += 5self.y_pos -= 5 行,以便在向上的过程中将越来越小的数字添加到 y 位置并减去更大的数字,而不是常量 ±5以及更多的下降。

    【讨论】:

    • 您可以添加一些代码,以便我可以将其应用到我的代码中吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-28
    • 2020-04-18
    • 2021-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多