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使用urllib模块和pyquery实现阿里巴巴排名查询
Jan 16 Python
PyQt 线程类 QThread使用详解
Jul 16 Python
python读取与写入csv格式文件的示例代码
Dec 16 Python
python通过百度地图API获取某地址的经纬度详解
Jan 28 Python
pandas DataFrame实现几列数据合并成为新的一列方法
Jun 08 Python
Pycharm设置去除显示的波浪线方法
Oct 28 Python
Python logging设置和logger解析
Aug 28 Python
python常用排序算法的实现代码
Nov 08 Python
PyTorch 普通卷积和空洞卷积实例
Jan 07 Python
python GUI框架pyqt5 对图片进行流式布局的方法(瀑布流flowlayout)
Mar 12 Python
python小白切忌乱用表达式
May 29 Python
python 爬取免费简历模板网站的示例
Sep 27 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设计聊天室步步通
2006/10/09 PHP
PHP 循环列出目录内容的函数代码
2010/05/26 PHP
php中通过curl检测页面是否被百度收录
2013/09/27 PHP
浅谈php正则表达式中的非贪婪模式匹配的使用
2014/11/25 PHP
PHP编程实现的TCP服务端和客户端功能示例
2018/04/13 PHP
jquery easyui的tabs使用时的问题
2010/03/23 Javascript
javascript实现禁止复制网页内容汇总
2015/12/30 Javascript
jQuery实现将div中滚动条滚动到指定位置的方法
2016/08/10 Javascript
使用BootStrap实现悬浮窗口的效果
2016/12/13 Javascript
nodejs处理图片的中间件node-images详解
2017/05/08 NodeJs
使用Vue.js和Flask来构建一个单页的App的示例
2018/03/21 Javascript
Vue实现侧边菜单栏手风琴效果实例代码
2018/05/31 Javascript
JQuery事件冒泡和默认行为代码实例
2020/05/13 jQuery
Python中的XML库4Suite Server的介绍
2015/04/14 Python
python 调用c语言函数的方法
2017/09/29 Python
python 实现求解字符串集的最长公共前缀方法
2018/07/20 Python
为何人工智能(AI)首选Python?读完这篇文章你就知道了(推荐)
2019/04/06 Python
Python中six模块基础用法
2019/12/08 Python
python实现加密的方式总结
2020/01/19 Python
Django之腾讯云短信的实现
2020/06/12 Python
python实现无边框进度条的实例代码
2020/12/30 Python
基于Python-Pycharm实现的猴子摘桃小游戏(源代码)
2021/02/20 Python
CSS3的first-child选择器实战攻略
2016/04/28 HTML / CSS
光声世纪笔试题目
2012/08/25 面试题
高中生毕业自我鉴定
2013/10/10 职场文书
大学生就业自我鉴定
2013/10/26 职场文书
医科学校毕业生自荐信
2013/11/09 职场文书
社区包粽子活动方案
2014/01/21 职场文书
服装设计专业毕业生求职信
2014/04/09 职场文书
机械工程师岗位职责
2014/06/16 职场文书
高中班主任评语
2014/12/30 职场文书
中班下学期幼儿评语
2014/12/30 职场文书
天河观后感
2015/06/11 职场文书
Python爬虫基础之初次使用scrapy爬虫实例
2021/06/26 Python
postman中form-data、x-www-form-urlencoded、raw、binary的区别介绍
2022/01/18 HTML / CSS
SQL CASE 表达式的具体使用
2022/03/21 SQL Server