pygame游戲之旅 游戲中添加顯示文字
本文為大家分享了pygame游戲之旅的第5篇,供大家參考,具體內(nèi)容如下
在游戲中添加顯示文字:
這里自己定義一個(gè)crash函數(shù)接口:
def crash():
message_diaplay('You Crashed')
然后實(shí)現(xiàn)接口函數(shù)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()
在這其中定義了一個(gè)函數(shù)text_objects(text, largeText),最后實(shí)現(xiàn)這個(gè)函數(shù)即可
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()
結(jié)果圖:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
win10系統(tǒng)Anaconda和Pycharm的Tensorflow2.0之CPU和GPU版本安裝教程
這篇文章主要介紹了win10系統(tǒng) Anaconda 和 Pycharm 的 Tensorflow2.0 之 CPU和 GPU 版本安裝教程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
如何解決pycharm中用matplotlib畫圖不顯示中文的問題
這篇文章主要介紹了如何解決pycharm中用matplotlib畫圖不顯示中文的問題,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下2022-06-06
Python列表推導(dǎo)式,元組推導(dǎo)式,字典推導(dǎo)式,集合推導(dǎo)式
這篇文章主要介紹了Python列表推導(dǎo)式,元組推導(dǎo)式,字典推導(dǎo)式,集合推導(dǎo)式,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-09-09
在python中利用最小二乘擬合二次拋物線函數(shù)的方法
今天小編就為大家分享一篇在python中利用最小二乘擬合二次拋物線函數(shù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-12-12
Python算法輸出1-9數(shù)組形成的結(jié)果為100的所有運(yùn)算式
這篇文章主要介紹了Python算法輸出1-9數(shù)組形成的結(jié)果為100的所有運(yùn)算式,然后介紹了另外一個(gè)相關(guān)實(shí)例,具體內(nèi)容請(qǐng)參閱正文,需要的朋友可以參考下。2017-11-11
詳解安裝mitmproxy以及遇到的坑和簡(jiǎn)單用法
mitmproxy 是一款工具,也可以說是 python 的一個(gè)包,在命令行操作的工具。這篇文章主要介紹了詳解安裝mitmproxy以及遇到的坑和簡(jiǎn)單用法,感興趣的小伙伴們可以參考一下2019-01-01

