#!/usr/bin/env python
# _*_ coding: utf-8 _*_
import pygame
from pygame.locals import *
import sys,os,codecs
screen_size = (640, 480)
def main():
pygame.init()
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption(u"位置指定、文字列指定")
text = Text("test")
while True:
screen.fill((255,200,200))
text.draw(screen, (20, 50))
pygame.display.update()
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
if event.type == KEYDOWN and event.key == K_SPACE:
text.toggle()
class Text:
text_images = [] #画像化した文字列を入れるリスト
file_data = [] #読み込んだファイルの文字列を入れるリスト
font_size = 40
def __init__(self, name):
self.name = name
self.font = pygame.font.SysFont("msminchomspmincho", self.font_size)
self.visible = False
self.render(u"ベイベー!")
def load(self):
file = os.path.join("data", self.name + ".txt")
fp = codecs.open(file, "r", "utf-8")
for line in fp:
line = line.rstrip()
self.file_data = line.split(",")
fp.close()
def render(self, str):
if str == None:
#ファイルから読み込んだ文字列を画像化
self.load()
for i in self.file_data:
text_image = self.font.render(i, True, (0,0,0))
self.text_images.append(text_image)
else:
#指定した文字列を画像化
text_image = self.font.render(str, True, (0,0,0))
self.text_images.append(text_image)
def draw(self, screen, pos):
x, y = pos
if self.visible == True:
for i, text_image in enumerate(self.text_images):
dy = y + self.font_size * i
screen.blit(text_image, (x, dy))
else:
return
def toggle(self):
self.visible = not self.visible
if __name__ == "__main__":
main()
IyEvdXNyL2Jpbi9lbnYgcHl0aG9uCiMgXypfIGNvZGluZzogdXRmLTggXypfCmltcG9ydCBweWdhbWUKZnJvbSBweWdhbWUubG9jYWxzIGltcG9ydCAqCmltcG9ydCBzeXMsb3MsY29kZWNzCgpzY3JlZW5fc2l6ZSA9ICg2NDAsIDQ4MCkKCmRlZiBtYWluKCk6CiAgICBweWdhbWUuaW5pdCgpCiAgICBzY3JlZW4gPSBweWdhbWUuZGlzcGxheS5zZXRfbW9kZShzY3JlZW5fc2l6ZSkKICAgIHB5Z2FtZS5kaXNwbGF5LnNldF9jYXB0aW9uKHUi5L2N572u5oyH5a6a44CB5paH5a2X5YiX5oyH5a6aIikKICAgIHRleHQgPSBUZXh0KCJ0ZXN0IikKICAgIHdoaWxlIFRydWU6CiAgICAgICAgc2NyZWVuLmZpbGwoKDI1NSwyMDAsMjAwKSkKICAgICAgICB0ZXh0LmRyYXcoc2NyZWVuLCAoMjAsIDUwKSkKICAgICAgICBweWdhbWUuZGlzcGxheS51cGRhdGUoKQogICAgICAgIGZvciBldmVudCBpbiBweWdhbWUuZXZlbnQuZ2V0KCk6CiAgICAgICAgICAgIGlmIGV2ZW50LnR5cGUgPT0gUVVJVDoKICAgICAgICAgICAgICAgIHN5cy5leGl0KCkKICAgICAgICAgICAgaWYgZXZlbnQudHlwZSA9PSBLRVlET1dOIGFuZCBldmVudC5rZXkgPT0gS19TUEFDRToKICAgICAgICAgICAgICAgIHRleHQudG9nZ2xlKCkKCmNsYXNzIFRleHQ6CiAgICB0ZXh0X2ltYWdlcyA9IFtdICPnlLvlg4/ljJbjgZfjgZ/mloflrZfliJfjgpLlhaXjgozjgovjg6rjgrnjg4gKICAgIGZpbGVfZGF0YSA9IFtdICPoqq3jgb/ovrzjgpPjgaDjg5XjgqHjgqTjg6vjga7mloflrZfliJfjgpLlhaXjgozjgovjg6rjgrnjg4gKICAgIGZvbnRfc2l6ZSA9IDQwCiAgICBkZWYgX19pbml0X18oc2VsZiwgbmFtZSk6CiAgICAgICAgc2VsZi5uYW1lID0gbmFtZQogICAgICAgIHNlbGYuZm9udCA9IHB5Z2FtZS5mb250LlN5c0ZvbnQoIm1zbWluY2hvbXNwbWluY2hvIiwgc2VsZi5mb250X3NpemUpCiAgICAgICAgc2VsZi52aXNpYmxlID0gRmFsc2UKICAgICAgICBzZWxmLnJlbmRlcih1IuODmeOCpOODmeODvO+8gSIpCiAgICBkZWYgbG9hZChzZWxmKToKICAgICAgICBmaWxlID0gb3MucGF0aC5qb2luKCJkYXRhIiwgc2VsZi5uYW1lICsgIi50eHQiKQogICAgICAgIGZwID0gY29kZWNzLm9wZW4oZmlsZSwgInIiLCAidXRmLTgiKQogICAgICAgIGZvciBsaW5lIGluIGZwOgogICAgICAgICAgICBsaW5lID0gbGluZS5yc3RyaXAoKQogICAgICAgICAgICBzZWxmLmZpbGVfZGF0YSA9IGxpbmUuc3BsaXQoIiwiKQogICAgICAgIGZwLmNsb3NlKCkKICAgIGRlZiByZW5kZXIoc2VsZiwgc3RyKToKICAgICAgICBpZiBzdHIgPT0gTm9uZToKICAgICAgICAgICAgI+ODleOCoeOCpOODq+OBi+OCieiqreOBv+i+vOOCk+OBoOaWh+Wtl+WIl+OCkueUu+WDj+WMlgogICAgICAgICAgICBzZWxmLmxvYWQoKQogICAgICAgICAgICBmb3IgaSBpbiBzZWxmLmZpbGVfZGF0YToKICAgICAgICAgICAgICAgIHRleHRfaW1hZ2UgPSBzZWxmLmZvbnQucmVuZGVyKGksIFRydWUsICgwLDAsMCkpCiAgICAgICAgICAgICAgICBzZWxmLnRleHRfaW1hZ2VzLmFwcGVuZCh0ZXh0X2ltYWdlKQogICAgICAgIGVsc2U6CiAgICAgICAgICAgICPmjIflrprjgZfjgZ/mloflrZfliJfjgpLnlLvlg4/ljJYKICAgICAgICAgICAgdGV4dF9pbWFnZSA9IHNlbGYuZm9udC5yZW5kZXIoc3RyLCBUcnVlLCAoMCwwLDApKQogICAgICAgICAgICBzZWxmLnRleHRfaW1hZ2VzLmFwcGVuZCh0ZXh0X2ltYWdlKQogICAgZGVmIGRyYXcoc2VsZiwgc2NyZWVuLCBwb3MpOgogICAgICAgIHgsIHkgPSBwb3MKICAgICAgICBpZiBzZWxmLnZpc2libGUgPT0gVHJ1ZToKICAgICAgICAgICAgZm9yIGksIHRleHRfaW1hZ2UgaW4gZW51bWVyYXRlKHNlbGYudGV4dF9pbWFnZXMpOgogICAgICAgICAgICAgICAgZHkgPSB5ICsgc2VsZi5mb250X3NpemUgKiBpCiAgICAgICAgICAgICAgICBzY3JlZW4uYmxpdCh0ZXh0X2ltYWdlLCAoeCwgZHkpKQogICAgICAgIGVsc2U6CiAgICAgICAgICAgIHJldHVybgogICAgZGVmIHRvZ2dsZShzZWxmKToKICAgICAgICBzZWxmLnZpc2libGUgPSBub3Qgc2VsZi52aXNpYmxlCgppZiBfX25hbWVfXyA9PSAiX19tYWluX18iOgogICAgbWFpbigpCg==