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的Django框架中实现Hacker News的一些功能
Apr 17 Python
Python中统计函数运行耗时的方法
May 05 Python
Python实现的根据文件名查找数据文件功能示例
May 02 Python
Python爬虫之正则表达式的使用教程详解
Oct 25 Python
python下载微信公众号相关文章
Feb 26 Python
PyQt打开保存对话框的方法和使用详解
Feb 27 Python
win10安装tesserocr配置 Python使用tesserocr识别字母数字验证码
Jan 16 Python
如何使用selenium和requests组合实现登录页面
Feb 03 Python
Python图像处理库PIL中图像格式转换的实现
Feb 26 Python
keras在构建LSTM模型时对变长序列的处理操作
Jun 29 Python
详解如何修改jupyter notebook的默认目录和默认浏览器
Jan 24 Python
Python机器学习之逻辑回归
May 11 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的魔术方法__get()和__set()使用介绍
2012/09/19 PHP
php通过ksort()函数给关联数组按照键排序的方法
2015/03/18 PHP
javascript 面向对象全新理练之原型继承
2009/12/03 Javascript
JS 对象介绍
2010/01/20 Javascript
JavaScript对象链式操作代码(jquery)
2010/07/04 Javascript
js密码强度实时检测代码
2016/03/02 Javascript
require简单实现单页应用程序(SPA)
2016/07/12 Javascript
让浏览器崩溃的12行JS代码(DoS攻击分析及防御)
2016/10/10 Javascript
jQuery实现的小图列表,大图展示效果幻灯片示例
2016/10/25 Javascript
简单实现vue验证码60秒倒计时功能
2017/10/11 Javascript
javascript深拷贝、浅拷贝和循环引用深入理解
2018/05/27 Javascript
vue绑定事件后获取绑定事件中的this方法
2018/09/15 Javascript
IE11下CKEditor在Bootstrap Modal中下拉问题的解决
2019/09/25 Javascript
Node.js API详解之 console模块用法详解
2020/05/12 Javascript
vuex中store存储store.commit和store.dispatch的用法
2020/07/24 Javascript
javascript实现移动端上传图片功能
2020/08/18 Javascript
JS中多层次排序算法的实现代码
2021/01/06 Javascript
jQuery实现购物车全功能
2021/01/11 jQuery
python递归计算N!的方法
2015/05/05 Python
详解Python3中的Sequence type的使用
2015/08/01 Python
Python网络编程基于多线程实现多用户全双工聊天功能示例
2018/04/10 Python
基于python的图片修复程序(实现水印去除)
2018/06/04 Python
python实现RabbitMQ的消息队列的示例代码
2018/11/08 Python
详解Python3除法之真除法、截断除法和下取整对比
2019/05/23 Python
通过pycharm使用git的步骤(图文详解)
2019/06/13 Python
学Python 3的理由和必要性
2019/11/19 Python
解决Keyerror ''acc'' KeyError: ''val_acc''问题
2020/06/18 Python
质检员的岗位职责
2013/11/15 职场文书
电子商务专业推荐信范文
2013/12/02 职场文书
一句话工作感言
2014/03/01 职场文书
田径运动会开幕式及主持词
2014/03/28 职场文书
教师自我剖析材料(群众路线)
2014/09/29 职场文书
大学生党员自我剖析材料
2014/10/06 职场文书
单位个人查摆问题及整改措施
2014/10/28 职场文书
php引用传递
2021/04/01 PHP
Node.js实现爬取网站图片的示例代码
2022/04/04 NodeJs