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()

如有侵权,请联系删除。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值