Python develops Yuanshen Scratch script, random self-made blind box cards

Hi everyone, hello! I am cheese ❤

Recently also playingGenshinMore bewildered

I didn’t buy any of the surrounding blind boxes.

It's too expensive to buy...

Don't buy it, it's always itchy...

then! ! !

I just made myself a scratch-off

Essentially similar to a blind box

Whichever one is scratched counts which one hahaha

That's all for the gossip,

start! ! !

insert image description here

insert image description here

preparation module

import os
import sys
import random
import pygame

Prepare materials

Music material preparation

insert image description here

Picture material preparation

insert image description here

start code

Define necessary constants, such as canvas, material path, etc.

BGMPATH = 'music/yuansheng.mp3'
IMAGEDIR = 'pictures'
SUPPORTEXTS = ['jpg', 'png', 'bmp']
SCREENSIZE = (600, 600)
WHITE = (255, 255, 255, 27)
GRAY = (192, 192, 192)

Read a random image

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)

main program

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
	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()
		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]:
		screen.blit(image_used, (0, 0))
		screen.blit(surface, (0, 0))
		pygame.display.update()

main loop runs

if __name__ == '__main__':
	main()

insert image description here

That's it for today's article~

I am Cheese, see you in the next article~

insert image description here

Guess you like

Origin blog.csdn.net/m0_74872863/article/details/130209738