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 realize raffle scratch Card by Python

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

Share

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

This article mainly introduces Python how to achieve raffle scratch card, the article is very detailed, has a certain reference value, interested friends must read it!

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

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) these are all the contents of the article "how to realize raffle scratch cards in Python". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

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