Python 带有鼠标事件的Pygame blit图像

Python 带有鼠标事件的Pygame blit图像,python,pygame,Python,Pygame,我在这段代码中使用Pygame。这就像一个游戏,当用户点击鼠标按钮时,从鼠标位置会出现一个激光图像,该图像将上升,并最终离开屏幕。当用户点击鼠标按钮时,我正在尝试blit一个图像。我正在使用的代码不起作用,我不知道为什么。我的问题从主for循环开始 import pygame # Initialize Pygame pygame.init() #___GLOBAL CONSTANTS___ # Define some colors BLACK

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

在闪烁激光后填充屏幕,因此激光不会出现。您应该在闪烁激光之前填充,以便激光出现。

其他人已经解释了为什么您看不到激光。这里有一个有效的解决方案。首先,我建议使用
pygame.Rect
s作为激光器的位置,并将其放入列表中(Rect也可用于碰撞检测)。然后在主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()

blit
fill
之前激光
fill
所以
fill
flip
之前移除激光
发送缓冲到视频卡,视频卡将在监视器上显示它。你将遇到另一个问题-
移动激光的循环-
不会像你期望的那样工作,因为你在一帧内(两次翻转之间)移动它50次所以你可以让激光立刻到达目标位置。你必须在每个
循环中做一个动作,而不做
循环-这意味着没有
for
。如果你有event
MOUSEBUTTONDOWN
,那么你在
事件中有鼠标位置。pos
@furas I在“screen.blit(激光图像,[鼠标x+激光x_向量,鼠标y+激光x_向量])之前删除了for循环但是它只给了我激光图像,而不是移动的激光图像,就像我说的“你会有另一个问题”:)你必须在
循环中移动激光,而在
循环中移动激光,但在I
循环中不移动
。您必须在
之后执行偶数
循环,因为
MOUSEBUTTONDOWN
仅存在于单个循环/帧中-当按钮的状态从“未按下”更改为“按下”时,但当您按住按钮时,则不存在