Итак, я пишу код для 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)