您現在的位置是:首頁 > 籃球

用python寫一個打磚塊小遊戲

  • 由 學建模的正確姿勢 發表于 籃球
  • 2021-09-30
簡介tick(100)Ball = ball(BallCenter,BallRadius,BallColor,BallSpeed,MoveAngle,Surface)paddle = Paddle(0,WindowHeight-Paddle

遊戲打磚塊怎麼玩

哈嘍,大家好!你們的仙女又出現了,有沒有想我呢?(有點自戀,別當真)

閒話少說:好的!

這期咱們來寫個打磚塊的小遊戲

下面先展示一下程式碼:

import pygame,sys,time,randomfrom pygame。locals import * #from static_params import * #引入所有靜態引數from GameClass import *pygame。init() #初始化遊戲mainclock = pygame。time。Clock() #時鐘設定Exit =0global Surface Surface = pygame。display。set_mode([WindowWidth,WindowHeight],0,32) #視窗設定pygame。display。set_caption(‘打磚塊遊戲’) #設定視窗標題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(): #設定一個暫停函式 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) # 設定一個磚塊類的矩陣 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 #判斷滑鼠的位置 if event。type == KEYDOWN: #按下空格鍵暫停 if event。key == K_SPACE: pause() Surface。fill((0,0,0)) #設定滑鼠為不可見狀態 pygame。mouse。set_visible(False) #判斷球的運動 #判斷是否撞上了邊界或者擋板 if Ball。center[1]+Ball。radius+paddle。height > WindowHeight: if Ball。center[0]>paddle。left and Ball。center[0]WindowWidth-interval: Ball。rebound2() #判斷是否撞上了上邊界 elif Ball。center[1]-Ball。radiusBall。center[1] and brick。top-Ball。center[1]-Ball。radius0 and Ball。center[0]>brick。left and Ball。center[0]brick。bottom and Ball。center[1]-Ball。radius-brick。bottombrick。left and Ball。center[0]0 and Ball。center[1]>brick。top and Ball。center[1]brick。right and Ball。center[0]-Ball。radius-brick。rightbrick。top and Ball。center[1]

出來的效果就是這樣的:

用python寫一個打磚塊小遊戲

用python寫一個打磚塊小遊戲

下期想看仙女寫什麼可以在評論區留言~~~

拜拜~~

Top