Python實(shí)現(xiàn)打磚塊小游戲代碼實(shí)例
這次用Python實(shí)現(xiàn)的是一個(gè)接球打磚塊的小游戲,需要導(dǎo)入pygame模塊,有以下兩條經(jīng)驗(yàn)總結(jié):
1.多父類的繼承2.碰撞檢測(cè)的數(shù)學(xué)模型
知識(shí)點(diǎn)稍后再說(shuō),我們先看看游戲的效果和實(shí)現(xiàn):
一、游戲效果

二、游戲代碼
#導(dǎo)入模塊
import pygame
from pygame.locals import *
import sys,random,time,math
class GameWindow(object):
'''創(chuàng)建游戲窗口類'''
def __init__(self,*args,**kw):
self.window_length = 600
self.window_wide = 500
#繪制游戲窗口,設(shè)置窗口尺寸
self.game_window = pygame.display.set_mode((self.window_length,self.window_wide))
#設(shè)置游戲窗口標(biāo)題
pygame.display.set_caption("CatchBallGame")
#定義游戲窗口背景顏色參數(shù)
self.window_color = (135,206,250)
def backgroud(self):
#繪制游戲窗口背景顏色
self.game_window.fill(self.window_color)
class Ball(object):
'''創(chuàng)建球類'''
def __init__(self,*args,**kw):
#設(shè)置球的半徑、顏色、移動(dòng)速度參數(shù)
self.ball_color = (255,215,0)
self.move_x = 1
self.move_y = 1
self.radius = 10
def ballready(self):
#設(shè)置球的初始位置、
self.ball_x = self.mouse_x
self.ball_y = self.window_wide-self.rect_wide-self.radius
#繪制球,設(shè)置反彈觸發(fā)條件
pygame.draw.circle(self.game_window,self.ball_color,(self.ball_x,self.ball_y),self.radius)
def ballmove(self):
#繪制球,設(shè)置反彈觸發(fā)條件
pygame.draw.circle(self.game_window,self.ball_color,(self.ball_x,self.ball_y),self.radius)
self.ball_x += self.move_x
self.ball_y -= self.move_y
#調(diào)用碰撞檢測(cè)函數(shù)
self.ball_window()
self.ball_rect()
#每接5次球球速增加一倍
if self.distance < self.radius:
self.frequency += 1
if self.frequency == 5:
self.frequency = 0
self.move_x += self.move_x
self.move_y += self.move_y
self.point += self.point
#設(shè)置游戲失敗條件
if self.ball_y > 520:
self.gameover = self.over_font.render("Game Over",False,(0,0,0))
self.game_window.blit(self.gameover,(100,130))
self.over_sign = 1
class Rect(object):
'''創(chuàng)建球拍類'''
def __init__(self,*args,**kw):
#設(shè)置球拍顏色參數(shù)
self.rect_color = (255,0,0)
self.rect_length = 100
self.rect_wide = 10
def rectmove(self):
#獲取鼠標(biāo)位置參數(shù)
self.mouse_x,self.mouse_y = pygame.mouse.get_pos()
#繪制球拍,限定橫向邊界
if self.mouse_x >= self.window_length-self.rect_length//2:
self.mouse_x = self.window_length-self.rect_length//2
if self.mouse_x <= self.rect_length//2:
self.mouse_x = self.rect_length//2
pygame.draw.rect(self.game_window,self.rect_color,((self.mouse_x-self.rect_length//2),(self.window_wide-self.rect_wide),self.rect_length,self.rect_wide))
class Brick(object):
def __init__(self,*args,**kw):
#設(shè)置磚塊顏色參數(shù)
self.brick_color = (139,126,102)
self.brick_list = [[1,1,1,1,1,1],[1,1,1,1,1,1],[1,1,1,1,1,1],[1,1,1,1,1,1],[1,1,1,1,1,1]]
self.brick_length = 80
self.brick_wide = 20
def brickarrange(self):
for i in range(5):
for j in range(6):
self.brick_x = j*(self.brick_length+24)
self.brick_y = i*(self.brick_wide+20)+40
if self.brick_list[i][j] == 1:
#繪制磚塊
pygame.draw.rect(self.game_window,self.brick_color,(self.brick_x,self.brick_y,self.brick_length,self.brick_wide))
#調(diào)用碰撞檢測(cè)函數(shù)
self.ball_brick()
if self.distanceb < self.radius:
self.brick_list[i][j] = 0
self.score += self.point
#設(shè)置游戲勝利條件
if self.brick_list == [[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0]]:
self.win = self.win_font.render("You Win",False,(0,0,0))
self.game_window.blit(self.win,(100,130))
self.win_sign = 1
class Score(object):
'''創(chuàng)建分?jǐn)?shù)類'''
def __init__(self,*args,**kw):
#設(shè)置初始分?jǐn)?shù)
self.score = 0
#設(shè)置分?jǐn)?shù)字體
self.score_font = pygame.font.SysFont('arial',20)
#設(shè)置初始加分點(diǎn)數(shù)
self.point = 1
#設(shè)置初始接球次數(shù)
self.frequency = 0
def countscore(self):
#繪制玩家分?jǐn)?shù)
my_score = self.score_font.render(str(self.score),False,(255,255,255))
self.game_window.blit(my_score,(555,15))
class GameOver(object):
'''創(chuàng)建游戲結(jié)束類'''
def __init__(self,*args,**kw):
#設(shè)置Game Over字體
self.over_font = pygame.font.SysFont('arial',80)
#定義GameOver標(biāo)識(shí)
self.over_sign = 0
class Win(object):
'''創(chuàng)建游戲勝利類'''
def __init__(self,*args,**kw):
#設(shè)置You Win字體
self.win_font = pygame.font.SysFont('arial',80)
#定義Win標(biāo)識(shí)
self.win_sign = 0
class Collision(object):
'''碰撞檢測(cè)類'''
#球與窗口邊框的碰撞檢測(cè)
def ball_window(self):
if self.ball_x <= self.radius or self.ball_x >= (self.window_length-self.radius):
self.move_x = -self.move_x
if self.ball_y <= self.radius:
self.move_y = -self.move_y
#球與球拍的碰撞檢測(cè)
def ball_rect(self):
#定義碰撞標(biāo)識(shí)
self.collision_sign_x = 0
self.collision_sign_y = 0
if self.ball_x < (self.mouse_x-self.rect_length//2):
self.closestpoint_x = self.mouse_x-self.rect_length//2
self.collision_sign_x = 1
elif self.ball_x > (self.mouse_x+self.rect_length//2):
self.closestpoint_x = self.mouse_x+self.rect_length//2
self.collision_sign_x = 2
else:
self.closestpoint_x = self.ball_x
self.collision_sign_x = 3
if self.ball_y < (self.window_wide-self.rect_wide):
self.closestpoint_y = (self.window_wide-self.rect_wide)
self.collision_sign_y = 1
elif self.ball_y > self.window_wide:
self.closestpoint_y = self.window_wide
self.collision_sign_y = 2
else:
self.closestpoint_y = self.ball_y
self.collision_sign_y = 3
#定義球拍到圓心最近點(diǎn)與圓心的距離
self.distance = math.sqrt(math.pow(self.closestpoint_x-self.ball_x,2)+math.pow(self.closestpoint_y-self.ball_y,2))
#球在球拍上左、上中、上右3種情況的碰撞檢測(cè)
if self.distance < self.radius and self.collision_sign_y == 1 and (self.collision_sign_x == 1 or self.collision_sign_x == 2):
if self.collision_sign_x == 1 and self.move_x > 0:
self.move_x = - self.move_x
self.move_y = - self.move_y
if self.collision_sign_x == 1 and self.move_x < 0:
self.move_y = - self.move_y
if self.collision_sign_x == 2 and self.move_x < 0:
self.move_x = - self.move_x
self.move_y = - self.move_y
if self.collision_sign_x == 2 and self.move_x > 0:
self.move_y = - self.move_y
if self.distance < self.radius and self.collision_sign_y == 1 and self.collision_sign_x == 3:
self.move_y = - self.move_y
#球在球拍左、右兩側(cè)中間的碰撞檢測(cè)
if self.distance < self.radius and self.collision_sign_y == 3:
self.move_x = - self.move_x
#球與磚塊的碰撞檢測(cè)
def ball_brick(self):
#定義碰撞標(biāo)識(shí)
self.collision_sign_bx = 0
self.collision_sign_by = 0
if self.ball_x < self.brick_x:
self.closestpoint_bx = self.brick_x
self.collision_sign_bx = 1
elif self.ball_x > self.brick_x+self.brick_length:
self.closestpoint_bx = self.brick_x+self.brick_length
self.collision_sign_bx = 2
else:
self.closestpoint_bx = self.ball_x
self.collision_sign_bx = 3
if self.ball_y < self.brick_y:
self.closestpoint_by = self.brick_y
self.collision_sign_by = 1
elif self.ball_y > self.brick_y+self.brick_wide:
self.closestpoint_by = self.brick_y+self.brick_wide
self.collision_sign_by = 2
else:
self.closestpoint_by = self.ball_y
self.collision_sign_by = 3
#定義磚塊到圓心最近點(diǎn)與圓心的距離
self.distanceb = math.sqrt(math.pow(self.closestpoint_bx-self.ball_x,2)+math.pow(self.closestpoint_by-self.ball_y,2))
#球在磚塊上左、上中、上右3種情況的碰撞檢測(cè)
if self.distanceb < self.radius and self.collision_sign_by == 1 and (self.collision_sign_bx == 1 or self.collision_sign_bx == 2):
if self.collision_sign_bx == 1 and self.move_x > 0:
self.move_x = - self.move_x
self.move_y = - self.move_y
if self.collision_sign_bx == 1 and self.move_x < 0:
self.move_y = - self.move_y
if self.collision_sign_bx == 2 and self.move_x < 0:
self.move_x = - self.move_x
self.move_y = - self.move_y
if self.collision_sign_bx == 2 and self.move_x > 0:
self.move_y = - self.move_y
if self.distanceb < self.radius and self.collision_sign_by == 1 and self.collision_sign_bx == 3:
self.move_y = - self.move_y
#球在磚塊下左、下中、下右3種情況的碰撞檢測(cè)
if self.distanceb < self.radius and self.collision_sign_by == 2 and (self.collision_sign_bx == 1 or self.collision_sign_bx == 2):
if self.collision_sign_bx == 1 and self.move_x > 0:
self.move_x = - self.move_x
self.move_y = - self.move_y
if self.collision_sign_bx == 1 and self.move_x < 0:
self.move_y = - self.move_y
if self.collision_sign_bx == 2 and self.move_x < 0:
self.move_x = - self.move_x
self.move_y = - self.move_y
if self.collision_sign_bx == 2 and self.move_x > 0:
self.move_y = - self.move_y
if self.distanceb < self.radius and self.collision_sign_by == 2 and self.collision_sign_bx == 3:
self.move_y = - self.move_y
#球在磚塊左、右兩側(cè)中間的碰撞檢測(cè)
if self.distanceb < self.radius and self.collision_sign_by == 3:
self.move_x = - self.move_x
class Main(GameWindow,Rect,Ball,Brick,Collision,Score,Win,GameOver):
'''創(chuàng)建主程序類'''
def __init__(self,*args,**kw):
super(Main,self).__init__(*args,**kw)
super(GameWindow,self).__init__(*args,**kw)
super(Rect,self).__init__(*args,**kw)
super(Ball,self).__init__(*args,**kw)
super(Brick,self).__init__(*args,**kw)
super(Collision,self).__init__(*args,**kw)
super(Score,self).__init__(*args,**kw)
super(Win,self).__init__(*args,**kw)
#定義游戲開始標(biāo)識(shí)
start_sign = 0
while True:
self.backgroud()
self.rectmove()
self.countscore()
if self.over_sign == 1 or self.win_sign == 1:
break
#獲取游戲窗口狀態(tài)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == MOUSEBUTTONDOWN:
pressed_array = pygame.mouse.get_pressed()
if pressed_array[0]:
start_sign = 1
if start_sign == 0:
self.ballready()
else:
self.ballmove()
self.brickarrange()
#更新游戲窗口
pygame.display.update()
#控制游戲窗口刷新頻率
time.sleep(0.010)
if __name__ == '__main__':
pygame.init()
pygame.font.init()
catchball = Main()
三、知識(shí)點(diǎn)1.多父類的繼承
Python的繼承方式分為深度優(yōu)先和廣度優(yōu)先,Python2分經(jīng)典類的深度優(yōu)先搜索繼承方式(class A:)、 新式類的廣度優(yōu)先搜索繼承方式(class A(object):)2種,Python3經(jīng)典類與新式類的繼承方式與python2的新式類繼承方式一致,都為廣度優(yōu)先的繼承方式。
經(jīng)典類的深度優(yōu)先搜索繼承方式:

如圖所示
class B(A)
class C(A)
class D(B,C)
(1)若D類有構(gòu)造函數(shù),則重寫所有父類的繼承
(2)若D類沒(méi)有構(gòu)造函數(shù),B類有構(gòu)造函數(shù),則D類會(huì)繼承B類的構(gòu)造函數(shù)
(3)若D類沒(méi)有構(gòu)造函數(shù),B類也沒(méi)有構(gòu)造函數(shù),則D類會(huì)繼承 A類的構(gòu)造函數(shù),而不是C類的構(gòu)造函數(shù)
(4)若D類沒(méi)有構(gòu)造函數(shù),B類也沒(méi)有構(gòu)造函數(shù),A類也沒(méi)有構(gòu)造函數(shù),則D類才會(huì)繼承C類的構(gòu)造函數(shù)
新式類的廣度優(yōu)先搜索繼承方式:

如圖所示
class B(A)
class C(A)
class D(B,C)
(1)若D類有構(gòu)造函數(shù),則重寫所有父類的繼承
(2)若D類沒(méi)有構(gòu)造函數(shù),B類有構(gòu)造函數(shù),則D類會(huì)繼承B類的構(gòu)造函數(shù)
(3)若D類沒(méi)有構(gòu)造函數(shù),B類也沒(méi)有構(gòu)造函數(shù),則D類會(huì)繼承 C類的構(gòu)造函數(shù),而不是A類的構(gòu)造函數(shù)
(4)若D類沒(méi)有構(gòu)造函數(shù),B類也沒(méi)有構(gòu)造函數(shù),C類也沒(méi)有構(gòu)造函數(shù),則D類才會(huì)繼承A類的構(gòu)造函數(shù)
通過(guò)上面的分析,大家應(yīng)該清楚了Python中類的繼承順序,那么問(wèn)題來(lái)了,如果我不想重寫父類的構(gòu)造函數(shù),要子類和父類的構(gòu)造函數(shù)都生效怎么辦?解決辦法需要用到super關(guān)鍵字,對(duì)直接父類對(duì)象的引用,可以通過(guò)super來(lái)訪問(wèn)父類中被子類覆蓋的方法或?qū)傩浴?/p>
class A(object): def __init__(self,*args,**kw) class B(A): def __init__(self,*args,**kw) super(B,self).__init__(*args,**kw) class C(A): def __init__(self,*args,**kw) super(C,self).__init__(*args,**kw) class D(B,C): def __init__(self,*args,**kw) super(D,self).__init__(*args,**kw) super(B,self).__init__(*args,**kw)
2.碰撞檢測(cè)的數(shù)學(xué)模型
其實(shí),編程問(wèn)題到最后就是數(shù)學(xué)問(wèn)題,這個(gè)游戲涉及到2D圓形與矩形的碰撞檢測(cè)問(wèn)題:
碰撞檢測(cè)原理:通過(guò)找出矩形上離圓心最近的點(diǎn),然后通過(guò)判斷該點(diǎn)與圓心的距離是否小于圓的半徑,若小于則為碰撞。
那如何找出矩形上離圓心最近的點(diǎn)呢?下面我們從 x 軸、y 軸兩個(gè)方向分別進(jìn)行尋找。為了方便描述,我們先約定以下變量:
(1)矩形上離圓心最近的點(diǎn)為變量:closestpoint = [x, y]
(2)矩形 rect = [x, y, l, w] 左上角與長(zhǎng)寬 length,wide
(3)圓形 circle = [x, y, r] 圓心與半徑

首先是 x 軸:
如果圓心在矩形的左側(cè)(if circle_x < rect_x),那么 closestpoint_x = rect_x。
如果圓心在矩形的右側(cè)(elif circle_x > rect_x + rect_l),那么 closestpoint_x = rect_x + rect_l。
如果圓心在矩形的正上下方(else),那么 closestpoint_x = circle_x。
同理,對(duì)于 y 軸:
如果圓心在矩形的上方(if circle_y < rect_y),那么 closestpoint_y = rect_y。
如果圓心在矩形的下方(elif circle_y > rect_y + rect_w)),那么 closestpoint_y = rect_y + rect_w。
圓形圓心在矩形的正左右兩側(cè)(else),那么 closestpoint_y = circle_y。
因此,通過(guò)上述方法即可找出矩形上離圓心最近的點(diǎn)了,然后通過(guò)“兩點(diǎn)之間的距離公式”得出“最近點(diǎn)”與“圓心”的距離,最后將其與圓的半徑相比,即可判斷是否發(fā)生碰撞。
distance=math.sqrt(math.pow(closestpoint_x-circle_x,2)+math.pow(closestpoint_y-circle_y,2))
if distance < circle.r :
return True – 發(fā)生碰撞
else :
return False – 未發(fā)生碰撞
以上所述是小編給大家介紹的Python打磚塊小游戲詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- python貪吃蛇游戲代碼
- 14 個(gè)Python小游戲 源碼分享
- 二十種Python代碼游戲源代碼分享
- 13個(gè)有趣又好玩的Python游戲代碼分享
- python基礎(chǔ)練習(xí)之幾個(gè)簡(jiǎn)單的游戲
- Python實(shí)現(xiàn)我的世界小游戲源代碼
- 用Python設(shè)計(jì)一個(gè)經(jīng)典小游戲
- 20行python代碼的入門級(jí)小游戲的詳解
- 用python制作游戲外掛
- Python寫的貪吃蛇游戲例子
- 基于Python實(shí)現(xiàn)的掃雷游戲?qū)嵗a
- python實(shí)現(xiàn)植物大戰(zhàn)僵尸游戲?qū)嵗a
- python開發(fā)的小球完全彈性碰撞游戲代碼
- Python開發(fā)文字版密室逃脫游戲的實(shí)例(含代碼)
相關(guān)文章
Python爬取破解無(wú)線網(wǎng)絡(luò)wifi密碼過(guò)程解析
這篇文章主要介紹了Python爬取破解無(wú)線網(wǎng)絡(luò)密碼過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
python?使用ctypes調(diào)用C/C++?dll詳情
這篇文章主要介紹了python?使用ctypes調(diào)用C/C++?dll詳情,文章首先通過(guò)導(dǎo)入ctypes模塊,加載C/C++?dll到python進(jìn)程空間展開主題相關(guān)內(nèi)容,需要的小伙伴可以參考一下2022-04-04
基于CentOS搭建Python Django環(huán)境過(guò)程解析
這篇文章主要介紹了基于CentOS搭建Python Django環(huán)境過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
Tensorflow的可視化工具Tensorboard的初步使用詳解
這篇文章主要介紹了Tensorflow的可視化工具Tensorboard的初步使用詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
Python實(shí)現(xiàn)讀取txt文件中的數(shù)據(jù)并繪制出圖形操作示例
這篇文章主要介紹了Python實(shí)現(xiàn)讀取txt文件中的數(shù)據(jù)并繪制出圖形操作,涉及Python文件讀取、數(shù)值運(yùn)算及基于pylab庫(kù)的圖形繪制相關(guān)操作技巧,需要的朋友可以參考下2019-02-02
Python創(chuàng)建普通菜單示例【基于win32ui模塊】
這篇文章主要介紹了Python創(chuàng)建普通菜單,結(jié)合實(shí)例形式分析了Python基于win32ui模塊創(chuàng)建普通菜單及添加菜單項(xiàng)的相關(guān)操作技巧,并附帶說(shuō)明了win32ui模塊的安裝命令,需要的朋友可以參考下2018-05-05
一文詳解如何在Python中實(shí)現(xiàn)switch語(yǔ)句
這篇文章主要給大家介紹了關(guān)于如何在Python中實(shí)現(xiàn)switch語(yǔ)句的相關(guān)資料,今天在學(xué)習(xí)python的過(guò)程中,發(fā)現(xiàn)python沒(méi)有switch這個(gè)語(yǔ)法,所以這里給大家總結(jié)下,需要的朋友可以參考下2023-09-09
基于python中staticmethod和classmethod的區(qū)別(詳解)
下面小編就為大家?guī)?lái)一篇基于python中staticmethod和classmethod的區(qū)別(詳解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10
利用python對(duì)月餅數(shù)據(jù)進(jìn)行可視化(看看哪家最劃算)
通過(guò)python對(duì)數(shù)據(jù)進(jìn)行可視化展示,可直觀地展示數(shù)據(jù)之間的關(guān)系,為用戶提供更多的信息,這篇文章主要給大家介紹了關(guān)于利用python對(duì)月餅數(shù)據(jù)進(jìn)行可視化的相關(guān)資料,看看哪家最劃算,需要的朋友可以參考下2022-09-09
python編程學(xué)習(xí)np.float 被刪除的問(wèn)題解析
這篇文章主要為大家介紹了python編程學(xué)習(xí)np.float 被刪除的問(wèn)題解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02

