【问题标题】:pygame- place picture in random location on the screenpygame-将图片放置在屏幕上的随机位置
【发布时间】:2015-08-06 07:26:20
【问题描述】:

我会解释一些程序。 我正在尝试制作蛇游戏。 我的问题是蛇总是从屏幕的角落开始,这是蛇应该死的地方.. 我想将“图像”(图像是照片的名称)放在屏幕上的随机位置。 我该怎么做?我尝试了几次,但程序被卡住了...... 这是代码..

  import sys, pygame,time
  FPS=30
  fpsClock=pygame.time.Clock()
  window_size = ( 819, 460 )

  white = ( 255, 255, 255 )
  screen = pygame.display.set_mode( window_size )


  move = (0,0) # init movement

  done = False

  image = pygame.image.load( 'snikebodydraw.png')
  image1 = pygame.image.load( 'deadarea.png')
  screen.blit( image, (100,100) )
  rect = image.get_rect()


  while not done:
    for event in pygame.event.get():
      if event.type == pygame.QUIT:
        sys.exit()

      if event.type == pygame.KEYDOWN:


        if event.key == pygame.K_LEFT:
          move = (-10, 0 )
        if event.key == pygame.K_RIGHT:
          move = ( 10, 0 )
        if event.key == pygame.K_UP:
          move = ( 0,-10 )
        if event.key == pygame.K_DOWN:
          move = ( 0, 10 )

    rect = rect.move(move)

    screen.blit(image1, (0,0))
    screen.blit( image, rect )

更多信息: http://www.siz.co.il/my.php?i=zlznmy3imumj.png

【问题讨论】:

    标签: python random position pygame


    【解决方案1】:

    pygame.display 假定显示器的位置 (0,0) 位于左上角,因此当您定位图像时,可能是在位置 (0,0) 处进行。你应该检查你在player.rect中的位置不是(0,0)当调用时:

      screen.blit( player.image, player.rect )
    

    如果您想将image 定位在屏幕的随机位置,您可以使用random 库生成两个随机数(xy)并将它们传递给第二个参数blit,例如在您的情况下(考虑到您的显示尺寸为 819x460):

      screen.blit( player.image, (random.randint(0,819),random.randint(0,460)) )
    

    更新: 如果您想从位置 (100,100) 开始,我认为您的变量 rect 有问题,因为代码上的行 rect = image.get_rect() 返回 rect=(0,0,50,50) 我认为是左上角和右下角你的形象。因此,当您稍后执行 rect = rect.move(move) 时,rect 的前两个值是 (0,0),它们将传递给 blit(),然后您的图像在循环的第一次迭代中回到位置 (0,0)。

    一个可能的解决方案是在您的rect = image.get_rect() 下方添加rect = rect.move(100,100),然后再进入循环。这将使其覆盖初始 (0,0) 值,就像这样:

    rect = image.get_rect()
    rect = rect.move(100,100)
    

    尝试解决这个问题,您的问题就会解决。

    您还必须考虑到,当您执行blit() 时,您定位的图像的点通常是图像的左上角。 例如,如果你这样做:

    # To center the point (0,0) of the image at the location (0,0) of the screen
    screen.blit(image,(0,0))
    # To center the point (0,0) of the image at the location (100,100) of the screen
    screen.blit(image,(100,100))
    

    因此,在定位图像时也要考虑到这一点。

    【讨论】:

    • 我编辑了帖子...它仍然不适合我..我稍后会随机进行,这不是问题。但是我怎样才能让蛇从位置(100,100)开始?
    • 你应该在你的while循环中有一行pygame.display.update()pygame.display.flip()(在调用blit()之后)来更新显示的新位置。如果您没有这些,则不会在显示屏上看到更改。
    • 我在程序启动时放了一个代码图片的链接。可以在代码末尾看到
    • 我通过在 screen.blit(image,(100,100)) 之后插入 pygame.display.update() 来尝试您的代码,并且我的图像显示在位置 (100,100)。这应该适用于您从 (100,100) 开始。
    • 见上面我的答案更新与您的代码问题。
    【解决方案2】:

    回答(仅)如何选择随机起始位置的问题:

    import random
    
    x = random.randint(0,xmax)
    y = random.randint(0,ymax)
    
    screen.blit(player.image, (x,y))
    

    在进入while 循环之前绘制一次启动配置,这样您就不必更改更新过程。

    【讨论】:

    • 我编辑了帖子...它仍然不适合我..我稍后会随机进行,这不是问题。但是我怎样才能让蛇从位置(100,100)开始?
    • 究竟是什么不起作用?尝试提供更多细节。此外,如果您可以提供您正在使用的图像,它会非常方便。我自己只是用一张图片试了一下,确实出现在指定的位置,所以这部分好像可以了。
    • 我在程序启动时放了一个代码图片的链接。您可以在代码末尾看到它。
    【解决方案3】:

    就像很多人在 cmets 中所说的那样,在 blitting 之后需要 pygame.display.flip()pygame.display.update()。这将使用上面的图像更新您的屏幕。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-14
      • 1970-01-01
      相关资源
      最近更新 更多