python3 pygame实现接小球游戏
作者:Higashino_Keigo 时间:2023-07-28 18:04:21
本文实例为大家分享了python3 pygame接小球游戏的具体代码,供大家参考,具体内容如下
操作方法:鼠标操作
截图:
直接放代码:
# -*- coding:utf-8 -*-
import sys,pygame,random #导入库
from pygame.locals import *
def print_text(font,x,y,text,color=(255,255,255)):
imgText = font.render(text,True,color) # 创建字体,三个参数是文本.抗锯齿.颜色
screen.blit(imgText,(x,y)) #built screen 创建文本窗口
pygame.init() #init 初始化
#窗口设置
screen = pygame.display.set_mode((600,500))#screen-size 窗口大小设置
pygame.display.set_caption('BallFall') #title 窗口标题
font1 = pygame.font.Font(None,24) #font,size 字体类型(None为pygame默认字体).字体大小
pygame.mouse.set_visible(False) #mouse-visible 光标可视
#颜色设置
white = 255,255,255 #rgb
red = 220,50,50
yellow = 230,230,50
blue = 0,0,100
#计数设置
lives = 3 #初始生命
score = 0 #初始分数
#初始化设置
game_over = True #游戏结束判断
mouse_x = mouse_y = 0 #光标初始化
pos_x = 300 #挡板位置初始化
pos_y = 460
bomb_x = random.randint(0,500) #小球位置随机初始化
bomb_y = -50 #小球下落高度初始化
vel_y = 0.3 #小球下落速度
while True:
for event in pygame.event.get(): #事件判断
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == MOUSEMOTION: #鼠标运动
mouse_x,mouse_y = event.pos
elif event.type == MOUSEBUTTONUP: #鼠标抬起
if game_over:
game_over = False
lives = 3
score = 0
keys = pygame.key.get_pressed() #获取键盘
if keys[K_ESCAPE]: #键盘右上角esc键
pygame.quit()
sys.exit()
screen.fill(blue) #背景颜色
if game_over:
print_text(font1,100,200,'click to play')
else: #判断小球运行轨迹
bomb_y += vel_y
if bomb_y > 500: #fallen
bomb_x = random.randint(0,500) #小球随机出现
bomb_y = -50
lives -= 1
if lives == 0:
game_over = True
elif bomb_y > pos_y:
if bomb_x > pos_x and bomb_x < pos_x + 120:
score += 1
bomb_x = random.randint(0,500)
bomb_y = -50
pygame.draw.circle(screen,yellow,(bomb_x,int(bomb_y)),30,0) #绘制圆形 五个参数为屏幕.颜色.位置.实心半径.空心半径
pos_x = mouse_x #挡板位置变化设置
if pos_x < 0:
pos_x = 0
elif pos_x > 500:
pos_x = 500
pygame.draw.rect(screen,red,(pos_x,pos_y,120,40),0) #绘制矩形 参数跟圆形一样
print_text(font1,0,0,'Lives:' + str(lives)) #文字显示
print_text(font1,500,0,'Score:' + str(score))
pygame.display.update() #更新
来源:https://blog.csdn.net/Higashino_Keigo/article/details/80276141
标签:python3,pygame,小球
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python机器学习实现决策树
2021-04-21 07:44:34
![](https://img.aspxhome.com/file/2023/1/100911_0s.jpg)
pytest-fixture简介及其用法讲解
2023-02-13 19:27:25
基于Tensorflow一维卷积用法详解
2022-12-31 20:04:18
![](https://img.aspxhome.com/file/2023/3/111923_0s.jpg)
python中如何提高图像质量
2023-05-17 17:02:03
![](https://img.aspxhome.com/file/2023/4/86424_0s.jpg)
一文搞懂Go Exec 僵尸与孤儿进程
2023-10-21 07:14:07
YUI学习笔记(1)
2009-01-12 18:06:00
python中re.findall函数实例用法
2021-03-28 07:51:20
ProC 连接Oracle代码
2009-06-10 18:12:00
Python制作词云图代码实例
2023-10-24 11:20:21
![](https://img.aspxhome.com/file/2023/2/131162_0s.png)
如何使用Git实现切换分支开发过程解析
2022-07-03 20:57:06
![](https://img.aspxhome.com/file/2023/2/132262_0s.png)
用js格式化金额可设置保留的小数位数
2024-05-21 10:20:44
Django权限设置及验证方式
2022-12-28 23:45:18
基于JS实现Android,iOS一个手势动画效果
2024-04-28 09:36:41
![](https://img.aspxhome.com/file/2023/9/132859_0s.gif)
Python 如何展开嵌套的序列
2022-10-12 03:15:37
Django框架实现在线考试系统的示例代码
2021-05-24 23:07:00
![](https://img.aspxhome.com/file/2023/0/89000_0s.png)
如何解决Oracle EBS R12 - 以Excel查看输出格式为“文本”的请求时乱码
2024-01-22 01:17:55
form的submit方法和submit事件(onsubmit)
2008-09-28 13:29:00
oracle 日期函数集合(集中版本)第1/2页
2009-06-19 17:23:00
CSS中写expression可能会在Chrome中有问题
2010-01-29 13:10:00
![](https://img.aspxhome.com/file/UploadPic/20101/29/k2010125224713-48s.gif)
浅谈Pytorch torch.optim优化器个性化的使用
2023-12-19 08:47:12