【问题标题】:Pygame blit image with mouse event带有鼠标事件的 Pygame blit 图像
【发布时间】:2017-12-21 04:57:21
【问题描述】:

我在这段代码中使用了 Pygame。这就像一个游戏,当用户点击鼠标按钮时,鼠标位置会出现一个激光图像,它会上升,并最终走出屏幕。当用户点击鼠标按钮时,我正在尝试对图像进行 blit。我使用的这段代码不起作用,我不知道为什么。我的问题从主 for 循环开始

    import pygame

    # Initialize Pygame
    pygame.init()

    #___GLOBAL CONSTANTS___
    # Define some colors
    BLACK = (0, 0, 0)
    WHITE = (255, 255, 255)
    GREEN = (0, 255, 0)
    RED = (255, 0, 0)


    # Set the height and width of the screen
    screen_width = 500
    screen_height = 500
    screen = pygame.display.set_mode([screen_width, screen_height])


    #Load Laser image of spaceship
    laser_image = pygame.image.load('laserRed16.png').convert()


    #Load sound music
    sound = pygame.mixer.Sound('laser5.ogg')


    # Loop until the user clicks the close button.
    done = False

    # Used to manage how fast the screen updates
    clock = pygame.time.Clock()


    # -------- Main Program Loop -----------
    while not done:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
            elif event.type == pygame.MOUSEBUTTONDOWN:
                # Get the current mouse position. This returns the position
                # as a list of two numbers.
                sound.play()
                #Get the mouse position 
                mouse_position = pygame.mouse.get_pos()
                mouse_x = mouse_position[0]
                mouse_y = mouse_position[1]
                # Set the laser image when the spaceship fires
                for i in range(50):
                    screen.blit(laser_image,[mouse_x + laser_x_vector,mouse_y + laser_x_vector])
                    laser_x_vector += 2
                    laser_x_vector += 2


        # Clear the screen
        screen.fill(WHITE)

        #Limit to 20 sec
        clock.tick(20)

        # Go ahead and update the screen with what we've drawn.
        pygame.display.flip()
pygame.quit()

【问题讨论】:

  • you blit 激光在fill 之前,所以fillflip 将缓冲区发送到将在显示器上显示的视频卡之前移除激光。
  • 您将遇到另一个问题 - for 移动激光的循环 - 它不会像您预期的那样工作,因为您在一帧中移动它 50 次(两次翻转之间),因此您可以在目标位置获得激光立刻。你必须在每个while not doneloop 中做一个动作——这意味着没有for
  • 如果你有事件MOUSEBUTTONDOWN那么你的鼠标位置在event.pos
  • @furas 我在“screen.blit(laser_image,[mouse_x + laser_x_vector,mouse_y + laser_x_vector])”之前删除了for循环,但它只给了我激光图像,而不是移动的激光图像跨度>
  • 正如我所说的'你会有另一个问题':) 你必须在while 循环中移动激光,但没有for i 循环。你必须在for even 循环之后做,因为MOUSEBUTTONDOWN 仅存在于单个循环/帧中 - 当按钮将状态从“未按下”更改为“按下”时,而不是当你按住它时

标签: python pygame


【解决方案1】:

blit 激光后会填满屏幕,因此激光不会出现。您应该在对激光进行 blit 之前进行填充,以便激光出现。

【讨论】:

    【解决方案2】:

    其他人已经解释了为什么您看不到激光。这是适合您的工作解决方案。首先我建议使用pygame.Rects 作为激光的位置并将它们放入一个列表中(rects 也可以用于碰撞检测)。然后在主 while 循环中迭代这些位置/矩形,更新并 blit 它们。我还向您展示了如何删除屏幕外的矩形。

    import pygame
    
    
    pygame.init()
    
    WHITE = (255, 255, 255)
    GREEN = (0, 255, 0)
    
    screen_width = 500
    screen_height = 500
    screen = pygame.display.set_mode([screen_width, screen_height])
    
    laser_image = pygame.Surface((10, 50))
    laser_image.fill(GREEN)
    done = False
    clock = pygame.time.Clock()
    laser_rects = []
    laser_velocity_y = -20
    
    while not done:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
            elif event.type == pygame.MOUSEBUTTONDOWN:
                # Turn the mouse position into a rect with the dimensions
                # of the laser_image. You can use the event.pos instead
                # of pygame.mouse.get_pos() and pass it as the `center`
                # or `topleft` argument.
                laser_rect = laser_image.get_rect(center=event.pos)
                laser_rects.append(laser_rect)
    
        remaining_lasers = []
        for laser_rect in laser_rects:
            # Change the y-position of the laser.
            laser_rect.y += laser_velocity_y
            # Only keep the laser_rects that are on the screen.
            if laser_rect.y > 0:
                remaining_lasers.append(laser_rect)
        # Assign the remaining lasers to the laser list.
        laser_rects = remaining_lasers
    
        screen.fill(WHITE)
        # Now iterate over the lasers rect and blit them.
        for laser_rect in laser_rects:
            screen.blit(laser_image, laser_rect)
    
        pygame.display.flip()
        clock.tick(30)  # 30 FPS is smoother.
    
    
    pygame.quit()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多