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随机生成一个6位的验证码代码分享
Mar 24 Python
Python基于DES算法加密解密实例
Jun 03 Python
python中pandas.DataFrame对行与列求和及添加新行与列示例
Mar 12 Python
Tornado高并发处理方法实例代码
Jan 15 Python
DataFrame 将某列数据转为数组的方法
Apr 13 Python
Python设计模式之简单工厂模式实例详解
Jan 22 Python
Pyqt5如何让QMessageBox按钮显示中文示例代码
Apr 11 Python
django 邮件发送模块smtp使用详解
Jul 22 Python
django创建简单的页面响应实例教程
Sep 06 Python
Python爬虫实现的根据分类爬取豆瓣电影信息功能示例
Sep 15 Python
Pytorch.nn.conv2d 过程验证方式(单,多通道卷积过程)
Jan 03 Python
使用scrapy实现增量式爬取方式
Jun 21 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框架的性能
2008/01/10 PHP
mysql+php分页类(已测)
2008/03/31 PHP
php include,include_once,require,require_once
2008/09/05 PHP
ThinkPHP实现ajax仿官网搜索功能实例
2014/12/02 PHP
在laravel中使用Symfony的Crawler组件分析HTML
2017/06/19 PHP
php-beanstalkd消息队列类实例分享
2017/07/19 PHP
PHP微信开发之微信录音临时转永久存储
2018/01/26 PHP
PHP JWT初识及其简单示例
2018/10/10 PHP
使用vs code编辑调试php配置的方法
2019/01/29 PHP
ymPrompt的doHandler方法来实现获取子窗口返回值的方法
2010/06/25 Javascript
javascript中创建对象的三种常用方法
2010/12/30 Javascript
深入理解javascript中的立即执行函数(function(){…})()
2014/06/12 Javascript
jQuery实现平滑滚动的标签分栏切换效果
2015/08/28 Javascript
jQuery插件EasyUI校验规则 validatebox验证框
2015/11/29 Javascript
最全面的JS倒计时代码
2016/09/17 Javascript
Canvas 制作动态进度加载水球详解及实例代码
2016/12/09 Javascript
vue实现随机验证码功能的实例代码
2019/04/30 Javascript
python操作mysql中文显示乱码的解决方法
2014/10/11 Python
python求列表交集的方法汇总
2014/11/10 Python
浅述python中argsort()函数的实例用法
2017/03/30 Python
python pandas中对Series数据进行轴向连接的实例
2018/06/08 Python
Python2.7实现多进程下开发多线程示例
2019/05/31 Python
Python学习笔记之读取文件、OS模块、异常处理、with as语法示例
2019/06/04 Python
Python检查 云备份进程是否正常运行代码实例
2019/08/22 Python
django实现类似触发器的功能
2019/11/15 Python
真正了解CSS3背景下的@font face规则
2017/05/04 HTML / CSS
Trench London官方网站:高级风衣和意大利皮夹克
2020/07/11 全球购物
投资协议书范本
2014/04/21 职场文书
公务员中国梦演讲稿
2014/08/19 职场文书
python正则表达式re.search()的基本使用教程
2021/05/21 Python
一小时学会TensorFlow2之基本操作2实例代码
2021/09/04 Python
「玫瑰之王的葬礼」舞台剧主视觉图公开
2022/03/21 日漫
Innodb存储引擎中的后台线程详解
2022/04/03 MySQL
Python开发五子棋小游戏
2022/04/28 Python
springboot读取resources下文件的方式详解
2022/06/21 Java/Android
CSS实现背景图片全屏铺满自适应的3种方式
2022/07/07 HTML / CSS