metagrid 0.1.3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- metagrid-0.1.3/.gitignore +11 -0
- metagrid-0.1.3/LICENSE +16 -0
- metagrid-0.1.3/NOTICE +5 -0
- metagrid-0.1.3/PKG-INFO +55 -0
- metagrid-0.1.3/README.md +19 -0
- metagrid-0.1.3/THIRD_PARTY_LICENSES/arcade.MIT.txt +14 -0
- metagrid-0.1.3/THIRD_PARTY_LICENSES/pillow.MIT.txt +33 -0
- metagrid-0.1.3/pyproject.toml +62 -0
- metagrid-0.1.3/src/metagrid/CrafterFactory.py +21 -0
- metagrid-0.1.3/src/metagrid/__init__.py +15 -0
- metagrid-0.1.3/src/metagrid/assets/grille_icon.ico +0 -0
- metagrid-0.1.3/src/metagrid/assets/grille_icon_256.png +0 -0
- metagrid-0.1.3/src/metagrid/backends/__init__.py +5 -0
- metagrid-0.1.3/src/metagrid/backends/abstract.py +73 -0
- metagrid-0.1.3/src/metagrid/backends/arcade_impl.py +239 -0
- metagrid-0.1.3/src/metagrid/py.typed +0 -0
metagrid-0.1.3/LICENSE
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
License
|
|
2
|
+
=======
|
|
3
|
+
|
|
4
|
+
Copyright 2025 Matthieu Marchand
|
|
5
|
+
|
|
6
|
+
Metagrid is licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
you may not use this file except in compliance with the License.
|
|
8
|
+
You may obtain a copy of the License at
|
|
9
|
+
|
|
10
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
|
|
12
|
+
Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
See the License for the specific language governing permissions and
|
|
16
|
+
limitations under the License.
|
metagrid-0.1.3/NOTICE
ADDED
metagrid-0.1.3/PKG-INFO
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: metagrid
|
|
3
|
+
Version: 0.1.3
|
|
4
|
+
Summary: Metagrid is a simple game engine for 2D Grid Games
|
|
5
|
+
Author-email: Matthieu Marchand <matthieu.marchand@gmail.com>
|
|
6
|
+
License: License
|
|
7
|
+
=======
|
|
8
|
+
|
|
9
|
+
Copyright 2025 Matthieu Marchand
|
|
10
|
+
|
|
11
|
+
Metagrid is licensed under the Apache License, Version 2.0 (the "License");
|
|
12
|
+
you may not use this file except in compliance with the License.
|
|
13
|
+
You may obtain a copy of the License at
|
|
14
|
+
|
|
15
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
16
|
+
|
|
17
|
+
Unless required by applicable law or agreed to in writing, software
|
|
18
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
19
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
20
|
+
See the License for the specific language governing permissions and
|
|
21
|
+
limitations under the License.
|
|
22
|
+
License-File: LICENSE
|
|
23
|
+
License-File: NOTICE
|
|
24
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
25
|
+
Classifier: Operating System :: OS Independent
|
|
26
|
+
Classifier: Programming Language :: Python :: 3
|
|
27
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
28
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
29
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
30
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
31
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
32
|
+
Requires-Python: >=3.10
|
|
33
|
+
Requires-Dist: arcade>=3.3.0
|
|
34
|
+
Requires-Dist: pillow>=11
|
|
35
|
+
Description-Content-Type: text/markdown
|
|
36
|
+
|
|
37
|
+
# Metagrid
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
## Description
|
|
41
|
+
|
|
42
|
+
Metagrid is a Python library for creating simple 2D grid games.
|
|
43
|
+
|
|
44
|
+
A grid cell can be:
|
|
45
|
+
|
|
46
|
+
- Plain colored
|
|
47
|
+
- An image
|
|
48
|
+
- A character
|
|
49
|
+
|
|
50
|
+
## Installation
|
|
51
|
+
|
|
52
|
+
You can install Metagrid using pip:
|
|
53
|
+
|
|
54
|
+
```shell
|
|
55
|
+
pip install metagrid
|
metagrid-0.1.3/README.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# Metagrid
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
## Description
|
|
5
|
+
|
|
6
|
+
Metagrid is a Python library for creating simple 2D grid games.
|
|
7
|
+
|
|
8
|
+
A grid cell can be:
|
|
9
|
+
|
|
10
|
+
- Plain colored
|
|
11
|
+
- An image
|
|
12
|
+
- A character
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
You can install Metagrid using pip:
|
|
17
|
+
|
|
18
|
+
```shell
|
|
19
|
+
pip install metagrid
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
License
|
|
2
|
+
=======
|
|
3
|
+
|
|
4
|
+
Copyright (c) 2025 Paul Vincent Craven
|
|
5
|
+
|
|
6
|
+
The Arcade library is licensed under the `MIT License`_.
|
|
7
|
+
|
|
8
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
11
|
+
|
|
12
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
13
|
+
|
|
14
|
+
.. _MIT License: https://tldrlegal.com/license/mit-license
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
License
|
|
2
|
+
=======
|
|
3
|
+
|
|
4
|
+
The Python Imaging Library (PIL) is
|
|
5
|
+
|
|
6
|
+
Copyright © 1997-2011 by Secret Labs AB
|
|
7
|
+
Copyright © 1995-2011 by Fredrik Lundh and contributors
|
|
8
|
+
|
|
9
|
+
Pillow is the friendly PIL fork. It is
|
|
10
|
+
|
|
11
|
+
Copyright © 2010 by Jeffrey A. Clark and contributors
|
|
12
|
+
|
|
13
|
+
Like PIL, Pillow is licensed under the open source MIT-CMU License:
|
|
14
|
+
|
|
15
|
+
By obtaining, using, and/or copying this software and/or its associated
|
|
16
|
+
documentation, you agree that you have read, understood, and will comply
|
|
17
|
+
with the following terms and conditions:
|
|
18
|
+
|
|
19
|
+
Permission to use, copy, modify and distribute this software and its
|
|
20
|
+
documentation for any purpose and without fee is hereby granted,
|
|
21
|
+
provided that the above copyright notice appears in all copies, and that
|
|
22
|
+
both that copyright notice and this permission notice appear in supporting
|
|
23
|
+
documentation, and that the name of Secret Labs AB or the author not be
|
|
24
|
+
used in advertising or publicity pertaining to distribution of the software
|
|
25
|
+
without specific, written prior permission.
|
|
26
|
+
|
|
27
|
+
SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
|
|
28
|
+
SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
|
|
29
|
+
IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR BE LIABLE FOR ANY SPECIAL,
|
|
30
|
+
INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
31
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
|
|
32
|
+
OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
33
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling>=1.25", "hatch-vcs>=0.4"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "metagrid"
|
|
7
|
+
dynamic = ["version"]
|
|
8
|
+
description = "Metagrid is a simple game engine for 2D Grid Games"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = { file = "LICENSE" }
|
|
12
|
+
authors = [{ name = "Matthieu Marchand", email = "matthieu.marchand@gmail.com" }]
|
|
13
|
+
classifiers = [
|
|
14
|
+
"Programming Language :: Python :: 3",
|
|
15
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
16
|
+
"Programming Language :: Python :: 3.10",
|
|
17
|
+
"Programming Language :: Python :: 3.11",
|
|
18
|
+
"Programming Language :: Python :: 3.12",
|
|
19
|
+
"Programming Language :: Python :: 3.13",
|
|
20
|
+
"License :: OSI Approved :: MIT License",
|
|
21
|
+
"Operating System :: OS Independent",
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
dependencies = [
|
|
25
|
+
"arcade>=3.3.0",
|
|
26
|
+
"pillow>=11"
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
#[project.urls]
|
|
31
|
+
#Homepage = "https://github.com/toncompte/grox"
|
|
32
|
+
#Repository = "https://github.com/toncompte/grox"
|
|
33
|
+
#Issues = "https://github.com/toncompte/grox/issues"
|
|
34
|
+
#Changelog = "https://github.com/toncompte/grox/blob/main/CHANGELOG.md"
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
[dependency-groups]
|
|
38
|
+
dev = [
|
|
39
|
+
"pytest>=8.3",
|
|
40
|
+
"ruff>=0.6",
|
|
41
|
+
"mypy>=1.11",
|
|
42
|
+
"twine>=6.2.0",
|
|
43
|
+
]
|
|
44
|
+
|
|
45
|
+
[tool.uv]
|
|
46
|
+
default-groups = ["dev"]
|
|
47
|
+
|
|
48
|
+
[tool.hatch.version]
|
|
49
|
+
source = "vcs"
|
|
50
|
+
|
|
51
|
+
[tool.hatch.build.targets.wheel]
|
|
52
|
+
packages = ["src/metagrid"]
|
|
53
|
+
|
|
54
|
+
[tool.hatch.build.targets.sdist]
|
|
55
|
+
include = [
|
|
56
|
+
"src/metagrid", # ← Changé de "src/boxy" à "src/metagrid"
|
|
57
|
+
"README.md",
|
|
58
|
+
"LICENSE",
|
|
59
|
+
"NOTICE",
|
|
60
|
+
"THIRD_PARTY_LICENSES/**",
|
|
61
|
+
"CHANGELOG.md",
|
|
62
|
+
]
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# pyright: reportImportCycles=false
|
|
2
|
+
from typing import Callable, Literal
|
|
3
|
+
from . import backends
|
|
4
|
+
|
|
5
|
+
BackendName = Literal["arcade"]
|
|
6
|
+
|
|
7
|
+
class CrafterFactory:
|
|
8
|
+
|
|
9
|
+
@staticmethod
|
|
10
|
+
def create(backend: BackendName, nrows: int,
|
|
11
|
+
ncols: int, cell_size: int, margin: int, init: Callable[[], None]) -> backends.AbstractCrafter:
|
|
12
|
+
"""
|
|
13
|
+
Crée une instance de moteur de grille selon le backend choisi.
|
|
14
|
+
|
|
15
|
+
Retourne une instance du crafter choisi (ArcadeCrafter est le seul backend supporté pour l'instant).
|
|
16
|
+
"""
|
|
17
|
+
if backend == "arcade":
|
|
18
|
+
from .backends.arcade_impl import ArcadeCrafter
|
|
19
|
+
return ArcadeCrafter(nrows, ncols, cell_size, margin, init)
|
|
20
|
+
else:
|
|
21
|
+
raise ValueError(f"Backend inconnu : {backend!r}") # pyright: ignore[reportUnreachable]
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
try:
|
|
2
|
+
from importlib.metadata import version
|
|
3
|
+
__version__ = version("metagrid")
|
|
4
|
+
except Exception:
|
|
5
|
+
__version__ = "0.0.0"
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
from typing import Callable
|
|
9
|
+
from .backends import AbstractCrafter
|
|
10
|
+
from .CrafterFactory import CrafterFactory
|
|
11
|
+
|
|
12
|
+
def create(nb_lignes: int, nb_colonnes: int, cell_size: int, margin: int, init: Callable[[], None]) -> AbstractCrafter:
|
|
13
|
+
return CrafterFactory.create("arcade", nb_lignes, nb_colonnes, cell_size, margin, init)
|
|
14
|
+
|
|
15
|
+
__all__ = ["create"]
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
from typing import Callable
|
|
2
|
+
from abc import ABCMeta, abstractmethod
|
|
3
|
+
|
|
4
|
+
class AbstractCrafter(metaclass = ABCMeta):
|
|
5
|
+
"""
|
|
6
|
+
Abstracting the functionalities independently of its implementation
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
def __init__(self, nb_lignes: int, nb_colonnes: int, cell_size: int, margin: int, init: Callable[[], None]):
|
|
10
|
+
"""
|
|
11
|
+
Initialisation du jeu.
|
|
12
|
+
"""
|
|
13
|
+
self.margin: int = margin # grid line with (px)
|
|
14
|
+
self.nrows: int = nb_lignes # Number of rows
|
|
15
|
+
self.ncols: int = nb_colonnes # Number of columns
|
|
16
|
+
self.cell_size: int = cell_size # Cell size
|
|
17
|
+
self.fps: int = 60 # FPS, defaults to 60
|
|
18
|
+
self.frame_no: int = 0 # Holds the number of frame since start
|
|
19
|
+
self.init: Callable[[], None] = init
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@abstractmethod
|
|
24
|
+
def start(self, fn_click: Callable[[int, int], None] | None,
|
|
25
|
+
fn_key: Callable[[str], None] | None,
|
|
26
|
+
fn_draw: Callable[[], None],
|
|
27
|
+
fn_update: Callable[[], None]):
|
|
28
|
+
"""
|
|
29
|
+
Start the engine, declaring callbacks
|
|
30
|
+
"""
|
|
31
|
+
self.fn_click: Callable[[int, int], None] | None = fn_click
|
|
32
|
+
self.fn_key: Callable[[str], None] | None= fn_key
|
|
33
|
+
self.fn_draw: Callable[[], None] | None = fn_draw
|
|
34
|
+
self.fn_update: Callable[[], None] | None = fn_update
|
|
35
|
+
...
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
@abstractmethod
|
|
39
|
+
def exit(self):
|
|
40
|
+
"""Permet de fermer l'application"""
|
|
41
|
+
...
|
|
42
|
+
|
|
43
|
+
@abstractmethod
|
|
44
|
+
def set_cell_color(self, i: int, j: int, couleur: str):
|
|
45
|
+
"""permet decolorier une case de la grille"""
|
|
46
|
+
...
|
|
47
|
+
|
|
48
|
+
@abstractmethod
|
|
49
|
+
def set_cell_image(self, i: int, j: int, image: str):
|
|
50
|
+
"""permet decolorier une case de la grille"""
|
|
51
|
+
...
|
|
52
|
+
|
|
53
|
+
@abstractmethod
|
|
54
|
+
def set_cell_char(self, i: int, j: int, char: str, color: str):
|
|
55
|
+
"""sets a cell to a character"""
|
|
56
|
+
assert len(char) < 2
|
|
57
|
+
...
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
@abstractmethod
|
|
61
|
+
def load_image(self, name: str, path: str):
|
|
62
|
+
"""Loads the image stored at `path` under the `name` key in the engine's textures cache"""
|
|
63
|
+
...
|
|
64
|
+
|
|
65
|
+
@abstractmethod
|
|
66
|
+
def show_init_dialog(self, texte1: str, texte2: str):
|
|
67
|
+
"""shows a screen with text1 and text2 right under.
|
|
68
|
+
Click on the screen to dismiss it then run the init callback"""
|
|
69
|
+
...
|
|
70
|
+
|
|
71
|
+
@abstractmethod
|
|
72
|
+
def play_sound(self, path: str):
|
|
73
|
+
...
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
from arcade.sound import Sound
|
|
2
|
+
from arcade.application import View
|
|
3
|
+
from arcade.application import Window
|
|
4
|
+
from arcade.sprite.colored import SpriteSolidColor
|
|
5
|
+
from arcade.texture.texture import Texture
|
|
6
|
+
from typing_extensions import override
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
from arcade.types import Color, RGBOrA255
|
|
10
|
+
from .abstract import AbstractCrafter
|
|
11
|
+
from typing import Callable
|
|
12
|
+
from PIL import Image
|
|
13
|
+
import arcade
|
|
14
|
+
import logging
|
|
15
|
+
import re
|
|
16
|
+
|
|
17
|
+
logger = logging.getLogger(__name__)
|
|
18
|
+
logger.setLevel(logging.INFO)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _hex_to_rgb(hex_color: str) -> RGBOrA255:
|
|
22
|
+
assert bool(re.fullmatch(r"#([0-9a-fA-F]{6}|[0-9a-fA-F]{8})", hex_color)), f"Bad color format for #RRGGBB/#RRGGBBAA: {hex_color}"
|
|
23
|
+
|
|
24
|
+
hex_color = hex_color.lstrip("#")
|
|
25
|
+
if len(hex_color) == 6:
|
|
26
|
+
return tuple[int, int, int](int(hex_color[i:i+2], 16) for i in (0, 2, 4)) #type: ignore
|
|
27
|
+
elif len(hex_color) == 8:
|
|
28
|
+
return tuple[int, int, int, int](int(hex_color[i:i+2], 16) for i in (0, 2, 4, 6)) #type: ignore
|
|
29
|
+
else:
|
|
30
|
+
raise ValueError("Invalid hex color format")
|
|
31
|
+
|
|
32
|
+
class ArcadeCrafter(AbstractCrafter):
|
|
33
|
+
def __init__(self, nb_lignes: int, nb_colonnes: int, cell_size: int, margin: int, init: Callable[[], None]):
|
|
34
|
+
super().__init__(nb_lignes, nb_colonnes, cell_size, margin, init)
|
|
35
|
+
WINDOW_WIDTH = (self.cell_size + self.margin) * self.ncols + self.margin
|
|
36
|
+
WINDOW_HEIGHT = (self.cell_size + self.margin) * self.nrows + self.margin
|
|
37
|
+
WINDOW_TITLE = ""
|
|
38
|
+
|
|
39
|
+
self.window: Window = arcade.Window(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE)
|
|
40
|
+
self.view: GameView = GameView(self)
|
|
41
|
+
self.frame_no: int = 0
|
|
42
|
+
|
|
43
|
+
@override
|
|
44
|
+
def start(self, fn_click: Callable[[int, int], None] | None,
|
|
45
|
+
fn_key: Callable[[str], None] | None,
|
|
46
|
+
fn_draw: Callable[[], None],
|
|
47
|
+
fn_update: Callable[[], None]):
|
|
48
|
+
"""Cette fonction permet de démarrer l'affichage
|
|
49
|
+
d'une grille de taille nb_lign x nb_colonnes.
|
|
50
|
+
la fonction chargée de la gestion du clic est la fonction fn_click
|
|
51
|
+
"""
|
|
52
|
+
super().start(fn_click, fn_key, fn_draw, fn_update)
|
|
53
|
+
|
|
54
|
+
self.window.show_view(self.view)
|
|
55
|
+
|
|
56
|
+
arcade.run()
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
@override
|
|
60
|
+
def exit(self):
|
|
61
|
+
pass
|
|
62
|
+
|
|
63
|
+
@override
|
|
64
|
+
def set_cell_color(self, i: int, j: int, couleur: str):
|
|
65
|
+
"""permet de colorier une case de la grille"""
|
|
66
|
+
if i >= self.nrows or j >= self.ncols:
|
|
67
|
+
return
|
|
68
|
+
i = self.nrows - 1 - i
|
|
69
|
+
|
|
70
|
+
# TODO improve by not recreating each time
|
|
71
|
+
empty_image = Image.new("RGBA", (self.cell_size, self.cell_size), (255, 255, 255, 255))
|
|
72
|
+
empty_texture = arcade.Texture(name="vide", image=empty_image)
|
|
73
|
+
|
|
74
|
+
self.view.grid_sprites[i][j].texture = empty_texture
|
|
75
|
+
self.view.grid_sprites[i][j].color = _hex_to_rgb(couleur)
|
|
76
|
+
|
|
77
|
+
@override
|
|
78
|
+
def set_cell_image(self, i: int, j: int, image: str):
|
|
79
|
+
"""Color cell i,j with image in cache"""
|
|
80
|
+
if i >= self.nrows or j >= self.ncols:
|
|
81
|
+
return
|
|
82
|
+
i = self.nrows - 1 - i
|
|
83
|
+
self.view.grid_sprites[i][j].color = _hex_to_rgb("#FFFFFFFF")
|
|
84
|
+
self.view.grid_sprites[i][j].texture = self.view.textures[image]
|
|
85
|
+
|
|
86
|
+
@override
|
|
87
|
+
def set_cell_char(self, i: int, j: int, char: str, color: str):
|
|
88
|
+
super().set_cell_char(i, j, char, color)
|
|
89
|
+
if 0 <= i < self.nrows and 0 <= j < self.ncols:
|
|
90
|
+
i_flipped = self.nrows - 1 - i
|
|
91
|
+
sprite = self.view.grid_sprites[i_flipped][j]
|
|
92
|
+
|
|
93
|
+
if char:
|
|
94
|
+
# Create or update the Text
|
|
95
|
+
if self.view.grid_chars[i][j] is None:
|
|
96
|
+
self.view.grid_chars[i][j] = arcade.Text(
|
|
97
|
+
text=char,
|
|
98
|
+
x=sprite.center_x,
|
|
99
|
+
y=sprite.center_y,
|
|
100
|
+
color=_hex_to_rgb(color),
|
|
101
|
+
font_size=48,
|
|
102
|
+
anchor_x="center",
|
|
103
|
+
anchor_y="center"
|
|
104
|
+
)
|
|
105
|
+
elif self.view.grid_chars[i][j].text != char: #type: ignore # pyright: ignore[reportOptionalMemberAccess]
|
|
106
|
+
self.view.grid_chars[i][j].text = char #type: ignore # pyright: ignore[reportOptionalMemberAccess]
|
|
107
|
+
else:
|
|
108
|
+
# Clear the character
|
|
109
|
+
self.view.grid_chars[i][j] = None
|
|
110
|
+
|
|
111
|
+
@override
|
|
112
|
+
def load_image(self, name: str, path: str):
|
|
113
|
+
self.view.textures[name] = arcade.load_texture(path)
|
|
114
|
+
self.view.textures[name].width = self.cell_size
|
|
115
|
+
self.view.textures[name].height = self.cell_size
|
|
116
|
+
|
|
117
|
+
@override
|
|
118
|
+
def show_init_dialog(self, texte1: str, texte2: str):
|
|
119
|
+
print("Not implemented yet")
|
|
120
|
+
|
|
121
|
+
@override
|
|
122
|
+
def play_sound(self, path: str):
|
|
123
|
+
son: Sound = arcade.load_sound(path)
|
|
124
|
+
_ = son.play()
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
class GameView(arcade.View):
|
|
129
|
+
"""
|
|
130
|
+
Main application class.
|
|
131
|
+
"""
|
|
132
|
+
|
|
133
|
+
def __init__(self, crafter: ArcadeCrafter):
|
|
134
|
+
"""
|
|
135
|
+
Set up the application.
|
|
136
|
+
"""
|
|
137
|
+
super().__init__()
|
|
138
|
+
self.crafter: ArcadeCrafter = crafter
|
|
139
|
+
# Set the background color of the window
|
|
140
|
+
self.background_color: Color = arcade.color.BLACK
|
|
141
|
+
|
|
142
|
+
self.textures: dict[str, arcade.Texture] = dict[str, Texture]()
|
|
143
|
+
# 1d list of all sprites in the two-dimensional sprite list
|
|
144
|
+
self.grid_sprite_list: arcade.SpriteList[arcade.Sprite] = arcade.SpriteList()
|
|
145
|
+
|
|
146
|
+
# 2d grid hat holds references to the spritelist
|
|
147
|
+
self.grid_sprites: list[list[arcade.Sprite]] = []
|
|
148
|
+
|
|
149
|
+
# 2d grid that holds Texts to be drawn on top of sprites
|
|
150
|
+
self.grid_chars: list[list[arcade.Text|None]] = [[None for _ in range(crafter.ncols)] for _ in range(crafter.nrows)]
|
|
151
|
+
|
|
152
|
+
# Create a list of solidcolor sprites to represent each cell
|
|
153
|
+
for row in range(self.crafter.nrows):
|
|
154
|
+
self.grid_sprites.append([])
|
|
155
|
+
for column in range(self.crafter.ncols):
|
|
156
|
+
x = column * (self.crafter.cell_size + self.crafter.margin) + (self.crafter.cell_size / 2 + self.crafter.margin)
|
|
157
|
+
y = row * (self.crafter.cell_size + self.crafter.margin) + (self.crafter.cell_size / 2 + self.crafter.margin)
|
|
158
|
+
sprite: SpriteSolidColor = arcade.SpriteSolidColor(self.crafter.cell_size, self.crafter.cell_size, color=arcade.color.WHITE)
|
|
159
|
+
sprite.center_x = x
|
|
160
|
+
sprite.center_y = y
|
|
161
|
+
self.grid_sprite_list.append(sprite)
|
|
162
|
+
self.grid_sprites[row].append(sprite)
|
|
163
|
+
|
|
164
|
+
@override
|
|
165
|
+
def on_update(self, delta_time: float) -> bool | None:
|
|
166
|
+
logger.debug(f"on_update({delta_time})")
|
|
167
|
+
self.crafter.frame_no += 1 #! Incrémentation du frame_no
|
|
168
|
+
#print("test")
|
|
169
|
+
_ = super().on_update(delta_time)
|
|
170
|
+
if self.crafter.fn_update:
|
|
171
|
+
self.crafter.fn_update()
|
|
172
|
+
|
|
173
|
+
@override
|
|
174
|
+
def on_key_press(self, symbol: int, modifiers: int) -> bool | None:
|
|
175
|
+
logger.debug(f"on_key_press({symbol}, {modifiers})")
|
|
176
|
+
_ = super().on_key_press(symbol, modifiers)
|
|
177
|
+
#print(symbol, chr(symbol), modifiers)
|
|
178
|
+
if self.crafter.fn_key:
|
|
179
|
+
self.crafter.fn_key(chr(symbol))
|
|
180
|
+
# self.immediate_update()
|
|
181
|
+
|
|
182
|
+
@override
|
|
183
|
+
def on_draw(self):
|
|
184
|
+
"""
|
|
185
|
+
Render the screen.
|
|
186
|
+
"""
|
|
187
|
+
if self.crafter.fn_draw:
|
|
188
|
+
self.crafter.fn_draw()
|
|
189
|
+
self.clear()
|
|
190
|
+
self.grid_sprite_list.draw()
|
|
191
|
+
#print(self.grid_chars)
|
|
192
|
+
for i in range(self.crafter.nrows):
|
|
193
|
+
for j in range(self.crafter.ncols):
|
|
194
|
+
text_obj = self.grid_chars[i][j]
|
|
195
|
+
if text_obj:
|
|
196
|
+
text_obj.draw()
|
|
197
|
+
|
|
198
|
+
@override
|
|
199
|
+
def on_mouse_press(self, x: int, y: int, button: int, modifiers: int):
|
|
200
|
+
"""
|
|
201
|
+
Called when the user presses a mouse button.
|
|
202
|
+
"""
|
|
203
|
+
logger.debug(f"on_mouse_press({x},{y},{button},{modifiers})")
|
|
204
|
+
column = int(x // (self.crafter.cell_size + self.crafter.margin))
|
|
205
|
+
row = self.crafter.nrows - 1 - int(y // (self.crafter.cell_size + self.crafter.margin))
|
|
206
|
+
logger.debug(f"Grid coordinates: ({row}, {column})")
|
|
207
|
+
if self.crafter.fn_click:
|
|
208
|
+
self.crafter.fn_click(row, column)
|
|
209
|
+
#self.immediate_update()
|
|
210
|
+
|
|
211
|
+
"""
|
|
212
|
+
def immediate_update(self):
|
|
213
|
+
self.on_update(0)
|
|
214
|
+
self.window.set_update_rate(0)
|
|
215
|
+
self.on_draw()
|
|
216
|
+
self.window.flip()
|
|
217
|
+
self.window.set_update_rate(1/self.crafter.fps)
|
|
218
|
+
"""
|
|
219
|
+
|
|
220
|
+
class InformationView(arcade.View):
|
|
221
|
+
|
|
222
|
+
def __init__(self, view: arcade.View, background_color: str) -> None:
|
|
223
|
+
self.window: Window = view.window
|
|
224
|
+
self.view: View = view
|
|
225
|
+
|
|
226
|
+
super().__init__(self.window, _hex_to_rgb(background_color) if background_color else None)
|
|
227
|
+
|
|
228
|
+
@override
|
|
229
|
+
def on_draw(self):
|
|
230
|
+
self.clear()
|
|
231
|
+
arcade.draw_text("Instructions Screen", self.window.width / 2, self.window.height / 2,
|
|
232
|
+
arcade.color.BLACK, font_size=50, anchor_x="center")
|
|
233
|
+
arcade.draw_text("Click to advance", self.window.width / 2 , self.window.height / 2 - 75,
|
|
234
|
+
arcade.color.GRAY, font_size=20, anchor_x="center")
|
|
235
|
+
|
|
236
|
+
@override
|
|
237
|
+
def on_mouse_press(self, x: int, y: int, button: int, modifiers: int):
|
|
238
|
+
game_view = self.view
|
|
239
|
+
self.window.show_view(game_view)
|
|
File without changes
|