#python #class #kivy
Вопрос:
Я пытаюсь создать простую игру в сапера, используя python и kivy, и я застрял, пытаясь создать экран «ВЫ ПРОИГРАЛИ». Я сделал метку с надписью «ВЫ ПРОИГРАЛИ» и поместил ее в Основной класс в файле kv с непрозрачностью 0, но когда я пытаюсь изменить непрозрачность метки на метку из класса ячеек, это выдает ошибку.
import random from math import floor from time import time from kivy.app import App from kivy.uix.floatlayout import FloatLayout from kivy.uix.label import Label from kivy.uix.button import Button class Cell(Button): t1 = None t2 = None flag = True def on_press(self): self.t1 = time() def on_release(self): self.t2 = time() interval = self.t2 - self.t1 if interval lt; 0.5: self.opacity = 0 self.bomb_check() print(Main().lose.opacity) else: self.flag_state() def flag_state(self): if self.flag == True: self.text = "F" else: self.text = "" self.flag = not self.flag def bomb_check(self): if Main.lst.count(self.id) gt; 0: print(self.id) Main.lose.opacity = 1 class Field(Label): pass class LoseScreen(Label): pass class Main(FloatLayout): lst = [] cells = 9 total_cells = cells ** 2 def __init__(self, **kwargs): super(Main, self).__init__(**kwargs) self.lose.opacity = 0 # The for loop bellow is for initializing buttons to grid1 for i in range(0, self.total_cells): b = Cell() b.id = i self.grid1.add_widget(b) # The for loop bellow is for initializing labels to grid2 for i in range(0, self.total_cells): f = Field() f.id = i self.grid2.add_widget(f) self.add_mines() def add_mines(self): # The for loop bellow is for making "BOMB" in random children of the grid2 for i in range(0 , self.cells): ind = floor(random.random()*self.total_cells) # The wile loop bellow is for making sure that random number doesn't repeat while len(self.lst) lt;= i: if self.lst.count(ind) gt; 0: ind = floor(random.random()*self.total_cells) else: self.lst.append(ind) #print(ind) for c in range (0, self.total_cells): if(self.grid2.children[c].id == ind): self.grid2.children[c].text = "BOMB" # The for loop bellow is to make how many numbers of "BOMB" sorrounds a cell for x in range(0, self.total_cells): directions = [x 1, x-1, x self.cells, x-self.cells, x self.cells 1, x-self.cells 1, x self.cells-1, x-self.cells-1] num = 0 # The if statement bellow is for the cell to not count outside the range of -gt; # total cells and to not count from the last column to the first column or vise versa if(self.grid2.children[x].text != "BOMB"): for y in directions: if y in range(0, self.total_cells): if (self.grid2.children[y].text == "BOMB"): if(x%self.cells == 0 and (y 1)%self.cells == 0): continue elif((x 1)%self.cells == 0 and y%self.cells == 0): continue else: num = 1 self.grid2.children[x].text = str(num) class MineSweeperApp(App): def build(self): return Main() MineSweeperApp().run()
lt;Maingt;: grid1: grid1 grid2: grid2 lose: lose GridLayout: id: grid2 rows: root.cells cols: root.cells size_hint: 0.6, 0.6 pos: root.width/2 - self.width/2, root.height/2 - self.height/2 GridLayout: id: grid1 rows: root.cells cols: root.cells spacing: 0, 0 size_hint: 0.6, 0.6 pos: root.width/2 - self.width/2, root.height/2 - self.height/2 LoseScreen: id: lose lt;Cellgt;: lt;Fieldgt;: background_color: 0.7, 0.7, 0.7, 1 color: 1, 0, 0, 1 padding: 100, 100 canvas.before: Color: rgba: self.background_color Rectangle: size: self.size pos: self.pos lt;LoseScreengt;: text: "YOU LOSE" font_size: 50 color: 1, 0, 0, 1 opacity: 0
Это сообщение об ошибке:
File "c:/Users/user/Desktop/Python Projects/kivy-minesweeper/main.py", line 45, in bomb_check Main.lose.opacity = 1 AttributeError: 'kivy.properties.ObjectProperty' object has no attribute 'opacity'