Skip to content

Commit

Permalink
Add menu
Browse files Browse the repository at this point in the history
  • Loading branch information
wellington36 committed Jan 29, 2022
1 parent 43cbd3c commit 9a7bcc0
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 7 deletions.
8 changes: 8 additions & 0 deletions src/galaxy.kv
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
#:import menu menu

MainWidget:


<MainWidget>:
menu_widget: menu_widget
MenuWidget:
id: menu_widget
19 changes: 17 additions & 2 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,20 @@
from kivy.graphics.vertex_instructions import Line, Quad, Triangle
from kivy.properties import Clock, NumericProperty
from kivy.uix.widget import Widget
from kivy.uix.relativelayout import RelativeLayout
from kivy.lang import Builder
from kivy.properties import ObjectProperty

Builder.load_file("menu.kv")

class MainWidget(Widget):

class MainWidget(RelativeLayout):
from transforms import tranform_perspective, transform, transform_2D
from user_actions import (keyboard_closed, on_keyboard_down, on_keyboard_up,
on_touch_down, on_touch_up)

menu_widget = ObjectProperty()

perspective_point_x = NumericProperty(0)
perspective_point_y = NumericProperty(0)
current_offset_y = NumericProperty(0)
Expand Down Expand Up @@ -47,6 +54,7 @@ class MainWidget(Widget):
ship_coordinates = [(0, 0), (0, 0), (0, 0)]

state_game_over = False
state_game_has_started = False


def __init__(self, **kwargs):
Expand Down Expand Up @@ -272,7 +280,7 @@ def update(self, dt):
self.update_tiles()
self.update_ship()

if not self.state_game_over:
if not self.state_game_over and self.state_game_has_started:
speed_y = self.SPEED * self.height / 100
self.current_offset_y += speed_y * time_factor

Expand All @@ -289,7 +297,14 @@ def update(self, dt):

if not self.check_ship_collision() and not self.state_game_over:
self.state_game_over = True
self.menu_widget_opacity = 1
print("GAME OVER")


def on_menu_button_pressed(self):
print("BUTTON")
self.state_game_has_started = True
self.menu_widget.opacity = 0


class GalaxyApp(App):
Expand Down
16 changes: 16 additions & 0 deletions src/menu.kv
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@


<MenuWidget>:
canvas.before:
Color:
rgba: 0, 0, 0, .8
Rectangle:
size: self.size
Label:
text: "TITLE"
pos_hint: { "center_x": .5, "center_y": .6 }
Button:
text: "OK"
pos_hint: { "center_x": .5, "center_y": .4 }
size_hint: .2, .1
on_press: root.parent.on_menu_button_pressed()
9 changes: 9 additions & 0 deletions src/menu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from kivy.uix.relativelayout import RelativeLayout


class MenuWidget(RelativeLayout):
def on_touch_down(self, touch):
if self.opacity == 0:
return False

return super(RelativeLayout, self).on_touch_down(touch)
17 changes: 12 additions & 5 deletions src/user_actions.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from kivy.uix.relativelayout import RelativeLayout


def keyboard_closed(self):
self._keyboard_unbind(on_key_down=self.on_keyboard_down)
self._keyboard_unbind(on_key_up=self.on_keyboard_up)
self._keyboard = None


def on_keyboard_down(self, keyboard, keycode, text, modifiers):

if keycode[1] == 'left' or keycode[0] == 1073741904:
self.current_speed_x = self.SPEED_X

Expand All @@ -21,10 +24,14 @@ def on_keyboard_up(self, keyboard, keycode):


def on_touch_down(self, touch):
if touch.x < self.width / 2:
self.current_speed_x = self.SPEED_X
else:
self.current_speed_x = -self.SPEED_X
if not self.state_game_over and self.state_game_has_started:
if touch.x < self.width / 2:
self.current_speed_x = self.SPEED_X

else:
self.current_speed_x = -self.SPEED_X

return super(RelativeLayout, self).on_touch_down(touch)


def on_touch_up(self, touch):
Expand Down

0 comments on commit 9a7bcc0

Please sign in to comment.