|
@@ -4,6 +4,7 @@ from typing import Type
|
|
|
|
|
|
|
|
import pygame
|
|
import pygame
|
|
|
|
|
|
|
|
|
|
+import jogai
|
|
|
from jogai import _events, logger, settings, utils
|
|
from jogai import _events, logger, settings, utils
|
|
|
from jogai._events import _get_keys
|
|
from jogai._events import _get_keys
|
|
|
from jogai._globals import _window
|
|
from jogai._globals import _window
|
|
@@ -118,9 +119,9 @@ class Character(pygame.sprite.Sprite):
|
|
|
relative: True for a incremental movement, False for an absolute movement
|
|
relative: True for a incremental movement, False for an absolute movement
|
|
|
|
|
|
|
|
Examples:
|
|
Examples:
|
|
|
- >>> ch = Character('protagonist.png')
|
|
|
|
|
- >>> ch.move(5, 3)
|
|
|
|
|
- >>> ch.move()
|
|
|
|
|
|
|
+ >>> protagonist = Character('protagonist.png')
|
|
|
|
|
+ >>> protagonist.move(5, 3)
|
|
|
|
|
+ >>> protagonist.move()
|
|
|
"""
|
|
"""
|
|
|
if x is None:
|
|
if x is None:
|
|
|
x = self.speed
|
|
x = self.speed
|
|
@@ -151,8 +152,8 @@ class Character(pygame.sprite.Sprite):
|
|
|
key: Optional key to perform the action
|
|
key: Optional key to perform the action
|
|
|
|
|
|
|
|
Examples:
|
|
Examples:
|
|
|
- >>> ch = Character('protagonist.png')
|
|
|
|
|
- >>> ch.move_to(0, 0)
|
|
|
|
|
|
|
+ >>> protagonist = Character('protagonist.png')
|
|
|
|
|
+ >>> protagonist.move_to(0, 0)
|
|
|
"""
|
|
"""
|
|
|
self.move(x, y, key, relative=False)
|
|
self.move(x, y, key, relative=False)
|
|
|
|
|
|
|
@@ -165,9 +166,9 @@ class Character(pygame.sprite.Sprite):
|
|
|
key: Optional key to perform the action
|
|
key: Optional key to perform the action
|
|
|
|
|
|
|
|
Examples:
|
|
Examples:
|
|
|
- >>> ch = Character('protagonist.png')
|
|
|
|
|
- >>> ch.backward(3)
|
|
|
|
|
- >>> ch.backward()
|
|
|
|
|
|
|
+ >>> protagonist = Character('protagonist.png')
|
|
|
|
|
+ >>> protagonist.backward(3)
|
|
|
|
|
+ >>> protagonist.backward()
|
|
|
"""
|
|
"""
|
|
|
if amount is None:
|
|
if amount is None:
|
|
|
amount = self.speed
|
|
amount = self.speed
|
|
@@ -182,9 +183,9 @@ class Character(pygame.sprite.Sprite):
|
|
|
key: Optional key to perform the action
|
|
key: Optional key to perform the action
|
|
|
|
|
|
|
|
Examples:
|
|
Examples:
|
|
|
- >>> ch = Character('protagonist.png')
|
|
|
|
|
- >>> ch.down(3)
|
|
|
|
|
- >>> ch.down()
|
|
|
|
|
|
|
+ >>> protagonist = Character('protagonist.png')
|
|
|
|
|
+ >>> protagonist.down(3)
|
|
|
|
|
+ >>> protagonist.down()
|
|
|
"""
|
|
"""
|
|
|
if amount is None:
|
|
if amount is None:
|
|
|
amount = self.speed
|
|
amount = self.speed
|
|
@@ -201,9 +202,9 @@ class Character(pygame.sprite.Sprite):
|
|
|
key: Optional key to perform the action
|
|
key: Optional key to perform the action
|
|
|
|
|
|
|
|
Examples:
|
|
Examples:
|
|
|
- >>> ch = Character('protagonist.png')
|
|
|
|
|
- >>> ch.forward(3)
|
|
|
|
|
- >>> ch.forward()
|
|
|
|
|
|
|
+ >>> protagonist = Character('protagonist.png')
|
|
|
|
|
+ >>> protagonist.forward(3)
|
|
|
|
|
+ >>> protagonist.forward()
|
|
|
"""
|
|
"""
|
|
|
if amount is None:
|
|
if amount is None:
|
|
|
amount = self.speed
|
|
amount = self.speed
|
|
@@ -218,9 +219,9 @@ class Character(pygame.sprite.Sprite):
|
|
|
key: Optional key to perform the action
|
|
key: Optional key to perform the action
|
|
|
|
|
|
|
|
Examples:
|
|
Examples:
|
|
|
- >>> ch = Character('protagonist.png')
|
|
|
|
|
- >>> ch.up(3)
|
|
|
|
|
- >>> ch.up()
|
|
|
|
|
|
|
+ >>> protagonist = Character('protagonist.png')
|
|
|
|
|
+ >>> protagonist.up(3)
|
|
|
|
|
+ >>> protagonist.up()
|
|
|
"""
|
|
"""
|
|
|
if amount is None:
|
|
if amount is None:
|
|
|
amount = self.speed
|
|
amount = self.speed
|
|
@@ -258,8 +259,8 @@ class Character(pygame.sprite.Sprite):
|
|
|
key: Optional key to perform the action
|
|
key: Optional key to perform the action
|
|
|
|
|
|
|
|
Examples:
|
|
Examples:
|
|
|
- >>> ch = Character('protagonist.png')
|
|
|
|
|
- >>> ch.decrease(0.1)
|
|
|
|
|
|
|
+ >>> protagonist = Character('protagonist.png')
|
|
|
|
|
+ >>> protagonist.decrease(0.1)
|
|
|
"""
|
|
"""
|
|
|
if isinstance(decrement, int):
|
|
if isinstance(decrement, int):
|
|
|
decrement = float(decrement)
|
|
decrement = float(decrement)
|
|
@@ -278,8 +279,8 @@ class Character(pygame.sprite.Sprite):
|
|
|
key: Optional key to perform the action
|
|
key: Optional key to perform the action
|
|
|
|
|
|
|
|
Examples:
|
|
Examples:
|
|
|
- >>> ch = Character('protagonist.png')
|
|
|
|
|
- >>> ch.increase(0.1)
|
|
|
|
|
|
|
+ >>> protagonist = Character('protagonist.png')
|
|
|
|
|
+ >>> protagonist.increase(0.1)
|
|
|
"""
|
|
"""
|
|
|
if isinstance(increment, int):
|
|
if isinstance(increment, int):
|
|
|
increment = float(increment)
|
|
increment = float(increment)
|
|
@@ -301,8 +302,8 @@ class Character(pygame.sprite.Sprite):
|
|
|
scale_factor: factor to scale
|
|
scale_factor: factor to scale
|
|
|
|
|
|
|
|
Examples:
|
|
Examples:
|
|
|
- >>> ch = Character('protagonist.png')
|
|
|
|
|
- >>> ch.scale_to(2)
|
|
|
|
|
|
|
+ >>> protagonist = Character('protagonist.png')
|
|
|
|
|
+ >>> protagonist.scale_to(2)
|
|
|
"""
|
|
"""
|
|
|
if not isinstance(scale_factor, float) and not isinstance(
|
|
if not isinstance(scale_factor, float) and not isinstance(
|
|
|
scale_factor, int
|
|
scale_factor, int
|
|
@@ -335,8 +336,8 @@ class Character(pygame.sprite.Sprite):
|
|
|
height: the character height in pixels
|
|
height: the character height in pixels
|
|
|
|
|
|
|
|
Examples:
|
|
Examples:
|
|
|
- >>> ch = Character('protagonist.png')
|
|
|
|
|
- >>> ch.set_height(100)
|
|
|
|
|
|
|
+ >>> protagonist = Character('protagonist.png')
|
|
|
|
|
+ >>> protagonist.set_height(100)
|
|
|
|
|
|
|
|
"""
|
|
"""
|
|
|
if not isinstance(height, int):
|
|
if not isinstance(height, int):
|
|
@@ -360,8 +361,8 @@ class Character(pygame.sprite.Sprite):
|
|
|
predefined_keys: The predefined keys with the associated action.
|
|
predefined_keys: The predefined keys with the associated action.
|
|
|
|
|
|
|
|
Examples:
|
|
Examples:
|
|
|
- >>> ch = Character('protagonist.png')
|
|
|
|
|
- >>> ch.set_keys({'forward': 'd'})
|
|
|
|
|
|
|
+ >>> protagonist = Character('protagonist.png')
|
|
|
|
|
+ >>> protagonist.set_keys({'forward': 'd'})
|
|
|
"""
|
|
"""
|
|
|
if not isinstance(predefined_keys, dict):
|
|
if not isinstance(predefined_keys, dict):
|
|
|
raise TypeError(
|
|
raise TypeError(
|
|
@@ -382,8 +383,8 @@ class Character(pygame.sprite.Sprite):
|
|
|
key: Optional key to perform the action
|
|
key: Optional key to perform the action
|
|
|
|
|
|
|
|
Examples:
|
|
Examples:
|
|
|
- >>> ch = Character('protagonist.png')
|
|
|
|
|
- >>> ch.set_position((0, 0))
|
|
|
|
|
|
|
+ >>> protagonist = Character('protagonist.png')
|
|
|
|
|
+ >>> protagonist.set_position((0, 0))
|
|
|
"""
|
|
"""
|
|
|
if not isinstance(position, tuple) and not isinstance(position, list):
|
|
if not isinstance(position, tuple) and not isinstance(position, list):
|
|
|
raise TypeError(_('The position must be a tuple or list (x, y).'))
|
|
raise TypeError(_('The position must be a tuple or list (x, y).'))
|
|
@@ -401,8 +402,8 @@ class Character(pygame.sprite.Sprite):
|
|
|
speed: The character speed
|
|
speed: The character speed
|
|
|
|
|
|
|
|
Examples:
|
|
Examples:
|
|
|
- >>> ch = Character('protagonist.png')
|
|
|
|
|
- >>> ch.set_speed(10)
|
|
|
|
|
|
|
+ >>> protagonist = Character('protagonist.png')
|
|
|
|
|
+ >>> protagonist.set_speed(10)
|
|
|
"""
|
|
"""
|
|
|
if isinstance(speed, int):
|
|
if isinstance(speed, int):
|
|
|
self.speed = speed
|
|
self.speed = speed
|
|
@@ -415,8 +416,8 @@ class Character(pygame.sprite.Sprite):
|
|
|
width: the character width in pixels
|
|
width: the character width in pixels
|
|
|
|
|
|
|
|
Examples:
|
|
Examples:
|
|
|
- >>> ch = Character('protagonist.png')
|
|
|
|
|
- >>> ch.set_width(100)
|
|
|
|
|
|
|
+ >>> protagonist = Character('protagonist.png')
|
|
|
|
|
+ >>> protagonist.set_width(100)
|
|
|
"""
|
|
"""
|
|
|
if not isinstance(width, int):
|
|
if not isinstance(width, int):
|
|
|
raise TypeError(_('Width must be a not negative integer.'))
|
|
raise TypeError(_('Width must be a not negative integer.'))
|
|
@@ -430,10 +431,59 @@ class Character(pygame.sprite.Sprite):
|
|
|
)
|
|
)
|
|
|
self.rect = self.image.get_rect()
|
|
self.rect = self.image.get_rect()
|
|
|
|
|
|
|
|
- def load(self, filename: str) -> bool:
|
|
|
|
|
|
|
+ def touch(
|
|
|
|
|
+ self,
|
|
|
|
|
+ other_characters: Type['Character'] | Type['jogai.group.Group'] | list,
|
|
|
|
|
+ key: str = '',
|
|
|
|
|
+ ) -> bool:
|
|
|
|
|
+ """
|
|
|
|
|
+ Detect if one character is touching another(s).
|
|
|
|
|
+
|
|
|
|
|
+ Parameters:
|
|
|
|
|
+ other_characters: character or list of characters to check
|
|
|
|
|
+
|
|
|
|
|
+ Returns:
|
|
|
|
|
+ True, if it is touching; False, otherwise.
|
|
|
|
|
+
|
|
|
|
|
+ Examples:
|
|
|
|
|
+ >>> protagonist = Character('protagonist.png')
|
|
|
|
|
+ >>> secondary = Character('secondary.png')
|
|
|
|
|
+ >>> protagonist.touch(secondary)
|
|
|
|
|
+ True
|
|
|
|
|
+ """
|
|
|
|
|
+
|
|
|
|
|
+ if isinstance(other_characters, Character):
|
|
|
|
|
+ other_characters = [other_characters]
|
|
|
|
|
+
|
|
|
|
|
+ if not isinstance(other_characters, list) and not isinstance(
|
|
|
|
|
+ other_characters, jogai.group.Group
|
|
|
|
|
+ ):
|
|
|
|
|
+ raise TypeError(
|
|
|
|
|
+ _(
|
|
|
|
|
+ 'The instances to be touched must be a character or a list of characters.'
|
|
|
|
|
+ )
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+ if isinstance(other_characters, list) and not all(
|
|
|
|
|
+ map(lambda x: isinstance(x, Character), other_characters)
|
|
|
|
|
+ ):
|
|
|
|
|
+ raise ValueError(
|
|
|
|
|
+ _('All instances to be touched must be characters.')
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+ if not isinstance(key, str):
|
|
|
|
|
+ raise TypeError(_('Key pressed must be a string.'))
|
|
|
|
|
+
|
|
|
|
|
+ for other_character in other_characters:
|
|
|
|
|
+ if not key or _get_keys()[utils.eval_key(key)]:
|
|
|
|
|
+ if self.rect.colliderect(other_character.rect):
|
|
|
|
|
+ return True
|
|
|
|
|
+ return False
|
|
|
|
|
+
|
|
|
|
|
+ def load(self, filename: str):
|
|
|
"""
|
|
"""
|
|
|
- >>> ch = Character('protagonist.png')
|
|
|
|
|
- >>> result = ch.load('protagonist.png')
|
|
|
|
|
|
|
+ >>> protagonist = Character('protagonist.png')
|
|
|
|
|
+ >>> protagonist.load('secondary.png')
|
|
|
"""
|
|
"""
|
|
|
self._original_image, self._original_rect = utils._load_image(
|
|
self._original_image, self._original_rect = utils._load_image(
|
|
|
settings.CHARACTERS_DIR, filename
|
|
settings.CHARACTERS_DIR, filename
|
|
@@ -450,8 +500,8 @@ class Character(pygame.sprite.Sprite):
|
|
|
Create a copy of this character keeping size and position.
|
|
Create a copy of this character keeping size and position.
|
|
|
|
|
|
|
|
Examples:
|
|
Examples:
|
|
|
- >>> ch = Character('protagonist.png')
|
|
|
|
|
- >>> other = ch.copy()
|
|
|
|
|
|
|
+ >>> protagonist = Character('protagonist.png')
|
|
|
|
|
+ >>> other = protagonist.copy()
|
|
|
"""
|
|
"""
|
|
|
new_character = Character(self.image_filename)
|
|
new_character = Character(self.image_filename)
|
|
|
new_character.set_height(self.height)
|
|
new_character.set_height(self.height)
|
|
@@ -464,8 +514,8 @@ class Character(pygame.sprite.Sprite):
|
|
|
Paint the character in the main surface (window).
|
|
Paint the character in the main surface (window).
|
|
|
|
|
|
|
|
Examples:
|
|
Examples:
|
|
|
- >>> ch = Character('protagonist.png')
|
|
|
|
|
- >>> ch.paint()
|
|
|
|
|
|
|
+ >>> protagonist = Character('protagonist.png')
|
|
|
|
|
+ >>> protagonist.paint()
|
|
|
"""
|
|
"""
|
|
|
if self.image:
|
|
if self.image:
|
|
|
_window.blit(self.image, self.rect)
|
|
_window.blit(self.image, self.rect)
|