中秋送禮分配不均這款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()

​總結

好瞭!文章就到這裡瞭哈!

到此這篇關於中秋送禮分配不均這款python刮刮卡完美解決問題的文章就介紹到這瞭,更多相關python 刮刮卡內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: