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传递参数方式小结
Apr 17 Python
Python OS模块常用函数说明
May 23 Python
python比较两个列表大小的方法
Jul 11 Python
Python 探针的实现原理
Apr 23 Python
python使用xlrd与xlwt对excel的读写和格式设定
Jan 21 Python
python 自定义异常和异常捕捉的方法
Oct 18 Python
用Pycharm实现鼠标滚轮控制字体大小的方法
Jan 15 Python
python3 json数据格式的转换(dumps/loads的使用、dict to str/str to dict、json字符串/字典的相互转换)
Apr 01 Python
Python列表切片常用操作实例解析
Mar 10 Python
Python如何对齐字符串
Jul 30 Python
python 常用日期处理-- datetime 模块的使用
Sep 02 Python
Python中猜拳游戏与猜筛子游戏的实现方法
Sep 04 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实现动态web服务器方法
2015/07/29 PHP
Smarty分页实现方法完整实例
2016/05/11 PHP
利用php实现一周之内自动登录存储机制(cookie、session、localStorage)
2016/10/31 PHP
php遍历替换目录下文件指定内容的方法
2016/11/10 PHP
php实现URL加密解密的方法
2016/11/17 PHP
JavaScript高级程序设计 学习笔记 js高级技巧
2011/09/20 Javascript
关于jQuery UI 使用心得及技巧
2012/10/10 Javascript
toggle一个div显示或隐藏且可扩展成自定义下拉框
2013/09/12 Javascript
javascipt匹配单行和多行注释的正则表达式
2013/11/20 Javascript
jQuery中nextAll()方法用法实例
2015/01/07 Javascript
jQuery 选择器详解
2015/01/19 Javascript
基于JavaScript实现TAB标签效果
2016/01/12 Javascript
Vue.js学习之过滤器详解
2017/01/22 Javascript
socket.io学习教程之基础介绍(一)
2017/04/29 Javascript
基于bootstrap页面渲染的问题解决方法
2018/08/09 Javascript
vuejs前后端数据交互之从后端请求数据的实例
2018/08/11 Javascript
微信小程序页面间传值与页面取值操作实例分析
2019/04/30 Javascript
nodejs文件夹深层复制功能
2019/09/03 NodeJs
js实现蒙版效果
2020/01/11 Javascript
Layui弹框中数据表格中可双击选择一条数据的实现
2020/05/06 Javascript
微信小程序获取当前时间及星期几的实例代码
2020/09/20 Javascript
[10:04]国际邀请赛采访专栏:DK.Farseer,mouz.Black^,采访员Josh专访
2013/08/05 DOTA
paramiko模块安装和使用(远程登录服务器)
2014/01/27 Python
Python学习笔记之常用函数及说明
2014/05/23 Python
Python科学计算之Pandas详解
2017/01/15 Python
Python面向对象之静态属性、类方法与静态方法分析
2018/08/24 Python
对python xlrd读取datetime类型数据的方法详解
2018/12/26 Python
CSS3媒体查询Media Queries基础学习教程
2016/02/29 HTML / CSS
vue路由实现登录拦截
2021/03/24 Vue.js
化学教育专业求职信
2014/07/08 职场文书
县长“四风”对照检查材料思想汇报
2014/10/05 职场文书
2014年煤矿工人工作总结
2014/12/08 职场文书
介绍信的写法
2015/01/31 职场文书
三下乡个人总结
2015/03/04 职场文书
放牛班的春天观后感
2015/06/01 职场文书
2016年暑假家长对孩子评语
2015/12/01 职场文书