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()
Costumes = [('yellow-ball', 'ball.png', 8, 8)] def __init__(self): Sprite.__init__(self) self.keys_pressed = '' self.go_to_xy(100, 50) @when_green_flag_clicked def move(self): self.change_x(50) pytch.wait_seconds(0.5) self.change_x(60) @when_key_pressed('w') def move_up(self): self.change_y(10) @when_key_pressed('s') def move_down_lots(self): self.change_y(-100) @when_I_receive('check-keys') def check_keys(self): self.keys_pressed = ''.join(kn for kn in 'abc' if key_is_pressed(kn)) # --cut-here-for-auto-config-- project = Project() project.register_sprite_class(Ball)
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)
# 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 ( Project, ) project = Project() project.go_live()
class Scanner(Sprite): @when_green_flag_clicked def init(self): # Store the IDs rather than the actual Alien objects, because # Sk.ffi.remapToJs() does not know how to handle Alien instances. self.got_ids = 0 @when_I_receive('get-original') def get_original(self): self.got_ids = Alien.the_original().id @when_I_receive('get-clones') def get_clones(self): self.got_ids = sorted([a.id for a in Alien.all_clones()]) @when_I_receive('get-instances') def get_instances(self): self.got_ids = sorted([a.id for a in Alien.all_instances()]) @when_I_receive('get-stage') def get_stage(self): self.got_ids = WhiteSheet.the_only().id # --cut-here-for-auto-config-- project = Project() project.register_stage_class(WhiteSheet) project.register_sprite_class(Alien) project.register_sprite_class(Scanner)
class Galaxies(Stage): def __init__(self): Stage.__init__(self) self.generated_id = 42 class Alien(Sprite): def __init__(self): Sprite.__init__(self) self.generated_id = next_global_id() @when_I_start_as_a_clone def get_fresh_id(self): self.generated_id = next_global_id() @when_I_receive('clone-self') def clone_self(self): pytch.create_clone_of(self) @when_I_receive('hide-if-lt-102') def hide_if_lt_102(self): if self.generated_id < 102: self.hide() # --cut-here-for-auto-config-- project = Project() project.register_sprite_class(Alien) project.register_stage_class(Galaxies)
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)
import pytch from pytch import ( Stage, Project, ) class InvisibleStage(Stage): Backdrops = [] project = Project() project.register_stage_class(InvisibleStage)
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)
import pytch from pytch import ( Project, Stage, ) class Scenery(Stage): def __init__(self): Stage.__init__(self) self.colour = 'red' # --cut-here-for-auto-config-- project = Project() project.register_stage_class(Scenery)
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)
import pytch from pytch import Project, Sprite, Stage, when_I_receive class Alien(Sprite): Costumes = [('marching', 'marching-alien.png', 25, 25)] @when_I_receive('switch-costume') def switch_to_bad_costume(self): self.switch_costume('angry') class Table(Stage): Backdrops = [('wooden', 'wooden-stage.png')] @when_I_receive('switch-backdrop') def switch_to_bad_backdrop(self): self.switch_backdrop('plastic') project = Project() project.register_sprite_class(Alien) project.register_stage_class(Table)
def switch_backdrop_by_number(self): self.switch_backdrop(1) self.print_current_backdrop() self.switch_backdrop(0) self.print_current_backdrop() self.switch_backdrop(1) self.print_current_backdrop() self.switch_backdrop(2) self.print_current_backdrop() self.switch_backdrop(1) self.print_current_backdrop() @pytch.when_I_receive("switch-to-wooden") def switch_to_wooden(self): self.switch_backdrop("wooden-stage") @pytch.when_I_receive("switch-to-sky") def switch_to_sky(self): self.switch_backdrop("sunny-sky") @pytch.when_I_receive("switch-to-white") def switch_to_white(self): self.switch_backdrop("solid-white-stage") # --cut-here-for-auto-config-- project = Project() project.register_sprite_class(Alien) project.register_stage_class(Background)
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)
@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)
@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)