买的中秋礼物分配出现问题?不存在的!这款Python小脚本帮大忙啦
导语
每次回家小编的身边都会聚集着一堆小朋友,这就是家住一个村的好处。
一回家就接收到七大姑八大姨的亲切的问候,关系那是特别不错的,小朋友也不怕我。
去年因为给小朋友带了一些礼物但是分配不均匀,导致了灾难现场哭声一片......
我老妈还以为我咋的她们了?
emmmmmm,完了我只想说一句,“打扰了”
今年中秋怕家里的小孩子们因为分配礼物重蹈覆辙,聪明的我制作了一款中秋礼物刮刮乐,刮到什么就拿什么!
正文
中秋送给孩子们的礼物已经选好了,当当当图片如下:
一堆中秋月饼的毛绒公仔玩偶~应该还行撒。
随机读取一张图片:
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()
总结
好了!文章就到这里了哈!如需完整打包好的的源码+素材记得老地方。
源码基地群:【私信小编06】即可免费领取哦!!
你们的支持点赞是我最大的动力,记得三连哦~
转载自:https://juejin.cn/post/7006524174323679246