Pygame blit图像与鼠标事件

时间:2017-12-21 04:57:22

标签: python pygame

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

    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()

2 个答案:

答案 0 :(得分:1)

在激活激光器后填充屏幕,因此激光器不会出现。你应该在激活激光器之前填充,这样就会出现激光器。

答案 1 :(得分:1)

其他人已经解释了为什么你没有看到激光。这是一个适合您的解决方案。首先,我建议使用pygame.Rect s作为激光器的位置并将它们放入一个列表中(rects也可以用于碰撞检测)。然后迭代主while循环中的这些位置/ rects,更新并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()