




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第用python实现打砖块小游戏globalSurface
Surface=pygame.display.set_mode([WindowWidth,WindowHeight],0,32)#窗口设置
pygame.display.set_caption('打砖块游戏')
#设置窗口标题
defBeforeGame():
StartImage=pygame.image.load('intro_Ball.png').convert_alpha()#开始图像的界面
button=Button(Surface,FontColor,TextLocation,'StartGame')
flag=True
whileflag:
foreventinpygame.event.get():
ifevent.type==QUIT:
Exit=1
pygame.quit()
exit()
ifevent.type==MOUSEBUTTONUP:
ifbutton.is_overed():
flag=False
Surface.blit(StartImage,ImageLocation)
button.ButtonBlit()
pygame.display.update()
mainclock.tick(100)
defGaming():
#设置一个暂停函数
defpause():
button=Button(Surface,FontColor,TextLocation,'Continue')
Surface.fill((0,0,0))
flag=True
whileflag:
foreventinpygame.event.get():
ifevent.type==QUIT:
Exit=1
pygame.quit()
exit()
ifevent.type==MOUSEBUTTONUP:
ifbutton.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)foriinrange(0,100,BrickWidth+3)ifi+BrickWidth640]\
forjinrange(0,50,BrickHeight+2)]
mouse_x,mouse_y=pygame.mouse.get_pos()
whileTrue:
foreventinpygame.event.get():
ifevent.type==QUIT:
pygame.quit()
exit()
ifevent.type==MOUSEMOTION:
mouse_x,mouse_y=event.pos
#判断鼠标的位置
ifevent.type==KEYDOWN:
#按下空格键暂停
ifevent.key==K_SPACE:
pause()
Surface.fill((0,0,0))
#设置鼠标为不可见状态
pygame.mouse.set_visible(False)
#判断球的运动
#判断是否撞上了边界或者挡板
ifBall.center[1]+Ball.radius+paddle.heightWindowHeight:
ifBall.center[0]paddle.leftandBall.center[0]paddle.left+paddle.width:
Ball.rebound4()
#判断是否装上了左边界
elifBall.center[0]-Ball.radiusinterval:
Ball.rebound1()
elifBall.center[0]+Ball.radiusWindowWidth-interval:
Ball.rebound2()
#判断是否撞上了上边界
elifBall.center[1]-Ball.radiusinterval:
Ball.rebound3()
forbricklineinBrickMatrix:
forbrickinbrickline:
ifbrick.exist==1:
ifbrick.topBall.center[1]andbrick.top-Ball.center[1]-Ball.radiusintervalandBall.speedy0andBall.center[0]brick.leftandBall.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
ifBall.center[1]brick.bottomandBall.center[1]-Ball.radius-brick.bottomintervalandBall.speedy0andBall.center[0]brick.leftandBall.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
ifBall.center[0]brick.leftandbrick.left-Ball.center[0]-Ball.radiusintervalandBall.speedx0andBall.center[1]brick.topandBall.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
ifBall.center[0]brick.rightandBall.center[0]-Ball.radius-brick.rightintervalandBall.speedx0andBall.center[1]brick.topandBall.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
ifbrick.hitnumber=0:
brick.exist=0
#所有的砖块都不存在了,则游戏胜利
ifall([notany([brick.existforbrickinline])forlineinBrickMatrix]):
return'Win'
#print(brick.hitnumber,brick.exist)
Ball.move()
paddle.get_pos(mouse_x)
ifBall.fall():
return'Fail'
#画出图形
forbricklineinBrickMatrix:
forbrickinbrickline:
brick.draw()
Ball.draw()
paddle.draw()
pygame.display.update()
#每秒钟执行100次该代码,用来控制游戏循环频率
mainclock.tick(100)
defAfterGame(text):
result=pygame.font.SysFont('comicsansms',100).render(text,1,(0,255,0))
Surface.blit(result,ImageLocation)
button1=Button(Surface,FontColor,TextLocation,'PLAYITAGAIN')
button2=Button(Surface,FontColor,TextLocation2,'QUIT')
flag=True
whileflag:
pygame.mouse.set_visible(True)
foreventinpygame.event.get():
ifevent.type==QUIT:
Exit=1
pygame.quit()
exit()
ifevent.type==MOUSEBUTTONUP:
ifbutton1
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 婚姻自动解约协议书
- 委托看护孩童协议书
- 安全设施维护协议书
- 婚内家务补偿协议书
- 墓碑买卖合同协议书
- 安全驾驶合同协议书
- 培训机构收钱协议书
- 合法讨债居间协议书
- 学生搬出宿舍协议书
- 大豆机械种植协议书
- 安徽省1号卷A10联盟2025届高三5月最后一卷化学试题及答案
- 2022《农产品质量安全法》全文解读与学习
- 工程总承包的试题及答案
- 《电磁感应原理解析》课件
- 成都输液剂项目可行性研究报告参考范文
- 2025年二级注册建筑师资格考试《建筑结构、建筑物理与设备》真题卷(附答案)
- 锂电池基础知识培训课件
- 【部编版】六年级语文下册《语文园地五》精美课件
- 登革热防控知识培训
- 2025年新能源汽车实训基地建设方案范文
- 糖尿病患者血脂管理中国专家共识(2024版)解读
评论
0/150
提交评论