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 第一步 hello world
Sep 25 Python
python随机生成指定长度密码的方法
Apr 04 Python
python实现报表自动化详解
Nov 16 Python
利用pandas将numpy数组导出生成excel的实例
Jun 14 Python
Python 访问限制 private public的详细介绍
Oct 16 Python
Python/Django后端使用PIL Image生成头像缩略图
Apr 30 Python
8种用Python实现线性回归的方法对比详解
Jul 10 Python
Pandas 缺失数据处理的实现
Nov 04 Python
如何使用Python发送HTML格式的邮件
Feb 11 Python
将数据集制作成VOC数据集格式的实例
Feb 17 Python
Python3读取和写入excel表格数据的示例代码
Jun 09 Python
Python装饰器详细介绍
Mar 25 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
DOTA2游戏同人动画《龙之血》导演接受采访
2021/03/05 欧美动漫
POSIX 风格和兼容 Perl 风格两种正则表达式主要函数的类比(preg_match, preg_replace, ereg, ereg_replace)
2010/10/12 PHP
使用php+apc实现上传进度条且在IE7下不显示的问题解决方法
2013/04/25 PHP
php访问数组最后一个元素的函数end()用法
2015/03/18 PHP
CodeIgniter整合Smarty的方法详解
2017/08/25 PHP
PHP实现的大文件切割与合并功能示例
2018/04/10 PHP
javascript SpiderMonkey中的函数序列化如何进行
2012/12/05 Javascript
IE关闭时判断及AJAX注销案例学习
2013/02/18 Javascript
整理Javascript基础语法学习笔记
2015/11/29 Javascript
jQuery获取file控件中图片的宽高与大小
2016/08/04 Javascript
浅谈jquery.form.js的ajaxSubmit和ajaxForm的使用
2016/09/09 Javascript
AngularJS 指令的交互详解及实例代码
2016/09/14 Javascript
深入理解Node.js的HTTP模块
2016/10/12 Javascript
Js实现京东无延迟菜单效果实例(demo)
2017/06/02 Javascript
webpack学习教程之publicPath路径问题详解
2017/06/17 Javascript
HTML5+JS+JQuery+ECharts实现异步加载问题
2017/12/16 jQuery
vue中element 上传功能的实现思路
2018/07/06 Javascript
不得不知的ES6小技巧
2018/07/28 Javascript
微信小程序时间轴实现方法示例
2019/01/14 Javascript
JavaScript+HTML5 canvas实现放大镜效果完整示例
2019/05/15 Javascript
怎样在vue项目下添加ESLint的方法
2019/05/16 Javascript
JavaScript实现网页跨年倒计时
2020/12/02 Javascript
Django的HttpRequest和HttpResponse对象详解
2018/01/26 Python
Python多线程原理与用法详解
2018/08/20 Python
python抖音表白程序源代码
2019/04/07 Python
在Python中过滤Windows文件名中的非法字符方法
2019/06/10 Python
django数据模型on_delete, db_constraint的使用详解
2019/12/24 Python
使用Tkinter制作信息提示框
2020/02/18 Python
Giglio英国站:意大利奢侈品购物网
2018/03/06 全球购物
《鸿门宴》教学反思
2014/04/22 职场文书
2014年国庆节活动总结
2014/08/26 职场文书
小学教育见习报告
2014/10/31 职场文书
2014村书记党建工作汇报材料
2014/11/02 职场文书
2014年教务处工作总结
2014/12/03 职场文书
教师党员自我评价范文
2015/03/04 职场文书
2015年幼师工作总结
2015/04/28 职场文书