用python實現(xiàn)打磚塊小游戲_第1頁
用python實現(xiàn)打磚塊小游戲_第2頁
用python實現(xiàn)打磚塊小游戲_第3頁
用python實現(xiàn)打磚塊小游戲_第4頁
用python實現(xiàn)打磚塊小游戲_第5頁
已閱讀5頁,還剩1頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論