Skip to content

Instantly share code, notes, and snippets.

@Exodus111
Created March 31, 2017 11:14
Show Gist options
  • Save Exodus111/7cd150b63804a89402af8fe026e29b65 to your computer and use it in GitHub Desktop.
Save Exodus111/7cd150b63804a89402af8fe026e29b65 to your computer and use it in GitHub Desktop.
<Main>
size: 400, 400
canvas:
Line:
points: root.line1
width: 1.0
Rectangle:
pos: self.pos1
size: 30, 30
#!/usr/bin/python3
import kivy
kivy.require("1.9.0")
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.widget import Widget
from kivy.vector import Vector
from kivy.clock import Clock
from kivy.properties import ListProperty, DictProperty
from path import Path
import json
class Main(Widget):
line1= ListProperty([100, 100, 600, 500])
pos1 = ListProperty([200, 400])
move = DictProperty({"up":False, "down":False, "right":False, "left":False})
def setup(self):
self.keyboard = Window.request_keyboard(self.key_off, self)
self.keyboard.bind(on_key_down=self.key_down)
self.keyboard.bind(on_key_up=self.key_up)
self.import_json("Main_walls.json")
def key_off(self):
pass
def key_down(self, keyboard, keycode, text, mod):
if keycode[1] == "w":
self.move["up"] = True
if keycode[1] == "s":
self.move["down"] = True
if keycode[1] == "d":
self.move["right"] = True
if keycode[1] == "a":
self.move["left"] = True
def key_up(self, k, keycode):
if keycode[1] == "w":
self.move["up"] = False
if keycode[1] == "s":
self.move["down"] = False
if keycode[1] == "d":
self.move["right"] = False
if keycode[1] == "a":
self.move["left"] = False
def on_touch_down(self, touch):
pass
def import_json(self, filename):
js_file = Path("./data/collision/church/{}".format(filename))
with open(js_file, "r+") as f:
js_dict = json.load(f)
self.line1 = [(int(i[0]/2), int(i[1]/2)) for i in js_dict["main"]]
print(self.line1)
def h_l(self, n1, n2):
"""
higher_lower. Returns the higher number first.
"""
if n1 > n2:
return n1, n2
else:
return n2, n1
def does_it_intersect(self):
a1 = (self.line1[0], self.line1[1])
a2 = (self.line1[2], self.line1[3])
b1 = (self.line2[0], self.line2[1])
b2 = (self.line2[2], self.line2[3])
x, y = Vector.line_intersection(a1, a2, b1, b2)
a_xh, a_xl = self.h_l(a1[0], a2[0])
a_yh, a_yl = self.h_l(a1[1], a2[1])
b_xh, b_xl = self.h_l(b1[0], b2[0])
b_yh, b_yl = self.h_l(b1[1], b2[1])
if x < a_xh and x > a_xl and y < a_yh and y > a_yl:
if x < b_xh and x > b_xl and y < b_yh and y > b_yl:
return True
return False
def update(self, dt):
if self.move["up"]:
self.pos1[1] += 5
if self.move["down"]:
self.pos1[1] -= 5
if self.move["right"]:
self.pos1[0] += 5
if self.move["left"]:
self.pos1[0] -= 5
class LineApp(App):
def build(self):
game = Main()
game.setup()
Clock.schedule_interval(game.update, 1/60)
return game
if __name__ == "__main__":
app = LineApp()
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment