Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to make a scratch card with Python code

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces you how to use Python code to make a scratch card, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Scratch card usually refers to a coating on the card covering numbers and letter codes, usually including paper and electronic types. Scratch cards are widely used in the market, and we should see all kinds of raffle activities the most. We use Python to make a simple raffle card.

Realize

First of all, let's get some pictures as the backplane, as shown below:

From the picture, we can see that the base picture includes: the first prize, the second prize, and thank you for patronage. If we have participated in the scratch card lottery, we will find that almost all the scratch cards are thank you for patronage and so on, that is, there is a question of probability. Here we simply set up one for the first prize, two for the second prize, three for Xie Hui, and random use of the base map when generating the scratch card.

To achieve the scratch card, we mainly use the pygame module, before doing Mini Game has been used several times, we should be more familiar with, let's take a look at the specific implementation.

Let's first define constants, such as path, image type, color, and so on. The code implementation is as follows:

Path = 'prize'

Ptype = ['jpg',' png', 'bmp',' JPG', 'PNG',' BMP']

# window size

Screen_size = (600,400)

White = (255,255,255,20)

Gray = (192,192,192)

Then create a window with the following code implementation:

Pygame.init ()

Pygame.mouse.set_cursor (* pygame.cursors.diamond)

Screen = pygame.display.set_mode (screen_size)

Pygame.display.set_caption ('scratch draw')

Then a random binding to the window is taken from all the base images, and the code is implemented 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))

Image_used = pygame.transform.scale (pygame.image.load (imgpath), screen_size)

Screen.blit (image_used, (0,0))

Then make a gray layer to overlay the base image, and the code is as follows:

Surface = pygame.Surface (screen_size). Convert_alpha ()

Surface.fill (gray)

Screen.blit (surface, (0,0))

Finally, let's define the mouse event. Set the layer to be transparent and leak out the bottom image where the mouse moves over. 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)

Image_used = pygame.transform.scale (pygame.image.load (imgpath), screen_size)

Let's take a look at the results:

Does it have an internal smell.

On how to use Python code to do a scratch card to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report