Home > Software design >  why does the button not draw?
why does the button not draw?

Time:01-30

I do not understand why the button does not draw on the background it worked before this does not show an error. here is the code------> is this code wrong ? is my problem even reproducible? it could be in the code for the button i would appreciate the help

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()
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)
            posmoose = pygame.mouse.get_pos()
            if self.rect.collidepoint(posmoose):
                if pygame.mouse.get_pressed()[0] == 1:
                    print("click")
        def draw(self):
            screen.blit(self.image,(self.rect.x,self.rect.y))
stat = Button(1550,700,button1,0.5) 




pygame.display.set_icon(icon)

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




CodePudding user response:

The indentation is wrong in your class: methods draw, intro, ... should be at the same level as __init__. But basically your class is useless since you're not creating any object of the class. You should have something like my_button = Button(...) in your code (and outside the class itself).

Also it is good practice to move your class at the top level.

So perhaps you should start from something like that:

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

CodePudding user response:

Your draw function doesn't know what screen is. You should add an argument screen to your draw function, and pass the function call in the main loop the screen.

  • Related