本篇文章为大家展示了如何使用python制作刮刮卡来解决月饼分配不均,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
正文
中秋送给孩子们的礼物已经选好了,当当当图片如下:
一堆中秋月饼的毛绒公仔玩偶~应该还行撒。
随机读取一张图片:
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)
主函数:
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('中秋月饼毛绒公仔—源码基地:#959755565#')
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()
效果图:
附代码:
'''
主题:
python中秋回家送一堆孩子的礼物
'''
import os
import sys
import random
import pygame
BGMPATH = 'music/bgm.mp3'
IMAGEDIR = 'pictures'
SUPPORTEXTS = ['jpg', 'png', 'bmp']
SCREENSIZE = (800, 600)
WHITE = (255, 255, 255, 27)
GRAY = (192, 192, 192)
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)
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('中秋月饼毛绒公仔—源码基地:#959755565#')
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()
'''run'''
if __name__ == '__main__':
main()
上述内容就是如何使用python制作刮刮卡来解决月饼分配不均,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注天达云行业资讯频道。