您好,登錄后才能下訂單哦!
本篇文章為大家展示了如何使用python制作刮刮卡來(lái)解決月餅分配不均,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
中秋送給孩子們的禮物已經(jīng)選好了,當(dāng)當(dāng)當(dāng)圖片如下:
一堆中秋月餅的毛絨公仔玩偶~應(yīng)該還行撒。
隨機(jī)讀取一張圖片:
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)
主函數(shù):
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()
上述內(nèi)容就是如何使用python制作刮刮卡來(lái)解決月餅分配不均,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。