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实现将目录中TXT合并成一个大TXT文件的方法
Jul 15 Python
深入理解python中的select模块
Apr 23 Python
Python实现PS滤镜Fish lens图像扭曲效果示例
Jan 29 Python
python实现mysql的读写分离及负载均衡
Feb 04 Python
Python 实现两个服务器之间文件的上传方法
Feb 13 Python
Python Django 前后端分离 API的方法
Aug 28 Python
Pytorch 数据加载与数据预处理方式
Dec 31 Python
python Tensor和Array对比分析
Jan 08 Python
谈谈Python:为什么类中的私有属性可以在外部赋值并访问
Mar 05 Python
用python爬虫批量下载pdf的实现
Dec 01 Python
OpenCV-Python实现轮廓拟合
Jun 08 Python
Python scrapy爬取起点中文网小说榜单
Jun 13 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中操作ini配置文件的方法
2013/04/25 PHP
使用PHP similar text计算两个字符串相似度
2015/11/06 PHP
php 无限分类 树形数据格式化代码
2016/10/11 PHP
THINKPHP5分页数据对象处理过程解析
2020/10/28 PHP
javascript 对象定义方法 简单易学
2009/03/22 Javascript
jQuery.extend 函数详解
2012/02/03 Javascript
js获取指定日期前后的日期代码
2013/08/20 Javascript
基于jQuery实现的仿百度首页滑动选项卡效果代码
2015/11/16 Javascript
基于Jquery和html5的7款个性化地图插件
2015/11/17 Javascript
jQuery解析与处理服务器端返回xml格式数据的方法详解
2016/07/04 Javascript
微信小程序 video组件详解
2016/10/25 Javascript
Bootstrap 实现查询的完美方法
2016/10/26 Javascript
Vue数据驱动模拟实现4
2017/01/12 Javascript
JavaScript之underscore_动力节点Java学院整理
2017/07/03 Javascript
Nuxt.js实战详解
2018/01/18 Javascript
angular4 JavaScript内存溢出问题
2018/03/06 Javascript
小程序实现抽奖动画
2020/04/16 Javascript
vue仿携程轮播图效果(滑动轮播,下方高度自适应)
2021/02/11 Vue.js
Python网站验证码识别
2016/01/25 Python
Python 描述符(Descriptor)入门
2016/11/20 Python
python函数的5种参数详解
2017/02/24 Python
关于python2 csv写入空白行的问题
2018/06/22 Python
Python 中字符串拼接的多种方法
2018/07/30 Python
Python实现的服务器示例小结【单进程、多进程、多线程、非阻塞式】
2019/05/23 Python
python函数修饰符@的使用方法解析
2019/09/02 Python
python打印直角三角形与等腰三角形实例代码
2019/10/20 Python
Python 实现opencv所使用的图片格式与 base64 转换
2020/01/09 Python
tensorflow实现在函数中用tf.Print输出中间值
2020/01/21 Python
基于OpenCV的网络实时视频流传输的实现
2020/11/15 Python
在css3中background-clip属性与background-origin属性的用法介绍
2012/11/13 HTML / CSS
北美最大的零售退货翻新商:VIP Outlet
2019/11/21 全球购物
如何实现一个自定义类的序列化
2012/05/22 面试题
工作作风承诺书
2014/08/30 职场文书
教你用Java在个人电脑上实现微信扫码支付
2021/06/13 Java/Android
Python编程中Python与GIL互斥锁关系作用分析
2021/09/15 Python
Java 数据结构七大排序使用分析
2022/04/02 Java/Android