|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import random |
| 4 | +import pygame |
| 5 | + |
| 6 | +path = 'prize' |
| 7 | +ptype = ['jpg', 'png', 'bmp', 'JPG', 'PNG', 'BMP'] |
| 8 | +# 窗口大小 |
| 9 | +screen_size = (600, 400) |
| 10 | +white = (255, 255, 255, 20) |
| 11 | +gray = (192, 192, 192) |
| 12 | +pygame.init() |
| 13 | +pygame.mouse.set_cursor(*pygame.cursors.diamond) |
| 14 | +screen = pygame.display.set_mode(screen_size) |
| 15 | +pygame.display.set_caption('刮一刮抽奖') |
| 16 | +surface = pygame.Surface(screen_size).convert_alpha() |
| 17 | +surface.fill(gray) |
| 18 | +filenames = os.listdir(path) |
| 19 | +filenames = [f for f in filenames if f.split('.')[-1] in ptype] |
| 20 | +imgpath = os.path.join(path, random.choice(filenames)) |
| 21 | +image_used = pygame.transform.scale(pygame.image.load(imgpath), screen_size) |
| 22 | +while True: |
| 23 | + for event in pygame.event.get(): |
| 24 | + if event.type == pygame.QUIT: |
| 25 | + pygame.quit() |
| 26 | + sys.exit(-1) |
| 27 | + mouse_event = pygame.mouse.get_pressed() |
| 28 | + if mouse_event[0]: |
| 29 | + pygame.draw.circle(surface, white, pygame.mouse.get_pos(), 40) |
| 30 | + elif mouse_event[-1]: |
| 31 | + surface.fill(gray) |
| 32 | + image_used = pygame.transform.scale(pygame.image.load(imgpath), screen_size) |
| 33 | + screen.blit(image_used, (0, 0)) |
| 34 | + screen.blit(surface, (0, 0)) |
| 35 | + pygame.display.update() |
| 36 | + |
| 37 | + |
0 commit comments