python 为什么按钮不画?

jxct1oxe  于 2023-01-29  发布在  Python
关注(0)|答案(1)|浏览(138)

我不明白为什么按钮不画在背景上,它工作之前,这不显示一个错误。这里是代码------〉这是代码错误吗?我的问题甚至是可复制的?它可能是在代码中的按钮,我将不胜感激的帮助

  1. import pygame
  2. pygame.init()
  3. screen = pygame.display.set_mode((3840,2160))
  4. running = True
  5. mouse = pygame.mouse.get_pos()
  6. pygame.display.set_caption("GermanBall")
  7. bg = pygame.image.load(r"C:\Users\tomarj\OneDrive - Tata Advanced Systems Limited\Desktop\War Crime\Tan.jpg")
  8. icon = pygame.image.load(r"C:\Users\tomarj\OneDrive - Tata Advanced Systems Limited\Desktop\War Crime\box.png")
  9. button1 = pygame.image.load(r"C:\Users\tomarj\OneDrive - Tata Advanced Systems Limited\Desktop\War Crime\shirt.png").convert_alpha()
  10. pygame.display.set_icon(icon)
  11. while running == True:
  12. class Button():
  13. def __init__(self,x,y,image, scale):
  14. width = image.get_width()
  15. height = image.get_height()
  16. self.image = pygame.transform.scale(image, (int(width * scale), int(height * scale)))
  17. self.rect = self.image.get_rect()
  18. self.rect.topleft = (x,y)
  19. self.state = "intro"
  20. posmoose = pygame.mouse.get_pos()
  21. if self.rect.collidepoint(posmoose):
  22. if pygame.mouse.get_pressed()[0] == 1:
  23. self.state = "game"
  24. print("click")
  25. def draw(self):
  26. screen.blit(self.image,(self.rect.x,self.rect.y))
  27. def intro(self):
  28. for event in pygame.event.get():
  29. if event.type == pygame.QUIT:
  30. running = False
  31. def game(self):
  32. for event in pygame.event.get():
  33. if event.type == pygame.QUIT:
  34. running = False
  35. def manager():
  36. if self.state == "intro":
  37. start = Button(1550,700,button1,0.5)
  38. start.draw()
  39. elif self.state == "game":
  40. screen.fill("black")
  41. pygame.display.update()
  42. for event in pygame.event.get():
  43. if event.type == pygame.QUIT:
  44. running = False
  45. screen.blit(bg,(0,0))
  46. pygame.display.update()
cgvd09ve

cgvd09ve1#

类中的缩进错误:方法drawintro,...应该和__init__在同一层。但是基本上你的类是无用的,因为你没有创建类的任何对象。你应该在你的代码中有类似my_button = Button(...)的东西(在类本身之外)。
此外,将类移动到顶级也是一种很好的做法。
所以也许你应该从这样的事情开始:

  1. import pygame
  2. pygame.init()
  3. screen = pygame.display.set_mode((3840,2160))
  4. running = True
  5. mouse = pygame.mouse.get_pos()
  6. pygame.display.set_caption("GermanBall")
  7. bg = pygame.image.load(r"C:\Users\tomarj\OneDrive - Tata Advanced Systems Limited\Desktop\War Crime\Tan.jpg")
  8. icon = pygame.image.load(r"C:\Users\tomarj\OneDrive - Tata Advanced Systems Limited\Desktop\War Crime\box.png")
  9. button1 = pygame.image.load(r"C:\Users\tomarj\OneDrive - Tata Advanced Systems Limited\Desktop\War Crime\shirt.png").convert_alpha()
  10. pygame.display.set_icon(icon)
  11. class Button():
  12. def __init__(self,x,y,image, scale):
  13. ...
  14. def draw(self):
  15. ...
  16. def intro(self):
  17. ...
  18. def game(self):
  19. ...
  20. def manager():
  21. ...
  22. my_button = Button(100, 200, button1, 1.0) # <- create a button
  23. while running == True:
  24. for event in pygame.event.get():
  25. if event.type == pygame.QUIT:
  26. running = False
  27. screen.blit(bg,(0,0))
  28. my_button.draw() # <- draw the button
  29. pygame.display.update()
展开查看全部

相关问题