self.hide() @when_I_receive('Update_Score') def show_correct_digit(self): self.switch_costume('digit-%d' % GlobalVariables.score_1) self.show() class Score_2(Sprite): Costumes = score_costumes @when_green_flag_clicked def set_position_and_size(self): self.go_to_xy(220, 162) self.hide() @when_I_receive('Update_Score') def show_correct_digit(self): self.switch_costume('digit-%d' % GlobalVariables.score_2) self.show() project = Project() project.register_stage_class(PongStage) project.register_sprite_class(Player_1) project.register_sprite_class(Player_2) project.register_sprite_class(Score_1) project.register_sprite_class(Score_2) project.register_sprite_class(Ball) project.go_live()
@when_I_receive('clone-self') def clone_self(self): pytch.create_clone_of(self) class Broom(Sprite): def __init__(self): Sprite.__init__(self) self.copied_id = 1 @when_I_start_as_a_clone def update_id(self): self.copied_id += 1 if self.copied_id < 5: pytch.create_clone_of(self) @when_I_receive('clone-self') def clone_self(self): pytch.create_clone_of(self) @when_I_receive('destroy-broom-clones') def self_destruct(self): self.delete_this_clone() # --cut-here-for-auto-config-- project = Project() project.register_sprite_class(Alien) project.register_sprite_class(Broom)
# It's a bit clunky to hard-code the location here, but we pass the # code in as if it were <stdin>, so there's no real way to get the # true path in this code. import sys sys.path.insert(0, "py/project") import increment_list_elements class Counter(Sprite): def __init__(self): Sprite.__init__(self) self.xs = [0] * 5 @when_I_receive("go-while") def modify_list_with_while(self): increment_list_elements.increment_every_elt_with_while(self.xs) for i in range(5): self.xs[i] += 1 @when_I_receive("go-for") def modify_list_with_for(self): increment_list_elements.increment_every_elt_with_for(self.xs) for i in range(5): self.xs[i] += 1 # --cut-here-for-auto-config-- project = Project() project.register_sprite_class(Counter)
import pytch from pytch import ( Sprite, Stage, Project, ) class Ball(Sprite): Costumes = [('yellow-ball', 'ball.png', 8, 8)] class Table(Stage): Backdrops = [('wooden', 'wooden-stage.png'), ('white', 'solid-white-stage.png')] project = Project() project.register_sprite_class(Ball) project.register_stage_class(Table)
import pytch from pytch import ( Sprite, Project, when_I_receive, ) class InvisibleThing(Sprite): Costumes = [] @when_I_receive('show-yourself') def try_to_show(self): self.show() project = Project() project.register_sprite_class(InvisibleThing)
class Sender(Sprite): def __init__(self): Sprite.__init__(self) self.n_steps = 0 @when_green_flag_clicked def send_message(self): self.n_steps += 1 pytch.broadcast_and_wait('something-happened') self.n_steps += 1 class Receiver(Sprite): def __init__(self): Sprite.__init__(self) self.n_events = 0 @when_I_receive('something-happened') def note_event(self): self.n_events += 1 pytch.wait_seconds(0) self.n_events += 1 # --cut-here-for-auto-config-- project = Project() project.register_sprite_class(Sender) project.register_sprite_class(Receiver)
class SetVis: def set_visibility(self, visible): if visible: self.show() else: self.hide() class Square(Sprite, SetVis): Costumes = [('square', 'square-80x80.png', 20, 30)] def __init__(self): Sprite.__init__(self) self.go_to_xy(-50, -90) class Rectangle(Sprite, SetVis): Costumes = [('rectangle', 'rectangle-60x30.png', 50, 10)] def __init__(self): Sprite.__init__(self) self.go_to_xy(10, -90) # --cut-here-for-auto-config-- project = Project() project.register_sprite_class(Square) project.register_sprite_class(Rectangle)
from pytch import ( Stage, Sprite, Project, when_this_sprite_clicked, when_stage_clicked, ) class Banana(Sprite): Costumes = [('yellow', 'yellow-banana.png', 50, 30)] @when_this_sprite_clicked def say_hello_banana(self): print('hello from Banana') class Table(Stage): Backdrops = [('wooden', 'wooden-stage.png')] @when_stage_clicked def say_hello_table(self): print('hello from Table') # --cut-here-for-auto-config-- project = Project() project.register_sprite_class(Banana) project.register_stage_class(Table)
when_green_flag_clicked, when_I_receive, when_key_pressed, ) class FlagClickCounter(Sprite): def __init__(self): Sprite.__init__(self) self.n_clicks = 0 @when_green_flag_clicked def note_click(self): self.n_clicks += 1 pytch.wait_seconds(0) self.n_clicks += 1 @when_I_receive('reset') def reset_n_clicks(self): self.n_clicks = 0 @when_key_pressed('x') def forget_a_click(self): self.n_clicks -= 1 # --cut-here-for-auto-config-- project = Project() project.register_sprite_class(FlagClickCounter)
class T1(Sprite): def __init__(self): Sprite.__init__(self) self.counter = 0 @when_green_flag_clicked def start_counting(self): self.counter = 0 while True: self.counter += 1 class T2(Sprite): def __init__(self): Sprite.__init__(self) self.counter = 0 @when_green_flag_clicked def start_counting(self): self.counter = 0 while True: self.counter += 1 # --cut-here-for-auto-config-- project = Project() project.register_sprite_class(T1) project.register_sprite_class(T2)
@when_I_receive('play-trumpet') def play_trumpet(self): self.played_trumpet = 'no' self.start_sound('trumpet') self.played_trumpet = 'yes' @when_I_receive('play-violin') def play_violin(self): self.played_violin = 'no' self.play_sound_until_done('violin') self.played_violin = 'yes' @when_I_receive('play-both') def play_both(self): self.played_both = 'no' self.start_sound('trumpet') self.played_both = 'nearly' self.play_sound_until_done('violin') self.played_both = 'yes' @when_I_receive('silence') def silence(self): pytch.stop_all_sounds() # --cut-here-for-auto-config-- project = Project() project.register_sprite_class(Orchestra)