用python实现打砖块小游戏
作者:彳余大胆 发布时间:2021-11-10 02:52:06
标签:python,打砖块
本文实例为大家分享了python实现打砖块小游戏的具体代码,供大家参考,具体内容如下
开发益智的打砖块小游戏,你可以试一下能打几块
import pygame,sys,time,random
from pygame.locals import * #
from static_params import * #引入所有静态参数
from GameClass import *
pygame.init() #初始化游戏
mainclock = pygame.time.Clock() #时钟设置
Exit =0
global Surface
Surface = pygame.display.set_mode([WindowWidth,WindowHeight],0,32) #窗口设置
pygame.display.set_caption('打砖块游戏') #设置窗口标题
def BeforeGame():
StartImage = pygame.image.load('intro_Ball.png').convert_alpha() #开始图像的界面
button = Button(Surface,FontColor,TextLocation,'StartGame')
flag = True
while flag:
for event in pygame.event.get():
if event.type ==QUIT:
Exit = 1
pygame.quit()
exit()
if event.type == MOUSEBUTTONUP:
if button.is_overed():
flag = False
Surface.blit(StartImage,ImageLocation)
button.ButtonBlit()
pygame.display.update()
mainclock.tick(100)
def Gaming():
#设置一个暂停函数
def pause():
button = Button(Surface,FontColor,TextLocation,'Continue')
Surface.fill((0,0,0))
flag = True
while flag:
for event in pygame.event.get():
if event.type ==QUIT:
Exit = 1
pygame.quit()
exit()
if event.type == MOUSEBUTTONUP:
if button.is_overed():
flag = False
pygame.mouse.set_visible(True)
button.ButtonBlit()
pygame.display.update()
mainclock.tick(100)
Ball = ball(BallCenter,BallRadius,BallColor,BallSpeed,MoveAngle,Surface)
paddle = Paddle(0,WindowHeight-PaddleHeight,PaddleWidth,PaddleHeight,PaddleColor,Surface)
# 设置一个砖块类的矩阵
BrickMatrix = [[Brick(i,j,BrickWidth,BrickHeight,BrickHitNumber,BrickColor,Surface) for i in range(0,100,BrickWidth+3) if i+BrickWidth<640]\
for j in range(0,50,BrickHeight+2)]
mouse_x,mouse_y = pygame.mouse.get_pos()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
exit()
if event.type == MOUSEMOTION:
mouse_x, mouse_y = event.pos #判断鼠标的位置
if event.type == KEYDOWN: #按下空格键暂停
if event.key == K_SPACE:
pause()
Surface.fill((0,0,0))
#设置鼠标为不可见状态
pygame.mouse.set_visible(False)
#判断球的运动
#判断是否撞上了边界或者挡板
if Ball.center[1]+Ball.radius+paddle.height > WindowHeight:
if Ball.center[0]>paddle.left and Ball.center[0]<paddle.left+paddle.width:
Ball.rebound4()
#判断是否装上了左边界
elif Ball.center[0]-Ball.radius<interval:
Ball.rebound1()
elif Ball.center[0]+Ball.radius>WindowWidth-interval:
Ball.rebound2()
#判断是否撞上了上边界
elif Ball.center[1]-Ball.radius<interval:
Ball.rebound3()
for brickline in BrickMatrix:
for brick in brickline:
if brick.exist == 1:
if brick.top >Ball.center[1] and brick.top-Ball.center[1]-Ball.radius<interval and Ball.speedy>0 and Ball.center[0]>brick.left and Ball.center[0]<brick.right:
print(1,Ball.center,brick.left,brick.right,brick.top,brick.bottom,Ball.radius)
Ball.rebound4()
brick.hitnumber =brick.hitnumber-1
if Ball.center[1]>brick.bottom and Ball.center[1]-Ball.radius-brick.bottom<interval and Ball.speedy<0 and Ball.center[0]>brick.left and Ball.center[0]<brick.right:
print(2,Ball.center,brick.left,brick.right,brick.top,brick.bottom,Ball.radius)
Ball.rebound3()
brick.hitnumber =brick.hitnumber-1
if Ball.center[0]< brick.left and brick.left-Ball.center[0]-Ball.radius<interval and Ball.speedx>0 and Ball.center[1]>brick.top and Ball.center[1]<brick.bottom:
print(3,Ball.center,brick.left,brick.right,brick.top,brick.bottom,Ball.radius)
Ball.rebound2()
brick.hitnumber =brick.hitnumber-1
if Ball.center[0]>brick.right and Ball.center[0]-Ball.radius-brick.right<interval and Ball.speedx<0 and Ball.center[1]>brick.top and Ball.center[1]<brick.bottom:
print(4,Ball.center,brick.left,brick.right,brick.top,brick.bottom,Ball.radius)
Ball.rebound1()
brick.hitnumber =brick.hitnumber-1
if brick.hitnumber <= 0:
brick.exist = 0
#所有的砖块都不存在了,则游戏胜利
if all([not any([brick.exist for brick in line]) for line in BrickMatrix] ):
return 'Win'
# print(brick.hitnumber,brick.exist)
Ball.move()
paddle.get_pos(mouse_x)
if Ball.fall():
return 'Fail'
#画出图形
for brickline in BrickMatrix:
for brick in brickline:
brick.draw()
Ball.draw()
paddle.draw()
pygame.display.update()
#每秒钟执行100次该代码,用来控制游戏循环频率
mainclock.tick(100)
def AfterGame(text):
result = pygame.font.SysFont('comicsansms',100).render(text,1,(0,255,0))
Surface.blit(result,ImageLocation)
button1 = Button(Surface,FontColor,TextLocation,'PLAY IT AGAIN')
button2 = Button(Surface,FontColor,TextLocation2,'QUIT')
flag = True
while flag:
pygame.mouse.set_visible(True)
for event in pygame.event.get():
if event.type == QUIT:
Exit = 1
pygame.quit()
exit()
if event.type == MOUSEBUTTONUP:
if button1.is_overed():
flag = False
if button2.is_overed():
Exit = 1
pygame.quit()
exit()
button1.ButtonBlit()
button2.ButtonBlit()
pygame.display.update()
mainclock.tick(100)
def main():
#展示游戏开始前的信息
BeforeGame()
print(Exit)
#开始游戏循环
while not Exit:
com=Gaming()
AfterGame(com)
if __name__ =='__main__':
main()
来源:https://blog.csdn.net/Gtieguo/article/details/113246477


