简简单单教你用python写个游戏(python3 pygame实现接小球游戏)
类别:脚本大全 浏览量:930
时间:2021-10-03 01:11:05 简简单单教你用python写个游戏
python3 pygame实现接小球游戏本文实例为大家分享了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编程过程(Python3模拟登录操作实例分析)
- python3字符串格式化怎么操作(python3实现字符串操作的实例代码)
- python3 for循环怎么用(Python3.5基础之变量、数据结构、条件和循环语句、break与continue语句实例详解)
- python 爬虫招聘(Python3获取拉勾网招聘信息的方法实例)
- python3配置教程(python3中property使用方法详解)
- python识别验证码教程(Python3.5 + sklearn利用SVM自动识别字母验证码方法示例)
- python numpy 安装(python3.6下Numpy库下载与安装图文教程)
- python3下urllib案例(URL Rewrite Module 2.1 URL重写模块规则写法)
- pythonselenium接口自动测试(python3+selenium自动化测试框架详解)
- python语言提供的3种基本数据类型(详解Python3 基本数据类型)
- python 多进程的启动和停止(Python3.5多进程原理与用法实例分析)
- python str类型怎么转换(Python3中的bytes和str类型详解)
- python一组数字求和(Python3数字求和的实例)
- python把str转成list(python3 字符串/列表/元组str/list/tuple相互转换方法及join函数的使用)
- python 调钉钉接口(python3实现钉钉消息推送的方法示例)
- python 组合数据类型(详解Python3 对象组合zip和回退方式*zip)
- 收藏 春节假期,这些景区巨划算(收藏春节假期这些景区巨划算)
- 景区游玩,这些安全知识要牢记(这些安全知识要牢记)
- 各地出招烘 热 旅游(各地出招烘热旅游)
- 2021款起亚霸锐到店了 更换车标,竞争宝马X5有戏吗(2021款起亚霸锐到店了)
- 新款起亚霸锐实拍,比普拉多气派,配3.0T V6,引入国内或许能大卖(新款起亚霸锐实拍)
- ()
热门推荐
- python微信消息模拟请求(python实现微信机器人: 登录微信、消息接收、自动回复功能)
- ant design 的vue批量操作(ant desing vue table 实现可伸缩列的完整例子)
- css导航条怎么做(CSS实现导航条Tab切换的三种方法介绍)
- python pyqt 教程(Python+PyQt5实现美剧爬虫可视工具的方法)
- 给页面加上运行代码功能
- docker容器退出错误码的步骤(docker容器退出错误码的步骤)
- sqlserver拒绝访问怎么办(SQL server服务显示远程过程调用失败的解决方法)
- SQL Server中的临时表的用法
- sqlserver中有几种锁定模式(SQL Server 开窗函数 Over代替游标的使用详解)
- html5页面强制刷新(移动端html5判断是否滚动到底部并且下拉加载)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9