如何在Python3中编译pygame

uemypmqf  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(403)

我不知道这段代码的确切错误,我没有得到任何错误,但我的代码没有执行。当我运行代码时,我看到了rect的代码运行,运行后,我看到屏幕上出现了黄色的RECTANGLE,与代码相关的打印文本不会执行。实际上,我认为这段代码中的def没有运行,也没有显示任何错误,而且代码末尾的打印文本也没有运行

  1. import pygame
  2. import sys
  3. import random
  4. from pygame.locals import *
  5. pygame.init()
  6. width = 800
  7. height = 600
  8. black = (0, 0, 0)
  9. red = (255, 0, 0)
  10. input_text = ''
  11. typing = False
  12. win = pygame.display.set_mode((width, height))
  13. pygame.display.set_caption("Type Speed Program")
  14. def print_text(screen, massage, x, y, font_s, clr):
  15. font = pygame.font.Font(None, font_s)
  16. text = font.render(massage, True, clr)
  17. screen.blit(text, (x, y))
  18. pygame.display.update()
  19. def get_sentence():
  20. t_f = open('new.txt').read()
  21. sentences = t_f.split("\n")
  22. sentence = random.choice(sentences)
  23. pygame.display.update()
  24. return sentence
  25. def start_game():
  26. sentence_s = get_sentence()
  27. print_text = (win, sentence_s, 100, 250, 40, (255, 0, 0))
  28. pygame.draw.rect(win, (255, 255, 0), (75, 300, 650, 50), 3)
  29. pygame.display.update()
  30. start_game()
  31. while True:
  32. # win.fill(red)
  33. win.fill(black, (75, 300, 650, 50))
  34. print_text(win, input_text, 80, 310, 35, (255, 255, 255))
  35. for event in pygame.event.get():
  36. if event.type == QUIT:
  37. pygame.quit()
  38. sys.exit()
  39. elif event.type == MOUSEBUTTONDOWN:
  40. x, y = pygame.mouse.get_pos()
  41. if event.button == 1:
  42. if (75 <= x <= 725) and (300 <= y <= 350):
  43. typing = True
  44. input_text = ''
  45. elif event.type == KEYDOWN:
  46. if typing:
  47. if event.key == K_RETURN:
  48. print(input_text)
  49. if event.key == K_BACKSPACE:
  50. input_text = input_text[:-1]
  51. else:
  52. input_text += event.unicode
  53. print_text = (win, "Typing Speed", 120, 80, 80, (0, 255, 255))
  54. pygame.display.update()
kgqe7b3p

kgqe7b3p1#

您的问题在最后一行的第二行:

  1. print_text = (win, "Typing Speed", 120, 80, 80, (0, 255, 255))

在这里,您用一个元组覆盖定义的函数(print_text),然后在游戏循环中调用该元组。
移除=解决了问题。

  1. print_text(win, "Typing Speed", 120, 80, 80, (0, 255, 255))

相关问题