0

this code when run changes the screen but changes to the original one when i release my mouse why does this happen?when i comment out the screen.blit(bg,(0,0))and when i click it it works and changes to the other screen normally here is the code to show the problem:

import pygame
import os

game = False
pygame.init()
screen = pygame.display.set_mode((3840, 2160))
running = True
os.chdir(r"C:\Users\tomarj\OneDrive - Tata Advanced Systems Limited\Desktop\War Crime")

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()
bg_new = pygame.image.load("big.jpg")

pygame.display.set_icon(icon)

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)

    def draw(self):
        screen.blit(self.image, (self.rect.x, self.rect.y))

stat = Button(1550, 700, button1, 0.5)

while running:
    pos = pygame.mouse.get_pos()
    if stat.rect.collidepoint(pos):
        if pygame.mouse.get_pressed()[0] == 1:
            game = True
            if game:
                new_screen = pygame.display.set_mode((3840, 2160))
                new_screen.blit(bg_new, (0, 0))
                pygame.display.update()

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

    screen.blit(bg, (0, 0))
    stat.draw()
    pygame.display.update()

pygame.quit()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174

1 Answers1

0

At every frame (iteration of the while loop (while running)) The screen is being painted over (blit) with bg. You are also checking if the mouse is pressed and if so, you are 'blitting' bg_new. However this blit gets overridden by the blit at the end of the loop hence why after you release, the screen goes back to bg.

jjislam
  • 557
  • 3
  • 8