mirror of
https://github.com/slint-ui/slint.git
synced 2025-08-04 02:39:28 +00:00
Python: Add Memory tutorial port
This commit is contained in:
parent
c1e8d7c07d
commit
b967ca4182
2 changed files with 64 additions and 0 deletions
15
examples/memory/Pipfile
Normal file
15
examples/memory/Pipfile
Normal file
|
@ -0,0 +1,15 @@
|
|||
# Copyright © SixtyFPS GmbH <info@slint.dev>
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
[[source]]
|
||||
url = "https://pypi.org/simple"
|
||||
verify_ssl = true
|
||||
name = "pypi"
|
||||
|
||||
[packages]
|
||||
slint = { file = "../../api/python", editable = true }
|
||||
|
||||
[dev-packages]
|
||||
|
||||
[requires]
|
||||
python_version = "3"
|
49
examples/memory/main.py
Normal file
49
examples/memory/main.py
Normal file
|
@ -0,0 +1,49 @@
|
|||
# Copyright © SixtyFPS GmbH <info@slint.dev>
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
# autopep8: off
|
||||
from datetime import timedelta, datetime
|
||||
import os
|
||||
import random
|
||||
import itertools
|
||||
import slint
|
||||
from slint import Color, ListModel, Timer, TimerMode
|
||||
import memory_slint
|
||||
# autopep8: on
|
||||
|
||||
|
||||
class MainWindow(memory_slint.MainWindow):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
initial_tiles = self.memory_tiles
|
||||
tiles = ListModel(itertools.chain(initial_tiles, initial_tiles))
|
||||
random.shuffle(tiles)
|
||||
self.memory_tiles = tiles
|
||||
|
||||
@slint.callback
|
||||
def check_if_pair_solved(self):
|
||||
flipped_tiles = [(index, tile) for index, tile in enumerate(self.memory_tiles) if tile["image-visible"] and not tile["solved"]]
|
||||
if len(flipped_tiles) == 2:
|
||||
tile1_index, tile1 = flipped_tiles[0]
|
||||
tile2_index, tile2 = flipped_tiles[1]
|
||||
is_pair_solved = tile1["image"].path == tile2["image"].path
|
||||
if is_pair_solved:
|
||||
tile1["solved"] = True
|
||||
self.memory_tiles[tile1_index] = tile1
|
||||
tile2["solved"] = True
|
||||
self.memory_tiles[tile2_index] = tile2
|
||||
else:
|
||||
self.disable_tiles = True
|
||||
|
||||
def reenable_tiles():
|
||||
self.disable_tiles = False
|
||||
tile1["image-visible"] = False
|
||||
self.memory_tiles[tile1_index] = tile1
|
||||
tile2["image-visible"] = False
|
||||
self.memory_tiles[tile2_index] = tile2
|
||||
|
||||
Timer.single_shot(timedelta(seconds=1), reenable_tiles)
|
||||
|
||||
|
||||
main_window = MainWindow()
|
||||
main_window.run()
|
Loading…
Add table
Add a link
Reference in a new issue