Skip to content

Instantly share code, notes, and snippets.

@tshirtman
Created July 9, 2019 22:47
Show Gist options
  • Save tshirtman/b173f1ad1bc463b690706424f7cbb822 to your computer and use it in GitHub Desktop.
Save tshirtman/b173f1ad1bc463b690706424f7cbb822 to your computer and use it in GitHub Desktop.
#:/usr/bin/env python3
from string import ascii_uppercase
from random import choice
from kivy.core.window import Window
from kivy.app import App
from kivy.lang import Builder
from kivy.properties import ObjectProperty, ListProperty, NumericProperty
from kivy.clock import Clock
from kivy.factory import Factory as F
KV = '''
#:import sin math.sin
#:import RGBA kivy.utils.rgba
FloatLayout:
Grid:
id: grid
grid_size: 5, 5
pos_hint: {'center': (.5, .5)}
<Cell>:
active: app.current_cell == self
color: RGBA('#000000')
font_size: 20
background_color:
(
'#000000' if self.disabled else
'#FFFFFF' if self.active else
'#BBBBBB'
)
on_touch_down:
if self.collide_point(*args[1].pos): app.current_cell = self
canvas.before:
Color:
rgba: RGBA(self.background_color or '#FFFFFF')
Rectangle:
size: self.size
pos: self.pos
Color:
rgba:
(
RGBA('#000000')[:3] +
[(sin(app.time * 16) * .25 + .5) if self.active else 1]
)
Line:
rectangle: self.pos + self.size
width: 2
<Grid>:
size_hint: None, None
rows: self.grid_size[0] + 1
cols: self.grid_size[1] + 1
width: (self.cols or 1) * 50
height: (self.rows or 1) * 50
'''
class Game(App):
current_cell = ObjectProperty(None)
time = NumericProperty(0)
def build(self):
Window.bind(on_key_down=self.handle_key)
Clock.schedule_interval(self.update_time, 0)
return Builder.load_string(KV)
def update_time(self, dt):
self.time += dt
def handle_key(self, win, code, scan, letter, modifiers):
print(code)
if self.current_cell:
grid = self.root.ids.grid
index = grid.children[::-1].index(self.current_cell)
row, col = divmod(index, grid.cols)
if letter:
self.current_cell.text = letter
elif code == 8:
# backspace
self.current_cell.text = ''
elif code == 273:
# UP
row -= 1
elif code == 274:
# DOWN
row += 1
elif code == 275:
# RIGHT
col += 1
elif code == 276:
# LEFT
col -= 1
if col >= grid.cols:
col = 1
elif col < 1:
col = grid.cols - 1
if row >= grid.rows:
row = 1
elif row < 1:
row = grid.rows - 1
index = grid.cols * row + col
self.current_cell = grid.children[::-1][index]
class Cell(F.Label):
pass
class Grid(F.GridLayout):
grid_size = ListProperty([5, 5])
def __init__(self, **kwargs):
super().__init__(**kwargs)
width, height = self.grid_size
# empty corner
self.add_widget(F.Widget())
# top row for letters
for _, L in zip(range(width), ascii_uppercase):
self.add_widget(F.Label(text=L))
for Y in range(height):
# first column for numbers
self.add_widget(F.Label(text=str(Y)))
for X in range(width):
self.add_widget(
F.Cell(
disabled=choice(
[False] * 10 + [True]
)
)
)
if __name__ == '__main__':
Game().run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment