Python游戲開發——打磚塊


打磚塊游戲向來大家也不會很陌生,今天來用python來開發一下這個小游戲

 1.引用對應數據庫

import pygame
from pygame.locals import *
import sys,random,time,math

 

2.設計游戲窗口初始數據

定義窗口內容,內容主要包括屏幕大小、標題、背景顏色。

def __init__(self,*args,**kw):        
        self.window_length = 600
        self.window_wide = 500
        #繪制游戲窗口,設置窗口尺寸
        self.game_window = pygame.display.set_mode((self.window_length,self.window_wide))
        #設置游戲窗口標題
        pygame.display.set_caption("CatchBallGame")
        #定義游戲窗口背景顏色參數
        self.window_color = (135,206,250)

    def backgroud(self):
        #繪制游戲窗口背景顏色
        self.game_window.fill(self.window_color)

 

3.設計球類

class Ball(object):
    '''創建球類'''
    def __init__(self,*args,**kw):
        #設置球的半徑、顏色、移動速度參數
        self.ball_color = (255,215,0)        
        self.move_x = 1
        self.move_y = 1
        self.radius = 10

    def ballready(self):
        #設置球的初始位置、
        self.ball_x = self.mouse_x
        self.ball_y = self.window_wide-self.rect_wide-self.radius
        #繪制球,設置反彈觸發條件            
        pygame.draw.circle(self.game_window,self.ball_color,(self.ball_x,self.ball_y),self.radius)

    def ballmove(self):
        #繪制球,設置反彈觸發條件            
        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
        #調用碰撞檢測函數
        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
        #設置游戲失敗條件
        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

 

4.設計球拍類

class Rect(object):
    '''創建球拍類'''
    def __init__(self,*args,**kw):
        #設置球拍顏色參數
        self.rect_color = (255,0,0)
        self.rect_length = 100
        self.rect_wide = 10

    def rectmove(self):
        #獲取鼠標位置參數
        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))

 

5.設計磚塊類

class Brick(object):
    def __init__(self,*args,**kw):
        #設置磚塊顏色參數
        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))                    
                    #調用碰撞檢測函數
                    self.ball_brick()                                        
                    if self.distanceb < self.radius:
                        self.brick_list[i][j] = 0
                        self.score += self.point
        #設置游戲勝利條件
        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

 

6.設計分數類

class Score(object):
    '''創建分數類'''
    def __init__(self,*args,**kw):        
        #設置初始分數
        self.score = 0
        #設置分數字體
        self.score_font = pygame.font.SysFont('arial',20)
        #設置初始加分點數
        self.point = 1
        #設置初始接球次數
        self.frequency = 0

    def countscore(self):
        #繪制玩家分數            
        my_score = self.score_font.render(str(self.score),False,(255,255,255))
        self.game_window.blit(my_score,(555,15))

class GameOver(object):
    '''創建游戲結束類'''
    def __init__(self,*args,**kw):
        #設置Game Over字體
        self.over_font = pygame.font.SysFont('arial',80)
        #定義GameOver標識
        self.over_sign = 0

class Win(object):
    '''創建游戲勝利類'''
    def __init__(self,*args,**kw):
        #設置You Win字體
        self.win_font = pygame.font.SysFont('arial',80)
        #定義Win標識
        self.win_sign = 0

 

7.設計碰撞事件類

class Collision(object):
    '''碰撞檢測類'''
    #球與窗口邊框的碰撞檢測
    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

    #球與球拍的碰撞檢測
    def ball_rect(self):
        #定義碰撞標識
        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
        #定義球拍到圓心最近點與圓心的距離
        self.distance = math.sqrt(math.pow(self.closestpoint_x-self.ball_x,2)+math.pow(self.closestpoint_y-self.ball_y,2))
        #球在球拍上左、上中、上右3種情況的碰撞檢測
        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
        #球在球拍左、右兩側中間的碰撞檢測
        if self.distance < self.radius and self.collision_sign_y == 3:
            self.move_x = - self.move_x

    #球與磚塊的碰撞檢測
    def ball_brick(self):
        #定義碰撞標識
        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
        #定義磚塊到圓心最近點與圓心的距離
        self.distanceb = math.sqrt(math.pow(self.closestpoint_bx-self.ball_x,2)+math.pow(self.closestpoint_by-self.ball_y,2))
        #球在磚塊上左、上中、上右3種情況的碰撞檢測
        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種情況的碰撞檢測
        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
        #球在磚塊左、右兩側中間的碰撞檢測
        if self.distanceb < self.radius and self.collision_sign_by == 3:
            self.move_x = - self.move_x

 

8.主函數

class Main(GameWindow,Rect,Ball,Brick,Collision,Score,Win,GameOver):
    '''創建主程序類'''
    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)
        #定義游戲開始標識
        start_sign = 0

        while True:            
            self.backgroud()
            self.rectmove()
            self.countscore()            
            
            if self.over_sign == 1 or self.win_sign == 1:
                break
            #獲取游戲窗口狀態
            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()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM