【问题标题】:Pygame - Issue with Randomly Generating SpritesPygame - 随机生成精灵的问题
【发布时间】:2019-03-23 22:17:28
【问题描述】:

我在随机生成精灵方面需要帮助。

这个想法是:一个头向下移动屏幕。一旦它离开屏幕,一个新的随机头开始在屏幕上向下移动。等等……

我想从中随机选择 4 张图片。

我的问题是:移动的“头部”图像只是列表中所有头部的不断闪烁循环。该函数似乎不断闪烁

以下是相关代码:

ran_head = [head1, head2, head3, head4]

def heads(img, headx, heady):
  gameDisplay.blit(img, (headx, heady))


def game_loop():
  head_width = 150
  head_height = 192
  head_startx = random.randrange(0, (display_width-150))
  head_starty = -200
  head_speed = 5

  heads(random.choice(ran_head), head_startx, head_starty)

  head_starty += head_speed

  if head_starty > display_height:
      head_starty = -200
      head_startx = random.randrange(0, (display_width-140))
      dodged += 1   #add 1 point
      head_speed += 0.5    #increase speed each time by 0.5

【问题讨论】:

标签: python-3.x random pygame sprite


【解决方案1】:

你在每一次滴答声中调用heads(random.choice(ran_head), head_startx, head_starty),所以每一次滴答声你都会blit一个随机图像。只需在循环之前调用random.choice(ran_head) 一次,并且仅在头部移出屏幕时调用,如下所示:

ran_head = [head1, head2, head3, head4]
head_img = random.choice(ran_head) # variable to store the currently choosen image

def heads(img, headx, heady):
  gameDisplay.blit(img, (headx, heady))


def game_loop():
  head_width = 150
  head_height = 192
  head_startx = random.randrange(0, (display_width-150))
  head_starty = -200
  head_speed = 5

  heads(head_img , head_startx, head_starty)

  head_starty += head_speed

  if head_starty > display_height:
      head_starty = -200
      head_startx = random.randrange(0, (display_width-140))
      dodged += 1   #add 1 point
      head_speed += 0.5    #increase speed each time by 0.5
      head_img = random.choice(ran_head) # select a new image randomly

【讨论】:

  • 非常感谢!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-07-02
  • 2014-05-23
  • 1970-01-01
  • 2021-05-23
  • 2023-03-23
  • 1970-01-01
  • 2013-06-12
相关资源
最近更新 更多