1. 程式人生 > >python幸運大抽獎互動作品,作者:李興球

python幸運大抽獎互動作品,作者:李興球

"""幸運大抽獎小程式,本作品要求安裝pygame模組才有聲音,作者:李興球,  按空格鍵進行抽獎,音樂好酷!"""

#-------------------------1、模組匯入-----------------------------

from turtle import * from random import randint import sys

#-------------------------2、螢幕初始化---------------------------

screen = Screen() screen.title("幸運大抽獎") screen.setup(800,600) screen.bgpic("轉盤.png") screen.delay(0) vertex = ((0,0),(25,0),(25,100),(50,100),(0,150),(-50,100),(-25,100),(-25,0)) #頂點表 screen.addshape("bigarrow",vertex)                        #新增大箭頭各頂點到形狀列表

#-------------------------3、載入造型列表----------------------------

sprite1_images = ["anjuli-1.gif","anjuli-2.gif","anjuli-3.gif","anjuli-4.gif","anjuli-5.gif"] sprite2_images = ["anna-1.gif","anna-2.gif","anna-3.gif"] sprite3_images = ["ballerina-a.gif","ballerina-b.gif","ballerina-c.gif","ballerina-d.gif"] sprite4_images = ["breakdancer-1.gif","breakdancer-2.gif","breakdancer-3.gif","breakdancer-4.gif"] sprite_images = [sprite1_images,sprite2_images,sprite3_images,sprite4_images] try:    [screen.addshape(image)  for images in sprite_images for image in images]       #雙重列表推導式 except:     screen.bye()     input("造型圖片丟失或損壞! 無法註冊造型到形狀列表")        sys.exit()        #--------------------------4、音訊初始化---------------------------

have_pygame = False try:     import pygame     pygame.mixer.init()     吱吱聲 = pygame.mixer.Sound("轉盤轉動(短)小.wav")   #   吱吱聲.play()     叮 = pygame.mixer.Sound("叮.wav")     pygame.mixer.music.load("眉飛色舞.wav")     pygame.mixer.music.play(-1,0)     have_pygame = True except:     print("Pygame音訊初始化錯誤或找不到音訊檔案。")      #--------------------------5、伴舞角色類----------------------------      class Sprite(Turtle):     def __init__(self,costume_list,x,y):         Turtle.__init__(self,visible=False)         self.up()         self.costume_amount = len(costume_list)          self.costume_list = costume_list     #造型列表         self.costume_index = 0               #初始造型索引         self.goto(x,y)                       #定位         self.showturtle()         self.next_costume()                  #下一個造型     def next_costume(self):         self.shape(self.costume_list[self.costume_index])         self.costume_index = self.costume_index + 1         self.costume_index = self.costume_index % self.costume_amount         screen.ontimer(self.next_costume,randint(300,500))

sprite1 = Sprite(sprite1_images,-300,200) sprite2 = Sprite(sprite2_images,300,200) sprite3 = Sprite(sprite3_images,-300,-200) sprite4 = Sprite(sprite4_images,300,-200)                        #----------------------------6、箭頭角色與旋轉--------------------------

arrow = Turtle(shape = "bigarrow") arrow.color("black","purple") numbers = randint(50,100)     #旋轉次數 angle = 30                    #每次旋轉的角度 def rotate():     global numbers,angle     screen.onkeypress(None,"space")   #取消註冊     if numbers:                       #numbers非0為真         if numbers<30:angle = numbers #形成減速效果         arrow.rt(angle)         numbers = numbers - 1         if have_pygame : 吱吱聲.play()          screen.ontimer(rotate,30)     else:                             #numbers為0就結束轉動了         numbers = randint(80,200)         angle = 30         if have_pygame :叮.play()         screen.onkeypress(rotate,"space")  #重新註冊

screen.onkeypress(rotate,"space") screen.listen() screen.mainloop() 

"""需要本作品素材請聯絡風火輪少兒程式設計李興球先生"""