python实现打砖块游戏


Posted in Python onFebruary 25, 2020

本文实例为大家分享了Python实现打砖块游戏的具体代码,供大家参考,具体内容如下

#导入模块
import pygame
from pygame.locals import *
import sys,random,time,math

class GameWindow(object):
 '''创建游戏窗口类'''
 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)

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

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

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

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

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

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python中反射用法实例
Mar 27 Python
通过代码实例展示Python中列表生成式的用法
Mar 31 Python
python对离散变量的one-hot编码方法
Jul 11 Python
使用python验证代理ip是否可用的实现方法
Jul 25 Python
Python socket实现的简单通信功能示例
Aug 21 Python
Python如何发布程序的详细教程
Oct 09 Python
python 实现敏感词过滤的方法
Jan 21 Python
Python如何基于selenium实现自动登录博客园
Dec 16 Python
如何在mac环境中用python处理protobuf
Dec 25 Python
python输出第n个默尼森数的实现示例
Mar 08 Python
Python如何爬取qq音乐歌词到本地
Jun 01 Python
python爬虫beautifulsoup库使用操作教程全解(python爬虫基础入门)
Feb 19 Python
Python实现企业微信机器人每天定时发消息实例
Feb 25 #Python
Django 设置多环境配置文件载入问题
Feb 25 #Python
python中resample函数实现重采样和降采样代码
Feb 25 #Python
python实现的分层随机抽样案例
Feb 25 #Python
Python可变对象与不可变对象原理解析
Feb 25 #Python
Python 使用 environs 库定义环境变量的方法
Feb 25 #Python
Python 序列化和反序列化库 MarshMallow 的用法实例代码
Feb 25 #Python
You might like
使用php验证复选框有效性的示例
2013/11/13 PHP
使用Discuz关键词服务器实现PHP中文分词
2014/03/11 PHP
php用户注册信息验证正则表达式
2015/11/12 PHP
Laravel5中Cookie的使用详解
2017/05/03 PHP
JavaScript 以对象为索引的关联数组
2010/05/19 Javascript
javascript forEach通用循环遍历方法
2010/10/11 Javascript
js简单实现交换Li的值
2014/05/22 Javascript
JS应用正则表达式转换大小写示例
2014/09/18 Javascript
jQuery实现的个性化返回底部与返回顶部特效代码
2015/10/30 Javascript
javascript多物体运动实现方法分析
2016/01/08 Javascript
Javascript生成带参数的二维码示例
2016/10/10 Javascript
Javascript blur与click冲突解决办法
2017/01/09 Javascript
js实现文字向上轮播功能
2017/01/13 Javascript
tab栏切换原理
2017/03/22 Javascript
vue+vux实现移动端文件上传样式
2017/07/28 Javascript
js捆绑TypeScript声明文件的方法教程
2018/04/13 Javascript
vuex直接赋值的三种方法总结
2018/09/16 Javascript
使用VueRouter的addRoutes方法实现动态添加用户的权限路由
2019/06/03 Javascript
json数据格式常见操作示例
2019/06/13 Javascript
js简单的分页器插件代码实例
2019/09/11 Javascript
解决Vue项目打包后打开index.html页面显示空白以及图片路径错误的问题
2019/10/25 Javascript
vue elementui tree 任意级别拖拽功能代码
2020/08/31 Javascript
基于vue中的scoped坑点解说
2020/09/04 Javascript
Python模拟登录12306的方法
2014/12/30 Python
详解python的四种内置数据结构
2019/03/19 Python
Pycharm保存不能自动同步到远程服务器的解决方法
2019/06/27 Python
Python如何输出警告信息
2020/07/30 Python
用python批量移动文件
2021/01/14 Python
基于 HTML5 Canvas实现 的交互式地铁线路图
2018/03/05 HTML / CSS
一道SQL存储过程面试题
2016/10/07 面试题
保险专业自荐信范文
2014/02/20 职场文书
签约仪式主持词
2014/03/19 职场文书
市级绿色学校申报材料
2014/08/25 职场文书
教师自我剖析材料范文
2014/09/30 职场文书
社区三八妇女节活动总结
2015/02/06 职场文书
2016年党员学习廉政准则心得体会
2016/01/20 职场文书