【问题标题】:Trying to blit an image at the cursor position when you left click in pygame , but the image disappears after less than a second当您在 pygame 中单击鼠标左键时,试图在光标位置对图像进行 blit,但图像在不到一秒钟后消失
【发布时间】:2021-06-14 14:30:00
【问题描述】:

我试图在 pygame 中制作一些 Minecraft 2d 克隆,并且我希望它在我左键单击时在光标位置处显示图像,但是当我测试游戏并左键单击时,图像会出现并很快消失。

代码如下:

import pygame

# Setup
pygame.init()
screen = pygame.display.set_mode((1080,720))
pygame.display.set_caption('2d minecraft')
clock = pygame.time.Clock()
logo = pygame.image.load("logo.png")
pygame.display.set_icon(logo)

# Variables
running = True
dirt = pygame.image.load("logo.png")

# Program
while running:
    # Customization
    screen.fill((110,150,200))

    # Update display
    pygame.display.update()

    # Events
    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONUP:
            screen.blit(dirt, (pygame.mouse.get_pos()))
        if event.type == pygame.QUIT:
            running = False
        pygame.display.flip()
        clock.tick(30)

【问题讨论】:

    标签: python pygame


    【解决方案1】:

    您必须在应用程序循环中绘制图像。单击鼠标时将位置添加到列表中。在应用循环的列表中的所有位置绘制图像:

    import pygame
    pygame.init()
    screen = pygame.display.set_mode((1080,720))
    pygame.display.set_caption('2d minecraft')
    clock = pygame.time.Clock()
    logo = pygame.image.load("logo.png")
    pygame.display.set_icon(logo)
    
    #Variables
    running = True
    dirt = pygame.image.load("logo.png")
    
    # list of positions
    positions = []
    
    #Program
    while running:
        #Events
        for event in pygame.event.get():
            if event.type == pygame.MOUSEBUTTONUP:
    
                # add position to list
                positions.append(event.pos)
                
            if event.type == pygame.QUIT:
                running = False
        
        screen.fill((110,150,200))
        # draw images at positions
        for pos in positions:
            screen.blit(dirt, pos)    
        pygame.display.flip()
    
        clock.tick(30)
    

    【讨论】:

      【解决方案2】:

      事件pygame.MOUSEBUTTONUP仅在您按下鼠标按钮时发生。

      这可用于处理按钮点击,但如果您想在按下鼠标时对图像进行 blit,则更喜欢使用 pygame.mouse.get_pressed()

      此函数返回对应于鼠标按钮的值列表。如果按下相应的按钮,则列表的每个值都设置为 True,否则设置为 False。

      使用此代码在鼠标位置显示一个红色矩形:

      import pygame
      from pygame.locals import *
      
      pygame.init()
      
      screen = pygame.display.set_mode((640, 480))
      
      red_rect = pygame.Surface((50, 50))
      red_rect.fill((255, 0, 0))
      
      while True:
          for event in pygame.event.get():
              if event.type == QUIT:
                  pygame.quit()
                  exit()
      
          if pygame.mouse.get_pressed()[0]: # first value of the list is the left click (check if left click pressed
              screen.blit(red_rect, pygame.mouse.get_pos())
      

      【讨论】:

      • @Cozmo 好的,Rabbid76 说得对——我不太明白你想要什么。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-26
      • 1970-01-01
      • 2011-01-21
      • 1970-01-01
      • 1970-01-01
      • 2023-01-30
      相关资源
      最近更新 更多