python 为什么按钮不画?

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

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

import pygame
pygame.init()
screen = pygame.display.set_mode((3840,2160))
running = True
mouse = pygame.mouse.get_pos()

pygame.display.set_caption("GermanBall")
bg = pygame.image.load(r"C:\Users\tomarj\OneDrive - Tata Advanced Systems Limited\Desktop\War Crime\Tan.jpg")
icon = pygame.image.load(r"C:\Users\tomarj\OneDrive - Tata Advanced Systems Limited\Desktop\War Crime\box.png")
button1 = pygame.image.load(r"C:\Users\tomarj\OneDrive - Tata Advanced Systems Limited\Desktop\War Crime\shirt.png").convert_alpha()

pygame.display.set_icon(icon)
while running == True:

    class Button():
        def __init__(self,x,y,image, scale):
            width = image.get_width()
            height = image.get_height()
            self.image = pygame.transform.scale(image, (int(width * scale), int(height * scale)))
            self.rect = self.image.get_rect()
            self.rect.topleft = (x,y)
            self.state = "intro"
            posmoose = pygame.mouse.get_pos()
            if self.rect.collidepoint(posmoose):
                if pygame.mouse.get_pressed()[0] == 1:
                    self.state = "game"
                    print("click")
            def draw(self):
                        screen.blit(self.image,(self.rect.x,self.rect.y)) 
            def intro(self):
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        running = False
            def game(self):
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        running = False
            def manager():
                if self.state == "intro":
                    start = Button(1550,700,button1,0.5)
                    start.draw()

                elif self.state == "game":
                        screen.fill("black")
                        pygame.display.update()
            

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    screen.blit(bg,(0,0))
    pygame.display.update()
cgvd09ve

cgvd09ve1#

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

import pygame
pygame.init()
screen = pygame.display.set_mode((3840,2160))
running = True
mouse = pygame.mouse.get_pos()

pygame.display.set_caption("GermanBall")
bg = pygame.image.load(r"C:\Users\tomarj\OneDrive - Tata Advanced Systems Limited\Desktop\War Crime\Tan.jpg")
icon = pygame.image.load(r"C:\Users\tomarj\OneDrive - Tata Advanced Systems Limited\Desktop\War Crime\box.png")
button1 = pygame.image.load(r"C:\Users\tomarj\OneDrive - Tata Advanced Systems Limited\Desktop\War Crime\shirt.png").convert_alpha()

pygame.display.set_icon(icon)

class Button():
    def __init__(self,x,y,image, scale):
        ...
    def draw(self):
        ...
    def intro(self):
        ...
    def game(self):
        ...
    def manager():
        ...

my_button = Button(100, 200, button1, 1.0)  # <- create a button
while running == True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    screen.blit(bg,(0,0))
    my_button.draw()  # <- draw the button
    pygame.display.update()

相关问题