This repository has been archived by the owner on Dec 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added spritesheets, fixed animation system and added spritesheet supp…
…ort to animation system
- Loading branch information
Showing
7 changed files
with
178 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,28 @@ | ||
# Version 5 Todo/Changelog | ||
|
||
## V5 | ||
- [ ] Docs cleanup | ||
- [ ] New color system | ||
- [ ] Optimised font drawing | ||
- [x] Docs cleanup | ||
- [x] New color system | ||
- [x] Optimised font drawing | ||
- [x] OpenGL rendering | ||
|
||
## V5 | ||
- [ ] New Window features | ||
- [ ] Full Screen | ||
- [ ] is_fullscreen | ||
- [ ] toggle_fullscreen | ||
- [ ] Screen Safer | ||
- [ ] get_screensafer_allowed | ||
- [ ] set_screensafer_allowed | ||
- [ ] get_vsync_enabled | ||
- [ ] get_screen_refresh_rate | ||
- [ ] get_display_amount | ||
- [ ] get_active | ||
|
||
- [ ] SpriteSheet class | ||
- [ ] __init__(Image, width, height) | ||
- [ ] frames (variable with all your extracted frames) | ||
- [ ] draw(speed) (gets a argument speed that specifies the speed of drawing in frames) | ||
|
||
- Image system updates | ||
- [ ] Added crop() function |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,38 @@ | ||
from fusionengine.engine.window import Window | ||
from fusionengine.engine.spritesheets import SpriteSheet | ||
|
||
|
||
class Animation: | ||
def __init__(self, window: Window, images: tuple, speed: int) -> None: | ||
def __init__(self, window: Window, images: tuple | SpriteSheet) -> None: | ||
""" | ||
The class to create a Animation. | ||
Args: | ||
window: Window | ||
image: tuple of Images | ||
image (Tuple | Spritesheets): Tuple of Images or a SpriteSheet | ||
Speed: Int (FPS) | ||
""" | ||
self.frame = 0 | ||
self.anim = images | ||
if isinstance(images, SpriteSheet): | ||
self.frames = images.frames | ||
elif isinstance(images, tuple): | ||
self.frames = images | ||
else: | ||
ValueError("Images must be a tuple of Images or a SpriteSheet") | ||
|
||
self.speed = speed | ||
self.window = window | ||
|
||
def draw(self) -> None: | ||
def play(self, speed: float) -> None: | ||
""" | ||
Draw the animation you made before | ||
""" | ||
self.window.set_fps(self.speed) | ||
if isinstance(self.frame, int) or ( | ||
isinstance(self.frame, float) and self.frame.is_integer() | ||
): | ||
if 0 <= int(self.frame) < len(self.frames): | ||
self.frames[int(self.frame)].draw() | ||
|
||
if self.frame >= len(self.anim): | ||
self.frame = 0 | ||
|
||
image = self.anim[self.frame] | ||
self.frame += speed | ||
|
||
image.draw() | ||
|
||
self.frame += 1 | ||
if self.frame >= len(self.frames): | ||
self.frame = 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
from fusionengine.engine.image import Image | ||
from PIL import Image as Imager | ||
|
||
|
||
class SpriteSheet: | ||
def __init__(self, image_path: str, sprite_width: int, sprite_height: int): | ||
""" | ||
Represents a SpriteSheet containing a grid of frames. | ||
Args: | ||
image_path (str): The path to the sprite sheet image file. | ||
sprite_width (int): Width of each sprite frame. | ||
sprite_height (int): Height of each sprite frame. | ||
""" | ||
self.sprite_sheet = Imager.open(image_path).convert("RGBA") | ||
self.sprite_width = sprite_width | ||
self.sprite_height = sprite_height | ||
self.frames = self.get_frames() | ||
self.width = 0 | ||
self.height = 0 | ||
|
||
def get_frames(self) -> list: | ||
""" | ||
Extract frames from the sprite sheet. | ||
Returns: | ||
list: List of Image objects representing individual frames. | ||
""" | ||
frames = [] | ||
columns = self.sprite_sheet.width // self.sprite_width | ||
rows = self.sprite_sheet.height // self.sprite_height | ||
|
||
for row in range(rows): | ||
for col in range(columns): | ||
frame = Image(self.extract_frame(col, row), 0, 0, 0, 0) | ||
frames.append(frame) | ||
|
||
return frames | ||
|
||
def extract_frame(self, col: int, row: int) -> Imager.Image: | ||
""" | ||
Extract a single frame from the sprite sheet. | ||
Args: | ||
col (int): Column index of the sprite. | ||
row (int): Row index of the sprite. | ||
Returns: | ||
Imager.Image: Image object representing the extracted frame. | ||
""" | ||
x = col * self.sprite_width | ||
y = row * self.sprite_height | ||
|
||
frame = self.sprite_sheet.crop( | ||
(x, y, x + self.sprite_width, y + self.sprite_height) | ||
) | ||
return frame | ||
|
||
def frame_size(self, width: int, height: int) -> None: | ||
""" | ||
Set the size of each frame in the sprite sheet. | ||
Args: | ||
width (int): Width to set for each frame. | ||
height (int): Height to set for each frame. | ||
""" | ||
for frame in self.frames: | ||
frame.width = width | ||
frame.height = height | ||
|
||
def frame_pos(self, x: int, y: int) -> None: | ||
""" | ||
Set the position of each frame in the sprite sheet. | ||
Args: | ||
x (int): X position to set for each frame. | ||
y (int): Y position to set for each frame. | ||
""" | ||
for frame in self.frames: | ||
frame.x = x | ||
frame.y = y |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import fusionengine as fusion | ||
|
||
window = fusion.Window("Spritesheet test", 500, 500) | ||
window.set_fps(30) | ||
main_image = fusion.Image(fusion.DEBUGIMAGE, 200, 200, 50, 50) | ||
|
||
spr = fusion.SpriteSheet(fusion.DEBUGIMAGE, 100, 100) | ||
spr.frame_size(60, 60) | ||
spr.frame_pos(50, 50) | ||
|
||
anim = fusion.Animation(window, spr) | ||
|
||
|
||
@window.loop | ||
def loop(): | ||
anim.play(0.5) | ||
main_image.draw() |