




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第用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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 国际安全生产培训的可持续发展评估指标体系构建考核试卷
- 3D打印在机器人传感器支架设计中的应用考核试卷
- 数字绘画软件发展分析考核试卷
- 西学医师考试试题及答案
- 拜城居民考试题及答案
- 供应链虚拟库存管理政策建议考核试卷
- 中部地区融入统一大市场策略
- 地下建筑防水施工
- 股票交易合同模板
- 大学生心理健康知识竞赛试题2
- 银行业反洗钱培训课件
- 医美行业营销策划方案模板
- 2025年人教版一年级下册数学期末模拟试卷(含答案)
- 资产评估风险管理制度
- 餐厅控烟制度管理制度
- 新媒体部笔试题目及答案
- 道路旅客运输安全生产管理制度x
- 人教(部编版)道德与法治八下5.3基本政治制度教学设计
- 黑龙江省2024年普通高校招生体育类本科批院校专业组投档分数线(历史类)
- 水闸地基施工方案
- 《建立合适边界:亲子教育课件》
评论
0/150
提交评论