版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
第用python實現(xiàn)打磚塊小游戲globalSurface
Surface=pygame.display.set_mode([WindowWidth,WindowHeight],0,32)#窗口設(shè)置
pygame.display.set_caption('打磚塊游戲')
#設(shè)置窗口標(biāo)題
defBeforeGame():
StartImage=pygame.image.load('intro_Ball.png').convert_alpha()#開始圖像的界面
button=Button(Surface,FontColor,TextLocation,'StartGame')
flag=True
whileflag:
foreventinpygame.event.get():
ifevent.type==QUIT:
Exit=1
pygame.quit()
exit()
ifevent.type==MOUSEBUTTONUP:
ifbutton.is_overed():
flag=False
Surface.blit(StartImage,ImageLocation)
button.ButtonBlit()
pygame.display.update()
mainclock.tick(100)
defGaming():
#設(shè)置一個暫停函數(shù)
defpause():
button=Button(Surface,FontColor,TextLocation,'Continue')
Surface.fill((0,0,0))
flag=True
whileflag:
foreventinpygame.event.get():
ifevent.type==QUIT:
Exit=1
pygame.quit()
exit()
ifevent.type==MOUSEBUTTONUP:
ifbutton.is_overed():
flag=False
pygame.mouse.set_visible(True)
button.ButtonBlit()
pygame.display.update()
mainclock.tick(100)
Ball=ball(BallCenter,BallRadius,BallColor,BallSpeed,MoveAngle,Surface)
paddle=Paddle(0,WindowHeight-PaddleHeight,PaddleWidth,PaddleHeight,PaddleColor,Surface)
#設(shè)置一個磚塊類的矩陣
BrickMatrix=[[Brick(i,j,BrickWidth,BrickHeight,BrickHitNumber,BrickColor,Surface)foriinrange(0,100,BrickWidth+3)ifi+BrickWidth640]\
forjinrange(0,50,BrickHeight+2)]
mouse_x,mouse_y=pygame.mouse.get_pos()
whileTrue:
foreventinpygame.event.get():
ifevent.type==QUIT:
pygame.quit()
exit()
ifevent.type==MOUSEMOTION:
mouse_x,mouse_y=event.pos
#判斷鼠標(biāo)的位置
ifevent.type==KEYDOWN:
#按下空格鍵暫停
ifevent.key==K_SPACE:
pause()
Surface.fill((0,0,0))
#設(shè)置鼠標(biāo)為不可見狀態(tài)
pygame.mouse.set_visible(False)
#判斷球的運動
#判斷是否撞上了邊界或者擋板
ifBall.center[1]+Ball.radius+paddle.heightWindowHeight:
ifBall.center[0]paddle.leftandBall.center[0]paddle.left+paddle.width:
Ball.rebound4()
#判斷是否裝上了左邊界
elifBall.center[0]-Ball.radiusinterval:
Ball.rebound1()
elifBall.center[0]+Ball.radiusWindowWidth-interval:
Ball.rebound2()
#判斷是否撞上了上邊界
elifBall.center[1]-Ball.radiusinterval:
Ball.rebound3()
forbricklineinBrickMatrix:
forbrickinbrickline:
ifbrick.exist==1:
ifbrick.topBall.center[1]andbrick.top-Ball.center[1]-Ball.radiusintervalandBall.speedy0andBall.center[0]brick.leftandBall.center[0]brick.right:
print(1,Ball.center,brick.left,brick.right,brick.top,brick.bottom,Ball.radius)
Ball.rebound4()
brick.hitnumber=brick.hitnumber-1
ifBall.center[1]brick.bottomandBall.center[1]-Ball.radius-brick.bottomintervalandBall.speedy0andBall.center[0]brick.leftandBall.center[0]brick.right:
print(2,Ball.center,brick.left,brick.right,brick.top,brick.bottom,Ball.radius)
Ball.rebound3()
brick.hitnumber=brick.hitnumber-1
ifBall.center[0]brick.leftandbrick.left-Ball.center[0]-Ball.radiusintervalandBall.speedx0andBall.center[1]brick.topandBall.center[1]brick.bottom:
print(3,Ball.center,brick.left,brick.right,brick.top,brick.bottom,Ball.radius)
Ball.rebound2()
brick.hitnumber=brick.hitnumber-1
ifBall.center[0]brick.rightandBall.center[0]-Ball.radius-brick.rightintervalandBall.speedx0andBall.center[1]brick.topandBall.center[1]brick.bottom:
print(4,Ball.center,brick.left,brick.right,brick.top,brick.bottom,Ball.radius)
Ball.rebound1()
brick.hitnumber=brick.hitnumber-1
ifbrick.hitnumber=0:
brick.exist=0
#所有的磚塊都不存在了,則游戲勝利
ifall([notany([brick.existforbrickinline])forlineinBrickMatrix]):
return'Win'
#print(brick.hitnumber,brick.exist)
Ball.move()
paddle.get_pos(mouse_x)
ifBall.fall():
return'Fail'
#畫出圖形
forbricklineinBrickMatrix:
forbrickinbrickline:
brick.draw()
Ball.draw()
paddle.draw()
pygame.display.update()
#每秒鐘執(zhí)行100次該代碼,用來控制游戲循環(huán)頻率
mainclock.tick(100)
defAfterGame(text):
result=pygame.font.SysFont('comicsansms',100).render(text,1,(0,255,0))
Surface.blit(result,ImageLocation)
button1=Button(Surface,FontColor,TextLocation,'PLAYITAGAIN')
button2=Button(Surface,FontColor,TextLocation2,'QUIT')
flag=True
whileflag:
pygame.mouse.set_visible(True)
foreventinpygame.event.get():
ifevent.type==QUIT:
Exit=1
pygame.quit()
exit()
ifevent.type==MOUSEBUTTONUP:
ifbutton1
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2026四川省醫(yī)學(xué)科學(xué)院四川省人民醫(yī)院專職科研人員、工程師招聘3人備考題庫(二)及1套參考答案詳解
- 2025廣西南寧市興寧區(qū)玉蟾路小學(xué)秋季學(xué)期工勤人員1人備考題庫及完整答案詳解一套
- 2026安徽省面向中南大學(xué)選調(diào)生招錄備考題庫及一套答案詳解
- 種植戶培訓(xùn)制度
- 貿(mào)易公司安全生產(chǎn)制度
- 少兒藝術(shù)培訓(xùn)中心制度
- 小學(xué)教師普法培訓(xùn)制度
- 普通黨員輪流培訓(xùn)制度
- 實驗中學(xué)教師培訓(xùn)制度
- 醫(yī)院信息系統(tǒng)培訓(xùn)制度
- 北京通州產(chǎn)業(yè)服務(wù)有限公司招聘考試備考題庫及答案解析
- 2025-2026學(xué)年滬科版八年級數(shù)學(xué)上冊期末測試卷(含答案)
- 委托市場調(diào)研合同范本
- 消防維保計劃實施方案
- 2025年度黨支部書記述職報告
- 有子女離婚協(xié)議書
- 2026四川省引大濟(jì)岷水資源開發(fā)限公司公開招聘易考易錯模擬試題(共500題)試卷后附參考答案
- 2025至2030中國汽車檢測行業(yè)市場深度研究與戰(zhàn)略咨詢分析報告
- 2026年南昌健康職業(yè)技術(shù)學(xué)院單招職業(yè)技能考試備考試題附答案詳解
- 2026年安徽糧食工程職業(yè)學(xué)院高職單招職業(yè)適應(yīng)性考試備考試題及答案詳解
- 雨課堂學(xué)堂在線學(xué)堂云《中國電影經(jīng)典影片鑒賞(北京師范大學(xué))》單元測試考核答案
評論
0/150
提交評論