My mini project: display a text in my own PC
2015-08-31 22:28
239 查看
This project display a text on a pygame screen.
1.获取指定路径下的所有文件名称:
# -*- coding:utf-8 -*- # 此程序读取D:\test目录下的一个文本, 然后每次播放一条句子, 循环播放. # ----> Importing and initializing Pygame # Import a library of functions called 'pygame' import time from random import * import pygame,codecs import math from pygame.locals import * from sys import exit import codecs import os # Initialize the game engine pygame.init() # Define some colors BLACK = ( 0, 0, 0) WHITE = ( 255, 255, 255) GREEN = ( 0, 255, 0) MY_GREEN = (5,232,90) RED = ( 255, 0, 0) screen_size = (960,540) padding_left = 32 padding_right = 32 TEXT_WIDTH = screen_size[0] - padding_left - padding_right # 定义文档显示宽度 FPS_SET = 10 pygame.mixer.music.load('resources\\music\\3.wav') pygame.mixer.Channel(0) pygame.mixer.music.play() #列表显示目录下的文件 file_index = 0 file_list = [] for x in os.listdir('D:\\test'): file_list.append(x) print "[%d]\t%s" % (file_index, x) file_index += 1 print file_list # 打开文件,并读取; file_name = int(input("\nPLEASE INPUT THE TXT NAME IN DIRECTORY: D:/test/")) file_path = 'D:/test/' + file_list[file_name] print u"文件路径:",file_path time.sleep(5) screen = pygame.display.set_mode(screen_size) font_list = ['COURIERTXT','Courier New','仿宋'] font = pygame.font.SysFont(font_list[0], 28) font_hight = font.get_linesize() # - 生成新的字符串组 fr = open(file_path,'r').readlines() #fr = codecs.open(file_path,'r',encoding='gbk').readlines() fr = fr[1:] line_index = 0 print u'\n文本内容的条目数量为', len(fr) # - 获取一个字符串 current_line = fr[line_index].decode('utf-8').encode('gb2312') # - 读取当前行内容; word_group = current_line.split() word_index = 0 letter_index = 0 LINE_NUM_SET = 1 text_line_number = LINE_NUM_SET width_temp = padding_left string_of_word = '' wait_seconds_left = 0 # print font_hight # print font.size('How are you?') # print font.size('How are you, Buddy?') print 'width of current_line is: ', font.size(current_line)[0] # font.size('string')得到这段字符串显示的长度和高度值; def fontplay(s,x,y): screen.blit(font.render(s, True,MY_GREEN), (x, font_hight * y - 16)) # - - - - - - - - - - - - - - - - - sound_type = pygame.mixer.Sound('resources\\audio\\Type.wav') done = False clock = pygame.time.Clock() # ---------------------|程序主循环|---------------------- while not done: # --- Main event loop for event in pygame.event.get(): if event.type == pygame.QUIT: done = True if event.type == KEYDOWN: if event.key == K_SPACE: wait_seconds_left = 0.8 if event.key == K_ESCAPE: #exit() pygame.quit() print 'len(fr):%d line_index:%d word_index:%d, letter_index:%d'% \ (len(fr), line_index, word_index, letter_index) print 'Length of current_line:', len(current_line) print 'wait_seconds_left:%.2f\n' % (wait_seconds_left) # 在一个单词输出完成时: 首先.输出空格,光标响应移动,单词索引加1. if letter_index > len(word_group[word_index]) - 1: #fontplay(' ', width_temp, text_line_number) width_temp += 22 # --------------------单词间距 word_index += 1 letter_index = 0 # 监测句子是否过界: if word_index > (len(word_group) - 1): text_line_number = 1 # 光标行数索引 word_index = 0 width_temp = padding_left line_index += 1 # 文章句子索引 # 增加时间等待: wait_seconds_left = len(current_line) * 0.15 + 2.25 if line_index > len(fr) - 1: line_index = 0 # 跳过空行 current_line = fr[line_index] while len(current_line) < 5: line_index += 1 current_line = fr[line_index] word_group = current_line.split() # 检查文章是否到达末尾: # 如果当前光标加长单词长度后过界: right_limit = width_temp + font.size(word_group[word_index])[0] if right_limit > screen_size[0] - padding_right: width_temp = padding_left text_line_number += 1 wait_seconds_left -= 1.0 / FPS_SET if wait_seconds_left < 0: wait_seconds_left = 0 if wait_seconds_left > 0.6 and wait_seconds_left < 0.8: screen.fill(BLACK) pygame.mixer.music.play() # SOUND PLAY if wait_seconds_left == 0: ch = word_group[word_index][letter_index] fontplay(ch, width_temp, text_line_number) width_temp += font.size(ch)[0] + 4 # -----------------字符间距 print 'width of \'%s\' is %d.' % (ch, font.size(ch)[0]) print 'width_temp ---> %d\n\n' %(width_temp) letter_index += 1 # --- Go ahead and update the screen with what we've drawn. pygame.display.flip() # --- Limit to 60 frames per second clock.tick(FPS_SET) pygame.quit()代码说明:
1.获取指定路径下的所有文件名称:
os.listdir(A_DIRECTORY)2.载入系统字体:
font = pygame.font.SysFont(font_list[0], 28)
相关文章推荐
- MySQL存储过程之新sql语句--变量
- 求最大连续乘积
- Executor
- VS快捷键
- Socket模型详解
- JVM的相关知识整理和学习 见解1
- J2EE 13个规范概览
- POJ 2752 Seek the Name, Seek the Fame (KMP next 数组 变形)
- 面向对象的三大特征--封装/继承/多态
- 1044. Shopping in Mars (25)
- 阴天快乐 -- 陈奕迅(eason)
- spring BeanFactory和ApplicationContext详解
- Codeforces GYM 100651 D I Conduit! (水计算几何)
- 链你妹系列1—关于链表,不得不吐槽的那些事
- Unity_一张图看懂MonoBehaviour的生命周期
- linux 文件系统解析及相关命令(转)
- 音频解析
- 14.高度最小的BST
- javascript学习总结(二):DOM常用方法。
- Context