Как вернуть переменные или даже значения через классы или даже функции с атрибутами в Python? - PullRequest
0 голосов
/ 24 февраля 2019

Итак, я пишу код для Stealth Game на python, я использую для него модуль pygame, но эта проблема может даже не касаться его.У меня есть этапы для моего меню, где я использую подход ООП (я новичок в ООП, поэтому, пожалуйста, не ненавидите так много в моем коде!), Где создаются кнопки для меню, в котором есть кнопка воспроизведения и выхода.Затем простые, средние, сложные и старые кнопки загружаются поверх старых кнопок (где предыдущие кнопки теряют функциональность), а затем кнопки 1, 2, 3 и 4 для уровней снова загружаются поверх предыдущих, если это приводит ксмысл.Однако, как я написал свой код, мне нужно для этапа переменной, где этап 1 - это игра и выход, 2 - сложность, а 3 - номер уровня, который необходимо передать методу и классу, в котором он должен быть. Iя не знаю, как это сделать, не объявив переменную с глобальной областью действия, которая побеждает цель ООП, я думаю.Так как же я?Потому что в противном случае код просто принимает ввод и проходит через все кнопки до конца.Вот мой код, вам понадобятся изображения, я думаю, по крайней мере, текстовых файлов.

#Stealth Assassin
import pygame #Imports the pygame module inclulding many in built functions that aids in game design
import time #Imports the time module for which I can implement delays into my program

pygame.init() #Runs pygame
clock = pygame.time.Clock() #Intialises the variable to control the game clock (FPS)
gameDisplay = pygame.display.set_mode((1920,1080),pygame.FULLSCREEN) #Variable which will set the resolution of the game window and put the window into fullscreen mode
pygame.display.set_caption("Stealth Assassin") #Sets the title of the pygame window for the game

class DisplayImage: #This class contains methods required to load images into the game

    def __init__(self, filename, xpos, ypos): #Method used to allow the class to intialise attributes

        self.filename = filename #Defines filename as the filename attribute passed through
        self.xpos = xpos #Defines the x axis positioning of the image as the attribute passed through
        self.ypos = ypos #Defines the y axis positioning of the image as the attribute passed through

    def LoadImage(self): #This method will load images into the game displaying them

        image = pygame.image.load(self.filename+".png") #Image is loaded into the program
        gameDisplay.blit(image, (self.xpos,self.ypos)) #Image is displayed to coordinates which were attributes that were defined prior


stage = 1 #Sets the menu as stage 1 which is the play and quit buttons

class Button: #This class contains methods for buttons including display and functionality

    def __init__(self, buttonname, buttonx, buttony, buttonwidth, buttonheight, textfile, textx, texty, stage): #Methods used to allow classes to intialise attributes

        self.buttonname = buttonname #Defines the name of the button as the attribute passed through
        self.buttonx = buttonx #Defines the x axis positioning of the button as the attribute passed through 
        self.buttony = buttony #Defines the y axis positioning of the button as the attribute passed through
        self.buttonwidth = buttonwidth #Defines the width of the button as the attribute passed through
        self.buttonheight = buttonheight #Defines the height of the button as the attribute passed through
        self.textfile = textfile #Sets the name of the textfile to be called
        self.textx = textx #Defines the x axis positioning of the text as the attribute passed through
        self.texty = texty #Defines the y axis positioning of the text as the attribute passed through
        self.stage = stage #Sets the stage of the menu which has 3 states of play/quit, difficulty and level choice

    def createbutton(self): #Method which creates a button for the menu

        buttonname = pygame.draw.rect(gameDisplay, (0,0,0), [self.buttonx, self.buttony, self.buttonwidth, self.buttonheight]) #Draws a rectangular button which is black and given the size and coordinates which were attributes
        text = pygame.image.load(self.textfile+".png")  #Loads the text file into the program      
        gameDisplay.blit(text, (self.textx,self.texty)) #Displays the text given coordinates

    def quitbutton(self): #Method which quits the program if the quit button is clicked

        if self.buttonx+self.buttonwidth > mouse[0] > self.buttonx and self.buttony+self.buttonheight > mouse[1] > self.buttony and pressed[0] == 1: #If the button is clicked (regarding its dimensions)

            pygame.quit() #Exits pygame
            quit() #Quits program

    def buttonaction(self): #Method which takes action for the particular button

        if self.buttonx+self.buttonwidth > mouse[0] > self.buttonx and self.buttony+self.buttonheight > mouse[1] > self.buttony and pressed[0] == 1: #If the button is clicked (regarding its dimensions)

            if self.stage == 1: #If the play/quit buttons are active

                EasyButton.createbutton() #Creates and displays the easy button through the button class and its method
                MediumButton.createbutton() #Creates and displays the medium button through the button class and its method
                HardButton.createbutton() #Creates and displays the hard button through the button class and its method
                VeteranButton.createbutton() #Creates and displays the veteran button through the button class and its method

            if self.stage == 2: #If the difficulty buttons are active

                OneButton.createbutton() #Creates and displays the one button through the button class and its method
                TwoButton.createbutton() #Creates and displays the two button through the button class and its method
                ThreeButton.createbutton() #Creates and displays the three button through the button class and its method
                FourButton.createbutton() #Creates and displays the four button through the button class and its method    

                if self.buttonname == 'easybutton':
                    difficulty = 'easy'

                if self.buttonname == 'mediumbutton':
                    difficulty = 'medium'

                if self.buttonname == 'hardbutton':
                    difficulty = 'hard'                                

                if self.buttonname == 'veteranbutton':
                    difficulty = 'veteran'        

                print(difficulty)

            time.sleep(0.5)




