用python实现打砖块小游戏
作者:彳余大胆 时间:2021-11-10 02:52:06
本文实例为大家分享了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
标签:python,打砖块
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
matplotlib之Font family [‘sans-serif‘] not found的问题解决
2021-03-31 09:55:42
![](https://img.aspxhome.com/file/2023/1/76831_0s.png)
python的函数和方法(上)
2021-02-04 11:46:16
![](https://img.aspxhome.com/file/2023/4/64274_0s.jpg)
给页面 unload 加上效果
2009-03-12 12:32:00
如何把一长串数字分位显示?
2009-11-06 14:01:00
如何使用PyTorch实现自由的数据读取
2022-06-16 12:28:16
![](https://img.aspxhome.com/file/2023/0/72120_0s.png)
Python开发工具Pycharm的安装以及使用步骤总结
2022-09-15 08:21:01
![](https://img.aspxhome.com/file/2023/3/72893_0s.png)
escape,encodeURI,encodeURIComponent函数比较
2008-01-27 11:19:00
MySQL Order By用法
2011-01-04 19:50:00
asp如何限制重复订阅邮件或重复投票?
2010-06-09 18:48:00
搜索结果页(SERP):前言
2009-07-22 20:56:00
![](https://img.aspxhome.com/file/UploadPic/20097/22/01-73s.jpg)
Django 生成登陆验证码代码分享
2021-07-31 06:48:21
![](https://img.aspxhome.com/file/2023/8/79398_0s.jpg)
Python选择网卡发包及接收数据包
2021-10-13 12:02:10
Python用Try语句捕获异常的实例方法
2021-07-14 10:28:54
![](https://img.aspxhome.com/file/2023/5/71515_0s.jpg)
Python3.x爬虫下载网页图片的实例讲解
2022-11-25 23:24:07
![](https://img.aspxhome.com/file/2023/5/76735_0s.jpg)
Django app配置多个数据库代码实例
2023-06-11 09:11:25
Python使用TextRank算法提取关键词
2021-05-31 07:29:44
![](https://img.aspxhome.com/file/2023/9/67839_0s.png)
JavaScript运动框架 多值运动(四)
2023-09-08 01:44:51
![](https://img.aspxhome.com/file/2023/5/56155_0s.jpg)
10条影响CSS渲染速度的写法与建议
2008-09-09 22:02:00
跟老齐学Python之集合(set)
2023-02-11 00:51:31
python中的各种运算符介绍
2021-08-09 12:49:04
![](https://img.aspxhome.com/file/2023/8/77618_0s.png)