1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-26 09:28:13 +00:00
trezor-firmware/core/src_game/game_2048.py

135 lines
4.6 KiB
Python
Raw Normal View History

2017-10-05 23:38:02 +00:00
#
2018-12-22 18:36:26 +00:00
# 2048 Game
# ported to TREZOR Core by Pavol Rusnak (stick@satoshilabs.com)
2017-10-05 23:38:02 +00:00
#
# inspired by https://github.com/gabrielecirulli/2048
#
2018-12-22 18:36:26 +00:00
from trezor import loop, ui, workflow
2017-10-05 23:33:28 +00:00
from trezor.crypto import random
from trezor.ui.swipe import Swipe, SWIPE_DOWN, SWIPE_UP, SWIPE_LEFT
2017-10-05 23:33:28 +00:00
2018-12-22 18:36:26 +00:00
2018-12-21 22:51:22 +00:00
color_fg1 = ui.rgb(0x77, 0x6E, 0x65)
color_fg2 = ui.rgb(0xF9, 0xF6, 0xF2)
2018-12-22 18:36:26 +00:00
color = {
"0": (ui.rgb(0xCC, 0xC0, 0xB3), ui.rgb(0xBB, 0xAD, 0xA0)),
"2": (ui.rgb(0xEE, 0xE4, 0xDA), color_fg1),
"4": (ui.rgb(0xED, 0xE0, 0xC8), color_fg1),
"8": (ui.rgb(0xF2, 0xB1, 0x79), color_fg2),
"16": (ui.rgb(0xF5, 0x95, 0x63), color_fg2),
"32": (ui.rgb(0xF6, 0x7C, 0x5F), color_fg2),
"64": (ui.rgb(0xF6, 0x5E, 0x3B), color_fg2),
"128": (ui.rgb(0xED, 0xCF, 0x72), color_fg2),
"256": (ui.rgb(0xED, 0xCC, 0x61), color_fg2),
"512": (ui.rgb(0xED, 0xC8, 0x50), color_fg2),
"1024": (ui.rgb(0xED, 0xC5, 0x3F), color_fg2),
"2048": (ui.rgb(0xED, 0xC2, 0x2E), color_fg2),
"lose": (ui.rgb(0xFF, 0x00, 0x00), ui.rgb(0xFF, 0xFF, 0xFF)),
"win": (ui.rgb(0x00, 0xFF, 0x00), ui.rgb(0xFF, 0xFF, 0xFF)),
}
class Game:
2017-10-05 23:33:28 +00:00
def __init__(self):
2018-12-23 12:28:42 +00:00
self.d = ui.display
2018-12-22 18:36:26 +00:00
self.d.backlight(ui.BACKLIGHT_NORMAL)
self.d.bar(0, 0, ui.WIDTH, ui.HEIGHT, color["0"][1])
2017-10-05 23:33:28 +00:00
self.m = [[0 for _ in range(4)] for _ in range(4)]
2018-12-22 18:36:26 +00:00
self.add()
self.add()
2017-10-05 23:33:28 +00:00
2018-12-22 18:36:26 +00:00
def add(self):
2017-10-05 23:33:28 +00:00
while True:
i, j = random.uniform(4), random.uniform(4)
if self.m[i][j] == 0:
self.m[i][j] = 2
break
2018-12-21 22:51:22 +00:00
def contains(self, val):
return val in [i for l in self.m for i in l]
2017-10-05 23:33:28 +00:00
def render(self):
2018-12-22 18:36:26 +00:00
# LOSE endstate
2018-12-21 22:51:22 +00:00
if not self.contains(0):
2018-12-22 18:36:26 +00:00
self.d.bar(0, 0, ui.WIDTH, ui.HEIGHT, color["lose"][0])
self.d.text_center(
120, 128, "YOU LOSE!", ui.BOLD, color["lose"][1], color["lose"][0]
)
# WIN endstate
2018-12-21 22:51:22 +00:00
elif self.contains(2048):
2018-12-22 18:36:26 +00:00
self.d.bar(0, 0, ui.WIDTH, ui.HEIGHT, color["win"][0])
self.d.text_center(
120, 128, "YOU WIN!", ui.BOLD, color["win"][1], color["win"][0]
)
# NORMAL game state
2017-10-05 23:33:28 +00:00
else:
for i in range(4):
for j in range(4):
2018-12-22 18:36:26 +00:00
v = self.m[i][j]
cb, cf = color[str(v)]
self.d.bar_radius(
8 + i * 58, 8 + j * 58, 50, 50, cb, color["0"][1], 2
)
if v:
self.d.text_center(
2018-12-21 22:51:22 +00:00
8 + i * 58 + 25, 8 + j * 58 + 33, str(v), ui.BOLD, cf, cb
)
2018-12-22 18:36:26 +00:00
self.d.backlight(ui.BACKLIGHT_NORMAL)
self.d.refresh()
2017-10-05 23:33:28 +00:00
2018-12-21 22:51:22 +00:00
def update(self, d):
2017-10-05 23:33:28 +00:00
for _ in range(4):
2018-12-21 22:51:22 +00:00
self.compact(d)
2018-12-23 12:28:42 +00:00
self.add()
2017-10-05 23:33:28 +00:00
2018-12-21 22:51:22 +00:00
def compact(self, d):
if d == SWIPE_DOWN:
2017-10-05 23:33:28 +00:00
for i in range(4):
for j in [2, 1, 0]:
if self.m[i][j] == self.m[i][j + 1]:
self.m[i][j + 1] = self.m[i][j] * 2
self.m[i][j] = 0
elif 0 == self.m[i][j + 1]:
self.m[i][j + 1] = self.m[i][j]
self.m[i][j] = 0
2018-12-21 22:51:22 +00:00
elif d == SWIPE_UP:
2017-10-05 23:33:28 +00:00
for i in range(4):
for j in [1, 2, 3]:
if self.m[i][j] == self.m[i][j - 1]:
self.m[i][j - 1] = self.m[i][j] * 2
self.m[i][j] = 0
elif 0 == self.m[i][j - 1]:
self.m[i][j - 1] = self.m[i][j]
self.m[i][j] = 0
2018-12-21 22:51:22 +00:00
elif d == SWIPE_LEFT:
2017-10-05 23:33:28 +00:00
for j in range(4):
for i in [1, 2, 3]:
if self.m[i][j] == self.m[i - 1][j]:
self.m[i - 1][j] = self.m[i][j] * 2
self.m[i][j] = 0
elif 0 == self.m[i - 1][j]:
self.m[i - 1][j] = self.m[i][j]
self.m[i][j] = 0
else: # SWIPE_RIGHT
for j in range(4):
for i in [2, 1, 0]:
if self.m[i][j] == self.m[i + 1][j]:
self.m[i + 1][j] = self.m[i][j] * 2
self.m[i][j] = 0
elif 0 == self.m[i + 1][j]:
self.m[i + 1][j] = self.m[i][j]
self.m[i][j] = 0
async def layout_game():
2018-12-22 18:36:26 +00:00
game = Game()
2017-10-05 23:33:28 +00:00
while True:
2018-12-22 18:36:26 +00:00
game.render()
swipe = await Swipe(absolute=True)
game.update(swipe)
2017-10-05 23:33:28 +00:00
workflow.startdefault(layout_game)
loop.run()