PlayButton = Button('playbutton',133,477,756,223,'PlayText',387,545,1) #Creates play button      
QuitButton = Button('quitbutton',133,731,756,223,'QuitText',387,806,None) #Creates quit button

EasyButton = Button('easybutton',127,477,362,223,'EasyText',214,548,2) #Creates easy button
MediumButton = Button('mediumbutton',533,477,362,223,'MediumText',560,548,2) #Creates medium button
HardButton = Button('hardbutton',127,727,362,223,'HardText',214,806,2) #Creates hard button
VeteranButton = Button('veteranbutton',533,727,362,223,'VeteranText',537,806,2) #Creates veteran button

OneButton = Button('onebutton',127,477,362,223,'OneText',287,550,3) #Creates the level 1 button
TwoButton = Button('twobutton',533,477,362,223,'TwoText',693,550,3) #Creates the level 2 button
ThreeButton = Button('threebutton',127,727,362,223,'ThreeText',285,810,3) #Creates the level 3 button
FourButton = Button('fourbutton',533,727,362,223,'FourText',685,810,3) #Creates the level 4 button
PlayButton.createbutton() #Creates the play button through the button class and its method
QuitButton.createbutton() #Creates the play button through the button class and its method




while True:

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

    mouse = pygame.mouse.get_pos() #Gets the x and y coordinates of the mouse cursor
    pressed = pygame.mouse.get_pressed() #Checks if the mouse has been pressed

    PlayButton.buttonaction() #Checks if the playbutton needs action
    QuitButton.quitbutton() #Checks if the quitbutton needs action
    EasyButton.buttonaction() #Checks if the easybutton needs action
    MediumButton.buttonaction() #Checks if the mediumbutton needs action
    HardButton.buttonaction() #Checks if the hardbutton needs action
    VeteranButton.buttonaction() #Checks if the veteranbutton needs action
    OneButton.buttonaction() #Checks if the onebutton needs action
    TwoButton.buttonaction() #Checks if the twobutton needs action
    ThreeButton.buttonaction() #Checks if the threebutton needs action
    FourButton.buttonaction() #Checks if the fourbutton needs action

    pressed = [0,0,0]
    pygame.display.update()
    clock.tick(5)

1 Ответ

0 голосов
/ 25 февраля 2019

Одним из принципов объектно-ориентированного проектирования является сохранение всех аспектов того, что объект моделирует / реализует внутри объекта, , но не более .Какова цель этой «кнопки» - чтобы ее видели на экране, чтобы принять пользовательский ввод, чтобы сообщить о состоянии основной программе.

Так что с точки зрения этого, ваш класс кнопок в основном в порядке.Однако в определение кнопки входят вещи, которые на самом деле не предназначены для кнопки, например:

