Article \ | wild guest

Source: Python Technology “ID: pythonall”

Scratch cards often refer to a coating on the card covering numbers and letters, passwords, etc., usually including paper and electronic two types, scratch cards in the market has a relatively wide range of applications, we see the most should be all kinds of lottery activities, this article we use Python to do a simple lottery scratch cards.

## implementation

First, let’s make some pictures for the base plate, as shown below:

From the picture, we can see that the base picture includes: First prize, second prize, thank you to patronize three, if we participate in a lottery scratch card, will find that almost scraping is thank you for your help, there is a probability problem, here we also set up a simple, put a first prize, second prize three feed two, thank you for your patronage to generate scratch CARDS randomly use the reproduction. \

To achieve scratch cards, we mainly use the PyGame module, before doing small games have been used several times, we should be more familiar with, let’s look at the specific implementation.

Let’s first define constants, such as: path, picture type, color, etc., code implementation is as follows:

``````path = 'prize'
ptype = ['jpg'.'png'.'bmp'.'JPG'.'PNG'.'BMP'] # screen_size = (600.400)
white = (255.255.255.20)
gray = (192.192.192)
Copy the code``````

Then create a window, the code implementation is as follows:

``````pygame.init()
pygame.mouse.set_cursor(*pygame.cursors.diamond)
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption('Scratch off raffle')
Copy the code``````

Then take out a binding window randomly from all the base images, the code implementation is as follows:

``````filenames = os.listdir(path)
filenames = [f for f in filenames if f.split('. ') [- 1] in ptype]
imgpath = os.path.join(path, random.choice(filenames))
screen.blit(image_used, (0.0))
Copy the code``````

Then make a gray layer to cover the bottom of the figure, the code implementation is as follows:

``````surface = pygame.Surface(screen_size).convert_alpha()
surface.fill(gray)
screen.blit(surface, (0.0))
Copy the code``````

Finally, we define the mouse event. In the place where the mouse moves over, make the layer transparent and leak the bottom image. The code is as follows:

``````mouse_event = pygame.mouse.get_pressed()
if mouse_event[0]:
pygame.draw.circle(surface, white, pygame.mouse.get_pos(), 40)
elif mouse_event[- 1]:
surface.fill(gray)
Copy the code``````

Let’s take a look at the implementation:

Is there an inside smell. \

## conclusion

In this article, we implemented a simple scratch card in Python, and you can refine the implementation details, effects, and more if you are interested.

PS: Reply “Python” within the public number to enter the Python novice learning exchange group, together with the 100-day plan!

Old rules, brothers still remember, the lower right corner of the “watching” click, if you feel the content of the article is good, remember to share moments to let more people know!

[Code access ****]

Identify the qr code at the end of the text, reply: 200716