Introduction:

Friends ~

Is it the weekend again?

Did you miss me? I missed you

Today, in my last hour at work,

I wrote you a “scratch-off prize”. It will be better if your boss gives you a scratch-off prize in your leisure time

Today I’m going to give you a prize for scratching random objects

The body of the

(1) Environmental installation:

**Python**** version: **3.6.4; Pygame module; And some modules that come with Python.

(2) Picture of scratch card prepared first:

If it is your boss holiday gift can be directly into the raffle, the picture is randomly generated. First prize, second prize what!! Free play ~~~~

Attach code to read a picture randomly:

Def readImageRandomly(): filenames = os.listdir(IMAGEDIR) filenames = [f for f in filenames if f.split('.')[-1] in SUPPORTEXTS] imgpath = os.path.join(IMAGEDIR, random.choice(filenames)) return pygame.transform.scale(pygame.image.load(imgpath), SCREENSIZE)Copy the code

(3) MusC is indispensable:

(4) Effect drawing:

Complete code attached:

Import OS Import sys Import Random Import PyGame "" define the necessary constants" "BGMPATH = 'music/ BGm.mp3 'IMAGEDIR = 'pictures' SUPPORTEXTS = ['jpg', 'png', 'bmp'] SCREENSIZE = (800, 600) WHITE = (255, 255, 255, 27) GRAY = (192, 192, 192) "" def readImageRandomly(): filenames = os.listdir(IMAGEDIR) filenames = [f for f in filenames if f.split('.')[-1] in SUPPORTEXTS] imgpath = os.path.join(IMAGEDIR, random.choice(filenames)) return pygame.transform.scale(pygame.image.load(imgpath), SCREENSIZE) "" def main(): pygame.init() pygame.mixer.init() pygame.mixer.music.load(BGMPATH) pygame.mixer.music.play(-1, 0.0) PyGame.mouse.set_cursor (* Pygame.cursors. Diamond) Screen = Pygame.display.set_mode (SCREENSIZE) Pygame.display. Set_caption (' Scratch off! 959755565 ') surface = pygame.Surface(SCREENSIZE).convert_alpha() surface.fill(GRAY) image_used = readImageRandomly() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit(-1) mouse_event_flags = pygame.mouse.get_pressed() if mouse_event_flags[0]: pygame.draw.circle(surface, WHITE, pygame.mouse.get_pos(), 40) elif mouse_event_flags[-1]: surface.fill(GRAY) image_used = readImageRandomly() screen.blit(image_used, (0, 0)) screen.blit(surface, (0, 0)) pygame.display.update() '''run''' if __name__ == '__main__': main()Copy the code

conclusion

All right! A scratch card small project is finished, very simple code is not long, try it

You can also scrape a lot of interesting little things oh: such as scraping facial expressions, scraping lottery activities, scraping……………

Source code base: #959755565

Ps: specially for the lazy little partner set ha ha ha! Those who are too lazy to type code go straight to group Naza