fork download
  1. #!/usr/bin/env python
  2. # _*_ coding: utf-8 _*_
  3. import pygame
  4. from pygame.locals import *
  5. import sys,os,codecs
  6.  
  7. screen_size = (640, 480)
  8.  
  9. def main():
  10. pygame.init()
  11. screen = pygame.display.set_mode(screen_size)
  12. pygame.display.set_caption(u"位置指定、文字列指定")
  13. text = Text("test")
  14. while True:
  15. screen.fill((255,200,200))
  16. text.draw(screen, (20, 50))
  17. pygame.display.update()
  18. for event in pygame.event.get():
  19. if event.type == QUIT:
  20. sys.exit()
  21. if event.type == KEYDOWN and event.key == K_SPACE:
  22. text.toggle()
  23.  
  24. class Text:
  25. text_images = [] #画像化した文字列を入れるリスト
  26. file_data = [] #読み込んだファイルの文字列を入れるリスト
  27. font_size = 40
  28. def __init__(self, name):
  29. self.name = name
  30. self.font = pygame.font.SysFont("msminchomspmincho", self.font_size)
  31. self.visible = False
  32. self.render(u"ベイベー!")
  33. def load(self):
  34. file = os.path.join("data", self.name + ".txt")
  35. fp = codecs.open(file, "r", "utf-8")
  36. for line in fp:
  37. line = line.rstrip()
  38. self.file_data = line.split(",")
  39. fp.close()
  40. def render(self, str):
  41. if str == None:
  42. #ファイルから読み込んだ文字列を画像化
  43. self.load()
  44. for i in self.file_data:
  45. text_image = self.font.render(i, True, (0,0,0))
  46. self.text_images.append(text_image)
  47. else:
  48. #指定した文字列を画像化
  49. text_image = self.font.render(str, True, (0,0,0))
  50. self.text_images.append(text_image)
  51. def draw(self, screen, pos):
  52. x, y = pos
  53. if self.visible == True:
  54. for i, text_image in enumerate(self.text_images):
  55. dy = y + self.font_size * i
  56. screen.blit(text_image, (x, dy))
  57. else:
  58. return
  59. def toggle(self):
  60. self.visible = not self.visible
  61.  
  62. if __name__ == "__main__":
  63. main()
  64.  
Runtime error #stdin #stdout #stderr 0.01s 7688KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "prog.py", line 3, in <module>
ImportError: No module named pygame