0

I am having an issue where I cannot seem to get the start buttons state to be 'unclicked', the issue is occuring in the button.py file, where in the class 'Button' in the method 'draw' the attribute 'clicked' will not go back to False. I am new to pygame and OOP

main.py

#imports pygame module
import pygame
#imports sys module
import sys

#imports mainMenuScreen object
from menu import *


#this creates a clock that will determine how frequently the screen is updated
clock = pygame.time.Clock()

#defining main function loop
def main():
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        #all screen updates will happen here

        #checks if startButton is clicked
        if startButton.draw():
            print("clicked")

        pygame.display.update()
        clock.tick(60)

#creates window
createMainMenuWindow()

#imports objects from MainMenuWindow
from menu import startButton

#runs main loop
main()

menu.py

#imports pygame module
import pygame
from button import *

#a class for a basic screen in pygame
class Menu:
    #attributes of the screen
    def __init__(self, width, height, colour):
        self.width = width
        self.height = height
        self.colour = pygame.Color(colour)


    #method to call screen
    def CreateWindow(self):
        global screen
        screen = pygame.display.set_mode((self.width, self.height))
        pygame.display.set_caption('Political Clash')
        screen.fill(self.colour)


#a class for the main menu
class MainMenu(Menu):
    pass

#creates main menu window
def createMainMenuWindow():
    global startButton
    #instanciates the MainMenu class
    mainMenuScreen = MainMenu(1500,700,"#F0a6bd")
    mainMenuScreen.CreateWindow()
    # loads images into variables
    startImg = pygame.image.load("PNG's\main_menu_start_button_unpressed.png").convert_alpha()
    startImgPressed = pygame.image.load("PNG's\main_menu_start_button_pressed.png").convert_alpha()
    scale = 0.25
    # instanciates button classes
    startButton = Button(((mainMenuScreen.width-(startImg.get_width()*scale))/2), ((mainMenuScreen.height-(startImg.get_height()*scale))/2 )
                         ,startImg, startImgPressed, scale)
    startButton.draw()



buttons.py

#imports pygame module
import pygame

#a class for a basic button in pygame
class Button:
    #attributes of button
    def __init__(self, x, y, image, imagePressed, scale):
        width = image.get_width()
        height = image.get_height()
        self.image = pygame.transform.scale(image, (int(width * scale), int(height * scale)))
        self.imagePressed = imagePressed
        self.rect = self.image.get_rect()
        self.rect.topleft = (x, y)
        self.clicked = False

    #method to add button to screen and check if clicked
    def draw(self):
        # imports menu screen
        from menu import screen

        # action variable
        action = False

        # checks where mouse curser is
        pos = pygame.mouse.get_pos()

        # checks for click
        if self.rect.collidepoint(pos):
            if pygame.mouse.get_pressed()[0] == 1 and self.clicked == False:
                self.clicked = True
                action = True

        # checks for unclicked or mouse moved off button
        if pygame.mouse.get_pressed()[0] == 0:
            self.clicked == False

        #draws object to screen
        screen.blit(self.image, (self.rect.x, self.rect.y))

        #returns whether something was clicked
        return action



I was expecting the button to remain unclicked after the left click was released.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174

0 Answers0