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中的几种数据类型
Jan 02 Python
Python黑魔法@property装饰器的使用技巧解析
Jun 16 Python
Python使用迭代器捕获Generator返回值的方法
Apr 05 Python
Python wxPython库消息对话框MessageDialog用法示例
Sep 03 Python
关于不懂Chromedriver如何配置环境变量问题解决方法
Jun 12 Python
python经典趣味24点游戏程序设计
Jul 26 Python
PyTorch实现AlexNet示例
Jan 14 Python
Python 一行代码能实现丧心病狂的功能
Jan 18 Python
python GUI库图形界面开发之PyQt5简单绘图板实例与代码分析
Mar 08 Python
Python 求向量的余弦值操作
Mar 04 Python
Python打包为exe详细教程
May 18 Python
python实现语音常用度量方法的代码详解
May 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
snoopy PHP版的网络客户端提供本地下载
2008/04/15 PHP
PHP MSSQL 存储过程的方法
2008/12/24 PHP
抛弃 PHP 代价太高
2016/04/26 PHP
PhpStorm的使用教程(本地运行PHP+远程开发+快捷键)
2020/03/26 PHP
javascript引用对象的方法
2007/01/11 Javascript
JS跨域总结
2012/08/30 Javascript
浅析JavaScript中的事件机制
2015/06/04 Javascript
用原生JS对AJAX做简单封装的实例代码
2016/07/13 Javascript
jQuery用FormData实现文件上传的方法
2016/11/21 Javascript
Angularjs 与 bower安装和使用详解
2017/05/11 Javascript
移动端网页开发调试神器Eruda的介绍与使用技巧
2017/10/30 Javascript
详解vue-cli快速构建vue应用并实现webpack打包
2017/12/13 Javascript
vue 中directive功能的简单实现
2018/01/05 Javascript
Angular+Ionic使用queryParams实现跳转页传值的方法
2020/09/05 Javascript
vue+swiper实现左右滑动的测试题功能
2020/10/30 Javascript
[02:19]DOTA2女子战队FOX视频专访:希望更多美眉一起加入
2013/10/15 DOTA
python根据距离和时长计算配速示例
2014/02/16 Python
Django中模型Model添加JSON类型字段的方法
2015/06/17 Python
Django Admin实现上传图片校验功能
2016/03/06 Python
对于Python中RawString的理解介绍
2016/07/07 Python
VScode编写第一个Python程序HelloWorld步骤
2018/04/06 Python
python opencv人脸检测提取及保存方法
2018/08/03 Python
python闭包、深浅拷贝、垃圾回收、with语句知识点汇总
2020/03/11 Python
Python使用pycharm导入pymysql教程
2020/09/16 Python
python反爬虫方法的优缺点分析
2020/11/25 Python
HTML+CSS3模拟心的跳动实例代码
2017/09/05 HTML / CSS
CSS3 实现飘动的云朵动画
2020/12/01 HTML / CSS
html5中 media(播放器)的api使用指南
2014/12/26 HTML / CSS
广告语设计及教案
2014/03/21 职场文书
《月球之谜》教学反思
2014/04/10 职场文书
个人股份转让协议书范本
2014/10/26 职场文书
车间统计员岗位职责
2015/04/14 职场文书
中国文明网2015年“向国旗敬礼”活动网上签名寄语
2015/09/24 职场文书
新学期开学寄语2016
2015/12/04 职场文书
如何使用JavaScript策略模式校验表单
2021/04/29 Javascript
GO语言字符串处理函数之处理Strings包
2022/04/14 Golang