pygame游戏之旅 游戏中添加显示文字
作者:观月执白 发布时间:2023-03-26 02:56:23
标签:pygame,添加文字
本文为大家分享了pygame游戏之旅的第5篇,供大家参考,具体内容如下
在游戏中添加显示文字:
这里自己定义一个crash函数接口:
def crash():
message_diaplay('You Crashed')
然后实现接口函数message_display(text)
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()
在这其中定义了一个函数text_objects(text, largeText),最后实现这个函数即可
def text_objects(text, font):
textSurface = font.render(text, True, white)
return textSurface, textSurface.get_rect()
全部代码:
import pygame
import time
pygame.init()
white = (255,255,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 car(x, y):
gameDisplay.blit(carImg, (x,y))
def text_objects(text, font):
textSurface = font.render(text, True, white)
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 game_loop():
x = display_width * 0.45
y = display_height * 0.8
x_change = 0
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
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)
car(x,y)
if x > display_width - car_width or x < 0:
gameExit = True
pygame.display.update()
clock.tick(60)
crash()
#game_loop()
pygame.quit()
quit()
结果图:
来源:https://blog.csdn.net/pianzang5201/article/details/78303954
0
投稿
猜你喜欢
- 一、创建堆heapq有两种方式创建堆, 一种是使用一个空列表,然后使用heapq.heappush()函数把值加入堆中,另外一种就是使用he
- 用JDBC实现对MySQL的“增删改查”:import java.sql.Connection;im
- Session作用Session的根本作用就是在服务端存储用户和服务器会话的一些信息。典型的应用有:1、判断用户是否登录。2、购物车功能。s
- 小编最近由于工作原因要用到python,一门新的知识需要接触,对于我来说难度还是很大的。python工程目录结构每次创建一个python工程
- 本文实例为大家分享了Python3 Tkinkter + SQLite 实现登录和注册界面,供大家参考,具体内容如下Ubuntu14 + P
- 作者:AngelGavin 出处:CSDNInternet Explorer 5.0 对 XML 提供哪个级别的支持?Inter
- SQL Server数据库日志清除的两个方法:方法一一般情况下,SQL数据库的收缩并不能很大程度上减小数据库大小,其主要作用是收缩日志大小,
- 当你使用UPDATE, INSERT, DELETE语句更新数据的时候,你就改变了两个地方的数据:log buffer和data buffe
- 本文实例讲述了JavaScript点击按钮后弹出透明浮动层的方法。分享给大家供大家参考。具体分析如下:这里实现点击后页面变灰色,并用JS弹出
- 错误代码错误消息说 明ASP0100Out of memory内存不足(不能分配要求的内存)ASP0101Unexpected
- 上一课:ACCESS入门教程:初识Access 2000窗口接口简介 通过上一课的学习,你是否感觉Access的窗口和接口还有点搞不清楚,对
- 一、通用爬虫通用网络爬虫是搜索引擎抓取系统(Baidu、Google、Sogou等)的一个重要组成部分。主要目的是将互联网上的网页下载到本地
- 如下所示:#! usr/bin/python#coding=utf-8 import numpy as npimport matplotli
- 最近人工智能等多门课需要复现论文,近两年的论文很多都是基于Pytorch环境做的实验,所以,这里总结一下Pytorch的安装教程,做好最快、
- 工厂模式(Factory Pattern)是什么工厂模式是一种创建型模式,它提供了一种创建对象的最佳方式。在工厂模式中,我们在创建对象时不会
- PHP Date/Time 简介Date/Time 函数允许您从 PHP 脚本运行的服务器上获取日期和时间。您可以使用 Date/Time
- 实例如下:#! /usr/bin/python# -*- coding: utf-8 -*-import osdef del_dir_tre
- Protocol Buffers (类似XML的一种数据描述语言)最新版本2.3里,protoc—py_out命令只生成原生的P
- 或许现在关心交互设计的设计师们大部分来自于了互联网行业,所以我们看到当你搜索“交互设计”时更多的BLOG和文章是在谈论互联网,网站的导航,注
- 在Jupyter Notebook上使用Python+opencv实现如下简单车牌字符切割。关于opencv库的安装可以参考:Python下