python實(shí)現(xiàn)打磚塊游戲
本文實(shí)例為大家分享了Python實(shí)現(xiàn)打磚塊游戲的具體代碼,供大家參考,具體內(nèi)容如下
#導(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()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
如何解決pycharm中用matplotlib畫圖不顯示中文的問題
這篇文章主要介紹了如何解決pycharm中用matplotlib畫圖不顯示中文的問題,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下2022-06-06
python搭建簡(jiǎn)易服務(wù)器分析與實(shí)現(xiàn)
本文將介紹python搭建簡(jiǎn)易服務(wù)器實(shí)現(xiàn)步驟,需要了解的朋友可以參考下2012-12-12
給你一面國(guó)旗 教你用python畫中國(guó)國(guó)旗
朋友圈許多小伙伴都在發(fā):請(qǐng)給我一面五星紅旗的動(dòng)態(tài),這篇文章就主要教大家如何使用python畫中國(guó)國(guó)旗,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09
使用Python腳本將文字轉(zhuǎn)換為圖片的實(shí)例分享
這篇文章主要介紹了使用Python腳本將文字轉(zhuǎn)換為圖片的實(shí)例分享,主要用到了PIL庫(kù),需要的朋友可以參考下2015-08-08
將字典轉(zhuǎn)換為DataFrame并進(jìn)行頻次統(tǒng)計(jì)的方法
下面小編就為大家分享一篇將字典轉(zhuǎn)換為DataFrame并進(jìn)行頻次統(tǒng)計(jì)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-04-04
Python爬蟲實(shí)戰(zhàn)之爬取京東商品數(shù)據(jù)并實(shí)實(shí)現(xiàn)數(shù)據(jù)可視化
今天再帶大家簡(jiǎn)單爬一波京東的商品數(shù)據(jù)唄,廢話不多說,文中有非常詳細(xì)的代碼示例,需要的朋友可以參考下2021-06-06
python selenium對(duì)應(yīng)的瀏覽器chromedriver版本不一致問題
這篇文章主要介紹了python selenium對(duì)應(yīng)的瀏覽器chromedriver版本不一致問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08

