def create_avatar(size, hash_val, with_background=True): """Returns a unicorn image with *twice* the given size (i.e. with size=128, you'll get a 256x256 image) -- it's aliased, so you'll want to run it through a resizing filter to have an antialiased image of your actual desired size.""" randomizer = Random() randint, choice, random = randomizer.randint, randomizer.choice, randomizer.random randomizer.seed(hash_val) unicorndata = UnicornData() backgrounddata = BackgroundData() # Here comes the randomizing. To keep consistence of unicorns between versions, # new random() calls should usually be made *after* the already existing calls unicorndata.randomize(randomizer) backgrounddata.randomize(randomizer) unicorn_scale_factor = (.5 + (random()**2) * 2.5) y_angle = 90 + choice((-1, 1)) * randint(10, 75) x_angle = randint(-20, 20) unicorndata.randomize2(randomizer) # new in v4 backgrounddata.randomize2(randomizer) unicorndata.randomize3(randomizer) # new in v5 # End of randomizing if (y_angle - 90) * unicorndata.neck_tilt > 0: # The unicorn should look at the camera. unicorndata.neck_tilt = -unicorndata.neck_tilt unicorndata.face_tilt = -unicorndata.face_tilt unicorn = Unicorn(unicorndata) if with_background: im = get_background(size, backgrounddata) else: im = SquareImage.plain(size * 2, (255, 255, 255)) unicorn.scale(unicorn_scale_factor * size / 200.0) wv = WorldView(y_angle, x_angle, (150, 0, 0), (0, 100)) unicorn.project(wv) headpos = unicorn.head.projection shoulderpos = unicorn.shoulder.projection headshift = (im.size / 2 - headpos[0], im.size / 3 - headpos[1]) shouldershift = (im.size / 2 - shoulderpos[0], im.size / 2 - shoulderpos[1]) # factor = 1 means center the head at (1/2, 1/3); factor = 0 means # center the shoulder at (1/2, 1/2) factor = sqrt((unicorn_scale_factor - .5) / 2.5) wv.shift = tuple(c0 + factor * (c1 - c0) for c0, c1 in zip(shouldershift, headshift)) unicorn.sort(wv) unicorn.draw(im, wv) return im.to_bmp()
def create_avatar(size, hash_val, with_background = True): """Returns a unicorn image with *twice* the given size (i.e. with size=128, you'll get a 256x256 image) -- it's aliased, so you'll want to run it through a resizing filter to have an antialiased image of your actual desired size.""" randomizer = Random() randint, choice, random = randomizer.randint, randomizer.choice, randomizer.random randomizer.seed(hash_val) unicorndata = UnicornData() backgrounddata = BackgroundData() # Here comes the randomizing. To keep consistence of unicorns between versions, # new random() calls should usually be made *after* the already existing calls unicorndata.randomize(randomizer) backgrounddata.randomize(randomizer) unicorn_scale_factor = (.5 + (random()**2) * 2.5) y_angle = 90 + choice((-1,1)) * randint(10, 75) x_angle = randint(-20, 20) unicorndata.randomize2(randomizer) # new in v4 backgrounddata.randomize2(randomizer) unicorndata.randomize3(randomizer) # new in v5 # End of randomizing if (y_angle - 90) * unicorndata.neck_tilt > 0: # The unicorn should look at the camera. unicorndata.neck_tilt = -unicorndata.neck_tilt unicorndata.face_tilt = -unicorndata.face_tilt unicorn = Unicorn(unicorndata) if with_background: im = get_background(size, backgrounddata) else: im = SquareImage.plain(size * 2, (255, 255, 255)) unicorn.scale(unicorn_scale_factor * size / 200.0) wv = WorldView(y_angle, x_angle, (150, 0, 0), (0, 100)) unicorn.project(wv) headpos = unicorn.head.projection shoulderpos = unicorn.shoulder.projection headshift = (im.size/2 - headpos[0], im.size/3 - headpos[1]) shouldershift = (im.size / 2 - shoulderpos[0], im.size/2 - shoulderpos[1]) # factor = 1 means center the head at (1/2, 1/3); factor = 0 means # center the shoulder at (1/2, 1/2) factor = sqrt((unicorn_scale_factor - .5) / 2.5) wv.shift = tuple(c0 + factor * (c1 - c0) for c0, c1 in zip(shouldershift, headshift)) unicorn.sort(wv) unicorn.draw(im, wv) return im.to_bmp()
def __init__(self): """ Constructor """ pygame.init() pygame.mixer.init() # Load game icon self.game_icon = pygame.image.load('images/game/icon.png') pygame.display.set_icon(self.game_icon) self.settings = Settings() # Set screen dimensions. self.SCREEN_HEIGHT, self.SCREEN_WIDTH = self.settings.screen_height, self.settings.screen_width pygame.display.set_caption("Flappy Unicorn") self.screen = pygame.display.set_mode( (self.SCREEN_WIDTH, self.SCREEN_HEIGHT)) self.screen_rect = self.screen.get_rect() self.game_active = False self.game_over = False self.pillar_time_elapsed = 0 self.cloud_time_elapsed = 0 self.clock_tick = 0 # Audio instance. self.audio = Audio() # Create start and game over screens. self.start_screen = StartScreen(self) self.game_over_screen = GameOver(self) # Create an animated flying unicorn self.unicorn = Unicorn(self) self.unicorn_sprite = pygame.sprite.Group(self.unicorn) # Create the background self.background = Background(self) # Create ground. self.ground = Ground(self) # Create cloud. self.clouds = pygame.sprite.Group() # Create pillar sprite group. self.pillars = pygame.sprite.Group() # Create the scoreboard. self.scoreboard = Scoreboard(self)
def prep_unicorns(self): self.unicorns = Group() for unicorn_number in range(self.stats.unicorns_left): unicorn = Unicorn(self.ai_settings, self.screen) unicorn.rect.x = 10 + unicorn_number * unicorn.rect.width unicorn.rect.y = 10 self.unicorns.add(unicorn)
def post(self): message = self.get_arg("message") user = session.get(self.request, "user", "anonymous") now = datetime.datetime.now().replace(microsecond=0).time() local_redis.publish("[{now}] {user}: {message}".format( now=now, user=user, message=message)) return Unicorn.Response(status=204)
def new(): print("-" * 50) print("add new>>>") name = input("name: ") age = input("age: ") number_id = input("number_id: ") new_unicorn = Unicorn(name, age, number_id) unicorn_dao.add_unicorn(new_unicorn)
def run_game(): pygame.init() ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption("Unicorn Invasion") play_button = Button(ai_settings, screen, "PLAY") stats = GameStats(ai_settings) sb = Scoreboard(ai_settings, screen, stats) bg_color = (249, 180, 222) unicorn = Unicorn(ai_settings, screen) bullets = Group() rainbows = Group() gf.create_fleet(ai_settings, screen, unicorn, rainbows) # rainbow = Rainbow(ai_settings, screen) pygame.mixer.music.load("song/UnicornsSong.mp3") pygame.mixer.music.play(-1, 0.0) while True: gf.check_events(ai_settings, screen, stats, sb, play_button, unicorn, rainbows, bullets) if stats.game_active: unicorn.update() gf.update_bullets(ai_settings, screen, stats, sb, unicorn, rainbows, bullets) gf.update_rainbows(ai_settings, stats, screen, sb, unicorn, rainbows, bullets) gf.update_screen(ai_settings, screen, stats, sb, unicorn, rainbows, bullets, play_button)
def test_it_has_a_name(): """A unicorn will be created with a name.""" rob = Unicorn('Robert') assert rob.name == 'Robert'
def test_it_is_white_by_default(): """Unicorns are white by default.""" rob = Unicorn('Robert') assert rob.color == 'White'
def get(self): return Unicorn.Response(response=event_stream(), mimetype="text/event-stream")
def test_algs(): unicorn = Unicorn(dim_reduc_alg="umap", cluster_alg="hdbscan") res = unicorn.cluster(data) assert isinstance(res, np.ndarray) assert res.shape == (n_samples, ) assert isinstance(res[0], (np.int32, np.int64))
def test_basic(): unicorn = Unicorn() res = unicorn.cluster(data) assert isinstance(res, np.ndarray) assert res.shape == (n_samples, ) assert isinstance(res[0], (np.int32, np.int64))
return self.render_file() unicorn_url_map = [ { 'url': '/', 'view': Index, 'endpoint': 'index' }, { 'url': '/login', 'view': Login, 'endpoint': 'test' }, { 'url': '/logout', 'view': Logout, 'endpoint': 'logout' }, { "url": "/api", "view": API, "endpoint": "api", }, { "url": "/download", "view": Download, "endpoint": "download" } ] app = Unicorn() index_controller = Controller("index", unicorn_url_map) app.load_controller(index_controller) app.run()
from color import Color from effects.candle_light import CandleLight from effects.ocean_boat_blue import OceanBoatBlue from effects.rainbow import Rainbow from unicorn import Unicorn app = Flask('unicorn') logger = app.logger medium_cyan = Color(hex="007F7F") # Siri color: "Aqua" # Initialize the lamp to medium-brightness "Aqua" (Siri's name for this color) unicorn = Unicorn( uh, medium_cyan, effect_modes={ 'FF9429': CandleLight, # Siri color: "Candle Light" '00A2FF': OceanBoatBlue, # Siri color: "Ocean Boat Blue" 'FF2977': Rainbow, # Siri color: "Razzmatazz" }) OK = "OK" @app.route('/unicorn/api/v1.0/status', methods=[ 'GET', ]) def get_status(): """ Returns the status (on/off) of the lamp as 1 or 0. :return: """
# -*- coding: utf-8 -*- # @Time : 2017/8/24 # @Author : ywh # @Email : [email protected] # @File : main.py # @Software: PyCharm from urls import controller_list from unicorn import Unicorn app = Unicorn() app.load_controller(controllers=controller_list) app.run(threaded=True)
class FlappyUnicorn: """ A general class to manage the game. """ def __init__(self): """ Constructor """ pygame.init() pygame.mixer.init() # Load game icon self.game_icon = pygame.image.load('images/game/icon.png') pygame.display.set_icon(self.game_icon) self.settings = Settings() # Set screen dimensions. self.SCREEN_HEIGHT, self.SCREEN_WIDTH = self.settings.screen_height, self.settings.screen_width pygame.display.set_caption("Flappy Unicorn") self.screen = pygame.display.set_mode( (self.SCREEN_WIDTH, self.SCREEN_HEIGHT)) self.screen_rect = self.screen.get_rect() self.game_active = False self.game_over = False self.pillar_time_elapsed = 0 self.cloud_time_elapsed = 0 self.clock_tick = 0 # Audio instance. self.audio = Audio() # Create start and game over screens. self.start_screen = StartScreen(self) self.game_over_screen = GameOver(self) # Create an animated flying unicorn self.unicorn = Unicorn(self) self.unicorn_sprite = pygame.sprite.Group(self.unicorn) # Create the background self.background = Background(self) # Create ground. self.ground = Ground(self) # Create cloud. self.clouds = pygame.sprite.Group() # Create pillar sprite group. self.pillars = pygame.sprite.Group() # Create the scoreboard. self.scoreboard = Scoreboard(self) def run(self): """ Main game loop. """ # Create clock to track time. clock = pygame.time.Clock() while True: # Check key pressed events. self._check_key_events() self._update_screen() # Update clock and time elapsed. self.pillar_time_elapsed += clock.get_time() self.cloud_time_elapsed += clock.get_time() clock.tick(60) def _check_key_events(self): """ Check for key pressed events. """ for event in pygame.event.get(): # Quit / Exit game. if event.type == pygame.QUIT: sys.exit(0) # Key pressed event. if event.type == pygame.KEYDOWN: self._check_key_down_events(event) # Mouse clicked event. if event.type == pygame.MOUSEBUTTONDOWN: mouse_position = pygame.mouse.get_pos() self._check_button_clicked(mouse_position) def _check_key_down_events(self, event): """ Respond to a key pressed down event. """ if not self.game_over and not self.start_screen.screen_active: if event.key == pygame.K_SPACE: self.unicorn.jump_count = 0 self.audio.play_sound('wings') elif event.key == pygame.K_q: sys.exit(0) elif event.key == pygame.K_p: self.game_active = True self.start_screen.screen_active = False def _check_button_clicked(self, mouse_position): """ Check if player clicked a button and process request. """ # Start game button if self.start_screen.start_button.rect.collidepoint(mouse_position): self.game_active = True self.start_screen.screen_active = False pygame.mouse.set_visible(False) # Retry Button if self.game_over_screen.retry_button.rect.collidepoint( mouse_position): self.restart() def _check_unicorn_collision(self): """ Respond to a collision event. """ for unicorn in self.unicorn_sprite: if unicorn.rect.bottom >= self.settings.gnd_col_zone or unicorn.rect.top <= self.screen_rect.top: self._game_over_loop() self.audio.play_sound('hit') return obstacle_collision = pygame.sprite.groupcollide( self.unicorn_sprite, self.pillars, False, False) if obstacle_collision: self._game_over_loop() self.audio.play_sound('hit') def _check_clouds(self): """ Manage clouds on screen. """ if self.cloud_time_elapsed > self.settings.cloud_frequency and len( self.clouds) < self.settings.cloud_qty: self.cloud_time_elapsed = 0 new_cloud = Cloud(self) self.clouds.add(new_cloud) self._check_cloud_edges() def _check_cloud_edges(self): """ Loop through cloud sprites and check if cloud is still on screen. Remove clouds that have scrolled off the display. """ for cloud in self.clouds: if cloud.cloud_rect.right <= 0: self.clouds.remove(cloud) def _create_pillar(self): """ Create a new pillar object and add to the pillar sprite group. """ top_pillar = PillarTop(self) bottom_pillar = PillarBottom(self, top_pillar.rect) pillars = [top_pillar, bottom_pillar] self.pillars.add(*pillars) def _check_pillars(self): """ A method that manages the pillar obstacles. """ if self.pillar_time_elapsed > self.settings.pillar_frequency: # If a certain time has elapsed (settings.pillar_frequency) create a new pillar. self.pillar_time_elapsed = 0 self._create_pillar() self._check_pillar_edges() def _check_pillar_edges(self): """ Loop through the group of pillar sprites and check their right edge. Remove pillar sprites that have left the screen. """ for pillar in self.pillars.copy(): if pillar.rect.right <= 0: self.pillars.remove(pillar) def _perform_checks(self): """ Check sprite positions. """ self._check_pillars() self._check_clouds() self._check_unicorn_collision() self.scoreboard.check_score_zone(self.unicorn_sprite, self.pillars) def _update_screen(self): """ Update surfaces and flip screen. """ # Update background image. self.background.update() if self.game_active: self._perform_checks() self.scoreboard.update() # Draw the pillar sprites that have been added to the pillar sprite group. for pillar in self.pillars.sprites(): pillar.draw_pillars() # Update unicorn position and animation. self.unicorn_sprite.update() self.unicorn_sprite.draw(self.screen) # Draw new clouds to screen. for cloud in self.clouds.sprites(): cloud.draw_cloud() if not self.game_over: # Update scrolling ground position. self.ground.update() # Update clouds position self.clouds.update() # Update pillar position self.pillars.update() elif not self.game_over: # Start Screen. self.ground.blit_background(start_screen=True) self.start_screen.blit_me() else: self._game_over_loop() # Flip the new display to screen. pygame.display.flip() def _game_over_loop(self): """ Halt game blit game over and retry button to screen. Enable mouse cursor. """ self.game_over = True self.game_active = False if self.unicorn.rect.bottom >= self.screen_rect.bottom: self.unicorn.rect.bottom = self.screen_rect.bottom self.unicorn_sprite.update(animation=False) self.unicorn_sprite.draw(self.screen) self.pillars.draw(self.screen) self.ground.blit_background() self.scoreboard.update() self.game_over_screen.blit_me() # Enable mouse cursor. pygame.mouse.set_visible(True) def restart(self): """ Reset game attributes and start a new game. """ self.pillars.empty() self.clouds.empty() self.unicorn.reset_rect() self.scoreboard.score = 0 self.pillar_time_elapsed = 0 self.cloud_time_elapsed = 0 self.game_over = False self.game_active = True pygame.mouse.set_visible(False)