-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
150 lines (138 loc) · 5.67 KB
/
main.py
File metadata and controls
150 lines (138 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.checkbox import CheckBox
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.recycleview import RecycleView
from kivy.uix.gridlayout import GridLayout
from kivy.core.window import Window
from kivy.config import ConfigParser
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.metrics import dp
from kivy.core.audio import SoundLoader
from kivy.uix.slider import Slider
import random
from datetime import datetime
from configp import *
import os
import ast
import time
class Menu(Screen):
def __init__(self, **kw):
super(Menu, self).__init__(**kw)
box = BoxLayout(orientation='vertical')
box.add_widget(Button(text='Настройки', on_press=lambda x:
set_screen('settings')))
box.add_widget(Button(text='Играть',
on_press=lambda x: set_screen('game')))
self.add_widget(box)
class Settings(Screen):
def __init__(self, *args, **kw):
super(Settings, self).__init__(**kw)
def on_enter(self, *args):
self.clear_widgets()
box = BoxLayout(orientation='vertical')
box.add_widget(Button(text='Назад', on_press=lambda x: set_screen('menu')))
grid = GridLayout(cols=2)
grid.add_widget(Label(text='Черные клавиши'))
black_keys = CheckBox(active=ConfigP().get_black_keys())
grid.add_widget(black_keys)
black_keys.bind(active=self.black_keys)
box.add_widget(grid)
box.add_widget(Label(text="От октавы: "))
first_octave = Slider(min=0, max=6, step=1, value=ConfigP().get_first_octave())
box.add_widget(first_octave)
self.first_octave_label = Label(text=octave_names[ConfigP().get_first_octave()])
box.add_widget(self.first_octave_label)
first_octave.bind(value=self.first_octave_setter)
box.add_widget(Label(text="До октавы:"))
second_octave = Slider(min=0, max=6, step=1, value=ConfigP().get_second_octave())
box.add_widget(second_octave)
self.second_octave_label = Label(text=octave_names[ConfigP().get_second_octave()])
box.add_widget(self.second_octave_label)
second_octave.bind(value=self.second_octave_setter)
self.add_widget(box)
def black_keys(self, checkbox, value):
ConfigP().set_black_keys(value)
def first_octave_setter(self, instance, value):
if (int(value) > ConfigP().get_second_octave()):
value = ConfigP().get_second_octave()
ConfigP().set_first_octave(int(value))
self.first_octave_label.text = octave_names[int(value)]
instance.value = ConfigP().get_first_octave()
def second_octave_setter(self, instance, value):
if (int(value) < ConfigP().get_first_octave()):
value = ConfigP().get_first_octave()
ConfigP().set_second_octave(int(value))
self.second_octave_label.text = octave_names[int(value)]
instance.value = ConfigP().get_second_octave()
def set_screen(name_screen):
sm.current = name_screen
class Game(Screen):
def __init__(self, *args, **kw):
super(Game, self).__init__(**kw)
def gen_note(self):
self.octave = random.randint(ConfigP().get_first_octave(), ConfigP().get_second_octave())
self.note = random.randint(0, len(self.notes) - 1)
self.note_name = str(self.octave) + '/' + self.notes[self.note]
def on_enter(self, *args):
self.chosen_note = -1
self.chosen_octave = -1
if ConfigP().get_black_keys():
self.notes = ["A", "Bb", "B", "C", "Db", "D", "Eb", "E", "F", "Gb", "G", "Ab"]
else:
self.notes = ["A", "B", "C", "D", "E", "F", "G"]
self.nums = {self.notes[i]: i for i in range(len(self.notes))}
self.octaves = {octave_names[i]: i for i in range(len(octave_names))}
print(self.nums)
self.clear_widgets()
box = BoxLayout(orientation='vertical')
self.gen_note()
box.add_widget(Button(text='Назад', on_press=lambda x: set_screen('menu')))
box.add_widget(Button(text='Проиграть', on_press=self.play))
row = GridLayout(cols=ConfigP().get_second_octave() - ConfigP().get_first_octave() + 1)
for i in range(ConfigP().get_first_octave(), ConfigP().get_second_octave() + 1):
b = Button(text=octave_names[i])
b.bind(on_press=self.chose_octave)
row.add_widget(b)
box.add_widget(row)
self.octave_label = Label(text="Выберите октаву")
box.add_widget(self.octave_label)
row = GridLayout(cols=len(self.notes) if len(self.notes) == 7 else len(self.notes) // 2)
for i in range(len(self.notes)):
b = Button(text=self.notes[i])
b.bind(on_press=self.chose_note)
row.add_widget(b)
box.add_widget(row)
self.note_label = Label(text="Выберите ноту")
box.add_widget(self.note_label)
box.add_widget(Button(text='Проверить', on_press=self.check))
self.check_label = Label(text="Проверка")
box.add_widget(self.check_label)
box.add_widget(Button(text='Обновить', on_press=self.on_enter))
self.add_widget(box)
def chose_octave(self, instance):
self.chosen_octave = self.octaves[instance.text]
self.octave_label.text = "Выбрана " + octave_names[self.chosen_octave]
def chose_note(self, instance):
self.chosen_note = self.nums[instance.text]
self.note_label.text = "Выбрана " + instance.text
def play(self, *args):
SoundLoader.load("nts/" + self.note_name + '.mp3').play()
def check(self, *args):
if self.chosen_octave == self.octave and self.chosen_note == self.note:
self.check_label.text = "Правильно"
else:
self.check_label.text = "Неправильно"
sm = ScreenManager()
sm.add_widget(Menu(name='menu'))
sm.add_widget(Settings(name='settings'))
sm.add_widget(Game(name='game'))
class NoteGuessApp(App):
def __init__(self, **kvargs):
super(NoteGuessApp, self).__init__(**kvargs)
def build(self):
return sm
if __name__ == '__main__':
NoteGuessApp().run()