資源簡介
新手通過上手Python的pygame模塊,可以更好上手學些新Python

代碼片段和文件信息
import?pygame
import?sys
import?math
import?traceback
from?pygame.locals?import?*
from?random?import?*
#球類繼承自Sprite類
class?Ball(pygame.sprite.Sprite):
?????def?__init__(selfgrayball_imagegreenball_imagepositionspeedbg_sizetarget):
??????????#初始化動畫精靈
??????????pygame.sprite.Sprite.__init__(self)
??????????self.grayball_image=pygame.image.load(grayball_image).convert_alpha()
??????????self.greenball_image=pygame.image.load(greenball_image).convert_alpha()
??????????self.rect=self.grayball_image.get_rect()
??????????#將小球放在指定位置
??????????self.rect.leftself.top=position
??????????self.speed=speed
??????????self.side=[choice([-11])choice([-11])]
??????????self.collide=False
??????????self.target=target
??????????self.control=False
??????????self.widthself.height=bg_size[0]?bg_size[1]
??????????self.radius=self.rect.width?/2?
?????def?move(self):
??????????if?self.control:
???????????????self.rect=self.rect.move(self.speed)
??????????else:
???????????????self.rect=self.rect.move(self.side[0]*self.speed[0]\
???????????????????????????????????self.side[1]*self.speed[1])
??????????#如果小球的左側出了邊界,那么將小球左側的位置改為右側的邊界
??????????#這樣便實現了從左邊進入,右邊出來的效果
??????????if?self.rect.right<=0:
???????????????self.rect.right=self.width
??????????elif?self.rect.left>=self.width:
???????????????self.rect.right=0
??????????elif?self.rect.bottom<=0:
???????????????self.rect.top=self.height
??????????elif?self.rect.top>=self.height:
???????????????self.rect.bottom=0
?????#為小球添加一個check方法,用于判斷鼠標在一秒鐘時間內產生的事件數是否匹配次目標
?????def?check(selfmotion):
??????????if?self.target<=motion<=self.target+5:
???????????????return?True
??????????else:
???????????????return?False
#定義玻璃面板類
class?Glass(pygame.sprite.Sprite):
?????def?__init__(selfglass_imagemouse_imagebg_size):
??????????#初始化動畫精靈
??????????pygame.sprite.Sprite.__init__(self)
??????????self.glass_image=pygame.image.load(glass_image).convert_alpha()
??????????self.glass_rect=self.glass_image.get_rect()
??????????self.glass_rect.leftself.glass_rect.top=\
???????????????????????????????????(bg_size[0]-self.glass_rect.width)?//2\
???????????????????????????????????(bg_size[1]-self.glass_rect.height)
??????????self.mouse_image=pygame.image.load(mouse_image).convert_alpha()
??????????self.mouse_rect=self.mouse_image.get_rect()
??????????self.mouse_rect.leftself.mouse_rect.top=\
???????????????????????????????????self.glass_rect.leftself.glass_rect.top
??????????pygame.mouse.set_visible(False)
def?main():
?????pygame.init()
?????grayball_image=‘gray_ball.png‘
?????greenball_image=‘green_ball.png‘
?????bg_image=‘bg.png‘
?????glass_image=‘glass.png‘
?????mouse_image=‘hand.png‘
?????bg_size=widthheight=1024681
?????screen=pygame.display.set_mode(bg_size)
?????pygame.display.set_caption(‘Play?the?ball?-FishC?Demo‘)
?????background=pygame.image.load(bg_image).convert_alpha()
?????hole=[(112122198204)\
???????????(222
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件?????176490??2018-09-06?09:23??play?the?ball+plus?music\bg.png
?????文件?????518065??2018-12-18?15:59??play?the?ball+plus?music\bg_music.ogg
?????文件????1360044??2018-12-18?15:38??play?the?ball+plus?music\bg_music.wav
?????文件????3424615??2018-12-18?16:09??play?the?ball+plus?music\bg_music_1.mp3
?????文件??????20652??2018-12-18?15:17??play?the?ball+plus?music\dog.wav
?????文件??????77501??2018-12-18?21:07??play?the?ball+plus?music\glass.png
?????文件??????10197??2018-09-06?09:23??play?the?ball+plus?music\gray_ball.png
?????文件??????12362??2018-12-18?22:10??play?the?ball+plus?music\green_ball.png
?????文件???????2774??2018-12-18?21:30??play?the?ball+plus?music\hand.png
?????文件?????350044??2018-12-18?15:27??play?the?ball+plus?music\hole.wav
?????文件?????130044??2018-12-18?16:18??play?the?ball+plus?music\laugh.wav
?????文件?????440044??2018-12-18?15:24??play?the?ball+plus?music\loser.wav
?????文件??????10240??2018-12-19?16:35??play?the?ball+plus?music\main.py
?????文件???????1620??2018-12-18?16:10??play?the?ball+plus?music\music.py
?????文件???????4969??2018-12-18?15:43??play?the?ball+plus?music\pause.png
?????文件?????120044??2018-12-18?16:07??play?the?ball+plus?music\pigeon.wav
?????文件???????6097??2018-12-18?15:44??play?the?ball+plus?music\play.png
?????文件??????14734??2018-12-19?11:59??play?the?ball+plus?music\win.png
?????文件?????140044??2018-12-18?15:23??play?the?ball+plus?music\winner.wav
?????文件??????48821??2018-12-18?22:10??play?the?ball+plus?music\新建?Microsoft?PowerPoint?演示文稿.pptx
?????目錄??????????0??2018-12-19?11:59??play?the?ball+plus?music
-----------?---------??----------?-----??----
??????????????6869401????????????????????21
評論
共有 條評論