This is my 15th day of the August Genwen Challenge
preface
Scratch scratch card simulation using Python, let’s have fun to start ~
Results show
The development tools
Python version: 3.6.4
Related modules:
Pygame module;
And some modules that come with Python.
Environment set up
Install Python and add it to the environment variables. PIP installs the required related modules.
Introduction of the principle
The principle is actually quite simple. Start by reading a random image from the folder:
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
Then bind the image to PyGame’s screen:
screen.blit(image_used, (0.0))
Copy the code
Then define a surface and set it to grey to overlay the image so that it is not initially visible:
surface = pygame.Surface(SCREENSIZE).convert_alpha()
surface.fill(GRAY)
screen.blit(surface, (0.0))
Copy the code
When we detect that we hold down the left mouse button and move around the screen, set the surface area where we move around the mouse to transparent white so that the picture below is visible:
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()
Copy the code
Of course, to make it easy to switch between images, we have added a feature that can randomly reload an image with a right mouse click. Organize the code so that it works:
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 -- Satellite: ilove-Python')
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()
Copy the code
That’s the end of this article, thank you for watching,Python image effects, next share Python implementation of eye shaking effects.
To thank you readers, I’d like to share some of my recent programming favorites to give back to each and every one of you in the hope that they can help you.
Dry goods mainly include:
① Over 2000 Python ebooks (both mainstream and classic books should be available)
②Python Standard Library (Most Complete Chinese version)
③ project source code (forty or fifty interesting and classic practice projects and source code)
④Python basic introduction, crawler, Web development, big data analysis video (suitable for small white learning)
⑤ A Roadmap for Learning Python
All done~ See personal homepage introduction to obtain the complete source code.