用python實現(xiàn)打磚塊小游戲
本文實例為大家分享了python實現(xiàn)打磚塊小游戲的具體代碼,供大家參考,具體內(nèi)容如下
開發(fā)益智的打磚塊小游戲,你可以試一下能打幾塊
import pygame,sys,time,random
from pygame.locals import *?? ??? ?#
from static_params import * ? #引入所有靜態(tài)參數(shù)
from GameClass import *
pygame.init() ? #初始化游戲
mainclock = pygame.time.Clock() #時鐘設(shè)置
Exit =0
global Surface?
Surface = pygame.display.set_mode([WindowWidth,WindowHeight],0,32) #窗口設(shè)置
pygame.display.set_caption('打磚塊游戲') ? ?#設(shè)置窗口標(biāo)題
def BeforeGame():
?? ?StartImage = pygame.image.load('intro_Ball.png').convert_alpha() #開始圖像的界面
?? ?button = Button(Surface,FontColor,TextLocation,'StartGame')
?? ?flag = True
?? ?while flag:
?? ??? ?for event in pygame.event.get():
?? ??? ??? ?if event.type ==QUIT:
?? ??? ??? ??? ?Exit = 1
?? ??? ??? ??? ?pygame.quit()
?? ??? ??? ??? ?exit()
?? ??? ??? ?if event.type == MOUSEBUTTONUP:
?? ??? ??? ??? ?if button.is_overed():
?? ??? ??? ??? ??? ?flag = False
?? ??? ?Surface.blit(StartImage,ImageLocation)
?? ??? ?button.ButtonBlit()
?? ??? ?pygame.display.update()
?? ??? ?mainclock.tick(100)
def Gaming():
?? ?#設(shè)置一個暫停函數(shù)
?? ?def pause():
?? ??? ?button = Button(Surface,FontColor,TextLocation,'Continue')
?? ??? ?Surface.fill((0,0,0))
?? ??? ?flag = True
?? ??? ?while flag:
?? ??? ??? ?for event in pygame.event.get():
?? ??? ??? ??? ?if event.type ==QUIT:
?? ??? ??? ??? ??? ?Exit = 1
?? ??? ??? ??? ??? ?pygame.quit()
?? ??? ??? ??? ??? ?exit()
?? ??? ??? ??? ?if event.type == MOUSEBUTTONUP:
?? ??? ??? ??? ??? ?if button.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) for i in range(0,100,BrickWidth+3) if i+BrickWidth<640]\
?? ?for j in range(0,50,BrickHeight+2)]
?? ?mouse_x,mouse_y = pygame.mouse.get_pos()
?? ?while True:
?? ??? ?for event in pygame.event.get():
?? ??? ??? ?if event.type == QUIT:
?? ??? ??? ??? ?pygame.quit()
?? ??? ??? ??? ?exit()
?? ??? ??? ?if event.type == MOUSEMOTION:
?? ??? ??? ??? ?mouse_x, mouse_y = event.pos ?#判斷鼠標(biāo)的位置
?? ??? ??? ?if event.type == KEYDOWN: ? #按下空格鍵暫停
?? ??? ??? ??? ?if event.key == K_SPACE:
?? ??? ??? ??? ??? ?pause()
?? ??? ?Surface.fill((0,0,0))
?? ??? ?#設(shè)置鼠標(biāo)為不可見狀態(tài)
?? ??? ?pygame.mouse.set_visible(False)
?? ??? ?#判斷球的運動
?? ??? ?#判斷是否撞上了邊界或者擋板
?? ??? ?if Ball.center[1]+Ball.radius+paddle.height > WindowHeight:
?? ??? ??? ?if Ball.center[0]>paddle.left and Ball.center[0]<paddle.left+paddle.width:
?? ??? ??? ??? ?Ball.rebound4()
?? ??? ?#判斷是否裝上了左邊界
?? ??? ?elif Ball.center[0]-Ball.radius<interval:
?? ??? ??? ?Ball.rebound1()
?? ??? ?elif Ball.center[0]+Ball.radius>WindowWidth-interval:
?? ??? ??? ?Ball.rebound2()
?? ??? ?#判斷是否撞上了上邊界
?? ??? ?elif Ball.center[1]-Ball.radius<interval:
?? ??? ??? ?Ball.rebound3()
?? ??? ?for brickline in BrickMatrix:
?? ??? ??? ?for brick in brickline:?? ?
?? ??? ??? ??? ?if brick.exist == 1:?? ?
?? ??? ??? ??? ??? ?if brick.top >Ball.center[1] and brick.top-Ball.center[1]-Ball.radius<interval and Ball.speedy>0 and Ball.center[0]>brick.left and Ball.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
?? ??? ??? ??? ??? ?if Ball.center[1]>brick.bottom and Ball.center[1]-Ball.radius-brick.bottom<interval and Ball.speedy<0 and Ball.center[0]>brick.left and Ball.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
?? ??? ??? ??? ??? ?if Ball.center[0]< brick.left and brick.left-Ball.center[0]-Ball.radius<interval and Ball.speedx>0 and Ball.center[1]>brick.top and Ball.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
?? ??? ??? ??? ??? ?if Ball.center[0]>brick.right and Ball.center[0]-Ball.radius-brick.right<interval and Ball.speedx<0 and Ball.center[1]>brick.top and Ball.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
?? ??? ??? ??? ??? ?if brick.hitnumber <= 0:
?? ??? ??? ??? ??? ??? ?brick.exist = 0
?? ??? ?#所有的磚塊都不存在了,則游戲勝利
?? ??? ?if all([not any([brick.exist for brick in line]) for line in BrickMatrix] ):
?? ??? ??? ?return 'Win'
?? ??? ?# print(brick.hitnumber,brick.exist)
?? ??? ?Ball.move()
?? ??? ?paddle.get_pos(mouse_x)
?? ??? ?if Ball.fall():
?? ??? ??? ?return 'Fail'
?? ??? ?#畫出圖形
?? ??? ?for brickline in BrickMatrix:
?? ??? ??? ?for brick in brickline:
?? ??? ??? ??? ?brick.draw()
?? ??? ?Ball.draw()
?? ??? ?paddle.draw()
?? ??? ?pygame.display.update()
?? ??? ?#每秒鐘執(zhí)行100次該代碼,用來控制游戲循環(huán)頻率
?? ??? ?mainclock.tick(100)
?? ?
def AfterGame(text):
?? ?result = pygame.font.SysFont('comicsansms',100).render(text,1,(0,255,0))
?? ?Surface.blit(result,ImageLocation)
?? ?button1 = Button(Surface,FontColor,TextLocation,'PLAY IT AGAIN')
?? ?button2 = Button(Surface,FontColor,TextLocation2,'QUIT')
?? ?flag = True
?? ?while flag:
?? ??? ?pygame.mouse.set_visible(True)
?? ??? ?for event in pygame.event.get():
?? ??? ??? ?if event.type == QUIT:
?? ??? ??? ??? ?Exit = 1
?? ??? ??? ??? ?pygame.quit()
?? ??? ??? ??? ?exit()
?? ??? ??? ?if event.type == MOUSEBUTTONUP:
?? ??? ??? ??? ?if button1.is_overed():
?? ??? ??? ??? ??? ?flag = False
?? ??? ??? ??? ?if button2.is_overed():
?? ??? ??? ??? ??? ?Exit = 1
?? ??? ??? ??? ??? ?pygame.quit()
?? ??? ??? ??? ??? ?exit()
?? ??? ?button1.ButtonBlit()
?? ??? ?button2.ButtonBlit()
?? ??? ?pygame.display.update()
?? ??? ?mainclock.tick(100)
def main():
?? ?#展示游戲開始前的信息
?? ?BeforeGame()
?? ?print(Exit)
?? ?#開始游戲循環(huán)
?? ?while not Exit:
?? ??? ?com=Gaming()
?? ??? ?AfterGame(com)
if __name__ =='__main__':
?? ?main()

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python基于keras訓(xùn)練實現(xiàn)微笑識別的示例詳解
Keras是一個由Python編寫的開源人工神經(jīng)網(wǎng)絡(luò)庫,可用于深度學(xué)習(xí)模型的設(shè)計、調(diào)試、評估、應(yīng)用和可視化。本文將基于keras訓(xùn)練實現(xiàn)微笑識別效果,需要的可以參考一下2022-01-01
使用Python編寫一個在Linux下實現(xiàn)截圖分享的腳本的教程
這篇文章主要介紹了使用Python編寫一個在Linux下實現(xiàn)截圖分享的腳本的教程,利用到了scrot和urllib2庫,需要的朋友可以參考下2015-04-04
Python實現(xiàn)實時顯示進(jìn)度條的6種方法
相信大家對進(jìn)度條一定不陌生了,很多安裝或者下載都會出現(xiàn)進(jìn)度條,本文主要介紹了Python實現(xiàn)實時顯示進(jìn)度條的6種方法,具有一定的參考價值,感興趣的可以了解一下2021-12-12
缺失值可能是數(shù)據(jù)科學(xué)中最不受歡迎的值,然而,它們總是在身邊。忽略缺失值也是不合理的,因此我們需要找到有效且適當(dāng)?shù)靥幚硭鼈兊姆椒ā1疚目偨Y(jié)了四個Python查詢?nèi)笔е档姆椒?,需要的可以參考一?/div> 2022-05-05
ubuntu?20.04系統(tǒng)下如何切換gcc/g++/python的版本
這篇文章主要給大家介紹了關(guān)于ubuntu?20.04系統(tǒng)下如何切換gcc/g++/python版本的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用ubuntu具有一定的參考借鑒價值,需要的朋友可以參考下2023-12-12
Python內(nèi)置函數(shù) next的具體使用方法
這篇文章主要介紹了Python內(nèi)置函數(shù) next的具體使用方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11
Python使用pyyaml模塊處理yaml數(shù)據(jù)
這篇文章主要介紹了Python使用pyyaml模塊處理yaml數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04
python決策樹預(yù)測學(xué)生成績等級實現(xiàn)詳情
這篇文章主要為介紹了python決策樹預(yù)測學(xué)生成績等級,使用決策樹完成學(xué)生成績等級預(yù)測,可選取部分或全部特征,分析參數(shù)對結(jié)果的影響,并進(jìn)行調(diào)參優(yōu)化,決策樹可視化進(jìn)行調(diào)參優(yōu)化分析2022-04-04
?python中pandas讀取csv文件?時如何省去csv.reader()操作指定列步驟
這篇文章主要介紹了?python中pandas讀取csv文件?時如何省去csv.reader()操作指定列步驟,對正在工作的你可能有一定的幫助,需要的朋友可以參考一下2022-01-01最新評論

