python制作个简单的打砖块游戏的代码是什么
Admin 2022-09-09 群英技术资讯 303 次浏览
本文实例为大家分享了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()
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
telepath库它提供了一种将包括Python对象在内的结构化数据打包为JSON可序列化格式的机制。通过向相应的JavaScript实现注册该机制,可以扩展该机制以支持任何Python类。然后,打包的数据可以包含在HTTP响应中,并在JavaScript中解压缩以获得与原始数据等效的数据结构。
今天抽空给大家介绍Django模板层过滤器和继承的问题,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
python3 连接sqlite实例,直接上代码吧importsqlite3db=r"D: pyWork test db" pyWork目录下test db数据库文件drp_tb_sql="droptableifexis
冒泡排序(Bubble Sort)是一种简单的排序算法。它重复地遍历要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。遍历数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。这个算法的名字由来是因为越小的元素会经由交换慢慢“浮”到数列的顶端。
python命令行传参有哪些方法?很多新手对于python命令行传参的方法可能不是很了解,这篇文章就主要给大家分享python命令行传参的方法,对新手学习有一定的借鉴价值,感兴趣的朋友可以参考一下,下面我们一起来学习吧。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008