Last active
September 13, 2017 05:36
-
-
Save valrus/d6b7b737c225f4d2fcb7a0b5686950e4 to your computer and use it in GitHub Desktop.
where's the label?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#:kivy 1.5.0 | |
<Tile>: | |
size_hint: (None, None) | |
anchor_x: 'center' | |
anchor_y: 'center' | |
canvas.before: | |
PushMatrix | |
Translate: | |
xy: (self.x, self.y) | |
Scale: | |
xyz: (self.scale, self.scale, self.scale) | |
canvas: | |
Color: | |
rgba: tuple(self.fillColor) | |
Mesh: | |
vertices: self.vertices | |
indices: [0, 1, 2, 3, 4, 5] | |
mode: 'triangle_fan' | |
canvas.after: | |
PopMatrix | |
<Letter>: | |
size_hint: None, None | |
borders: 2, 'solid', (1, 1, 1, 1.) | |
on_touch_down: self.collide_point(*args[1].pos) and self.parent.letter_clicked(letter_label) | |
pos: (self.x, self.y) | |
canvas: | |
Color: | |
rgba: 1, 1, 1, 1. | |
Rectangle: | |
pos: self.pos | |
size: self.size | |
Label: | |
id: letter_label | |
pos: self.parent.pos | |
size: self.parent.size | |
text: "@" | |
color: 1, 0, 0, 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
""" | |
import os | |
import kivy | |
kivy.require("1.5.0") | |
from kivy.animation import Animation | |
from kivy.uix.anchorlayout import AnchorLayout | |
from kivy.uix.label import Label | |
from kivy.uix.relativelayout import RelativeLayout | |
from kivy.uix.widget import Widget | |
from kivy.app import App | |
from kivy.logger import Logger | |
from kivy.metrics import dp | |
from kivy.properties import ReferenceListProperty, NumericProperty, StringProperty | |
from math import sin, pi | |
import itertools | |
import random | |
SIN60 = sin(pi / 3.0) | |
SLOPE = (2.0 / 3.0) * SIN60 | |
SCALE = dp(20.0) | |
THICKNESS = 2 | |
HEX_VERTICES = ([SIN60, 0], [2 * SIN60, 0.5], | |
[2 * SIN60, 1.5], [SIN60, 2.0], | |
[0, 1.5], [0, 0.5]) | |
def hex_to_pos(hex_x, hex_y): | |
return SCALE * (hex_x * 2.0 + (hex_y % 2)) * SIN60, SCALE * 1.5 * hex_y | |
class Letter(Widget): | |
text = StringProperty("@") | |
def on_parent(self, widget, parent): | |
self.size = parent.size | |
class Player(object): | |
def __init__(self, hex_x, hex_y, **kw): | |
super(Player, self).__init__() | |
self.x, self.y = hex_x, hex_y | |
self.widget = Letter(pos=hex_to_pos(hex_x, hex_y)) | |
class Tile(AnchorLayout): | |
r = NumericProperty(1.0) | |
g = NumericProperty(1.0) | |
b = NumericProperty(1.0) | |
a = NumericProperty(1.0) | |
fillColor = ReferenceListProperty(r, g, b, a) | |
def __init__(self, hex_x, hex_y, scale=SCALE): | |
self.hex_x, self.hex_y = hex_x, hex_y | |
self.scale = scale | |
xyuv = [v + [0, 0] for v in HEX_VERTICES] | |
self.vertices = list(itertools.chain(*xyuv)) | |
super(Tile, self).__init__( | |
pos=hex_to_pos(hex_x, hex_y), | |
size=(2 * SIN60 * self.scale, 2.0 * self.scale) | |
) | |
def on_touch_down(self, touch): | |
if not self.collide_point(*touch.pos): | |
return False | |
self.g, self.b = 0.0, 0.0 | |
Animation(r=0.0).start(self) | |
Animation(b=1.0).start(self) | |
print("Touch widget at ({0}, {1})".format(self.hex_x, self.hex_y)) | |
print("Color of touched widget is", self.fillColor) | |
print("Widget vertices: ", self.vertices) | |
return False | |
def on_touch_move(self, touch): | |
if self.collide_point(*touch.pos): | |
return self.on_touch_down(touch) | |
return False | |
def collide_point(self, *point): | |
if not super(Tile, self).collide_point(*point): | |
return False | |
rel_x, rel_y = self.to_local(*point, relative=True) | |
dy = SLOPE * rel_x | |
if rel_x <= SIN60 * self.scale: | |
y_min, y_max = (self.scale * 0.5) - dy, (self.scale * 1.5) + dy | |
else: | |
y_min, y_max = dy - (self.scale * 0.5), (self.scale * 2.5) - dy | |
return y_min < rel_y < y_max | |
def letter_clicked(self, label): | |
print("Click on {}".format(label)) | |
class Biscuit(App): | |
def build(self): | |
root = RelativeLayout(pos=(SCALE / 2, SCALE / 2)) | |
widgetArray = [] | |
for x in range(0, 22): | |
widgetArray.append([]) | |
for y in range(0, 19): | |
widgetArray[x].append(Tile(x, y)) | |
root.add_widget(widgetArray[x][y]) | |
player_x, player_y = random.randrange(0, 22), random.randrange(0, 19) | |
player_tile = widgetArray[player_x][player_y] | |
player = Player(player_x, player_y, | |
anchor_x="center", anchor_y="center", | |
text_size=(None, None)) | |
player_tile.add_widget(player.widget) | |
player_tile.r = 0.0 | |
print("Player location: ({0}, {1})".format(player_x, player_y)) | |
print("Player position: ({0}, {1})".format(player.widget.x, | |
player.widget.y)) | |
print(player.widget.parent) | |
return root | |
if __name__ == '__main__': | |
Biscuit().run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment