2

I'm using Python 3.6.

I'm trying to have Pygame do the following when I left-click on the 'Start Game' rectangle object:

  1. Change to a new background image from 'bgHome' image to 'bgIntro' image via the variable 'background'.
  2. Hold the new background image, and remove the rectangle objects when I release the left mouse button.
  3. Display the text in the file 'intro.txt' and retain the footer text which sits in the variable text1.

The code works as expected until I release the left mouse button, where it reverts back to what is the previous screen, effectively my title screen at the beginning of mainloop().

I'm thinking I need to add in some kind of line(s) after the left click action to tell Pygame the mouse button is still down?

Full code as follows:

# Imports
import sys
import os
import random
import time
import pygame
from pygame.locals import *

# Must add to initialize pygame
pygame.init()


#Colour chart
LIGHT_BLUE = (51, 102, 255)
BLUE = (0, 0, 255)
DARK_BLUE = (0, 0, 102)
LIGHT_RED = (255, 65, 65)
RED = (255, 0, 0)
DARK_RED = (153, 0, 0)
LIGHT_GREEN = (102, 255, 0)
GREEN = (51, 153, 0)
DARK_GREEN = (0, 51, 0)
WHITE = (255, 255, 255)
GREY = (102, 102, 102)
BLACK = (0, 0, 0)
YELLOW = (255, 255, 0)
ORANGE = (255, 102, 0)
PURPLE = (153, 0, 204)


#Other variables
res = (500,500)
screen = pygame.display.set_mode(res)
width = screen.get_width()
height = screen.get_height()
leftclick = (1, 0, 0)
middleclick = (0, 1, 0)
rightclick = (0, 0, 1)


#Backgrounds for game
background = pygame.image.load("bg1.png")
bgHome = pygame.image.load("bg1.png")
bgOptions = 0
bgIntro = pygame.image.load("bg2.png")
bgSimulate = 0
bgAssets = 0
bgEmpl = 0


#Setting up Fonts
smallfont = pygame.font.SysFont('Corbel',20)
mediumfont = pygame.font.SysFont('Corbel',32)
largefont = pygame.font.SysFont('Corbel',46)

#Common text featured throughout game
text1 = "Footer text goes here."
copyright_text = smallfont.render(text1 , True , WHITE)


#Load text file
f = open('Intro.txt', 'r')
content = f.read()
intromsg = smallfont.render(content, True, BLACK)


#Set texts for buttons
start_butt_text = smallfont.render('Start Game' , True , WHITE)
load_butt_text = smallfont.render('Load Game' , True , WHITE)
quit_butt_text = smallfont.render('Quit Game' , True , WHITE)


#Player class
class Player():
    def __init__(self, name):
        self.name = name
        self.cash = 10000
        self.assets = 0
        self.execs = 0
        self.mgmt = 0
        self.directlab = 0
        self.indirectlab = 0
        self.liab = 0
        self.equity = self.cash + self.assets - self.liab
        


#Create a white screen 
DISPLAYSURF = pygame.display.set_mode((500,500))
DISPLAYSURF.fill(WHITE)
pygame.display.set_caption("Capitalism: The Game")
    
    

def ButtonSG():
        pygame.draw.rect(screen,GREY,[10,10,140,40])
        pygame.draw.rect(screen,RED,[15,15,130,30]) 
        pygame.draw.rect(screen,GREY,[10,60,140,40])
        pygame.draw.rect(screen,BLACK,[15,65,130,30])
        pygame.draw.rect(screen,GREY,[10,110,140,40])
        pygame.draw.rect(screen,BLACK,[15,115,130,30])


def ButtonLG():
        pygame.draw.rect(screen,GREY,[10,10,140,40])
        pygame.draw.rect(screen,BLACK,[15,15,130,30]) 
        pygame.draw.rect(screen,GREY,[10,60,140,40])
        pygame.draw.rect(screen,RED,[15,65,130,30])
        pygame.draw.rect(screen,GREY,[10,110,140,40])
        pygame.draw.rect(screen,BLACK,[15,115,130,30])
        
        
def ButtonQG():
        pygame.draw.rect(screen,GREY,[10,10,140,40])
        pygame.draw.rect(screen,BLACK,[15,15,130,30])
        pygame.draw.rect(screen,GREY,[10,60,140,40])
        pygame.draw.rect(screen,BLACK,[15,65,130,30])
        pygame.draw.rect(screen,GREY,[10,110,140,40])
        pygame.draw.rect(screen,RED,[15,115,130,30])
        
def ButtonNil():
        pygame.draw.rect(screen,GREY,[10,10,140,40])
        pygame.draw.rect(screen,BLACK,[15,15,130,30])
        pygame.draw.rect(screen,GREY,[10,60,140,40])
        pygame.draw.rect(screen,BLACK,[15,65,130,30])
        pygame.draw.rect(screen,GREY,[10,110,140,40])
        pygame.draw.rect(screen,BLACK,[15,115,130,30])  


def IntroScreen():
    DISPLAYSURF.fill(WHITE)
    background = bgIntro
    DISPLAYSURF.blit(background, (0, 0))
    DISPLAYSURF.blit(intromsg, (10,10))


#Game loop
def resetscreen():
    DISPLAYSURF.blit(bgHome, (0,0))


def mainloop():
    while True: 
                  
        background = pygame.image.load("bg1.png")
            
        # fills the screen with a color 
        
        DISPLAYSURF.blit(background, (0,0))
      
        # stores the (x,y) coordinates into 
        # the variable as a tuple 
        mouse = pygame.mouse.get_pos() 
      
        # if mouse is hovered on a button it changes colour
        if width-width+10 <= mouse[0] <= width-width+150 and height-height+10 <= mouse[1] <= height-height+50:
            ButtonSG()
        elif width-width+10 <= mouse[0] <= width-width+150 and height-height+60 <= mouse[1] <= height-height+100:
            ButtonLG() 
        elif width-width+10 <= mouse[0] <= width-width+150 and height-height+110 <= mouse[1] <= height-height+150:
            ButtonQG()
        else: 
            ButtonNil()
      
        # superimposing the text onto our button 
        screen.blit(start_butt_text, (35,22))
        screen.blit(load_butt_text, (35,72)) 
        screen.blit(quit_butt_text, (35,122))
        screen.blit(copyright_text, (10,height-25)) 

              
        #checks if a mouse is clicked 
        if pygame.mouse.get_pressed() == (leftclick):
            if width-width+10 <= mouse[0] <= width-width+150 and height-height+10 <= mouse[1] <= height-height+50:
                IntroScreen()
            elif width-width+10 <= mouse[0] <= width-width+150 and height-height+60 <= mouse[1] <= height-height+100:
                pass
            elif width-width+10 <= mouse[0] <= width-width+150 and height-height+110 <= mouse[1] <= height-height+150:
                pygame.quit()
                sys.exit()
            else:
                pass
        
            screen.blit(copyright_text, (10,height-25)) 


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

        # updates the frames of the game 
        pygame.display.update() 


mainloop()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174

1 Answers1

0

The background variable is set continuously in the main application loop. Hence if you change the variable it will be reset at the beginning of the loop. Remove this line of code:

def mainloop():
    while True: 
                  
        # background = pygame.image.load("bg1.png") <--- DELETE

If you want to change the background, all you have to do is change the Background variable:

background = bgIntro

or

background = bgHome

Be careful, if you want to change this inside a function, you have to use the global statement because background is a variable in the global namespace. See Using global variables in a function:

def clicked_start():
    global background
    
    background = bgIntro
    
    # [...]

Click detection does not work as expected. There are many questions and answers on this subject. For instance:

Rabbid76
  • 202,892
  • 27
  • 131
  • 174