-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
171 lines (132 loc) · 5.41 KB
/
main.py
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.clock import Clock
from kivy.uix.gridlayout import GridLayout
from kivy.uix.popup import Popup
from kivy.graphics import Rectangle
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager, Screen
def switchScreen(btn):
if '->' in btn.id:
x = btn.id.split('->')
name = x[0]
dir = x[1]
else:
name = btn.id
dir = 'right'
confirm = Confirm()
popup = Popup(
title='Tem certeza que desejá voltar ao menu?',
content=confirm,
size_hint=(None, None),
size=(win_w / 2, win_h / 3),
auto_dismiss=False,
title_size=20
)
go_on = lambda btn: (manager.switch_to(screens[name], direction=dir), popup.dismiss())
close = lambda btn: popup.dismiss()
confirm.no.bind(on_press=close)
confirm.yes.bind(on_press=go_on)
if name == 'Home' or name == 'Exit':
popup.open()
else:
manager.switch_to(screens[name], direction=dir)
class Controller:
def __init__(self, widget, player, movables = None):
self.widget = widget
self.player = player
self.movables = movables
self.pressed = set()
self.buttons = {
Button(text='Right', id='right', pos=(win_w - default_w - 20, 50), size=(default_w, default_h)),
Button(text='Left', id='left', pos=(win_w - default_w * 2 - 20, 50), size=(default_w, default_h)),
Button(text='Jump', id='jump', pos=(20, 50), size=(default_w, default_h)),
Button(text='Attack', id='attack', pos=(default_w + 20, 50), size=(default_w, default_h)),
Button(text='Pause', id='Pause->down', pos=(20, win_h - 70), size=(default_w, default_h), on_press=switchScreen),
}
for btn in self.buttons:
btn.bind(state=self.on_press)
widget.add_widget(btn)
def on_press(self, btn, state):
if state is 'down':
self.pressed.add(btn.id)
else:
self.pressed.remove(btn.id)
def start(self, ms):
posx, posy = self.player.pos
speed = ms * 500
if 'right' in self.pressed:
posx += speed
if 'left' in self.pressed:
posx -= speed
self.player.pos = (posx, posy)
class Confirm(GridLayout):
def __init__(self, **kwargs):
self.name = self.__class__.__name__
super(eval(self.name), self).__init__(**kwargs)
self.cols = 2
self.row_force_default = True
self.row_default_height = 95
self.yes = Button(text='Sim', size_hint=(100, None), height=default_h)
self.no = Button(text='Não', size_hint=(100, None), height=default_h)
self.add_widget(self.yes)
self.add_widget(self.no)
class Home(Widget):
def __init__(self, **kwargs):
self.name = self.__class__.__name__
super(eval(self.name), self).__init__(**kwargs)
self.next = Button(text='Iniciar', id='Game->left', size=(default_w*2, default_h*2), pos=(win_w / 2 - default_w, win_h / 2 - default_h), on_release=switchScreen)
self.add_widget(self.next)
class Game(Widget):
def __init__(self, **kwargs):
self.name = self.__class__.__name__
super(eval(self.name), self).__init__(**kwargs)
with self.canvas:
self.player = Rectangle(size=(50, 50), pos=(100, 150))
self.controller = Controller(self, self.player)
Clock.schedule_interval(self.movement, 0)
def movement(self, ms):
self.controller.start(ms)
class Pause(Widget):
def __init__(self, **kwargs):
self.name = self.__class__.__name__
self.index = list(screens.keys()).index(manager.current)
super(eval(self.name), self).__init__(**kwargs)
self.buttons = {
Button(text='Continuar', id='Game->up', size=(default_w, default_h), pos=(20, win_h - default_h - 20), on_press=switchScreen),
Button(text='Início', id='Home->right', size=(default_w, default_h), pos=(40 + default_w, win_h - default_h - 20), on_press=switchScreen),
Button(text='Opções', id='Options->left', size=(default_w, default_h), pos=(win_w - default_w - 20, win_h - default_h - 20), on_press=switchScreen)
}
for btn in self.buttons:
self.add_widget(btn)
class Options(Widget):
def __init__(self, **kwargs):
self.name = self.__class__.__name__
super(eval(self.name), self).__init__(**kwargs)
self.buttons = {
Button(text='Voltar', id='Pause->right', size=(default_w, default_h), pos=(20, win_h - default_h - 20), on_press=switchScreen)
}
for btn in self.buttons:
self.add_widget(btn)
manager = ScreenManager()
ACTUAL = None # actual screen
win_w = Window.size[0]
win_h = Window.size[1]
default_w = win_w * .15
default_h = win_h * .08
screens = {
'Home': Screen(name='Home'),
'Game': Screen(name='Game'),
'Pause': Screen(name='Pause'),
'Options': Screen(name='Options')
}
for x in screens:
screens[x].add_widget(eval(x+'()'))
manager.add_widget(screens[x])
class ScreenApp(App):
def build(self):
return manager
ScreenApp().run()