Skip to content

Instantly share code, notes, and snippets.

@valrus
Created January 2, 2013 00:26
Show Gist options
  • Save valrus/4431263 to your computer and use it in GitHub Desktop.
Save valrus/4431263 to your computer and use it in GitHub Desktop.
Letter widget doesn't show up
#:kivy 1.1.0
<Tile>:
size_hint: (None, None)
canvas.before:
PushMatrix
Translate:
xy: (self.x, self.y)
Scale:
scale: self.scale
canvas:
Color:
rgb: tuple(self.fillColor)
Mesh:
vertices: self.vertices
indices: [0, 1, 2, 3, 4, 5]
mode: 'triangle_fan'
canvas.after:
PopMatrix
<Letter>:
canvas.before:
PushMatrix
canvas:
Color:
rgb: 0, 1, 0
Rectangle:
pos: self.pos[0], self.pos[1]
size: 55, 55
canvas.after:
PopMatrix
"""
"""
from __future__ import print_function
import os
os.environ['KIVY_TEXT'] = 'pygame'
os.environ['KIVY_IMAGE'] = 'pygame'
import kivy
kivy.require("1.1.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.properties import ReferenceListProperty, NumericProperty
from math import sin, pi
import itertools
import random
SIN60 = sin(pi / 3.0)
SLOPE = (2.0 / 3.0) * SIN60
SCALE = 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])
class Letter(Label):
def __init__(self, **kw):
if not len(kw["text"]) == 1:
raise ValueError("Letter label must be length 1")
kw["size_hint"] = (None, None)
super(Letter, self).__init__(**kw)
def on_touch_down(self, touch):
return False
class Player(object):
def __init__(self, pos_x, pos_y, **kw):
super(Player, self).__init__()
self.widget = Letter(text="@", **kw)
self.x, self.y = pos_x, pos_y
class Tile(AnchorLayout):
r = NumericProperty(1.0)
g = NumericProperty(1.0)
b = NumericProperty(1.0)
fillColor = ReferenceListProperty(r, g, b)
def __init__(self, hex_x, hex_y, scale=SCALE):
self.hex_x, self.hex_y = hex_x, hex_y
self.scale = scale
x_pos = self.scale * (hex_x * 2.0 + (hex_y % 2)) * SIN60
y_pos = self.scale * 1.5 * hex_y
xyuv = [v + [0, 0] for v in HEX_VERTICES]
self.vertices = list(itertools.chain(*xyuv))
super(Tile, self).__init__(pos=(x_pos, y_pos),
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)
return True
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)
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))
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