Python选择特定区间的随机数

所以我正在制作一个棒球从上面掉下来的 PyGame,底部的用户必须接住球。球以随机速度下落,但我很难让球以不同速度下落。


例如,我当前的球代码是:


def update(self):

    if self.is_falling:

        """Move the ball down."""

        self.y += random.randint(10, 200) / 100

        self.rect.y = self.y

在这里,当我运行程序时,球以不同的速度下落,但几乎没有什么不同。如果我将数字更改为 (10, 20000) / 100,那么每个球都会掉得非常快。原因是什么?似乎随机数不是那么随机。我使用的功能有误吗?


我想让每个球以非常不同的速度下降,例如一个非常快,另一个非常慢。而且我希望它是随机数,以便用户可以以多种不同的速度进行游戏...


我想知道,是否有可以在生成的随机数之间设置特定间隔的随机函数?或者我应该尝试不同的方法?如果是这样,我该怎么做?


我是 Python 的初学者,所以如果你能尽可能简单地解释它,那将不胜感激!


慕运维8079593
浏览 267回答 3
3回答

牛魔王的故事

稍微调整一下数字,20000 与 200 相比有很大的跳跃。因为您现在得到的值是 10/100 和 200/100。这是 0.1 到 2 像素,所以我不认为它们会有很大不同。你跳到 10/100 到 20000/100,这是一个巨大的速度,大约是 200 像素上限与原始 2。所以这就是你的问题。可能是 200 到 2000 甚至 200 到 2500 的范围。你不需要像你说的球开始很快下降那样大的调整。我以前也做过类似的游戏,我可以说你只需要稍微调整一下数字。

GCT1015

如果这是 Python 2,则有问题random.randint(10, 200) / 100因为除法将在整数数学中完成。你应该使用random.randint(10, 200) / 100.另一个问题是您在每次更新(可能是每一帧)时选择随机步骤,这不会产生速度的错觉,而是更多的随机抖动运动。选择一个随机速度可能会更好,但至少在几帧甚至整个秋季动画中保持相同。

SMILET

部分问题是亚像素距离。我认为你的主要问题是你的y动作。看看这个方程self.y +=,有一半的时间它会导致像素距离只有一个像素。将其添加到 self.rect 时,舍入(或截断)将使小于 1 像素的数量消失。比如生成的随机整数是99,再除以100,就是0.99一个像素。在pythonint(0.99)中为零。因此,大约一半的时间,移动为零,另一半,移动只有 1 个像素,因为int(150/100)=> 1。(每约 190 个时刻中就有一个是 2 像素。)def update(self):    if self.is_falling:        """Move the ball down."""        self.y += random.randint(10, 200) / 100        self.rect.y = self.y同样正如@6502 指出的那样,这将产生生涩的随机运动。最好在 class 中生成每次更新像素的速度__init__,并坚持下去。def __init__( self, ... ):    ...    self.fall_speed = random.randint(10, 200)   # pixels per updatedef update(self):    if self.is_falling:        """Move the ball down."""        self.y += self.fall_speed        self.rect.y = self.y我喜欢根据实时计算让事情发生变化。这需要对象速度和帧间时间来计算对象移动了多少。我喜欢这个,因为如果你想在程序中添加重力(或其他),很容易计算新的位置。class FallingSprite( pygame.sprite.Sprite ):    """ A falling sprite.  Falls at a constant velocity in real-time """    def __init__( self, image ):        pygame.sprite.Sprite.__init__( self )        self.image       = image        self.rect        = self.image.get_rect()        self.fall_speed  = random.randint(10, 200) # pixels / second        self.last_update = int( time.time() * 1000.0 )        self.rect.center = ( random.randrange( 0, WINDOW_WIDTH ), 0 )      def update( self ):        # There may have been movement since the last update        # so calculate the new position (if any)        if ( self.fall_speed > 0 ):            time_now    = int( time.time() * 1000.0 )            time_change = time_now - self.last_update      # How long since last update?            if ( time_change > 0 ):                distance_moved   = time_change * self.fall_speed / 1000                now_x, now_y     = self.rect.center        # Where am I, right now                updated_y        = now_y + distance_moved                # Did we fall off the bottom of the screen?                if ( updated_y > WINDOW_HEIGHT ):                    # sprite disappears                    self.kill()                else:                    self.rect.center = ( now_x, updated_y )                    self.last_update = time_now
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python