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解析nginx日志文件
May 11 Python
使用rst2pdf实现将sphinx生成PDF
Jun 07 Python
Python爬虫利用cookie实现模拟登陆实例详解
Jan 12 Python
python+matplotlib绘制饼图散点图实例代码
Jan 20 Python
pandas or sql计算前后两行数据间的增值方法
Apr 20 Python
Pyspider中给爬虫伪造随机请求头的实例
May 07 Python
python TKinter获取文本框内容的方法
Oct 11 Python
基于keras 模型、结构、权重保存的实现
Jan 24 Python
Python ORM编程基础示例
Feb 02 Python
matplotlib 曲线图 和 折线图 plt.plot()实例
Apr 17 Python
PyQt5实现仿QQ贴边隐藏功能的实例代码
May 24 Python
Python基于template实现字符串替换
Nov 27 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 Memcache 中实现消息队列
2009/11/24 PHP
Linux下安装PHP MSSQL扩展教程
2014/10/24 PHP
php实现上传图片文件代码
2015/07/19 PHP
PHP命令空间namespace及use的用法小结
2017/11/27 PHP
浅析PHP数据导出知识点
2018/02/17 PHP
Laravel开启跨域请求的方法
2019/10/13 PHP
php实现断点续传大文件示例代码
2020/06/19 PHP
使用CSS3实现字体颜色渐变的实现
2021/03/09 HTML / CSS
js构造函数、索引数组和属性的实现方式和使用
2014/11/16 Javascript
jQuery实现的登录浮动框效果代码
2015/09/26 Javascript
JS中用childNodes获取子元素换行会产生一个子元素
2016/12/08 Javascript
jQuery实现遍历XML节点和属性的方法示例
2018/04/29 jQuery
vue+element-ui动态生成多级表头的方法
2018/08/28 Javascript
vue 解决无法对未定义的值,空值或基元值设置反应属性报错问题
2020/07/31 Javascript
JS事件循环机制event loop宏任务微任务原理解析
2020/08/04 Javascript
Vue实现穿梭框效果
2020/09/30 Javascript
[01:42]TI4西雅图DOTA2前线报道 第一顿早饭哦
2014/07/08 DOTA
[03:34]2014DOTA2西雅图国际邀请赛 淘汰赛7月15日TOPPLAY
2014/07/15 DOTA
使用Python来编写HTTP服务器的超级指南
2016/02/18 Python
python中yaml配置文件模块的使用详解
2018/04/27 Python
Django项目主urls导入应用中views的红线问题解决
2019/08/10 Python
python面向对象 反射原理解析
2019/08/12 Python
Java如何基于wsimport调用wcf接口
2020/06/17 Python
python实现简易版学生成绩管理系统
2020/06/22 Python
Elasticsearch py客户端库安装及使用方法解析
2020/09/14 Python
CSS3近阶段篇之酷炫的3D旋转透视
2016/04/28 HTML / CSS
测量实习生自我鉴定
2013/09/19 职场文书
建筑施工员岗位职责
2013/11/26 职场文书
领班岗位职责范文
2014/02/06 职场文书
教师业务学习材料
2014/12/16 职场文书
2015年高校就业工作总结
2015/05/04 职场文书
2015年税务稽查工作总结
2015/05/26 职场文书
2016年秋季运动会广播稿
2015/12/21 职场文书
2016年学生会感恩节活动总结
2016/04/01 职场文书
再也不用花钱买漫画!Python爬取某漫画的脚本及源码
2021/06/09 Python
详解Anyscript开发指南绕过typescript类型检查
2022/09/23 Javascript