pygame游戏之旅 按钮上添加文字的方法


Posted in Python onNovember 21, 2018

本文为大家分享了pygame游戏之旅的第11篇,供大家参考,具体内容如下

定义一个button函数,将文字,颜色等作为参数。

def button (msg, x, y, w, h, ic, ac):
 mouse =pygame.mouse.get_pos()
 if x + w > mouse[0] > x and y + h > mouse[1] > y:
 pygame.draw.rect(gameDisplay, ac, (x,y,w,h))
 else:
 pygame.draw.rect(gameDisplay, ic, (x,y,w,h))
 smallText = pygame.font.Font("freesansbold.ttf", 20)
 textSurf, textRect = text_objects(msg, smallText)
 textRect.center = ( (x+(w/2)), (y+(h/2)))
 gameDisplay.blit(textSurf, textRect)

全部代码为:

import pygame
import time
import random
 
pygame.init()
 
white = (255,255,255)
black = (0,0,0)
gray = (128,128,128)
red = (200,0,0)
green = (0,200,0)
bright_red = (255,0,0)
bright_green = (0,255,0)
blue = (0,0,255)
 
 
car_width = 100
 
display_width = 800
display_height = 600
 
 
gameDisplay = pygame.display.set_mode( (display_width,display_height) )
pygame.display.set_caption('A bit Racey')
clock = pygame.time.Clock()
 
carImg = pygame.image.load('car.png')
 
def things_dodged(count):
 font = pygame.font.SysFont(None, 25)
 text = font.render("Dodged:"+str(count), True, black)
 gameDisplay.blit(text,(0,0))
 
def things(thingx, thingy, thingw, thingh, color):
 pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh])
 
 
 
def car(x, y):
 gameDisplay.blit(carImg, (x,y))
 
 
def text_objects(text, font):
 textSurface = font.render(text, True, black)
 return textSurface, textSurface.get_rect()
 
def message_diaplay(text):
 largeText = pygame.font.Font('freesansbold.ttf',115)
 TextSurf, TextRect = text_objects(text, largeText)
 TextRect.center = ((display_width/2),(display_height/2))
 gameDisplay.blit(TextSurf, TextRect)
 pygame.display.update()
 time.sleep(2)
 game_loop()
 
def crash():
 message_diaplay('You Crashed')
 
def button (msg, x, y, w, h, ic, ac):
 mouse =pygame.mouse.get_pos()
 if x + w > mouse[0] > x and y + h > mouse[1] > y:
 pygame.draw.rect(gameDisplay, ac, (x,y,w,h))
 else:
 pygame.draw.rect(gameDisplay, ic, (x,y,w,h))
 smallText = pygame.font.Font("freesansbold.ttf", 20)
 textSurf, textRect = text_objects(msg, smallText)
 textRect.center = ( (x+(w/2)), (y+(h/2)))
 gameDisplay.blit(textSurf, textRect)
 
def game_intro():
 intro = True
 while intro:
 for event in pygame.event.get():
 print(event)
 if event.type == pygame.QUIT:
 pygame.quit()
 quit()
 gameDisplay.fill(white)
 largeText = pygame.font.Font('freesansbold.ttf',115)
 TextSurf, TextRect = text_objects('A bit Racey', largeText)
 TextRect.center = ((display_width/2),(display_height/2))
 gameDisplay.blit(TextSurf, TextRect)
 button("GO", 150, 450, 100, 50, green, bright_green)
 button("Quit",550, 450, 100, 50, red, bright_red)
 pygame.display.update()
 clock.tick(15)
 
def game_loop():
 x = display_width * 0.45
 y = display_height * 0.8
 x_change = 0
 
 dodged = 0
 
 gameExit = False
 
 thing_startx = random.randrange(0, display_width)
 thing_starty = -600
 thing_speed = 7
 thing_width = 100
 thing_height = 100
 
 while not gameExit:
 for event in pygame.event.get():
 if event.type == pygame.QUIT:
 pygame.quit()
 quit()
 if event.type == pygame.KEYDOWN:
 if event.key == pygame.K_LEFT:
  x_change = -5
 elif event.key == pygame.K_RIGHT:
  x_change = 5
 if event.type == pygame.KEYUP:
 if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
  x_change = 0
 print(event)
 x += x_change
 gameDisplay.fill(white)
 
 things(thing_startx, thing_starty, thing_width, thing_height, black)
 thing_starty += thing_speed
 
 car(x,y)
 things_dodged(dodged)
 if x > display_width - car_width or x < 0:
 gameExit = True
 if thing_starty > display_height:
 thing_starty = 0 - thing_height
 thing_startx = random.randrange(0, display_width)
 dodged += 1
 thing_speed += 1
 thing_width += (dodged * 1.2)
 if y < thing_starty + thing_height:
 print('y crossover')
 if x > thing_startx and x < thing_startx + thing_width or x + car_width > thing_startx and x + car_width < thing_startx + thing_width:
 print('x crossover')
 crash()
 pygame.display.update()
 clock.tick(60)
