<s> from __future__ import division , print_function , unicode_literals <EOL> import sys <EOL> import os <EOL> sys . path . insert ( <NUM_LIT:0> , os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT:..>' ) ) <EOL> testinfo = "<STR_LIT>" <EOL> tags = "<STR_LIT>" <EOL> autotest = <NUM_LIT:0> <EOL> import math <EOL> import pyglet <EOL> from pyglet . window import key <EOL> from pyglet . gl import * <EOL> pyglet . resource . path . append ( pyglet . resource . get_script_home ( ) ) <EOL> pyglet . resource . reindex ( ) <EOL> import cocos <EOL> from cocos import tiles , actions , layer <EOL> from cocos . director import director <EOL> import cocos . euclid as eu <EOL> from cocos . actions import Delay , CallFunc , ScaleTo <EOL> view_width = <NUM_LIT> <EOL> view_height = <NUM_LIT> <EOL> world_width = <NUM_LIT:1000> + <NUM_LIT:4> * <NUM_LIT> <EOL> world_height = <NUM_LIT:1000> <EOL> class ProbeQuad ( cocos . cocosnode . CocosNode ) : <EOL> def __init__ ( self , r , color4 ) : <EOL> super ( ProbeQuad , self ) . __init__ ( ) <EOL> self . color4 = color4 <EOL> self . vertexes = [ ( r , <NUM_LIT:0> , <NUM_LIT:0> ) , ( <NUM_LIT:0> , r , <NUM_LIT:0> ) , ( - r , <NUM_LIT:0> , <NUM_LIT:0> ) , ( <NUM_LIT:0> , - r , <NUM_LIT:0> ) ] <EOL> def draw ( self ) : <EOL> glPushMatrix ( ) <EOL> self . transform ( ) <EOL> glBegin ( GL_QUADS ) <EOL> glColor4ub ( * self . color4 ) <EOL> for v in self . vertexes : <EOL> glVertex3i ( * v ) <EOL> glEnd ( ) <EOL> glPopMatrix ( ) <EOL> class SquareLand ( cocos . layer . ScrollableLayer ) : <EOL> is_event_handler = True <EOL> def __init__ ( self , world_width , world_height ) : <EOL> self . world_width = world_width <EOL> self . world_height = world_height <EOL> super ( SquareLand , self ) . __init__ ( ) <EOL> self . px_width = world_width <EOL> self . px_height = world_height <EOL> bg = cocos . layer . ColorLayer ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0> , <NUM_LIT:255> , width = world_width , height = world_height ) <EOL> self . add ( bg , z = <NUM_LIT:0> ) <EOL> margin = int ( world_width * <NUM_LIT> ) <EOL> self . margin = margin <EOL> bg = cocos . layer . ColorLayer ( <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:255> , width = world_width - <NUM_LIT:2> * margin , height = world_height - <NUM_LIT:2> * margin ) <EOL> bg . position = ( margin , margin ) <EOL> self . add ( bg , z = <NUM_LIT:1> ) <EOL> mod = ( world_width - <NUM_LIT> * margin ) / <NUM_LIT> <EOL> y = margin + mod <EOL> self . marks_positions = [ ] <EOL> while y < world_height - mod : <EOL> x = margin + mod <EOL> while x < world_width - mod : <EOL> red = <NUM_LIT> + int ( <NUM_LIT> * x / world_width ) <EOL> blue = <NUM_LIT> + int ( <NUM_LIT> * y / world_height ) <EOL> actor = cocos . layer . ColorLayer ( red , <NUM_LIT:0> , blue , <NUM_LIT:255> , <EOL> width = <NUM_LIT:2> * int ( mod ) , height = <NUM_LIT:2> * int ( mod ) ) <EOL> actor . position = x , y <EOL> self . marks_positions . append ( ( x , y ) ) <EOL> self . add ( actor , z = <NUM_LIT:3> ) <EOL> x += <NUM_LIT:3> * mod <EOL> y += <NUM_LIT:3> * mod <EOL> self . marks_positions = self . marks_positions [ : <NUM_LIT:3> ] <EOL> self . player = cocos . sprite . Sprite ( '<STR_LIT>' ) <EOL> self . player . scale = <NUM_LIT> <EOL> self . player . model_width = self . player . width <EOL> self . player . model_height = self . player . height <EOL> self . player . position = ( mod / <NUM_LIT:2> + margin , mod / <NUM_LIT:2> + margin ) <EOL> self . player . fastness = <NUM_LIT:200> <EOL> self . add ( self . player , z = <NUM_LIT:4> ) <EOL> self . bindings = { <EOL> key . LEFT : '<STR_LIT:left>' , <EOL> key . RIGHT : '<STR_LIT:right>' , <EOL> key . UP : '<STR_LIT>' , <EOL> key . DOWN : '<STR_LIT>' , <EOL> key . PLUS : '<STR_LIT>' , <EOL> key . MINUS : '<STR_LIT>' <EOL> } <EOL> self . buttons = { <EOL> '<STR_LIT:left>' : <NUM_LIT:0> , <EOL> '<STR_LIT:right>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:0> <EOL> } <EOL> self . schedule ( self . step ) <EOL> def on_enter ( self ) : <EOL> super ( SquareLand , self ) . on_enter ( ) <EOL> self . scroller = self . get_ancestor ( cocos . layer . ScrollingManager ) <EOL> self . scene = self . get_ancestor ( cocos . scene . Scene ) <EOL> self . scene . f_refresh_marks = self . refresh_marks <EOL> def on_key_press ( self , k , m ) : <EOL> binds = self . bindings <EOL> if k in binds : <EOL> self . buttons [ binds [ k ] ] = <NUM_LIT:1> <EOL> return True <EOL> return False <EOL> def on_key_release ( self , k , m ) : <EOL> binds = self . bindings <EOL> if k in binds : <EOL> self . buttons [ binds [ k ] ] = <NUM_LIT:0> <EOL> return True <EOL> return False <EOL> def on_mouse_press ( self , x , y , button , modifiers ) : <EOL> print ( '<STR_LIT>' ) <EOL> vx , vy = self . scroller . pixel_from_screen ( x , y ) <EOL> print ( '<STR_LIT>' , vx , vy ) <EOL> def clamp ( self , actor , new_pos ) : <EOL> x , y = new_pos <EOL> if x - actor . model_width // <NUM_LIT:2> < self . margin : <EOL> x = self . margin + actor . model_width // <NUM_LIT:2> <EOL> elif x + actor . model_width // <NUM_LIT:2> > self . world_width - self . margin : <EOL> x = self . world_width - self . margin - actor . model_width // <NUM_LIT:2> <EOL> if y - actor . model_height // <NUM_LIT:2> < self . margin : <EOL> y = self . margin + actor . model_height // <NUM_LIT:2> <EOL> elif y + actor . model_height // <NUM_LIT:2> > self . world_height - self . margin : <EOL> y = self . world_height - self . margin - actor . model_height // <NUM_LIT:2> <EOL> return x , y <EOL> def step ( self , dt ) : <EOL> buttons = self . buttons <EOL> move_dir = eu . Vector2 ( buttons [ '<STR_LIT:right>' ] - buttons [ '<STR_LIT:left>' ] , <EOL> buttons [ '<STR_LIT>' ] - buttons [ '<STR_LIT>' ] ) <EOL> changed = False <EOL> if move_dir : <EOL> new_pos = self . player . position + self . player . fastness * dt * move_dir . normalize ( ) <EOL> new_pos = self . clamp ( self . player , new_pos ) <EOL> self . player . position = new_pos <EOL> changed = True <EOL> new_zoom = self . scroller . scale + ( buttons [ '<STR_LIT>' ] - buttons [ '<STR_LIT>' ] ) * <NUM_LIT> * dt <EOL> if new_zoom < <NUM_LIT> : <EOL> new_zoom = <NUM_LIT> <EOL> elif new_zoom > <NUM_LIT> : <EOL> new_zoom = <NUM_LIT> <EOL> if new_zoom != self . scroller . scale : <EOL> self . scroller . scale = new_zoom <EOL> changed = True <EOL> if changed : <EOL> self . update_after_change ( ) <EOL> def update_after_change ( self ) : <EOL> self . scroller . set_focus ( * self . player . position ) <EOL> self . refresh_marks ( ) <EOL> def refresh_marks ( self ) : <EOL> for mark , position in zip ( self . scene . marks , self . marks_positions ) : <EOL> screen_pos = self . scroller . pixel_to_screen ( * position ) <EOL> mark . position = screen_pos <EOL> def teleport_player ( self , x , y ) : <EOL> """<STR_LIT>""" <EOL> self . player . position = x , y <EOL> self . update_after_change ( ) <EOL> class TestScene ( cocos . scene . Scene ) : <EOL> def __init__ ( self ) : <EOL> super ( TestScene , self ) . __init__ ( ) <EOL> self . marks = [ ] <EOL> for i in range ( <NUM_LIT:1> , <NUM_LIT:4> ) : <EOL> mark = ProbeQuad ( <NUM_LIT:3> , ( <NUM_LIT:0> , <NUM_LIT:255> // i , <NUM_LIT:0> , <NUM_LIT:255> ) ) <EOL> mark . position = ( - <NUM_LIT:20> , - <NUM_LIT:20> ) <EOL> self . marks . append ( mark ) <EOL> self . add ( mark , z = <NUM_LIT:2> ) <EOL> def on_enter ( self ) : <EOL> director . push_handlers ( self . on_cocos_resize ) <EOL> super ( TestScene , self ) . on_enter ( ) <EOL> def on_cocos_resize ( self , usable_width , usable_height ) : <EOL> self . f_refresh_marks ( ) <EOL> def show_common_text ( ) : <EOL> print ( """<STR_LIT>""" ) <EOL> def show_mode_1_text ( ) : <EOL> print ( """<STR_LIT>""" ) <EOL> def show_mode_2_text ( ) : <EOL> print ( """<STR_LIT>""" ) <EOL> def main ( ) : <EOL> show_common_text ( ) <EOL> autoscale = True <EOL> if autoscale : <EOL> show_mode_1_text ( ) <EOL> else : <EOL> show_mode_2_text ( ) <EOL> director . init ( view_width , view_height , autoscale = autoscale ) <EOL> scene = TestScene ( ) <EOL> world_layer = SquareLand ( world_width , world_height ) <EOL> scroller = cocos . layer . ScrollingManager ( ) <EOL> if autotest : <EOL> def resize_scroller ( ) : <EOL> scroller . scale = <NUM_LIT> <EOL> w , h = world_width , world_height <EOL> template_action = ( <EOL> Delay ( <NUM_LIT> ) + <EOL> CallFunc ( world_layer . teleport_player , <NUM_LIT:0> , <NUM_LIT:0> ) + Delay ( <NUM_LIT:1> ) + <EOL> CallFunc ( world_layer . teleport_player , w // <NUM_LIT:2> , <NUM_LIT:0> ) + Delay ( <NUM_LIT:1> ) + <EOL> CallFunc ( world_layer . teleport_player , w // <NUM_LIT:2> , h ) + Delay ( <NUM_LIT:1> ) + <EOL> CallFunc ( world_layer . teleport_player , w , h ) + Delay ( <NUM_LIT:1> ) + <EOL> CallFunc ( resize_scroller ) + Delay ( <NUM_LIT:1> ) + <EOL> CallFunc ( director . window . set_size , <NUM_LIT> , <NUM_LIT> ) + CallFunc ( world_layer . update_after_change ) <EOL> ) <EOL> world_layer . do ( template_action ) <EOL> scroller . add ( world_layer ) <EOL> scene . add ( scroller ) <EOL> director . run ( scene ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