猜你喜欢
- WSGI协议首先弄清下面几个概念:WSGI:全称是Web Server Gateway Interface,WSGI不是服务器,python
- 组合集总计: group by with rollup/cube grouping sets 子查询按执行方式分:标准子查询、关联子查询 标
- 生成静态页的页面非常的简单就是定义好模板与模板标题,之后利用str_replace进行替换了,是最常用的方法,另一种是利用ob_get_co
- 初学python,我们必须干点有意思的事!从微信下手吧!头像集样例如下: 大家可以发朋友圈开启辨认大赛哈哈~话不多说,直接上代码,注释我写了
- 如果想对一个列表做实时的更新,传统的做法是采用轮询的方式。以web为例,通过Ajax定时请求服务端然后获取数据显示在页面。这种方式实现简单,
- 在删除每个字典的时候有些方法和删除其他拥有独立内存的数据使用的方法是一样的,比如del,直接清空内存,clear()是值清除变量值。字典的删
- 目录安装基本操作打开图像转换格式展示图片剪裁合并缩略图旋转滤镜二次创作画线文字总结文 | 豆豆来源:Python 技术「ID: python
- 本文实例讲述了php实现的美国50个州选择列表。分享给大家供大家参考。具体如下:这里展示的是php生成的美国50个州的选择列表,自动选择当前
- profile是什么当我们要对某一条sql的性能进行分析时,可以使用它。Profiling是从 mysql5.0.3版本以后才开放的。启动p
- Vue加载流程1.初始化的第一阶段是Vue实例也就是vm对象创建前后:首先Vue进行生命周期,事件初始化发生在beforeCreate生命周
- 图像边缘信息主要集中在高频段,通常说图像锐化或检测边缘,实质就是高频滤波。我们知道微分运算是求信号的变化率,具有加强高频分量的作用。在空域运
- 五子棋游戏相信大部分人都玩过,今天我们用python来实现一次具体代码可以访问我的GitHub地址获取构建五子棋棋盘from collect
- PDOStatement::columnCountPDOStatement::columnCount — 返回结果集中的列数。(PHP 5
- Python关键字 global与nonlocalglobaldef test(): #1函数内如果没定义x,则x默认为全局变量
- CHAR和VARCHAR类型相似,差别主要在存储,尾随空格和检索方式上。CHAR和VARCHAR相同的是:CHAR和VARCHAR都指定了字
- Css3引入了新的盒模型——弹性盒模型,该模型决定一个盒子在其他盒子中的分布方式以及如何处理可用的空间。这与XUL(火狐使用的用户交互语言)
- 本文实例为大家分享了python学生信息管理系统的具体代码,供大家参考,具体内容如下#编译环境为python3 #学生信息管理系统包括基本的
- 科讯5.0 标签和之前版本变化不大,如果用老版本的科讯,可以参考这个标签使用。相关文章:新云4.0 模板通用标签说明 标签清单:======
- 在pandas中,经常对数据进行处理 而导致数据索引顺序混乱,从而影响数据读取、插入等。小笔总结了以下几种重置索引的方法:import pa
- 杭州最美的季节里,淘宝无障碍访问改善小组有幸邀请到盲人在线站长——争渡读屏团队成员——杨永全同学和我们一起面对面交流网站无障碍访问方面的问题