#crash()
game_intro()
game_loop()
pygame.quit()
quit()

结果图:

pygame游戏之旅 按钮上添加文字的方法

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python中多线程及程序锁浅析
Jan 21 Python
python通过post提交数据的方法
May 06 Python
编写Python脚本抓取网络小说来制作自己的阅读器
Aug 20 Python
详解python 拆包可迭代数据如tuple, list
Dec 29 Python
python实现决策树ID3算法的示例代码
May 30 Python
利用PyCharm Profile分析异步爬虫效率详解
May 08 Python
python flask框架实现重定向功能示例
Jul 02 Python
python之拟合的实现
Jul 19 Python
Python3简单爬虫抓取网页图片代码实例
Aug 26 Python
python Xpath语法的使用
Nov 26 Python
Flask处理Web表单的实现方法
Jan 31 Python
Python机器学习之KNN近邻算法
May 14 Python
Face++ API实现手势识别系统设计
Nov 21 #Python
详解django自定义中间件处理
Nov 21 #Python
pygame游戏之旅 添加游戏界面按键图形
Nov 20 #Python
pygame游戏之旅 添加游戏介绍
Nov 20 #Python
pygame游戏之旅 计算游戏中躲过的障碍数量
Nov 20 #Python
pygame游戏之旅 添加碰撞效果的方法
Nov 20 #Python
pygame游戏之旅 如何制作游戏障碍
Nov 20 #Python
You might like
用PHP动态生成虚拟现实VRML网页
2006/10/09 PHP
PHP缓存技术的多种方法小结
2012/08/14 PHP
set_exception_handler函数在ThinkPHP中的用法
2014/10/31 PHP
php json相关函数用法示例
2017/03/28 PHP
在 PHP 和 Laravel 中使用 Traits的方法
2019/11/13 PHP
(JS实现)MapBar中坐标的加密和解密的脚本
2007/05/16 Javascript
javascript学习笔记(十四) window对象使用介绍
2012/06/20 Javascript
js 获取input点选按钮的值的方法
2014/04/14 Javascript
兼容IE、firefox以及chrome的js获取时间(getFullYear)
2014/07/04 Javascript
ie8下修改input的type属性报错的解决方法
2014/09/16 Javascript
JavaScript函数节流和函数防抖之间的区别
2017/02/15 Javascript
js如何获取网页所有图片
2017/05/12 Javascript
详解jQuery中关于Ajax的几个常用的函数
2017/07/17 jQuery
js模拟百度模糊搜索的实例
2017/08/04 Javascript
angular内置provider之$compileProvider详解
2017/09/27 Javascript
vue.js vue-router如何实现无效路由(404)的友好提示
2017/12/20 Javascript
Vue 项目分环境打包的方法示例
2018/08/03 Javascript
vue 优化CDN加速的方法示例
2018/09/19 Javascript
vue读取本地的excel文件并显示在网页上方法示例
2019/05/29 Javascript
微信小程序实现点击卡片 翻转效果
2019/09/04 Javascript
vue移动端模态框(可传参)的实现
2019/11/20 Javascript
Python实现的爬取百度贴吧图片功能完整示例
2019/05/10 Python
简单易懂Pytorch实战实例VGG深度网络
2019/08/27 Python
大家都说好用的Python命令行库click的使用
2019/11/07 Python
Django对接支付宝实现支付宝充值金币功能示例
2019/12/17 Python
python使用pyecharts库画地图数据可视化的实现
2020/03/25 Python
澳大利亚男士西服品牌:M.J.Bale
2018/02/06 全球购物
华为智利官方商店:Huawei Chile
2020/05/09 全球购物
与C++相比,Java中的数组有什么不同
2014/03/25 面试题
优秀求职信范文分享
2014/01/26 职场文书
家长给小学生的评语
2014/01/30 职场文书
代办委托书怎样写
2014/04/08 职场文书
毕业生找工作自荐书
2014/06/30 职场文书
离职报告范文
2014/11/04 职场文书
研究生学习计划书应该怎么写?
2019/09/10 职场文书
一定要知道的 25 个 Vue 技巧
2021/11/02 Vue.js