def __init__( self ): super(ControlLayer, self).__init__() self.text_title = pyglet.text.Label("Transition Demos", font_size=32, x=5, y=director.get_window_size()[1], anchor_x=font.Text.LEFT, anchor_y=font.Text.TOP ) self.text_subtitle = pyglet.text.Label( transition_list[current_transition].__name__, font_size=18, multiline=True, width=600, x=5, y=director.get_window_size()[1] - 80, anchor_x=font.Text.LEFT, anchor_y=font.Text.TOP ) self.text_help = pyglet.text.Label("Press LEFT / RIGHT for prev/next test, ENTER to restart test", font_size=16, x=director.get_window_size()[0] /2, y=20, anchor_x=font.Text.CENTER, anchor_y=font.Text.CENTER)
def __init__(self): super(Zone, self).__init__() w, h = director.get_window_size() self.mode = model.INDUSTRY self.zone_images = { model.INDUSTRY: pyglet.resource.image('industry.png'), model.LOGISTICS: pyglet.resource.image('logistics.png'), model.MILITARY: pyglet.resource.image('military.png') } self.icons = { ('privileged', False): pyglet.resource.image('icon-priv_off.png'), ('privileged', True): pyglet.resource.image('icon-priv_on.png'), } w, h = director.get_window_size() self.active = Sprite(self.zone_images[model.INDUSTRY], anchor=((-(w-550), -(h-605))) ) self.add(self.active, z=-1) self.add(Button('but-ind_off.png', (self.zone1_x, self.y), 'industry', self.switch_zone_to), name=model.INDUSTRY) self.add(Button('but-log_off.png', (self.zone2_x, self.y), 'logistics', self.switch_zone_to), name=model.LOGISTICS) self.add(Button('but-mil_off.png', (self.zone3_x, self.y), 'military', self.switch_zone_to), name=model.MILITARY)
def __init__( self, title="Sprite Exmaple #", subtitle ="Goto()" ): super( FontLayer, self ).__init__() self.title = title self.subtitle = subtitle self.batch = pyglet.graphics.Batch() self.text_title = pyglet.text.Label(self.title, font_size=50, x=2, y=director.get_window_size()[1] - 20, anchor_x=font.Text.LEFT, anchor_y=font.Text.TOP, batch=self.batch) self.text_subtitle = pyglet.text.Label(self.subtitle, multiline=True, width=600, font_size=24, x=5, y=director.get_window_size()[1] - 120, anchor_x=font.Text.LEFT, anchor_y=font.Text.TOP, batch=self.batch )
def __init__( self, title="Sprite Exmaple #", subtitle ="Goto()" ): super( FontLayer, self ).__init__() self.title = title self.subtitle = subtitle self.batch = pyglet.graphics.Batch() self.text_title = pyglet.text.Label(self.title, font_size=32, x=5, y=director.get_window_size()[1], anchor_x=font.Text.LEFT, anchor_y=font.Text.TOP, batch=self.batch) self.text_subtitle = pyglet.text.Label(self.subtitle, multiline=True, width=600, font_size=16, x=5, y=director.get_window_size()[1] - 80, anchor_x=font.Text.LEFT, anchor_y=font.Text.TOP, batch=self.batch ) self.text_help = pyglet.text.Label("Press LEFT / RIGHT for prev/next test, ENTER to restart test", font_size=16, x=director.get_window_size()[0] /2, y=20, anchor_x=font.Text.CENTER, anchor_y=font.Text.CENTER, batch=self.batch )
def get_action( self, e, times=1 ): '''returns the actions for a specific effects''' w,h= director.get_window_size() center = (w/2.0, (ROWS * SQUARE_SIZE) / 2.0 ) # basic actions if e == Colors.ROTATE: a = RotateBy( 360 * times, duration=self.duration * times) elif e == Colors.SCALE: a = ScaleTo( 0.5, duration=self.duration/2.0) + ScaleTo(1, duration=self.duration/2.0) a *= times # Grid actions elif e == Colors.LIQUID: a = Liquid( grid=self.grid_size, waves=self.duration*times // 2, duration=self.duration * times) + StopGrid() elif e == Colors.WAVES: a = Waves( grid=self.grid_size, waves=self.duration*times // 2, duration=self.duration * times) + StopGrid() elif e == Colors.TWIRL: a = Twirl( grid=self.grid_size, center=center, twirls=self.duration*times // 2, duration=self.duration * times ) + StopGrid() elif e == Colors.LENS: w,h = director.get_window_size() a = Lens3D( radius=h//2, grid=self.grid_size, duration=self.duration * times) + StopGrid() else: raise Exception("Effect not implemented: %s" % str(e) ) return a
def on_key_press(self, k, m): if k == key.BACKSPACE: self.name.element.text = self.name.element.text[0:-1] return True elif k == key.ENTER: if len(self.name.element.text) <= 2: w, h = director.get_window_size() label_s = Label( 'Name too short! Choose at least 3 characters', font_name=_font_, font_size=20, anchor_y='top', anchor_x='center') label_s.position = (w/2., 700.) self.add(label_s) elif len(self.name.element.text) >= 15: w, h = director.get_window_size() label_l = Label( 'Name too long! Not more than 15 characters allowed', font_name=_font_, font_size=20, anchor_y='top', anchor_x='center') label_l.position = (w/2., 750.) self.add(label_l) else: new_score(self.name.element.text, self.wave) director.pop() return True elif k == key.ESCAPE: director.pop() return True return False
def __init__(self): super(TestLayer, self).__init__() skin, anim = getAnim('handInHand') x, y = director.get_window_size() self.skin = skin self.add(self.skin) x, y = director.get_window_size() self.skin.position = x / 2, y / 2 self.skin.do(cocos.actions.Repeat(skeleton.Animate(anim)))
def __init__(self): super( TestLayer, self ).__init__() x,y = director.get_window_size() self.skin = skeleton.BitmapSkin(sample_skeleton.skeleton, sample_skin.skin) self.add( self.skin ) x, y = director.get_window_size() self.skin.position = x//2, y//2
def __init__(self): super( TestLayer, self ).__init__() x,y = director.get_window_size() self.skin = skeleton.ColorSkin(sample_skeleton.skeleton, (255,0,0,255)) self.add( self.skin ) x, y = director.get_window_size() self.skin.position = x/2, y/2
def __init__(self): super(TestLayer, self).__init__() x, y = director.get_window_size() self.skin = skeleton.BitmapSkin(sample_skeleton.skeleton, sample_skin.skin) self.add(self.skin) x, y = director.get_window_size() self.skin.position = x / 2, y / 2 anim = cPickle.load(open("SAMPLE.anim")) self.skin.do(cocos.actions.Repeat(skeleton.Animate(anim)))
def __init__(self): super(MainMenuScene, self).__init__() background = Sprite('eclipse.jpg') background.position = (director.get_window_size()[0]/2, director.get_window_size()[1]/2) self.add(background) multiplexLayer = MultiplexLayer( MainMenu(), OptionsMenu(), AboutLayer()) multiplexLayer.position = (0, -100) self.add(multiplexLayer, 1) director.run(self)
def __init__(self): super( TestLayer, self ).__init__() x,y = director.get_window_size() # create a ColorSkin for our skeleton self.skin = skeleton.BitmapSkin(root_bone.skeleton, root_skin.skin) # add the skin to the scene self.add( self.skin ) x, y = director.get_window_size() self.skin.position = x/2, y/2
def __init__(self): super( TestLayer, self ).__init__() x,y = director.get_window_size() # create a ColorSkin for our skeleton sk_file = imp.load_source("user_skeleton", sys.argv[1]) self.skin = cocos.skeleton.ColorSkin(sk_file.skeleton, (255,0,0,255)) # add the skin to the scene self.add( self.skin ) x, y = director.get_window_size() self.skin.position = x // 2, y // 2
def __init__( self, index=1 ): super(SpriteLayer, self ).__init__() self.index = index self.image = pyglet.resource.image('grossini.png') self.sprite1 = Sprite(self.image, blend_mode=Sprite.BLEND_STANDARD) self.sprite1.x = director.get_window_size()[0] / 2 - 100 self.sprite1.y = director.get_window_size()[0] / 2 self.add( self.sprite1 ) self.sprite2 = Sprite(self.image, blend_mode=Sprite.BLEND_ADDITIVE) self.sprite2.x = director.get_window_size()[0] / 2 + 100 self.sprite2.y = director.get_window_size()[0] / 2 self.add( self.sprite2 ) self.sprite3 = Sprite(self.image, blend_mode=Sprite.BLEND_STANDARD, filter_mode=Sprite.FILTER_NONE) self.sprite3.x = director.get_window_size()[0] / 2 - 100 self.sprite3.y = director.get_window_size()[0] / 2 - 150 self.add( self.sprite3 ) self.sprite4 = Sprite(self.image, blend_mode=Sprite.BLEND_ADDITIVE, filter_mode=Sprite.FILTER_NONE) self.sprite4.x = director.get_window_size()[0] / 2 + 100 self.sprite4.y = director.get_window_size()[0] / 2 - 150 self.add( self.sprite4 )
def init(self, center=(-1,-1), radius=240, waves=15, amplitude=60, *args, **kw): ''' :Parameters: `center` : (int,int) Center of the ripple. Default: (win_size_width /2, win_size_height /2 ) `radius` : int Radius of the ripple. Default: 240 `waves` : int Number of waves (2 * pi) that the action will perform. Default: 15 `amplitude` : int Wave amplitude (height). Default is 60 ''' super(Ripple3D,self).init( *args, **kw) x,y = director.get_window_size() if center==(-1,-1): center=(x//2, y//2) #: Center of the ripple. Type: (int,int). #: This value can be modified by other actions, like `JumpBy` to simulate a jumping ripple effect self.position = Point2( center[0]+1, center[1]+1 ) #: radius of the ripple. Type: float self.radius = radius #: number of waves. Type: int self.waves = waves #: amplitude rate. Default: 1.0. #: This value is modified by other actions like `AccelAmplitude`. self.amplitude_rate = 1.0 self.amplitude=amplitude
def betterVerticalLayout(menu): width, height = director.get_window_size() fo = pyglet.font.load(menu.font_item['font_name'], menu.font_item['font_size']) fo_height = int((fo.ascent - fo.descent) * 0.9) + padding if menu.menu_halign == CENTER: pos_x = width // 2 elif menu.menu_halign == RIGHT: pos_x = width - menu.menu_hmargin elif menu.menu_halign == LEFT: pos_x = menu.menu_hmargin else: raise Exception("Invalid anchor_x value for menu") for idx,i in enumerate( menu.children): item = i[1] if menu.menu_valign == CENTER: pos_y = (height + (len(menu.children) - 2 * idx) * fo_height - menu.title_height) * 0.5 elif menu.menu_valign == TOP: pos_y = (height - ((idx + 0.8) * fo_height ) - menu.title_height - menu.menu_vmargin) elif menu.menu_valign == BOTTOM: pos_y = (0 + fo_height * (len(menu.children) - idx) + menu.menu_vmargin) item.transform_anchor = (pos_x, pos_y) item.generateWidgets (pos_x, pos_y, menu.font_item, menu.font_item_selected)
def __init__(self): super(TestLayer, self).__init__() x, y = director.get_window_size() # rotation and childs, with default transform_anchor s self.sprite_a1 = Sprite("grossini.png", (x // 4, int(y * 0.66))) self.sprite_a2 = Sprite("grossini.png", (0, 0), rotation=30) self.sprite_a3 = Sprite("grossini.png", (0, 0), rotation=30) self.sprite_a1.add(self.sprite_a2) self.sprite_a2.add(self.sprite_a3) self.add(self.sprite_a1) # position and childs, with default transform_anchor s self.sprite_b1 = Sprite("grossinis_sister1.png", (x // 2, int(y * 0.66))) self.sprite_b2 = Sprite("grossinis_sister1.png", (100, 0)) self.sprite_b3 = Sprite("grossinis_sister1.png", (100, 0)) self.sprite_b1.add(self.sprite_b2) self.sprite_b2.add(self.sprite_b3) self.add(self.sprite_b1) # combo, with default transform_anchor s self.sprite_c1 = Sprite("grossinis_sister2.png", (int(x * 0.33), int(y * 0.33))) self.sprite_c2 = Sprite("grossinis_sister2.png", (100, 0), rotation=30) self.sprite_c3 = Sprite("grossinis_sister2.png", (100, 0), rotation=30) self.sprite_c1.add(self.sprite_c2) self.sprite_c2.add(self.sprite_c3) self.add(self.sprite_c1)
def __init__(self, name, life, sprite, arme, CollisionManagerPlayer, CollisionManagerEnnemi, shield, bomb): super(Vaisseau,self).__init__() width, height = director.get_window_size() self.sprite = sprite self.sprite.scale = 2 self.sprite.position = (width//2, height//2) self.sprite.cshape = cm.AARectShape( self.sprite.position, self.sprite.width//2, self.sprite.height//2 ) CollisionManagerPlayer.add(self.sprite) CollisionManagerEnnemi.add(self.sprite) self.CollisionManagerEnnemi = CollisionManagerEnnemi self.name = name self.life = life self.add(self.sprite) self.arme = arme self.missileSprites = [] self.schedule(self.update) self.shieldClass = shield self.shieldClass.shield.position = self.sprite.position self.bombClass = bomb
def init(self, grid): """Initializes the grid creating both a vertex_list for an independent-tiled grid and creating also a vertex_list_indexed for a "united" (non independent tile) grid. :Parameters: `grid` : euclid.Point2 size of a 2D grid """ #: size of the grid. (rows, columns) self.grid = grid width, height = director.get_window_size() if self.texture is None: self.texture = image.Texture.create_for_size( GL_TEXTURE_2D, width, height, GL_RGBA) self.grabber = framegrabber.TextureGrabber() self.grabber.grab(self.texture) #: x pixels between each vertex (int) self.x_step = width // self.grid.x #: y pixels between each vertex (int) self.y_step = height // self.grid.y self._init()
def __init__(self): super().__init__('Ball.png') width, height = director.get_window_size() self.x = 1/2*width #random.randint(0,500) self.y = 3/4*height #random.randint(0,500) target_x, target_y = random.randint(0,width), 0 self.do( MoveTo((target_x,target_y),10) | RotateBy(random.randint(0,90),1) )
def __init__(self, editor): super(HUDLayer, self).__init__() atts = dict(color=(255,255,255,255), font_size=14, anchor_x='right', anchor_y='bottom') x, y = director.get_window_size() self.hud_x = x - 5 self.hud_y = y - 20 self.pointerLabel = Label('0,0', position=(self.hud_x, self.hud_y), **atts) self.add(self.pointerLabel, z=1) if editor.current_layer is not None: self.layerNameLabel = Label(editor.current_layer.label, position=(self.hud_x - 140, self.hud_y), **atts) else: self.layerNameLabel = Label("<no layers>", position=(self.hud_x - 140, self.hud_y), **atts) self.add(self.layerNameLabel, z=1) self.editor = editor self.showingLayerMenu = False # translucent = ColorLayer( 64,64,64,192, x, BOTTOM_BAR_HEIGHT) # translucent.position = (0,0) # self.add( translucent, name="bottom-bar" ) self.add_mode_buttons()
def __init__(self, model, hud ): super(GameView,self).__init__() width, height = director.get_window_size() aspect = width / float(height) self.grid_size = ( int(20 *aspect),20) self.duration = 8 self.position = ( width//2 - COLUMNS * SQUARE_SIZE // 2, 0 ) self.transform_anchor = ( COLUMNS*SQUARE_SIZE //2, ROWS * SQUARE_SIZE//2) # background layer to delimit the pieces visually cl = ColorLayer( 112,66,20,50, width = COLUMNS * SQUARE_SIZE, height=ROWS * SQUARE_SIZE ) self.add( cl, z=-1) self.model = model self.hud = hud self.model.push_handlers( self.on_line_complete, \ self.on_special_effect, \ self.on_game_over, \ self.on_level_complete, \ self.on_move_block, \ self.on_drop_block, \ self.on_new_level, \ self.on_win, \ ) self.hud.show_message( 'GET READY', self.model.start )
def __init__(self, model): super(Instructions, self).__init__() self.model = model # Queue for our instruction messages self.messages = deque() self.messages.append('Press ENTER to move to the next instruction') self.messages.append('This game is a shoot-em-up with a math puzzle twist.\nYou are Sigma, the ultimate Mathemagician.\nYour enemies are an angry group of regular polygons.') self.messages.append('They have shields to protect themselves, but they have a weakness...\n...They have a "kill vertex" that is marked by a large dot.') self.messages.append('You must shoot the kill vertex to kill the enemy.\nTo do that you must fire transformation bullets to make the kill vertex face downward.') self.messages.append('If you manage to kill all of the minions, you will fight the mighty Nonagon!\nBeware, for the Nonagon has 9 lives!') self.messages.append('Control your ship with the UP, DOWN, LEFT, and RIGHT arrow keys') self.messages.append('Press A to fire a ROTATE CLOCKWISE bullet') self.messages.append('Press S to fire a ROTATE COUNTER-CLOCKWISE bullet') self.messages.append('Press D to fire a FLIP LEFT bullet') self.messages.append('Press F to fire a FLIP RIGHT bullet') self.messages.append('Press SPACE to fire a DAMAGE bullet') self.messages.append('Do NOT hit an enemy with same transformation bullet twice in a row!\nThey will be annoyed by this and become un-transformable for a short period of time.') self.messages.append('If you kill 9 enemies without dying or hitting one with the same transformation bullet twice in a row, you will gain 9 extra lives!\nTrust me, you\'ll need them.') self.messages.append('GOOD LUCK!') # Create text box pad = 10 w, h = director.get_window_size() self.text = cocos.text.Label('', multiline=True, width=w-pad*2, color=(0, 0, 0, 255), font_name='Orbitron', font_size=32, anchor_x='center', anchor_y='center') self.text.position = w/2, 200 self.next_message() # Add text box to node self.add(self.text)
def shoot(self): music = pyglet.media.load("Song/laser.wav") music.play() x, y = self.sprite.position width, height = director.get_window_size() if(self.arme.name == "Simple"): sprite = cocos.sprite.Sprite(self.arme.missileSprite) self.add_collider_missile(sprite) self.add_to_layer(x + self.sprite.width//2 + 20, y) self.missileSprites[-1].do( MoveByAdditive((width, 0), 1) ) elif(self.arme.name == "Triple"): sprite1 = cocos.sprite.Sprite(self.arme.missileSprite) self.add_collider_missile(sprite1) self.add_to_layer(x + self.sprite.width//2 + 20, y) self.missileSprites[-1].do( MoveByAdditive((width, 0), 1)) sprite2 = cocos.sprite.Sprite(self.arme.missileSprite) self.add_collider_missile(sprite2) self.add_to_layer(x + self.sprite.width//2 + 20, y) self.missileSprites[-1].do( MoveBy((width, height//2), 1.5) ) sprite3 = cocos.sprite.Sprite(self.arme.missileSprite) self.add_collider_missile(sprite3) self.add_to_layer(x + self.sprite.width//2 + 20, y) self.missileSprites[-1].do( MoveBy((width, -height//2), 1.5) ) return self.missileSprites[-1]
def start( self ): new_grid = self.get_grid() if self.target.grid and self.target.grid.reuse_grid > 0: # Reusing the grid if self.target.grid.active \ and self.grid == self.target.grid.grid \ and type(new_grid) == type(self.target.grid): # since we are reusing the grid, # we must "cheat" the action that the original vertex coords are # the ones that were inhereted. self.target.grid.vertex_points = self.target.grid.vertex_list.vertices[:] self.target.grid.reuse_grid -= 1 self.target.grid.reuse_grid = max(0, self.target.grid.reuse_grid) else: # condition are not met raise GridException("Cannot reuse grid. class grid or grid size did not match: %s vs %s and %s vs %s" % ( str(self.grid), str(self.target.grid.grid), type(new_grid), type(self.target.grid) ) ) else: # Not reusing the grid if self.target.grid and self.target.grid.active: self.target.grid.active = False self.target.grid = new_grid self.target.grid.init( self.grid ) self.target.grid.active = True x,y = director.get_window_size() self.size_x = x // self.grid.x self.size_y = y // self.grid.y
def __init__(self, client): super(HeadPositionLayer, self).__init__() self.client = client self.screen = director.get_window_size() self.font = font.load('Cut Outs for 3D FX', 384) arrow1_img = self.font.get_glyphs("I")[0].get_texture(True) arrow1_img.anchor_x = 'center' arrow1_img.anchor_y = 'center' arrow2_img = self.font.get_glyphs("J")[0].get_texture(True) arrow2_img.anchor_x = 'center' arrow2_img.anchor_y = 'center' self.arrows = [Sprite(arrow1_img, position=(self.screen[0] / 2, self.screen[1] / 8 * 7), color=(255, 0, 0), opacity=0, scale=.75, rotation=270), Sprite(arrow1_img, position=(self.screen[0] / 2, self.screen[1] / 8 * 7), color=(255, 0, 0), opacity=0, scale=.75, rotation=90), Sprite(arrow1_img, position=(self.screen[0] / 8, self.screen[1] / 2), color=(255, 0, 0), opacity=0, scale=.75, rotation=0), Sprite(arrow1_img, position=(self.screen[0] / 8 * 7, self.screen[1] / 2), color=(255, 0, 0), opacity=0, scale=.75, rotation=180), Sprite(arrow2_img, position=(self.screen[0] / 2, self.screen[1] / 8), color=(255, 0, 0), opacity=0, scale=.75, rotation=90), Sprite(arrow2_img, position=(self.screen[0] / 2, self.screen[1] / 8), color=(255, 0, 0), opacity=0, scale=.75, rotation=270)] for arrow in self.arrows: self.add(arrow) self.head = (0,0,0)
def pixel_from_screen(self, x, y): '''Look up the Layer-space pixel matching the screen-space pixel. Account for viewport, layer and screen transformations. ''' # director display scaling if not director.do_not_scale_window: x, y = director.get_virtual_coordinates(x, y) # normalise x,y coord ww, wh = director.get_window_size() sx = x / self.view_w sy = y / self.view_h # get the map-space dimensions vx, vy = self.childs_ox, self.childs_oy # get our scaled view size w = int(self.view_w / self.scale) h = int(self.view_h / self.scale) #print (int(x), int(y)), (vx, vy, w, h), int(vx + sx * w), int(vy + sy * h) # convert screen pixel to map pixel return int(vx + sx * w), int(vy + sy * h)
def __init__(self): ''' initialize the scrolling text and color ''' super(ForFunLayer, self).__init__() x, y = director.get_window_size() title = text.Label( "CincyPy Flasklet Demo", (x / 2, y / 2), font_name='Gill Sans', font_size=60, anchor_x='center', anchor_y='center') title.element.color = (255, 255, 0, 255) self.objects.append(title) self.add(title) addresses = get_ipaddresses() k=0 for interface, ip in addresses: if ip != '0.0.0.0': t = text.Label("http://%s:%d (%s)" % (ip, flask_api.DEFAULT_PORT, interface), (0, (k + 1)*30+100), font_size=20) t.element.color = (255, 0, 0, 255) self.add(t) k += 1 sys.stderr.flush() # For handling of on_add_text flask_api.eventdispatcher.push_handlers(self) localdispatcher.push_handlers(self)
def init(self, center=(-1,-1), radius=160, lens_effect=0.7, *args, **kw): ''' :Parameters: `center` : (int,int) Center of the lens. Default: (win_size_width /2, win_size_height /2 ) `radius` : int Radius of the lens. `lens_effect` : float How strong is the lens effect. Default: 0.7. 0 is no effect at all, 1 is a very strong lens effect. ''' super(Lens3D,self).init( *args, **kw) x,y = director.get_window_size() if center==(-1,-1): center=(x//2, y//2) #: position of the center of the len. Type: (int,int). #: This value can be modified by other actions, like `JumpBy` to simulate a jumping lens self.position = Point2( center[0]+1, center[1]+1 ) #: radius of the lens. Type: float self.radius = radius #: lens effect factor. Type: float self.lens_effect = lens_effect self._last_position = (-1000,-1000) # dirty vrbl
def init( self, center=(-1,-1), twirls=4, amplitude=1, *args, **kw ): ''' :Parameters: `twirls` : int Number of twirls (2 * pi) that the action will perform. Default is 4 `amplitude` : flaot Twirl amplitude. Default is 1 `center` : (int,int) Center of the twirl in x,y coordinates. Default: center of the screen ''' super(Twirl, self).init( *args, **kw ) x,y = director.get_window_size() if center==(-1,-1): center=(x//2, y//2) #: position of the center of the Twril. Type: (int,int). #: This value can be modified by other actions, like `JumpBy` to simulate a jumping Twirl self.position = Point2( center[0]+1, center[1]+1 ) #: total number of twirls self.twirls = twirls #: amplitude of the twirls self.amplitude=amplitude #: amplitude rate. Default: 1.0. #: This value is modified by other actions like `AccelAmplitude`. self.amplitude_rate = 1.0
def game_over(over=True): #print("game_over") w, h = director.get_window_size() layer = cocos.layer.Layer() if over: label_txt = "Game Over" else: label_txt = "Game Pass" text = cocos.text.Label(label_txt, position=(w * 0.5, h * 0.5), font_name='Oswald', font_size=72, anchor_x='center', anchor_y='center') layer.add(text) scene = cocos.scene.Scene(layer) new_scene = FadeTransition(mainmenu.new_menu()) func = lambda: director.replace(new_scene) scene.do(ac.Delay(3) + ac.CallFunc(func)) return scene
def __init__(self, uri, width=None, height=None, fit=False): self.uri = uri image = pyglet.resource.image(uri) if fit: # XXX actually use this somehow some day # go for best fit width, height = map(float, director.get_window_size()) scale = min(width / image.width, height / image.height) width = int(image.width * scale) height = int(image.height * scale) self.width_spec = width self.height_spec = height self.scale = 1.0 self.opacity = 255 self.width, self.height = calculate_dimensions(width, height, image) super(ImageElement, self).__init__(image, self.width, self.height)
def __init__(self, hud): super(GameOver, self).__init__() self.hud = hud self.is_on_exiting = True levels = self.hud.levels gold = self.hud.gold death = self.hud.death label = Label('Game Over', **font_set(42)) centerx = director.get_window_size()[0] / 2 label.position = (centerx, 300) info = 'Level: ' + str(levels) + ' Gold: ' + str( gold) + ' Death: ' + str(death) label2 = Label(info, **font_set(22)) label2.position = (centerx, 150) label3 = Label('press R to restart, press C to continue', **font_set(18)) label3.position = (centerx, 120) self.add(label) self.add(label2) self.add(label3)
def layer1_reset(self): w, h = director.get_window_size() for picolo in self.group1: picolo.visible = False for k, xd in enumerate(self.xds.get_children()): xd.rotation = k * 10 self.picolos.stop() self.picolos.rotation = 0 self.group1[0].visible = True self.group1[0].scale = 0.1 self.wixa1.position = -300, h // 2 self.wixa2.position = w + 300, h // 2 self.wixa1.opacity = self.wixa2.opacity = 255 self.wixa1.scale = self.wixa2.scale = 0.3
def __init__(self): super(TestLayer, self).__init__() x, y = director.get_window_size() sprite1 = Sprite('grossini.png') sprite2 = Sprite('grossinis_sister1.png') sprite3 = Sprite('grossinis_sister2.png') sprite1.position = (x / 2, y / 2) sprite2.position = (x / 4, y / 2) sprite3.position = (3 * x / 4.0, y / 2) self.add(sprite2) self.add(sprite1) self.add(sprite3) sprite1.do(RotateBy(360, 1) * 16) sprite2.do(RotateBy(-360, 1) * 16) sprite3.do(RotateBy(-360, 1) * 16)
def __init__(self, hud, scenario): super(GameLayer, self).__init__() self.hud = hud self.scenario = scenario self.score = self._score = 0 self.points = self._points = 60 self.turrets = [] w, h = director.get_window_size() #使用兩個碰撞管理器,因為slots只是要放砲塔而已所以可以獨立開來,並不需要發生碰撞,雖然全放在一起也可以,但在判斷是否是敵人或是on_mouse_press()時就要多判斷一堆,因此獨立開來可以改善效能 cell_size = 32 self.coll_man = cm.CollisionManagerGrid(0, w, 0, h, cell_size, cell_size) self.coll_man_slots = cm.CollisionManagerGrid(0, w, 0, h, cell_size, cell_size) for slot in scenario.turret_slots: #在腳本設定的位子全部放入砲塔槽演員,其全部加入coll_man_slots碰撞管理器 self.coll_man_slots.add(actors.TurretSlot(slot, cell_size)) self.bunker = actors.Bunker(*scenario.bunker_position) #碉堡 self.add(self.bunker) self.schedule(self.game_loop) #註冊排程game_loop 開始每幀循環
def __init__( self, *args, **kwargs ): super(FlipAngular3DTransition, self ).__init__( *args, **kwargs) width, height = director.get_window_size() turnongrid = Waves3D( amplitude=0, duration=0, grid=(1,1), waves=2 ) flip90 = OrbitCamera( angle_x=45, delta_z=90, duration = self.duration / 2.0 ) flipback90 = OrbitCamera( angle_x=45, angle_z=90, delta_z=90, duration = self.duration / 2.0 ) self.in_scene.visible = False flip = turnongrid + \ flip90 + \ CallFunc(self.hide_all) + \ FlipX3D(duration=0) + \ CallFunc( self.hide_out_show_in ) + \ flipback90 self.do( flip + \ CallFunc(self.finish) + \ StopGrid() )
def update(self, dt): self.cshape.center = self.position win_width, win_height = director.get_window_size() xmin = self.width//2 xmax = win_width - xmin ymin = self.height//2 if self.position[1] <= ymin: self.position = (self.position[0], ymin) self.velocity = (self.velocity[0], 0) self.jumping = False if self.position[0] < xmin: self.position = (xmin, self.position[1]) elif self.position[0] > xmax: self.position = (xmax, self.position[1]) else: pass
def __init__(self, start_timestamp): """initializer""" Frame.__init__(self, side="manual") self.config = Config() self.gallery = Gallery() self.transform_anchor = 0, 0 scaler = ResolutionScaler(director.get_window_size()) self.scale = scaler.get_ratio() self.is_event_handler = True events.push_handlers(speed_was_modified=self.align_speed_hand) self.hands = {} self.clock_face = None self.create_face() self.label_day_ind = self._load_label_gen("label_day_ind") self.add(self.label_day_ind) self.day = float((start_timestamp // 0.5) * 0.5) self.label_day_ind.element.text = str(self.day) director.core.TSP = start_timestamp self.create_hands(start_timestamp) self.pre_angle = 0
def __init__(self): super(Manekin, self).__init__() w, h = director.get_window_size() self.sprite = helper.get_sprite('manekin-nobg.png', w // 2, h // 2) self.sprite.scale = 1.7 self.add(self.sprite) self.heart = helper.get_sprite('wxpl_heart-nobg.png', w // 2 + 30, h // 2 + 250) self.heart.do(R_ScaleFromTo(5, 1, duration=0.5)) self.add(self.heart) self.do( Repeat( Twirl(center=(1000, 500), grid=(16, 12), duration=6, twirls=10, amplitude=1.3)))
def __init__(self): super( TestLayer, self ).__init__() x,y = director.get_window_size() self.sprite = Sprite( 'grossini.png', (x//4, y//4), rotation=355 ) self.add( self.sprite ) self.sprite.do( cocos.actions.RotateTo( 45, 3 ) ) self.sprite = Sprite( 'grossini.png', (x//4*3, y//4) ) self.add( self.sprite ) self.sprite.do( cocos.actions.RotateTo( -45, 3 ) ) self.sprite = Sprite( 'grossini.png', (x//4, y//4*3), rotation=135 ) self.add( self.sprite ) self.sprite.do( cocos.actions.RotateTo( 45, 3 ) ) self.sprite = Sprite('grossini.png', (x//4*3, y//4*3), rotation=135) self.add( self.sprite ) self.sprite.do( cocos.actions.RotateTo( -45, 3 ) )
def render(self): x, y = director.get_window_size() ye = 15 xs = 15 line_width = self.line_width self.set_color((255, 255, 0, 125)) self.set_stroke_width(line_width) parts = 5 # draw lines self.set_endcap(draw.ROUND_CAP) self.translate((x // 2, y // 2)) for j in range(parts): self.move_to((0, 0)) self.rotate(2 * math.pi / parts) self.push() for i in range(parts): self.line_to((xs, ye)) self.translate((xs, ye)) self.rotate(math.pi / parts) self.pop()
def __init__(self, *args, **kwargs): super(JumpZoomTransition, self).__init__(*args, **kwargs) width, height = director.get_window_size() self.in_scene.scale = 0.5 self.in_scene.position = (width, 0) self.in_scene.transform_anchor = (width // 2, height // 2) self.out_scene.transform_anchor = (width // 2, height // 2) jump = JumpBy((-width, 0), width // 4, 2, duration=self.duration / 4.0) scalein = ScaleTo(1, duration=self.duration / 4.0) scaleout = ScaleTo(0.5, duration=self.duration / 4.0) jumpzoomout = scaleout + jump jumpzoomin = jump + scalein delay = Delay(self.duration / 2.0) self.out_scene.do(jumpzoomout) self.in_scene.do(delay + jumpzoomin + CallFunc(self.finish))
def pixel_from_screen(self, x, y): '''Look up the Layer-space pixel matching the screen-space pixel. Account for viewport, layer and screen transformations. ''' # director display scaling x, y = director.get_virtual_coordinates(x, y) # normalise x,y coord ww, wh = director.get_window_size() sx = x / ww sy = y / wh # get the map-space dimensions vx, vy, w, h = self.view_x, self.view_y, self.view_w, self.view_h #print (int(x), int(y)), (vx, vy, w, h), int(vx + sx * w), int(vy + sy * h) # convert screen pixel to map pixel return int(vx + sx * w), int(vy + sy * h)
def __init__(self, hud, scenario): super(GameLayer, self).__init__() self.hud = hud self.scenario = scenario self.score = self._score = 0 self.points = self._points = 60 self.turrets = [] w, h = director.get_window_size() cell_size = 32 self.coll_man = cm.CollisionManagerGrid(0, w, 0, h, cell_size, cell_size) self.coll_man_slots = cm.CollisionManagerGrid(0, w, 0, h, cell_size, cell_size) for slot in scenario.turret_slots: self.coll_man_slots.add(actors.TurretSlot(slot, cell_size)) self.bunker = actors.Bunker(*scenario.bunker_position) self.add(self.bunker) self.schedule(self.game_loop)
def __init__(self): super(JanGabber2, self).__init__() w, h = director.get_window_size() self.add(self.get_spinner('red', w - 250, h - 250)) self.add(self.get_spinner('green', w // 2, 250)) self.add(self.get_spinner('blue', 250, h - 250)) l = Line("JAN GABBER 2", 50, (255, 255, 0, 255), w // 2 - 225, h - 300, "November") l.do(R_ScaleFromTo(1.3, 0.6, 0.2)) self.add(l) l_right = Line("xD " * 20, 50, (255, 255, 0, 255), 0, h - 200, "November") # l2.position = -w//4-100, -150 l_right.rotation = 90 l_right.do( Repeat(CallFunc(l_right.update_line_reversed) + Delay(0.05))) self.add(l_right) wixa_left = Line("WIXAPOL S.A ", 50, (255, 255, 0, 255), 0, h // 2 - 100, "November") wixa_left.do(Repeat(CallFunc(wixa_left.update_line) + Delay(0.05))) self.add(wixa_left) # wixa_right = Line("WIXAPOL S.A ", 50, (255, 255, 0, 255), w - 550, h // 2 - 100, "November") wixa_right.do(Repeat(CallFunc(wixa_right.update_line) + Delay(0.05))) self.add(wixa_right) l_middle = LineCenterAnchor("PEACE", 50, (255, 255, 0, 255), 0, h // 2, "November") l_middle.word_index = 0 l_middle.do(Repeat(CallFuncS(self.plur_callback) + Delay(0.5))) self.add(l_middle) l_left = Line("xD " * 20, 50, (255, 255, 0, 255), 0, 100, "November") l_left.rotation = 90 l_left.do(Repeat(CallFunc(l_left.update_line) + Delay(0.05))) self.add(l_left)
def __init__(self): super( TestLayer, self ).__init__() self.mouse_x = self.mouse_y = 0 x,y = director.get_window_size() self.sprite1 = Sprite('grossini.png', anchor=(0, 0)) self.sprite_rect = None self.add(self.sprite1, z=2) self.sprite1.position = x // 3, y // 2 self.show_rect() self.do( Delay(2) + CallFunc(self.mov) + Delay(2) + CallFunc(self.zoom) + Delay(2) + CallFunc(self.scalex) + Delay(2) + CallFunc(self.rot) ) self.mouse_mark = cocos.layer.ColorLayer(0, 0, 255, 255, 20, 20) self.add(self.mouse_mark, z=3)
def __init__(self): w,h = director.get_window_size() super( ScoresLayer,self).__init__( 32,32,32,16, width=w, height=h-86) self.font_title = {} # you can override the font that will be used for the title and the items self.font_title['font_name'] = 'Edit Undo Line BRK' self.font_title['font_size'] = 72 self.font_title['color'] = (204,164,164,255) self.font_title['anchor_y'] ='top' self.font_title['anchor_x'] ='center' title = Label('TETRICO', **self.font_title ) title.position=(w/2.0,h) self.add(title,z=1) self.table = None
def update(self, dt): d = self.ball.width // 2 w, h = director.get_window_size() self.ball.x += self.ball.dx * dt self.ball.y += self.ball.dy * dt if not (d < self.ball.y < h-d): self.ball.dy *= -1 self.sound.play() self.score += 1 self.label.element.text = str(self.score) if self.ball.x > w-d: self.ball.dx *= -1 self.score += 1 self.label.element.text = str(self.score) if self.ball.x < d + 20: if self.pad.y < self.ball.y < self.ball.y + 100: self.ball.dx *= -1 self.sound.play() else: gameover = cocos.scene.Scene(GameOver()) director.replace(gameover)
def on_enter(self): super().on_enter() w, h = director.get_window_size() about_text = text.Label('PyFense ist ein Tower Defense Spiel ' + 'welches im Rahmen des Python ' + 'Projektpraktikums an der TU ' + 'München von fünf Studenten programmiert ' + 'wurde.\nMitglieder: Daniel, Jakob, ' + 'Matthias, Nimar, Robin' + '\nMusic from:\n' + 'www.freesound.org/people/djgriffin/', font_name=_font_, font_size=20, anchor_x='center', anchor_y='center') about_text.element.width = w * 0.3 about_text.element.multiline = True about_text.element.wrap_lines = True about_text.position = w / 2., h / 2. self.add(about_text)
def __init__(self, items, title=None, *args, **kwargs): super(ListMenu, self).__init__(*args, **kwargs) self.items = items self.labels = [] self.index = 0 w, h = director.get_window_size() if title == None: title = 'List Menu' self.title = Label(title, font_size=24, bold=True, position=(10, h-30)) self.add(self.title) # Create labels for each item in the list x, y, dy = 10, h-60, -20 for item in items: label = Label(item, position=(x, y)) y += dy self.add(label) self.labels.append(label) # Make the selected item bold self.labels[self.index].element.bold = True
def reset(self): # Start ball off in a random direction, heading to the right, within 45 degrees of 0 if random.random() > 0.5: self.set_dir(random.random() * math.pi / 8 + math.pi / 4) else: self.set_dir((random.random() * math.pi / 8 + math.pi / 4) * -1) # Start ball in random position, roughly near the center of screen dx = director.get_window_size()[0] / 6 * random.random( ) - director.get_window_size()[0] / 12 dy = director.get_window_size()[1] / 6 * random.random( ) - director.get_window_size()[1] / 12 self.x_pos = director.get_window_size()[0] / 2 + dx self.y_pos = director.get_window_size()[1] / 2 + dy self.position = self.x_pos, self.y_pos
def init(self, radius=None, delta_radius=0, angle_z=None, delta_z=0, angle_x=None, delta_x=0, *args, **kw): """Initialize the camera with spherical coordinates :Parameters: `radius` : float Radius of the orbit. Default: current radius `delta_radius` : float Delta movement of the radius. Default: 0 `angle_z` : float The zenith angle of the spherical coordinate in degrees. Default: current `delta_z` : float Relative movement of the zenith angle. Default: 0 `angle_x` : float The azimuth angle of the spherical coordinate in degrees. Default: 0 `delta_x` : float Relative movement of the azimuth angle. Default: 0 For more information regarding spherical coordinates, read this: http://en.wikipedia.org/wiki/Spherical_coordinates """ super(OrbitCamera, self).init(*args, **kw) width, height = director.get_window_size() self.radius = radius self.delta_radius = delta_radius self.angle_x = angle_x self.rad_delta_x = math.radians(delta_x) self.angle_z = angle_z self.rad_delta_z = math.radians(delta_z)
def show_tick(self, when): l = self.height + 5 x,y = director.get_window_size() ym = self.y_margin p = self.map_to_pixel( when ) # draw line glColor4ub(128, 128, 128,100) glLineWidth(1) glBegin(GL_LINES) glVertex2f( p, y-ym ) glVertex2f( p, y-ym-l ) glEnd() # draw label label = pyglet.text.Label(str(when), font_name='Monotype', #font_name='Times New Roman', font_size=8, x=p, y=y-ym-l-7, anchor_x='center', anchor_y='center') label.draw()
def set_menu(self): size = director.get_window_size() obj_list = [Bowman, Warrior, Priest, Carpenter] positions = [(size[0] / 20 * 17.4, 18.5 * size[1] / 20 - 150), (size[0] / 20 * 15.3, 18.5 * size[1] / 20 - 150), (size[0] / 20 * 17.4, 16.3 * size[1] / 20 - 150), (size[0] / 20 * 15.4, 16.3 * size[1] / 20 - 150)] for i in range(len(obj_list)): temp = obj_list[i](self, positions[i], copyable=True, scale_by=1.5) free_units_names = [] for unit in self.free_units: free_units_names.append(unit.name) if temp.name not in free_units_names or self.free_units[temp] == 0: temp.set_yellow_color() temp.opacity = self.menu_hide_opacity else: temp.set_green_color() temp.opacity = self.menu_hide_opacity self.spawn_unit(temp)
def __init__(self, controller): super().__init__('Deck') self.ctrl = controller set_menu_style(self, item_size=24) window_size = director.get_window_size() items = [ menu.MenuItem('Back', self.on_quit), ] items_positions = [ abs_pos(0.95, 0.05, window_size), ] self.create_menu( items, selected_effect=menu.shake(), unselected_effect=menu.shake_back(), layout_strategy=menu.fixedPositionMenuLayout(items_positions), )
def __init__(self, world_complete): super().__init__() width, height = director.get_window_size() if world_complete: self.create_text("WORLD COMPLETE", width // 2, height // 2) return if stats.life <= 0: self.create_text("GAME OVER", width // 2, height // 2) else: text = " WORLD " + str(stats.major_world) + "-" + str( stats.minor_world) self.create_text(text, width // 2, height // 2 + 100) sprite = Sprite(Image.mario) sprite.position = (width // 2 - 40, height // 2) self.add(sprite) self.create_text("x " + str(stats.life), width // 2 + 40, height // 2)
def layout_multiply(content, row, column, pos_range=None, color=(127, 255, 170, 255), font_name='times new roman', font_size=36): if len(content) == 0: return [] if pos_range is None: from cocos.director import director (x1, y1) = director.get_window_size() (x0, y0) = (0, 0) else: (x0, y0), (x1, y1) = pos_range _step = (x1 - x0) // (column + 1) _pos_range = (x0, y0), (x0 + _step, y1) _content = [] textmap = [] for i, text in enumerate(content): _content.append(text) if i % row == row - 1: _column = (i + 1) // row textmap.append( layout(_content, _pos_range, color, font_name, font_size)) _content = [] _pos_range = (x0 + _step * _column, y0), (x0 + _step * (_column + 1), y1) while not i % row == row - 1: i += 1 _content.append(' ') _column = (i + 1) // row _pos_range = (x0, y0), (x0 + _step * (_column + 1), y1) textmap.append(layout(_content, _pos_range, color, font_name, font_size)) return textmap
def show_message(self, msg, callback=None): w, h = director.get_window_size() self.msg = cocos.text.Label(msg, font_size=52, font_name=consts['view']['font_name'], anchor_y='center', anchor_x='center') self.msg.position = (w / 2.0, h) self.add(self.msg) actions = (ac.Show() + ac.Accelerate(ac.MoveBy( (0, -h / 2.0), duration=0.5)) + ac.Delay(1) + ac.Accelerate(ac.MoveBy( (0, -h / 2.0), duration=0.5)) + ac.Hide()) if callback: actions += ac.CallFunc(callback) self.msg.do(actions)
def __init__(self): self.visible=True super(Mainmenu, self).__init__() self.position = (0,0) '''text = cocos.text.Label('GAME MODE', font_name='times new roman', font_size=36, color=define.GRAY) text.position = 165,300 self.add(text)''' x0, y0 = director.get_window_size() self.gm = Sprite(image='Game Mode1.png', scale=1.5) self.gmpos = self.gm.width/2 + x0/2 - 180, self.gm.height/2+y0/2 +50 self.gm.position = self.gmpos self.add(self.gm) self.rm = Sprite(image='Learning Mode 1.png', scale=1.5) self.rmpos = self.rm.width/2 + x0/2 - 240, self.rm.height/2+y0/2 -80 self.rm.position = self.rmpos self.add(self.rm) self.x0 = x0 self.y0 = y0 self.chosen = 'none'