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 相关文章推荐
centos系统升级python 2.7.3
Jul 03 Python
Python实现检测服务器是否可以ping通的2种方法
Jan 01 Python
python获取标准北京时间的方法
Mar 24 Python
Python远程桌面协议RDPY安装使用介绍
Apr 15 Python
利用Python实现命令行版的火车票查看器
Aug 05 Python
python绘制铅球的运行轨迹代码分享
Nov 14 Python
Python爬虫实现(伪)球迷速成
Jun 10 Python
python读取txt文件并取其某一列数据的示例
Feb 19 Python
flask框架路由常用定义方式总结
Jul 23 Python
解决Django中修改js css文件但浏览器无法及时与之改变的问题
Aug 31 Python
Python figure参数及subplot子图绘制代码
Apr 18 Python
Python if else条件语句形式详解
Mar 24 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
zend framework多模块多布局配置
2011/02/26 PHP
php不用正则验证真假身份证
2013/11/06 PHP
php使用Jpgraph绘制复杂X-Y坐标图的方法
2015/06/10 PHP
PHP基于imagick扩展实现合成图片的两种方法【附imagick扩展下载】
2017/11/14 PHP
chrome浏览器不支持onmouseleave事件的解决技巧
2013/05/31 Javascript
JS简单实现文件上传实例代码(无需插件)
2013/11/15 Javascript
js 操作select与option(示例讲解)
2013/12/20 Javascript
js Object2String方便查看js对象内容
2014/11/24 Javascript
Nodejs为什么选择javascript为载体语言
2015/01/13 NodeJs
localResizeIMG先压缩后使用ajax无刷新上传(移动端)
2015/08/11 Javascript
iframe跨域通信封装详解
2015/08/11 Javascript
JS和jQuery使用submit方法无法提交表单的原因分析及解决办法
2016/05/17 Javascript
js 声明数组和向数组中添加对象变量的简单实例
2016/07/28 Javascript
移动端触屏幻灯片图片切换插件idangerous swiper.js
2017/04/10 Javascript
react build 后打包发布总结
2018/08/24 Javascript
vue-cli配置flexible过程详解
2019/07/04 Javascript
Vue 2.0双向绑定原理的实现方法
2019/10/23 Javascript
小程序自定义圆形进度条
2020/11/17 Javascript
python使用any判断一个对象是否为空的方法
2014/11/19 Python
python字典排序实例详解
2015/05/20 Python
Python用Bottle轻量级框架进行Web开发
2016/06/08 Python
kaggle+mnist实现手写字体识别
2018/07/26 Python
python使用folium库绘制地图点击框
2018/09/21 Python
python实现键盘控制鼠标移动
2020/11/27 Python
Python3实现腾讯云OCR识别
2018/11/27 Python
python验证码图片处理(二值化)
2019/11/01 Python
python实现将json多行数据传入到mysql中使用
2019/12/31 Python
Windows下Anaconda和PyCharm的安装与使用详解
2020/04/23 Python
python代码区分大小写吗
2020/06/17 Python
德国的大型美妆个护电商:Flaconi
2020/06/26 全球购物
中药专业自荐信范文
2014/03/18 职场文书
个人委托书范本
2014/09/13 职场文书
小学班主任事迹材料
2014/12/17 职场文书
村级干部党员公开承诺事项
2015/05/04 职场文书
如何通过一篇文章了解Python中的生成器
2022/04/02 Python
postgresql之greenplum字符串去重拼接方式
2023/05/08 PostgreSQL