Pygame blit图像与鼠标事件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Pygame blit图像与鼠标事件相关的知识,希望对你有一定的参考价值。

我在这段代码中使用了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()
答案

在激活激光器后填充屏幕,因此激光器不会出现。你应该先填充激光器,然后出现激光器。

另一答案

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

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

以上是关于Pygame blit图像与鼠标事件的主要内容,如果未能解决你的问题,请参考以下文章

python pygame blit。获取要显示的图像

Pygame Blitting错误

pygame.surface.blit()方法4个参数的使用方法

Pygame blit area参数?

Pygame Blit 一次?

python pygame blit 函数