def quitbutton(self): 
    #If the button is clicked (regarding its dimensions)
    if self.buttonx+self.buttonwidth > mouse[0] > self.buttonx and self.buttony+self.buttonheight > mouse[1] > self.buttony and pressed[0] == 1: 

        pygame.quit() #Exits pygame
        quit()        #Quits program

Общий класс кнопки должен не отвечать за выход из программы,это просто нужно нажать и снять.Точно так же для кода, который переключается между наборами кнопок Play + Quit и уровнями сложности - это не работа кнопки.

Я думаю, что лучшая кнопка удаляет помехи, чтобы быть проще:

class Button: 

    def __init__(self, buttonname, buttonx, buttony, buttonwidth, buttonheight, textfile, textx, texty ): 

        self.buttonname  = buttonname   # Name of the button
        self.buttonx     = buttonx      # X-axis position
        self.buttony     = buttony      # Y-axis position
        self.buttonwidth = buttonwidth  # Width of the button
        self.buttonheight= buttonheight # Height of the button
        self.text_image  = pygame.image.load( textfile+".png" )  # Button Label
        self.textx       = textx        # X-axis positioning of the text
        self.texty       = texty        # Y-axis positioning of the text

    def drawButton( self, screen ):
        """ Paint the button to the screen """
        # Black rectangle the size of the button
        pygame.draw.rect( screen, (0,0,0), [self.buttonx, self.buttony, self.buttonwidth, self.buttonheight]) 
        # Overlay the button text onto the background
        screen.blit( text_image, ( self.textx, self.texty ) )

    def mouseIsOver( self, mouse_position ):
        """ Returns true if the mouse is within this buttons area """
        inside = self.buttonx+self.buttonwidth > mouse_position[0] > self.buttonx and self.buttony+self.buttonheight > mouse_position[1] > self.buttony
        return inside

Затем в вашей основной программе:

PlayButton = Button('playbutton',133,477,756,223,'rock1_64',387,545,1) #Creates play button      
QuitButton = Button('quitbutton',133,731,756,223,'rock1_64',387,806,None) #Creates quit button

EasyButton = Button('easybutton',127,477,362,223,'rock1_64',214,548,2) #Creates easy button
MediumButton = Button('mediumbutton',533,477,362,223,'rock1_64',560,548,2) 
...

all_buttons = [ PlayButton, QuitButton, EasyButton, MediumButton ]


# Main Loop
while True:

    # Handle user-input
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            break
        if event.type == pygame.MOUSEBUTTONUP:
            # The mouse button was clicked, was it inside a button?
            click_location = pygame.mouse.get_pos()
            for b in all_buttons:
                if ( b.mouseIsOver( click_location ) ):
                    print( "Button [%s] pressed" % ( b.buttonname ) )

     # Re-paint screen
     gameDisplay.fill( BACKGROUND_COLOUR )
     for b in all_buttons:
         b.drawButton( gameDisplay )
     pygame.display.flip()
     clock.tick_busy_loop( 60 ) # Limit FPS


 pygame.quit()
 quit()

Или, если это не удастся, класс Button может post и событие в очереди PyGame: (РЕДАКТИРОВАТЬ: в этом была ошибка, нужно было создатьсобытие для публикации, ИСПРАВЛЕНО).

BUTTON_CLICK_EVENT = pygame.USEREVENT + 1

...

class Button ( ... ):

    def checkClick( self, mouse_pos ):
        """ If the mouse-click is inside our rectangle, post a message to the queue """
        if ( self.buttonx+self.buttonwidth > mouse_position[0] > self.buttonx and self.buttony+self.buttonheight > mouse_position[1] > self.buttony ):
            pygame.event.post( pygame.event.Event( BUTTON_CLICK_EVENT, { "button_name" : self.buttonname } ) )

...

# Main loop

    if event.type == BUTTON_CLICK_EVENT:
        print("Clicked "+ event.button_name )

Это позволяет отделить логику кнопок от остальной части программы и предотвратить необходимость использования глобальных переменных.Очевидно, вам нужно переключаться между банками кнопок, но это не проблема для объекта Button, он должен идти куда-то еще.Возможно в ButtonGroup или подобном.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...