def firePlaceDemo(wait, iterate): for i in range(iterate): pixel = random.randint(0, num_leds - 1) color = (random.randint(50, 255), random.randint(0, 40), 0) pixels[pixel] = color pixels.write() time.sleep(wait)
def display_food(self): self.food.color = color565(0xff, 0x00, 0x00) self.food.type = 1 if randint(0, 40) == 5: self.food.color = color565(0x00, 0xff, 0x00) self.food.type = 3 while (self.food.pos in self.snake.body): self.food.set_pos() self.food.display() elif randint(0, 4) == 2: self.food.color = color565(0x00, 0x00, 0xff) self.food.type = 4 while (self.food.pos in self.snake.body): self.food.set_pos() self.food.display() elif len(self.snake.body) > 10 and randint(0, 16) == 5: self.food.color = color565(0xff, 0xff, 0xff) self.food.type = 2 while (self.food.pos in self.snake.body): self.food.set_pos() self.food.display() else: while (self.food.pos in self.snake.body): self.food.set_pos() self.food.display() print(self.food.type)
def firePlaced(wait, itr): for i in range(itr): pixel = random.randint(0, leds - 1) color = (random.randint(50, 255), random.randint(0, 40), 0) pi[pixel] = color pi.write() time.sleep(wait)
def random_wave(he, wi): wave[he][wi][upper] = -1 # Always start below head of strip wave[he][wi][lower] = -16 * (3 + random.randint(0,4)) # Lower end starts ~3-7 pixels back wave[he][wi][mid] = (wave[he][wi][lower]+ wave[he][wi][upper]) / 2 wave[he][wi][vlower] = 3 + random.randint(0,4) # Lower end moves at ~1/8 to 1/pixels wave[he][wi][vupper] = wave[he][wi][vlower]+ random.randint(0,4) # Upper end moves a bit faster wave[he][wi][intensity] = 300 + random.randint(0,600)
def pattern_2(): global p2_color, p2_delta, p2_direction, p2_length, p2_x, p2_y if p2_length == 0: p2_length = urandom.randint(int(led.length_x / 4), int(led.length_x * 3/4)) while True: # avoid 180 degree turns direction = urandom.randint(0, 3) if direction != (p2_direction + 2) % 4: p2_direction = direction break color = led.random_color() # smooth transition delta_red = (color[0] - p2_color[0]) / p2_length delta_green = (color[1] - p2_color[1]) / p2_length delta_blue = (color[2] - p2_color[2]) / p2_length p2_delta = (delta_red, delta_green, delta_blue) if p2_direction == 0: p2_x = (p2_x + 1) % led.length_x if p2_direction == 1: p2_y = (p2_y + 1) % led.length_x if p2_direction == 2: p2_x = (p2_x - 1) % led.length_x if p2_direction == 3: p2_y = (p2_y - 1) % led.length_x red = p2_color[0] + p2_delta[0] green = p2_color[1] + p2_delta[1] blue = p2_color[2] + p2_delta[2] p2_color = (red, green, blue) led.pixel_xy(p2_color, p2_x, p2_y, write=True) p2_length -= 1
def update(self): if self.active: # 障碍物离开屏幕,就得分,然后随机生成下一个障碍物 if self.obs.off_screen(): self.game.score += 1 self.type = randint(0, 3) self.obs = Obstacle(self.game, randint(80, 120), 114, self.type) else: self.obs.update()
def accgyromag_update(self): # accel = pyb.Accel() # acc_axis_x, acc_axis_y, acc_axis_z = accel.x(), accel.y(), accel.z() tick = utime.time() acc_axis_x, acc_axis_y, acc_axis_z = ( urandom.randint(0, 32767) % X_OFFSET, urandom.randint(0, 32767) % Y_OFFSET, urandom.randint(0, 32767) % Z_OFFSET) gyto_axis_x, gyto_axis_y, gyto_axis_z = ( urandom.randint(0, 32767) % X_OFFSET, urandom.randint(0, 32767) % Y_OFFSET, urandom.randint(0, 32767) % Z_OFFSET) mag_axis_x, mag_axis_y, mag_axis_z = ( urandom.randint(0, 32767) % X_OFFSET, urandom.randint(0, 32767) % Y_OFFSET, urandom.randint(0, 32767) % Z_OFFSET) buffer = ustruct.pack( "<HHHHHHHHHH", tick, acc_axis_x, acc_axis_y, acc_axis_z, gyto_axis_x, gyto_axis_y, gyto_axis_z, mag_axis_x, mag_axis_y, mag_axis_z) result = self.aci_gatt_update_char_value( serv_handle=self.hw_serv_handle, char_handle=self.acc_gyro_mag_bluest_char_handle, char_val_offset=0, char_value_len=len(buffer), char_value=buffer).response_struct if result.status != status.BLE_STATUS_SUCCESS: raise ValueError("aci_gatt_update_char_value status: {:02x}".format( result.status)) log.debug("aci_gatt_update_char_value %02x", result.status)
def sparksDemo(wait, iterate): for i in range(iterate): sparks_colorsList = [ RED, YELLOW, BLACK, ORANGE, GREEN, TEAL, BLACK, CYAN, BLACK, BLUE, PURPLE, MAGENTA, WHITE, BLACK ] pixel = random.randint(0, num_leds - 1) color = random.randint(0, len(colorsList) - 1) pixels[pixel] = sparks_colorsList[color] pixels.write() time.sleep(wait)
def whiteSparklesDemo(wait, iterate): for i in range(iterate): pixel = random.randint(0, num_leds - 1) colorbool = random.randint(0, 1) if colorbool == 1: color = WHITE else: color = BLACK pixels[pixel] = color pixels.write() time.sleep(wait)
def do_calibrate(t_channel, np): cycles = 48 cul = 0 for i in range(cycles): cul += t_channel.read() if i < LEDS: np[i] = (randint(0, 255), randint(0, 255), randint(0, 255)) np.write() utime.sleep_ms(10) utime.sleep_ms(800) # just so we can see all the LEDs lit up return int(cul / cycles)
def __init__(self, x, y, r): # 初始化位置及球体大小 self.x = x self.y = y self.r = r self.vx = randint(1, 4) self.vy = randint(1, 4) self.epslion = 0.99 self.old_x = x self.old_y = y self.color = randint(0, 16579836)
def sparklesDemo(color, wait,iterate): for i in range(iterate): pixel = random.randint(0, num_leds-1) colorbool = random.randint(0, 1) if colorbool == 1: colorNOW = color else: colorNOW = BLACK pixels[pixel] = colorNOW pixels.write() time.sleep(wait)
def sparksd(wait, itr): for i in range(itr): sparks_clrLst = [ RED, YELLOW, BLACK, ORANGE, GREEN, TEAL, BLACK, CYAN, BLACK, BLUE, PURPLE, MAGENTA, WHITE, BLACK ] pixel = random.randint(0, leds - 1) color = random.randint(0, len(clrLst) - 1) pi[pixel] = sparks_clrLst[color] pi.write() time.sleep(wait)
def sparklesD(clr, wait, itr): for i in range(itr): pixel = random.randint(0, leds - 1) clrbool = random.randint(0, 1) if clrbool == 1: clrNOW = clr else: clrNOW = BLACK pi[pixel] = clrNOW pi.write() time.sleep(wait)
def blinkrng(np, config, strip_number, strip_data, compressedOutput, solid=False): animation_data = strip_data["animations"][ strip_data["animation_index"]]["animation_data"] if not "fullCycle" in animation_data: animation_data["fullCycle"] = 0 if not "drawn" in animation_data or animation_data["drawn"] == False: if "quotient" in animation_data: del animation_data["quotient"] strip_data["previous"] = strip_data["animations"][ strip_data["animation_index"]] if "transitDone" in animation_data: del animation_data["transitDone"] if "transition_frame" in animation_data: del animation_data["transition_frame"] if "transition_frames_count" in animation_data: del animation_data["transition_frames_count"] if "transition_position" in animation_data: del animation_data["transition_position"] color = (0, 0, 0) animation_data["fullCycle"] = 0 color = [ random.randint(0, 8) * 8, random.randint(0, 8) * 8, random.randint(0, 8) * 8 ] color[random.randint(0, 2)] = random.randint(1, 2) * 127 color = tuple(color) animation_data["color"] = color animation_data["quotient"] = False animation_data["faded"] = True animation_data["fade_to_black"] = False if solid: solid_color(np, config, strip_number, strip_data, compressedOutput) else: blink(np, config, strip_number, strip_data, compressedOutput, solid=solid)
def led_disco(can, leds, delay_ms=500): max_led_id = len(leds) - 1 while True: led_id = randint(0, max_led_id) state = randint(0, 1) if state: leds[led_id].on() else: leds[led_id].off() can.send(state << 2 | led_id, 11, timeout=10000) delay(delay_ms)
def CreatRandomStr(length): # The limit for the extended ASCII Character set MAX_LENGTH = 16 random_string = '' if length > MAX_LENGTH: length = length % MAX_LENGTH if length == 0: length = random.randint(1, MAX_LENGTH) for _ in range(length): # 0 ~ z random_integer = random.randint(48, 122) # Keep appending random characters using chr(x) random_string += (chr(random_integer)) return random_string
def quiz(lang, marcador): if lang == "bribri": num = random.randint(1, len(num_bribri)) return (num, num_bribri[num]) if lang == "zapoteco": num = random.randint(1, len(num_zapoteco)) print(num_zapoteco[num]) respuesta = int(input("¿A que numero corresponde? ")) if respuesta == num: print("Numero correcto") marcador = marcador + 1 else: print("Numero incorrecto, el correcto es el numero ", num) marcador = marcador - 1 return marcador
def flash_random(wait, howmany): for _ in range(howmany): c = random.randint(0, len(colors) - 1) # Choose random color index j = random.randint(0, numpix - 1) # Choose random pixel strip[j] = colors[c] # Set pixel to color for i in range(1, 5): strip.brightness = i / 5.0 # Ramp up brightness time.sleep(wait) for i in range(5, 0, -1): strip.brightness = i / 5.0 # Ramp down brightness time.sleep(wait) strip[j] = [0, 0, 0] # Set pixel to 'off'
def nextrandomcolor(): global color # granularity = 1 --> [0 .. 255] * 1 --> 0,1,2,3 ... 255 # granularity = 10 --> [0 .. 25] * 10 --> 0,10,20,30 ... 250 # granularity = 100 --> [0 .. 2] * 100 --> 0,100, 200 (boaring...) random_red = random.randint(0, int(256 / curr_color_granularity)) random_red *= curr_color_granularity random_green = random.randint(0, int(256 / curr_color_granularity)) random_green *= curr_color_granularity random_blue = random.randint(0, int(256 / curr_color_granularity)) random_blue *= curr_color_granularity color = (random_red, random_green, random_blue)
def reset(self): # must be called when puppy is sitting down. self.left_leg_motor.reset_angle(0) self.right_leg_motor.reset_angle(0) # Pick a random number of time to pet the puppy. self.pet_target = urandom.randint(3, 6) # Pick a random number of time to feed the puppy. self.feed_target = urandom.randint(2, 4) # Pet count and feed count both start at 1 self.pet_count, self.feed_count = 1, 1 # Reset timers. self.pet_count_timer.reset() self.feed_count_timer.reset() self.count_changed_timer.reset() # Set initial behavior. self.behavior = self.idle
def _learn(self): index = randint(0, len(self._examples) - 1) example = self._examples.pop(index) is_success = self._mlp.QLearningLearnForChosenAction( example.state, example.reward, example.past_state, example.action) if not is_success: print("Failed to learn")
def party(): print("Big american party!") setTone(0) if t0.read() < thresh and t1.read() < thresh and t2.read( ) < thresh and t3.read() < thresh: while t0.read() < thresh: print("Move your fat fingers") while True: for i in range(10): choice = urandom.randint(0, 4) if choice == 0: np[i] = (255, 0, 0) if choice == 1: np[i] = (0, 255, 0) if choice == 2: np[i] = (0, 0, 255) if choice == 3: np[i] = (200, 200, 0) if choice == 4: np[i] = (0, 0, 0) #np[i] = (urandom.randint(0,255),urandom.randint(0,255),urandom.randint(0,255)) np.write() time.sleep(0.05) if t0.read() < thresh and t1.read() < thresh and t2.read( ) < thresh and t3.read() < thresh: while t0.read() < thresh: print("Move your fat fingers") keys()
def rnd_move(self): #make rundom distance move forward distance = random.randint(uPyBot.MOVE_MIN, uPyBot.MOVE_MAX) self.forward(distance, uPyBot.HIGH_SPEED) result = self.wait_until_move_done() if result != uPyBot.MOVE_OK: self.step_back() return result
def sensorsDisplay(): if ios["temp"]: temp = get_temp(*ts) tempDisplay(temp) light = randint(1, 10) # test displBar(light)
def __init__(self, x, size): self.size = size self.gap = urandom.randint(6 + self.size, HEIGHT - 6 - self.size) # 随机生成间隙大小 self.x = x # 距离鸟大小 self.score = 0 # 分数 self.rate = 3 # 速率
def render(self): lcd.setColor(0) for rect in self.roundRect[self.type]: lcd.fillRoundRect(self.old_x + rect[0], self.y + rect[1], rect[2], rect[3], rect[4]) lcd.setColor(0x999999) if not self.off_screen(): for rect in self.roundRect[self.type]: lcd.fillRoundRect(self.x + rect[0], self.y + rect[1], rect[2], rect[3], rect[4]) else: # 重新生成一片云 self.type = randint(0, 2) self.x = randint(80, 100) self.y = randint(30, 60) self.vx = randint(1, 3)
def loop_swirl(itr, spd): for i in range(itr): backc = random.randint(0,1) rc = random.randint(0,2) for y in range(8): for led in range(num_leds): pi[led] = swirl_colour(y, led, rc) pi.show() time.sleep(spd) if backc==1: for y in range(8,-2,-2): for led in range(num_leds): pi[led] = swirl_colour(y, led, rc) pi.show() time.sleep(spd)
def main(): clr = color.WHITE print("main") leds.set_all([clr,clr,clr,clr,clr,clr,clr,clr,clr,clr,clr]) switch = False bpm = 120 sleep_t = sleepIntervalFromBPM(bpm) print(str(sleep_t) + " ms") disp = display.open() disp.clear() while True: if(switch): leds.dim_top(0) switch = False else: leds.dim_top(8) switch=True pressed = buttons.read(buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT | buttons.TOP_RIGHT) if pressed & buttons.BOTTOM_LEFT != 0: bpm -= 10 if pressed & buttons.BOTTOM_RIGHT != 0: bpm += 10 if pressed & buttons.TOP_RIGHT != 0: r = urandom.randint(0,255) g = urandom.randint(0,255) b = urandom.randint(0,255) clr = color.Color(r,g,b) leds.dim_top(8) setRandomLEDColor(clr) switch = False bpm_str = str(bpm) + " BPM" sleep_t = sleepIntervalFromBPM(bpm) xOffset = int(round((len(bpm_str) * 20) / 2)) if(switch): disp.clear([clr.red,clr.green,clr.blue]) disp.print(bpm_str, fg=color.BLACK, bg= clr, posx=90-xOffset, posy = 40 - 20) else: disp.clear([0,0,0]) disp.print(bpm_str, fg=color.WHITE, bg= color.BLACK, posx=90-xOffset, posy = 40 - 20) disp.update() utime.sleep_ms(int(round(sleep_t)))
def doRandom(self): newrgb = lerpRGB(self.begin_rgb, self.end_rgb, self.timer.getProgress()) self.setColourAll(newrgb) if self.timer.hasExpired(): self.begin_rgb = self.end_rgb self.end_rgb = colours[urandom.randint(0, 6)] self.timer.reset(5000)
# zero step try: random.randrange(2, 1, 0) except ValueError: print('ValueError') # empty range try: random.randrange(2, 1, 1) except ValueError: print('ValueError') print('randint') for i in range(50): assert 0 <= random.randint(0, 4) <= 4 assert 2 <= random.randint(2, 6) <= 6 assert -2 <= random.randint(-2, 2) <= 2 # empty range try: random.randint(2, 1) except ValueError: print('ValueError') print('choice') lst = [1, 2, 5, 6] for i in range(50): assert random.choice(lst) in lst # empty sequence