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实现批量转换文件编码的方法
Jul 28 Python
python中OrderedDict的使用方法详解
May 05 Python
读取本地json文件,解析json(实例讲解)
Dec 06 Python
python实现远程通过网络邮件控制计算机重启或关机
Feb 22 Python
Windows下Python3.6安装第三方模块的方法
Nov 22 Python
Python 运行.py文件和交互式运行代码的区别详解
Jul 02 Python
python制作英语翻译小工具代码实例
Sep 09 Python
python中的split()函数和os.path.split()函数使用详解
Dec 21 Python
Python调用Windows API函数编写录音机和音乐播放器功能
Jan 05 Python
django实现HttpResponse返回json数据为中文
Mar 27 Python
Python select及selectors模块概念用法详解
Jun 22 Python
python 实现&quot;神经衰弱&quot;翻牌游戏
Nov 09 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
一个可以删除字符串中HTML标记的PHP函数
2006/10/09 PHP
php解决抢购秒杀抽奖等大流量并发入库导致的库存负数的问题
2014/06/19 PHP
ThinkPHP2.x防范XSS跨站攻击的方法
2015/09/25 PHP
PHP面向对象程序设计重载(overloading)操作详解
2019/06/13 PHP
为数据添加append,remove功能
2006/10/03 Javascript
(function(){})()的用法与优点
2007/03/11 Javascript
HTML node相关的一些资料整理
2010/01/01 Javascript
jQuery 过滤not()与filter()实例代码
2012/05/10 Javascript
阻止子元素继承父元素事件具体思路及实现
2013/05/02 Javascript
浅析showModalDialog数据缓存问题(用禁止浏览器缓存解决)
2013/07/09 Javascript
关于javascript event flow 的一个bug详解
2013/09/17 Javascript
制作jquery遮罩层效果导航菜单代码分享
2013/12/25 Javascript
深入分析JSONP跨域的原理
2014/12/10 Javascript
jQuery实现精美的多级下拉菜单特效
2015/03/14 Javascript
详解jQuery中的empty、remove和detach
2016/04/11 Javascript
原生JS取代一些JQuery方法的简单实现
2016/09/20 Javascript
js实现简易垂直滚动条
2017/02/22 Javascript
js获取浏览器和屏幕的各种宽度高度
2017/02/22 Javascript
在使用JSON格式处理数据时应该注意的问题小结
2017/05/20 Javascript
微信小程序实现预览图片功能
2020/10/22 Javascript
Python随机读取文件实现实例
2017/05/25 Python
python实现将一个数组逆序输出的方法
2018/06/25 Python
深入浅析Python中list的复制及深拷贝与浅拷贝
2018/09/03 Python
Python简单过滤字母和数字的方法小结
2019/01/09 Python
Python的Tkinter点击按钮触发事件的例子
2019/07/19 Python
Numpy一维线性插值函数的用法
2020/04/22 Python
Python基于Webhook实现github自动化部署
2020/11/28 Python
澳大利亚女装精品店:Alannah Hill
2020/07/29 全球购物
成人大专生实习期的自我评价
2013/10/02 职场文书
文明礼貌演讲稿
2014/05/12 职场文书
2014年高二班主任工作总结
2014/12/16 职场文书
教代会闭幕词
2015/01/28 职场文书
老公出轨后的保证书
2015/05/08 职场文书
《槐乡的孩子》教学反思
2016/02/20 职场文书
创业分两种人:那么哪些适合创业?,哪些适合不适合创业呢?
2019/08/23 职场文书
帮你提高开发效率的JavaScript20个技巧
2021/06/18 Javascript