您好,登錄后才能下訂單哦!
本篇文章為大家展示了如何使用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制作刮刮卡來解決月餅分配不均,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。