繁体   English   中英

python pygame:在当前鼠标单击时绘制加载的图像 position

[英]python pygame: Draw loaded image at current mouse click position

我正在尝试在当前鼠标单击 pygame 中的 position 时显示加载的图像...请帮助我...我做错了什么?

import pygame

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

FPS = 25  # for more than 220 it has no time to update screen

BLACK = (0, 0, 0)
RED = (255, 0, 0)

pygame.init()


def main():

    screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
    screen_rect = screen.get_rect()
    image = pygame.image.load('images/explo_trans.png').convert()
    image_rect = image.get_rect()
    all_items = []
    clock = pygame.time.Clock()
    running = True
    while running:

        current_time = pygame.time.get_ticks()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

            elif event.type == pygame.KEYUP:
                if event.key == pygame.K_ESCAPE:
                    running = False

            elif event.type == pygame.MOUSEBUTTONDOWN:
                x, y = event.pos
                image_rect.x = x
                image_rect.y = y
                item = {
                    'rect': image_rect,
                    'remove_at': current_time + 500,  # 500ms = 0.5s
                    }
                all_items.append(item)
                print("add at:", x, y)

        keep_items = []

        for item in all_items:
            if item['remove_at'] > current_time:
                keep_items.append(item)
            else:
                print('removed:', item)

        all_items = keep_items

        screen.fill(BLACK)

        for item in all_items:
            #pygame.draw.rect(screen, item['rect'])
            screen.blit(item['rect'], (x, y))

        pygame.display.flip()

        ms = clock.tick(FPS)
        # pygame.display.set_caption('{}ms'.format(ms)) # 40ms for 25FPS, 16ms for 60FPS
        fps = clock.get_fps()
        pygame.display.set_caption('FPS: {}'.format(fps))

    pygame.quit()


if __name__ == "__main__":
    main()

pygame.Surface.blit的第一个参数是图像( pygame.Surface ),第二个参数是 position:

screen.blit(item['rect'], (x, y))

screen.blit(image, item['rect'])

另请参阅使用 pygame 绘制图像的好方法是什么?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM