Home > Net >  Why is this code not drawing the start button I want?
Why is this code not drawing the start button I want?

Time:01-11

I have tried a lot and still don't understand why it doesn't load the 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("Tan.jpg")
icon = pygame.image.load("box.png")
button1 = pygame.image.load("shirt.png").convert_alpha()
class Button():
    def __init__(self,x,y,image):
        self.image = image 
        self.rect = self.image.get_rect()
        self.rect.topleft = (x,y)

    def draw(self):
        screen.blit(self.image,(self.rect.x,self.rect.y)) 
start = Button(0,0,button1)


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


I want the button to load. It doesn't give me an error and just doesn't load.

CodePudding user response:

You have to call start.draw() after screen.blit(bg,(0,0)), but before pygame.display.update(). Note: screen.blit(bg, (0,0) draws the background image over the entire screen, so that everything that was previously drawn is overdrawn. pygame.display.update() updates the display and makes all changes to the display Surface visible at once:

while running == True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # draw background
    screen.blit(bg,(0,0))

    # draw button
    start.draw()

    # update display
    pygame.display.update()
  • Related