【问题标题】:why does the button not draw?为什么按钮不绘制?
【发布时间】:2023-01-28 03:21:15
【问题描述】:

我不明白为什么按钮没有在以前工作的背景上绘制 这不显示错误。 这是代码------> 这段代码错了吗?我的问题甚至可以重现吗?它可能在按钮的代码中,我将不胜感激

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()


【问题讨论】:

    标签: python python-3.x button pygame


    【解决方案1】:

    您班级中的缩进错误:方法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	omarjOneDrive - Tata Advanced Systems LimitedDesktopWar CrimeTan.jpg")
    icon = pygame.image.load(r"C:Users	omarjOneDrive - Tata Advanced Systems LimitedDesktopWar Crimeox.png")
    button1 = pygame.image.load(r"C:Users	omarjOneDrive - Tata Advanced Systems LimitedDesktopWar Crimeshirt.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()
    

    【讨论】:

    • 感谢您的回答,但即使在您告诉我这样做之后它也不起作用...顺便说一句,我确实创建了一个变量“start = Button(x,y,z,q)”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-09
    • 2021-07-19
    • 2012-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多