在类的示例pygame/python上将文本另存为变量

pkmbmrz7  于 2021-09-08  发布在  Java
关注(0)|答案(1)|浏览(544)

我正试着列一张待办事项清单。我可以输入文本,在下次打开应用程序或更改应用程序之前,文本会一直保留在那里。我想我应该将文本保存在一个.txt文件中,但是有16个示例/输入框。如果我将文本保存在txt文件中,如何按顺序调用文本(如input_box1=第1行,input_box2=第2行)。或者,如果我为每个示例创建一个txt文件,我如何将其放入我的类中。
我希望在单击enter时保存文本,并在打开应用程序时显示文本。
代码如下:

  1. import pygame
  2. import datetime
  3. import time
  4. pygame.init()
  5. clock = pygame.time.Clock()
  6. pygame.font.init()
  7. # Font
  8. text_font = pygame.font.Font('dogicapixelbold.ttf', 16)
  9. color = (233, 248, 215)
  10. active = False
  11. # screen resolution
  12. Width = 800
  13. Height = 600
  14. screen = pygame.display.set_mode((Width, Height))
  15. class InputBox:
  16. def __init__(self, x, y, w, h, text=''):
  17. self.rect = pygame.Rect(x, y, w, h)
  18. self.color = color
  19. self.text = text
  20. self.txt_surface = text_font.render(text, True, self.color)
  21. self.active = False
  22. self.txt_rect = self.txt_surface.get_rect()
  23. self.cursor = pygame.Rect(self.txt_rect.topright, (3, self.txt_rect.height + 5))
  24. def handle_event(self, event):
  25. if event.type == pygame.MOUSEBUTTONDOWN:
  26. # If the user clicked on the input_box rect.
  27. if self.rect.collidepoint(event.pos):
  28. # Toggle the active variable.
  29. self.active = not self.active
  30. else:
  31. self.active = False
  32. if event.type == pygame.KEYDOWN:
  33. if self.active:
  34. if event.key == pygame.K_RETURN:
  35. print(self.text)
  36. self.text = ''
  37. self.active = False
  38. elif event.key == pygame.K_BACKSPACE:
  39. self.text = self.text[:-1]
  40. else:
  41. self.text += event.unicode
  42. # Cursor
  43. self.txt_rect.size = self.txt_surface.get_size()
  44. self.cursor.topleft = self.txt_rect.topright
  45. # Limit characters -20 for border width
  46. if self.txt_surface.get_width() > self.rect.w - 15:
  47. self.text = self.text[:-1]
  48. def draw(self, screen):
  49. # Blit the text.
  50. screen.blit(self.txt_surface, (self.rect.x + 5, self.rect.y + 10))
  51. # Blit the rect.
  52. pygame.draw.rect(screen, self.color, self.rect, 1)
  53. # Blit the cursor
  54. if self.active:
  55. if time.time() % 1 > 0.5:
  56. # bounding rectangle of the text
  57. text_rect = self.txt_surface.get_rect(topleft=(self.rect.x + 5, self.rect.y + 10))
  58. # set cursor position
  59. self.cursor.midleft = text_rect.midright
  60. pygame.draw.rect(screen, self.color, self.cursor)
  61. def update(self):
  62. # Re-render the text.
  63. self.txt_surface = text_font.render(self.text, True, self.color)
  64. def main():
  65. clock = pygame.time.Clock()
  66. input_box1 = InputBox(115, 170, 250, 36)
  67. input_box2 = InputBox(115, 224, 250, 36)
  68. input_box3 = InputBox(115, 278, 250, 36)
  69. input_box4 = InputBox(115, 333, 250, 36)
  70. input_box5 = InputBox(115, 386, 250, 36)
  71. input_box6 = InputBox(115, 440, 250, 36)
  72. input_box7 = InputBox(115, 494, 250, 36)
  73. input_box8 = InputBox(440, 170, 250, 36)
  74. input_box9 = InputBox(440, 224, 250, 36)
  75. input_box10 = InputBox(440, 278, 250, 36)
  76. input_box11 = InputBox(440, 333, 250, 36)
  77. input_box12 = InputBox(440, 386, 250, 36)
  78. input_box13 = InputBox(440, 440, 250, 36)
  79. input_box14 = InputBox(440, 494, 250, 36)
  80. input_box15 = InputBox(440, 115, 250, 36)
  81. input_box16 = InputBox(440, 61, 250, 36)
  82. input_boxes = [input_box1, input_box2, input_box3, input_box4, input_box5, input_box6, input_box7, input_box8,
  83. input_box9, input_box10, input_box11, input_box12, input_box13, input_box14,
  84. input_box15, input_box16]
  85. done = False
  86. while not done:
  87. # Background
  88. screen.fill((0, 0, 0))
  89. for event in pygame.event.get():
  90. if event.type == pygame.QUIT:
  91. done = True
  92. for box in input_boxes:
  93. box.handle_event(event)
  94. for box in input_boxes:
  95. box.update()
  96. for box in input_boxes:
  97. box.draw(screen)
  98. pygame.display.update()
  99. clock.tick(120)
  100. if __name__ == '__main__':
  101. main()
  102. pygame.quit()
zlwx9yxi

zlwx9yxi1#

这是个人喜好,但一个文件听起来比16个文件更易于维护。
我想您正在寻找类似这样的东西来保存文件:

  1. with open("savefile.txt", "w") as savefile:
  2. for i in input_boxes:
  3. savefile.write(i.text + "\r\n")

要读取文件,请执行以下操作:

  1. for line in open("savefile.txt", "r").readlines():
  2. #add text to input box

相关问题