pyscratch-pysc 1.0.4__py3-none-any.whl → 2.0.0__py3-none-any.whl
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.
- assets/startup-assets/tutorial-steps/1-create-sprites/main.py +10 -0
- assets/startup-assets/tutorial-steps/1-create-sprites/player.py +5 -0
- assets/startup-assets/tutorial-steps/1-create-sprites-task/enemy.py +4 -0
- assets/startup-assets/tutorial-steps/1-create-sprites-task/main.py +11 -0
- assets/startup-assets/tutorial-steps/1-create-sprites-task/player.py +5 -0
- assets/startup-assets/tutorial-steps/2-basic-events/enemy.py +4 -0
- assets/startup-assets/tutorial-steps/2-basic-events/main.py +11 -0
- assets/startup-assets/tutorial-steps/2-basic-events/player.py +31 -0
- assets/startup-assets/tutorial-steps/2-basic-events-task1/enemy.py +4 -0
- assets/startup-assets/tutorial-steps/2-basic-events-task1/main.py +11 -0
- assets/startup-assets/tutorial-steps/2-basic-events-task1/player.py +23 -0
- assets/startup-assets/tutorial-steps/2-basic-events-task2/enemy.py +14 -0
- assets/startup-assets/tutorial-steps/2-basic-events-task2/main.py +11 -0
- assets/startup-assets/tutorial-steps/2-basic-events-task2/player.py +23 -0
- assets/startup-assets/tutorial-steps/3-loops-conditions/enemy.py +14 -0
- assets/startup-assets/tutorial-steps/3-loops-conditions/main.py +11 -0
- assets/startup-assets/tutorial-steps/3-loops-conditions/player.py +23 -0
- assets/startup-assets/tutorial-steps/3-loops-conditions-task1/enemy.py +14 -0
- assets/startup-assets/tutorial-steps/3-loops-conditions-task1/main.py +11 -0
- assets/startup-assets/tutorial-steps/3-loops-conditions-task1/player.py +34 -0
- assets/startup-assets/tutorial-steps/3-loops-conditions-task2/enemy.py +23 -0
- assets/startup-assets/tutorial-steps/3-loops-conditions-task2/main.py +11 -0
- assets/startup-assets/tutorial-steps/3-loops-conditions-task2/player.py +41 -0
- assets/startup-assets/tutorial-steps/4-variables/enemy.py +23 -0
- assets/startup-assets/tutorial-steps/4-variables/main.py +11 -0
- assets/startup-assets/tutorial-steps/4-variables/player.py +59 -0
- assets/startup-assets/tutorial-steps/5-sprite-object/enemy.py +19 -0
- assets/startup-assets/tutorial-steps/5-sprite-object/main.py +11 -0
- assets/startup-assets/tutorial-steps/5-sprite-object/player.py +20 -0
- assets/startup-assets/tutorial-steps/5-sprite-object-task1/enemy.py +19 -0
- assets/startup-assets/tutorial-steps/5-sprite-object-task1/main.py +11 -0
- assets/startup-assets/tutorial-steps/5-sprite-object-task1/player.py +38 -0
- assets/startup-assets/tutorial-steps/5-sprite-object-task2/enemy.py +20 -0
- assets/startup-assets/tutorial-steps/5-sprite-object-task2/main.py +11 -0
- assets/startup-assets/tutorial-steps/5-sprite-object-task2/player.py +58 -0
- assets/startup-assets/tutorial-steps/6-referencing-sprites/enemy.py +32 -0
- assets/startup-assets/tutorial-steps/6-referencing-sprites/main.py +11 -0
- assets/startup-assets/tutorial-steps/6-referencing-sprites/player.py +59 -0
- assets/startup-assets/tutorial-steps/6-referencing-sprites-task/enemy.py +37 -0
- assets/startup-assets/tutorial-steps/6-referencing-sprites-task/main.py +11 -0
- assets/startup-assets/tutorial-steps/6-referencing-sprites-task/player.py +60 -0
- assets/startup-assets/tutorial-steps/7-parameterised-events/enemy.py +37 -0
- assets/startup-assets/tutorial-steps/7-parameterised-events/main.py +11 -0
- assets/startup-assets/tutorial-steps/7-parameterised-events/player.py +83 -0
- assets/startup-assets/tutorial-steps/7-parameterised-events-msg/enemy.py +37 -0
- assets/startup-assets/tutorial-steps/7-parameterised-events-msg/main.py +11 -0
- assets/startup-assets/tutorial-steps/7-parameterised-events-msg/player.py +86 -0
- assets/startup-assets/tutorial-steps/7-parameterised-events-task/enemy.py +35 -0
- assets/startup-assets/tutorial-steps/7-parameterised-events-task/main.py +11 -0
- assets/startup-assets/tutorial-steps/7-parameterised-events-task/player.py +86 -0
- assets/startup-assets/tutorial-steps/8-backdrop-music/enemy.py +35 -0
- assets/startup-assets/tutorial-steps/8-backdrop-music/main.py +11 -0
- assets/startup-assets/tutorial-steps/8-backdrop-music/player.py +86 -0
- assets/startup-assets/tutorial-steps/9-polish/enemy.py +39 -0
- assets/startup-assets/tutorial-steps/9-polish/enemy_red.py +40 -0
- assets/startup-assets/tutorial-steps/9-polish/main.py +12 -0
- assets/startup-assets/tutorial-steps/9-polish/player.py +119 -0
- examples/fish/assets/startup-assets/tutorial-steps/1-create-sprites/main.py +10 -0
- examples/fish/assets/startup-assets/tutorial-steps/1-create-sprites/player.py +5 -0
- examples/fish/assets/startup-assets/tutorial-steps/1-create-sprites-task/enemy.py +4 -0
- examples/fish/assets/startup-assets/tutorial-steps/1-create-sprites-task/main.py +11 -0
- examples/fish/assets/startup-assets/tutorial-steps/1-create-sprites-task/player.py +5 -0
- examples/fish/assets/startup-assets/tutorial-steps/2-basic-events/enemy.py +4 -0
- examples/fish/assets/startup-assets/tutorial-steps/2-basic-events/main.py +11 -0
- examples/fish/assets/startup-assets/tutorial-steps/2-basic-events/player.py +31 -0
- examples/fish/assets/startup-assets/tutorial-steps/2-basic-events-task1/enemy.py +4 -0
- examples/fish/assets/startup-assets/tutorial-steps/2-basic-events-task1/main.py +11 -0
- examples/fish/assets/startup-assets/tutorial-steps/2-basic-events-task1/player.py +23 -0
- examples/fish/assets/startup-assets/tutorial-steps/2-basic-events-task2/enemy.py +14 -0
- examples/fish/assets/startup-assets/tutorial-steps/2-basic-events-task2/main.py +11 -0
- examples/fish/assets/startup-assets/tutorial-steps/2-basic-events-task2/player.py +23 -0
- examples/fish/assets/startup-assets/tutorial-steps/3-loops-conditions/enemy.py +14 -0
- examples/fish/assets/startup-assets/tutorial-steps/3-loops-conditions/main.py +11 -0
- examples/fish/assets/startup-assets/tutorial-steps/3-loops-conditions/player.py +23 -0
- examples/fish/assets/startup-assets/tutorial-steps/3-loops-conditions-task1/enemy.py +14 -0
- examples/fish/assets/startup-assets/tutorial-steps/3-loops-conditions-task1/main.py +11 -0
- examples/fish/assets/startup-assets/tutorial-steps/3-loops-conditions-task1/player.py +34 -0
- examples/fish/assets/startup-assets/tutorial-steps/3-loops-conditions-task2/enemy.py +23 -0
- examples/fish/assets/startup-assets/tutorial-steps/3-loops-conditions-task2/main.py +11 -0
- examples/fish/assets/startup-assets/tutorial-steps/3-loops-conditions-task2/player.py +41 -0
- examples/fish/assets/startup-assets/tutorial-steps/4-variables/enemy.py +23 -0
- examples/fish/assets/startup-assets/tutorial-steps/4-variables/main.py +11 -0
- examples/fish/assets/startup-assets/tutorial-steps/4-variables/player.py +59 -0
- examples/fish/assets/startup-assets/tutorial-steps/5-sprite-object/enemy.py +19 -0
- examples/fish/assets/startup-assets/tutorial-steps/5-sprite-object/main.py +11 -0
- examples/fish/assets/startup-assets/tutorial-steps/5-sprite-object/player.py +20 -0
- examples/fish/assets/startup-assets/tutorial-steps/5-sprite-object-task1/enemy.py +19 -0
- examples/fish/assets/startup-assets/tutorial-steps/5-sprite-object-task1/main.py +11 -0
- examples/fish/assets/startup-assets/tutorial-steps/5-sprite-object-task1/player.py +38 -0
- examples/fish/assets/startup-assets/tutorial-steps/5-sprite-object-task2/enemy.py +20 -0
- examples/fish/assets/startup-assets/tutorial-steps/5-sprite-object-task2/main.py +11 -0
- examples/fish/assets/startup-assets/tutorial-steps/5-sprite-object-task2/player.py +58 -0
- examples/fish/assets/startup-assets/tutorial-steps/6-referencing-sprites/enemy.py +32 -0
- examples/fish/assets/startup-assets/tutorial-steps/6-referencing-sprites/main.py +11 -0
- examples/fish/assets/startup-assets/tutorial-steps/6-referencing-sprites/player.py +59 -0
- examples/fish/assets/startup-assets/tutorial-steps/6-referencing-sprites-task/enemy.py +37 -0
- examples/fish/assets/startup-assets/tutorial-steps/6-referencing-sprites-task/main.py +11 -0
- examples/fish/assets/startup-assets/tutorial-steps/6-referencing-sprites-task/player.py +60 -0
- examples/fish/assets/startup-assets/tutorial-steps/7-parameterised-events/enemy.py +37 -0
- examples/fish/assets/startup-assets/tutorial-steps/7-parameterised-events/main.py +11 -0
- examples/fish/assets/startup-assets/tutorial-steps/7-parameterised-events/player.py +83 -0
- examples/fish/assets/startup-assets/tutorial-steps/7-parameterised-events-msg/enemy.py +37 -0
- examples/fish/assets/startup-assets/tutorial-steps/7-parameterised-events-msg/main.py +11 -0
- examples/fish/assets/startup-assets/tutorial-steps/7-parameterised-events-msg/player.py +86 -0
- examples/fish/assets/startup-assets/tutorial-steps/7-parameterised-events-task/enemy.py +35 -0
- examples/fish/assets/startup-assets/tutorial-steps/7-parameterised-events-task/main.py +11 -0
- examples/fish/assets/startup-assets/tutorial-steps/7-parameterised-events-task/player.py +86 -0
- examples/fish/assets/startup-assets/tutorial-steps/8-backdrop-music/enemy.py +35 -0
- examples/fish/assets/startup-assets/tutorial-steps/8-backdrop-music/main.py +11 -0
- examples/fish/assets/startup-assets/tutorial-steps/8-backdrop-music/player.py +86 -0
- examples/fish/assets/startup-assets/tutorial-steps/9-polish/enemy.py +39 -0
- examples/fish/assets/startup-assets/tutorial-steps/9-polish/enemy_red.py +40 -0
- examples/fish/assets/startup-assets/tutorial-steps/9-polish/main.py +12 -0
- examples/fish/assets/startup-assets/tutorial-steps/9-polish/player.py +119 -0
- examples/tutorial-day1/chest.py +21 -0
- examples/tutorial-day1/enemy.py +106 -0
- examples/tutorial-day1/friend.py +51 -0
- examples/tutorial-day1/in-steps/1-create-sprites/chest.py +5 -0
- examples/tutorial-day1/in-steps/1-create-sprites/enemy.py +5 -0
- examples/tutorial-day1/in-steps/1-create-sprites/main.py +9 -0
- examples/tutorial-day1/in-steps/2-basic-events/chest.py +24 -0
- examples/tutorial-day1/in-steps/2-basic-events/enemy.py +18 -0
- examples/tutorial-day1/in-steps/2-basic-events/main.py +9 -0
- examples/tutorial-day1/in-steps/3-flow/chest.py +25 -0
- examples/tutorial-day1/in-steps/3-flow/enemy.py +56 -0
- examples/tutorial-day1/in-steps/3-flow/friend.py +47 -0
- examples/tutorial-day1/in-steps/3-flow/main.py +9 -0
- examples/tutorial-day1/in-steps/4-variables/chest.py +37 -0
- examples/tutorial-day1/in-steps/4-variables/enemy.py +76 -0
- examples/tutorial-day1/in-steps/4-variables/friend.py +49 -0
- examples/tutorial-day1/in-steps/4-variables/main.py +9 -0
- examples/tutorial-day1/in-steps/5-backdrops/chest.py +37 -0
- examples/tutorial-day1/in-steps/5-backdrops/enemy.py +76 -0
- examples/tutorial-day1/in-steps/5-backdrops/friend.py +49 -0
- examples/tutorial-day1/in-steps/5-backdrops/main.py +26 -0
- examples/tutorial-day1/main.py +48 -0
- examples/tutorial-day1/target.py +14 -0
- examples/tutorial-day1/text.py +23 -0
- pyscratch/game_module.py +66 -25
- pyscratch/sprite.py +12 -4
- {pyscratch_pysc-1.0.4.dist-info → pyscratch_pysc-2.0.0.dist-info}/METADATA +5 -1
- pyscratch_pysc-2.0.0.dist-info/RECORD +239 -0
- pyscratch_pysc-1.0.4.dist-info/RECORD +0 -101
- {pyscratch_pysc-1.0.4.dist-info → pyscratch_pysc-2.0.0.dist-info}/WHEEL +0 -0
- {pyscratch_pysc-1.0.4.dist-info → pyscratch_pysc-2.0.0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,49 @@
|
|
1
|
+
import pyscratch as pysc
|
2
|
+
import pygame
|
3
|
+
game = pysc.game
|
4
|
+
|
5
|
+
friend = pysc.create_single_costume_sprite("assets/fish_orange_outline.png")
|
6
|
+
|
7
|
+
def movement():
|
8
|
+
"""
|
9
|
+
when_game_start:
|
10
|
+
the movement of the friend: always moving away from the curosr
|
11
|
+
"""
|
12
|
+
centre = game.screen_width/2, game.screen_height/2
|
13
|
+
while True:
|
14
|
+
yield 1/game.framerate
|
15
|
+
mouse_x, mouse_y = pysc.get_mouse_pos()
|
16
|
+
if friend.distance_to((mouse_x, mouse_y)) < 200:
|
17
|
+
friend.point_towards_mouse()
|
18
|
+
friend.direction += 180
|
19
|
+
friend.move_indir(2)
|
20
|
+
|
21
|
+
else:
|
22
|
+
friend.point_towards(centre)
|
23
|
+
friend.move_indir(2)
|
24
|
+
|
25
|
+
friend.when_game_start().add_handler(movement)
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
def check_if_centred():
|
30
|
+
"""
|
31
|
+
when_game_start:
|
32
|
+
move away when near the centre
|
33
|
+
"""
|
34
|
+
centre = game.screen_width/2, game.screen_height/2
|
35
|
+
while True:
|
36
|
+
yield 1/game.framerate
|
37
|
+
if friend.distance_to(centre) < 50:
|
38
|
+
game['score'] += 10
|
39
|
+
|
40
|
+
new_x = pysc.random_number(0, game.screen_width)
|
41
|
+
new_y = pysc.random_number(0, game.screen_height)
|
42
|
+
|
43
|
+
friend.x = new_x
|
44
|
+
friend.y = new_y
|
45
|
+
|
46
|
+
friend.when_game_start().add_handler(check_if_centred)
|
47
|
+
|
48
|
+
|
49
|
+
|
@@ -0,0 +1,26 @@
|
|
1
|
+
import pyscratch as pysc
|
2
|
+
from pyscratch import game
|
3
|
+
import chest, enemy, friend
|
4
|
+
|
5
|
+
# background
|
6
|
+
bg0 = pysc.load_image("assets/undersea_bg.png")
|
7
|
+
bg0 = pysc.scale_to_fit_aspect(bg0, (1024, 576))
|
8
|
+
game.add_backdrop('bg0', bg0)
|
9
|
+
|
10
|
+
bg1 = pysc.load_image("assets/Cat In Space Wallpaper Hq.jpg")
|
11
|
+
bg1 = pysc.scale_to_fit_aspect(bg1, (1024, 576))
|
12
|
+
game.add_backdrop('bg1', bg1)
|
13
|
+
|
14
|
+
|
15
|
+
|
16
|
+
def on_game_start():
|
17
|
+
game.switch_backdrop('bg0')
|
18
|
+
|
19
|
+
game.when_game_start().add_handler(on_game_start)
|
20
|
+
|
21
|
+
|
22
|
+
# starting the game
|
23
|
+
game.update_screen_mode((1024, 576))
|
24
|
+
game.start(show_mouse_position=True)
|
25
|
+
|
26
|
+
|
@@ -0,0 +1,48 @@
|
|
1
|
+
import pyscratch as pysc
|
2
|
+
import pygame
|
3
|
+
import target, enemy, chest, friend#, text
|
4
|
+
|
5
|
+
game = pysc.game
|
6
|
+
|
7
|
+
# mouse
|
8
|
+
pygame.mouse.set_visible(False)
|
9
|
+
|
10
|
+
|
11
|
+
# background
|
12
|
+
bg0 = pysc.load_image("assets/undersea_bg.png")
|
13
|
+
bg0 = pysc.scale_to_fit_aspect(bg0, (1024, 576))
|
14
|
+
game.add_backdrop('bg0', bg0)
|
15
|
+
|
16
|
+
bg1 = pysc.load_image("assets/Cat In Space Wallpaper Hq.jpg")
|
17
|
+
bg1 = pysc.scale_to_fit_aspect(bg1, (1024, 576))
|
18
|
+
game.add_backdrop('bg1', bg1)
|
19
|
+
|
20
|
+
game.switch_backdrop('bg0')
|
21
|
+
|
22
|
+
# sound
|
23
|
+
game.load_sound("hit", "assets/impactMetal_light_004.ogg")
|
24
|
+
game.load_sound("background", "assets/Circus-Theme-Entry-of-the-Gladiators-Ragtime-Version(chosic.com).mp3")
|
25
|
+
|
26
|
+
def play_loop():
|
27
|
+
while True:
|
28
|
+
game.play_sound("background", volume=0)
|
29
|
+
yield 2*60+33
|
30
|
+
game.when_game_start().add_handler(play_loop)
|
31
|
+
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
def background():
|
36
|
+
while True:
|
37
|
+
yield 1/game.framerate
|
38
|
+
if game['score'] < 0:
|
39
|
+
game.switch_backdrop('bg1')
|
40
|
+
|
41
|
+
|
42
|
+
game.when_game_start().add_handler(background)
|
43
|
+
|
44
|
+
|
45
|
+
game.update_screen_mode((1024, 576))
|
46
|
+
game.start(show_mouse_position=True)
|
47
|
+
|
48
|
+
|
@@ -0,0 +1,14 @@
|
|
1
|
+
import pyscratch as pysc
|
2
|
+
game = pysc.game
|
3
|
+
target = pysc.create_single_costume_sprite("assets/target_b.png")
|
4
|
+
|
5
|
+
# 3. Flow control
|
6
|
+
def follow_mouse():
|
7
|
+
game.bring_to_front(target)
|
8
|
+
while True:
|
9
|
+
yield 1/game.framerate
|
10
|
+
mouse_x, mouse_y = pysc.get_mouse_pos()
|
11
|
+
target.x = mouse_x
|
12
|
+
target.y = mouse_y
|
13
|
+
|
14
|
+
target.when_game_start().add_handler(follow_mouse)
|
@@ -0,0 +1,23 @@
|
|
1
|
+
import pyscratch as pysc
|
2
|
+
import pygame
|
3
|
+
game = pysc.game
|
4
|
+
|
5
|
+
text_box = pysc.create_rect_sprite((200, 200, 200, 20), 700, 90)
|
6
|
+
font = pygame.font.Font('assets/Kenney Future.ttf', size=96)
|
7
|
+
|
8
|
+
text_box.write_text("YOU LOSE", font, offset=(350, 45))
|
9
|
+
|
10
|
+
|
11
|
+
text_box.hide()
|
12
|
+
|
13
|
+
text_box.set_draggable(True)
|
14
|
+
text_box.set_xy((260, 195))
|
15
|
+
text_box.direction = -35
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
def game_end():
|
20
|
+
text_box.show()
|
21
|
+
|
22
|
+
text_box.when_backdrop_switched(1).add_handler(game_end)
|
23
|
+
|
pyscratch/game_module.py
CHANGED
@@ -12,6 +12,7 @@ from pathlib import Path
|
|
12
12
|
import threading
|
13
13
|
import time
|
14
14
|
import json, inspect
|
15
|
+
from typing_extensions import deprecated
|
15
16
|
|
16
17
|
import numpy as np
|
17
18
|
import pygame
|
@@ -309,16 +310,23 @@ class Game:
|
|
309
310
|
self._mouse_drag_trigger = self.create_pygame_event([pygame.MOUSEBUTTONDOWN, pygame.MOUSEBUTTONUP, pygame.MOUSEMOTION])
|
310
311
|
self._mouse_drag_trigger.add_handler(self.__mouse_drag_handler)
|
311
312
|
|
312
|
-
|
313
|
-
self.
|
314
|
-
|
313
|
+
# Backdrops
|
314
|
+
self.backdrops_by_key: Dict[str, pygame.Surface] = {}
|
315
|
+
self.backdrop_indices_by_key: Dict[str, int] = {}
|
316
|
+
self.backdrop_keys: List[str] = []
|
317
|
+
|
318
|
+
# TODO: testing needed
|
315
319
|
|
316
320
|
self.__screen_width: int = 0
|
317
321
|
self.__screen_height: int = 0
|
318
322
|
self.__framerate: float = 0
|
319
323
|
|
320
|
-
|
324
|
+
# TODO index and key need to be changed together
|
325
|
+
# the index is remained for the next_backdrop function
|
321
326
|
self.__backdrop_index = None
|
327
|
+
self.__backdrop_key = None
|
328
|
+
|
329
|
+
# TODO: the event is now triggered by the name not the index
|
322
330
|
self._backdrop_change_triggers: List[Event] = []
|
323
331
|
|
324
332
|
self._top_edge: Sprite
|
@@ -627,8 +635,8 @@ class Game:
|
|
627
635
|
# Drawing
|
628
636
|
|
629
637
|
#self._screen.fill((30, 30, 30))
|
630
|
-
if not (self.
|
631
|
-
self._screen.blit(self.
|
638
|
+
if not (self.__backdrop_key is None):
|
639
|
+
self._screen.blit(self.backdrops_by_key[self.__backdrop_key], (0, 0))
|
632
640
|
else:
|
633
641
|
self._screen.fill((255,255,255))
|
634
642
|
helper._draw_guide_lines(self._screen, guide_lines_font, 100, 500)
|
@@ -745,7 +753,7 @@ class Game:
|
|
745
753
|
# if to_show:
|
746
754
|
# sprite.show()
|
747
755
|
#self._all_sprites_to_show.add(sprite)
|
748
|
-
sprite.update()
|
756
|
+
#sprite.update()
|
749
757
|
|
750
758
|
if self.__started_interactive:
|
751
759
|
sprite.set_draggable(True)
|
@@ -831,6 +839,7 @@ class Game:
|
|
831
839
|
"""
|
832
840
|
self._all_sprites_to_show.get_layer_of_sprite(sprite)
|
833
841
|
|
842
|
+
@deprecated("use add_backdrop")
|
834
843
|
def set_backdrops(self, images: List[pygame.Surface]):
|
835
844
|
"""
|
836
845
|
Set the list of all available backdrops. This function is meant to be run before the game start.
|
@@ -849,32 +858,63 @@ class Game:
|
|
849
858
|
pysc.game.switch_backdrop(1)
|
850
859
|
```
|
851
860
|
"""
|
852
|
-
|
853
|
-
|
861
|
+
for idx, img in enumerate(images):
|
862
|
+
self.add_backdrop(str(idx), img)
|
863
|
+
|
864
|
+
def add_backdrop(self, key, image: pygame.Surface):
|
865
|
+
"""
|
866
|
+
Add the image as a backdrop, and index it with the key so it can be switched to using switch_backdrop
|
867
|
+
|
868
|
+
Example:
|
869
|
+
```python
|
870
|
+
bg0 = pysc.load_image("assets/undersea_bg.png")'
|
871
|
+
|
872
|
+
game.add_backdrop('background0', bg0)
|
873
|
+
game.switch_backdrop('background0')
|
874
|
+
```
|
875
|
+
|
876
|
+
"""
|
877
|
+
assert not key in self.backdrops_by_key, f"the name '{key}' is already in used"
|
878
|
+
self.backdrop_keys.append(key)
|
879
|
+
self.backdrops_by_key[key] = image
|
880
|
+
self.backdrop_indices_by_key[key] = len(self.backdrop_keys) - 1 # minus one since the new one is added in the previous line
|
881
|
+
|
882
|
+
|
854
883
|
@property
|
855
884
|
def backdrop_index(self):
|
856
885
|
"""
|
857
|
-
The index of the current backdrops.
|
886
|
+
The index of the current backdrops.
|
858
887
|
"""
|
859
888
|
return self.__backdrop_index
|
889
|
+
|
860
890
|
|
861
|
-
|
891
|
+
@property
|
892
|
+
def backdrop_key(self):
|
893
|
+
"""
|
894
|
+
The key (i.e. name) of the current backdrops.
|
895
|
+
"""
|
896
|
+
return self.__backdrop_key
|
897
|
+
|
898
|
+
def switch_backdrop(self, key:Optional[str]=None):
|
862
899
|
"""
|
863
900
|
Change the backdrop by specifying the index of the backdrop.
|
864
901
|
"""
|
865
902
|
|
866
|
-
if
|
867
|
-
self.
|
903
|
+
if key != self.__backdrop_key:
|
904
|
+
self.__backdrop_key = key
|
905
|
+
self.__backdrop_index = None if key is None else self.backdrop_indices_by_key[key]
|
868
906
|
for t in self._backdrop_change_triggers:
|
869
|
-
t.trigger(
|
870
|
-
self._specific_backdrop_event_emitter.on_event(
|
907
|
+
t.trigger(key)
|
908
|
+
self._specific_backdrop_event_emitter.on_event(key)
|
871
909
|
|
910
|
+
# TODO: testing needed
|
872
911
|
def next_backdrop(self):
|
873
912
|
"""
|
874
913
|
Switch to the next backdrop.
|
875
914
|
"""
|
876
915
|
if not self.__backdrop_index is None:
|
877
|
-
|
916
|
+
idx = (self.__backdrop_index+1) % len(self.backdrops_by_key)
|
917
|
+
self.switch_backdrop(self.backdrop_keys[idx])
|
878
918
|
|
879
919
|
# all events
|
880
920
|
|
@@ -1026,19 +1066,19 @@ class Game:
|
|
1026
1066
|
t = _declare_callback_type(t, sample_callback)
|
1027
1067
|
return t
|
1028
1068
|
|
1029
|
-
|
1030
|
-
def when_backdrop_switched(self,
|
1069
|
+
# TODO: testing needed
|
1070
|
+
def when_backdrop_switched(self, backdrop_key: str, associated_sprites : Iterable[Sprite]=[]) -> Event[[]]:
|
1031
1071
|
"""
|
1032
1072
|
It is recommended to use the `Sprite.when_backdrop_switched` alias instead of this method,
|
1033
1073
|
so you don't need to specify the `associated_sprites` in every event.
|
1034
1074
|
|
1035
|
-
Returns an `Event` that is triggered when the game is switched to a backdrop
|
1075
|
+
Returns an `Event` that is triggered when the game is switched to a backdrop with the key `backdrop_key`.
|
1036
1076
|
|
1037
1077
|
The event handler does not take in any parameter.
|
1038
1078
|
|
1039
1079
|
Parameters
|
1040
1080
|
---
|
1041
|
-
|
1081
|
+
backdrop_key: str
|
1042
1082
|
The index of the backdrop
|
1043
1083
|
|
1044
1084
|
associated_sprites: List[Sprite]
|
@@ -1053,10 +1093,11 @@ class Game:
|
|
1053
1093
|
return
|
1054
1094
|
t = _declare_callback_type(t, sample_callback)
|
1055
1095
|
|
1056
|
-
self._specific_backdrop_event_emitter.add_event(
|
1096
|
+
self._specific_backdrop_event_emitter.add_event(backdrop_key, t)
|
1057
1097
|
return t
|
1058
|
-
|
1059
|
-
|
1098
|
+
|
1099
|
+
# TODO: testing needed
|
1100
|
+
def when_any_backdrop_switched(self, associated_sprites : Iterable[Sprite]=[]) -> Event[[Union[str,None]]]:
|
1060
1101
|
"""
|
1061
1102
|
It is recommended to use the `Sprite.when_any_backdrop_switched` alias instead of this method,
|
1062
1103
|
so you don't need to specify the `associated_sprites` in every event.
|
@@ -1064,7 +1105,7 @@ class Game:
|
|
1064
1105
|
Returns an `Event` that is triggered when the backdrop is switched.
|
1065
1106
|
|
1066
1107
|
The event handler have to take one parameter:
|
1067
|
-
- **
|
1108
|
+
- **str** (str): The key of the new backdrop
|
1068
1109
|
|
1069
1110
|
Parameters
|
1070
1111
|
---
|
@@ -1075,7 +1116,7 @@ class Game:
|
|
1075
1116
|
t = self._create_event(associated_sprites)
|
1076
1117
|
self._backdrop_change_triggers.append(t)
|
1077
1118
|
if TYPE_CHECKING:
|
1078
|
-
def sample_callback(
|
1119
|
+
def sample_callback(key: Union[str, None])-> Any:
|
1079
1120
|
return
|
1080
1121
|
t = _declare_callback_type(t, sample_callback)
|
1081
1122
|
|
pyscratch/sprite.py
CHANGED
@@ -165,7 +165,7 @@ def create_single_costume_sprite(image_path, *args, **kwargs):
|
|
165
165
|
return Sprite(frame_dict, "always", *args, **kwargs)
|
166
166
|
|
167
167
|
|
168
|
-
def create_shared_data_display_sprite(key, font, size = (150, 50), bg_colour=(127, 127, 127), text_colour=(255,255,255), position: Optional[Tuple[float, float]]=None, update_period=0.1, **kwargs):
|
168
|
+
def create_shared_data_display_sprite(key, font:Optional[pygame.font.Font]=None, size = (150, 50), bg_colour=(127, 127, 127), text_colour=(255,255,255), position: Optional[Tuple[float, float]]=None, update_period=0.1, **kwargs):
|
169
169
|
"""
|
170
170
|
Create a display for a variable inside shared_data given the dictionary key (i.e. the name of the variable).
|
171
171
|
The variable display will update every `update_period` seconds.
|
@@ -191,8 +191,9 @@ def create_shared_data_display_sprite(key, font, size = (150, 50), bg_colour=(12
|
|
191
191
|
---
|
192
192
|
key: str
|
193
193
|
The dictionary key of the variable in `game.shared_data` that you want to display.
|
194
|
-
font: pygame.font.Font
|
194
|
+
font: None or pygame.font.Font
|
195
195
|
The pygame font object. Refer to the website of pygame for more details.
|
196
|
+
If None, a default font will be used.
|
196
197
|
size: Tuple[float, float]
|
197
198
|
The size of the display panel
|
198
199
|
bg_colour: Tuple[int, int, int] or Tuple[int, int, int, int]
|
@@ -207,6 +208,9 @@ def create_shared_data_display_sprite(key, font, size = (150, 50), bg_colour=(12
|
|
207
208
|
Whatever the `Sprite` constructor takes, except `frame_dict`,`starting_animation` & `position`
|
208
209
|
"""
|
209
210
|
|
211
|
+
if font is None:
|
212
|
+
font = pygame.font.Font(None, 36)
|
213
|
+
|
210
214
|
w, h = size
|
211
215
|
if position is None:
|
212
216
|
position = w/2+25, h/2+25
|
@@ -716,10 +720,13 @@ class Sprite(pygame.sprite.Sprite):
|
|
716
720
|
|
717
721
|
self._intend_to_show = True
|
718
722
|
self._shown = False
|
719
|
-
self.
|
723
|
+
self.hide()
|
720
724
|
|
721
725
|
count = game._add_sprite(self, caller_file=caller_file)
|
722
726
|
|
727
|
+
self.update()
|
728
|
+
self.show()
|
729
|
+
|
723
730
|
|
724
731
|
self.identifier: str
|
725
732
|
"""
|
@@ -1452,7 +1459,8 @@ class Sprite(pygame.sprite.Sprite):
|
|
1452
1459
|
my_sprite.remove()
|
1453
1460
|
```
|
1454
1461
|
"""
|
1455
|
-
|
1462
|
+
self.hide() # prevent collision
|
1463
|
+
game._remove_sprite(self)
|
1456
1464
|
self.__removed = True
|
1457
1465
|
|
1458
1466
|
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.4
|
2
2
|
Name: pyscratch-pysc
|
3
|
-
Version:
|
3
|
+
Version: 2.0.0
|
4
4
|
Summary: A Python game development framework designed to provide an easy transitioning from Scratch to Python
|
5
5
|
Author-email: Daniel Ka-Wa Chan <kwdaniel525@protonmail.com>
|
6
6
|
License: MIT
|
@@ -23,6 +23,10 @@ For more information, see: https://kwdchan.github.io/pyscratch/
|
|
23
23
|
|
24
24
|
|
25
25
|
## Change History
|
26
|
+
### 12 Oct 2025
|
27
|
+
**v2.0.0**
|
28
|
+
The backdrops are now indexed by keys instead of integers
|
29
|
+
|
26
30
|
|
27
31
|
### 12 Sep 2025
|
28
32
|
**v1.0.4**
|