def ball(canvas): def clamp_bounds(solid): rect = ball.rect if solid.name == 'right': rect.right = solid.rect.left if solid.name == 'left': rect.left = solid.rect.right if solid.name == 'bottom': rect.bottom = solid.rect.top if solid.name == 'top': rect.top = solid.rect.bottom ball.rect = rect def check_collisions(): solid = collidesp(ball.rect) w, h = BALL_SIZE if solid: fire_hooks('play:sfx', 'hit.wav') increase_score() clamp_bounds(solid) reverse(x=(solid.name in ['left', 'right']) ,y=(solid.name in ['top', 'bottom'])) def outta_screen_p(): return ball.rect.left < 0 or ball.rect.right > canvas.rect.right \ or ball.rect.top < 0 or ball.rect.bottom > canvas.bottom def input_handler(): move() check_collisions() if outta_screen_p(): fire_hooks('game:over') return GAME_RUNNING def move(): pos[0] += speed['x'] pos[1] += speed['y'] ball.move(*pos) def init_ball(ball): ball.resize(*BALL_SIZE) ball.move(*canvas.rect.center) ball.show() animator_add(input_handler) return ball def reverse(x=False, y=False): w, h = BALL_SIZE if x: speed['x'] = max(min(speed['x'], w/2), -w/2) * -1.1 if y: speed['y'] = max(min(speed['y'], h/2), -h/2) * -1.1 ball = init_ball(canvas.Rectangle(color=(171, 180, 161, 200))) speed = {'x': randf(1, 2), 'y': randf(1, 2)} pos = list(ball.pos) return ball
def CpuGuarantee(): util = psutil.cpu_percent() cpunr = psutil.cpu_count() return ("cpu_guarantee", select_by_weight([(12, select_equal([ str(randint(0, int(util * cpunr))), str(randf() * util) + "c" ])), (1, select_equal(["0", "0c"])), (2, select_equal([ str(randint(int(util * cpunr), sys.maxint)), str(randf() * randint(1, sys.maxint)) ]))]))
def CpuLimit(): cpunr = psutil.cpu_count() return ("cpu_limit", select_by_weight([ (12, select_by_weight([(1, str(randint(0, cpunr * 100))), (1, str(randf()) + "c")])), (1, select_equal(["0", "0c"])), (2, select_by_weight([ (1, str(randint(cpunr * 100 + 1, sys.maxint))), (1, str(randf() * randint(1, sys.maxint)) + "c") ])) ]))
def CpuLimit(): cpunr = psutil.cpu_count() return ("cpu_limit", select_by_weight( [ (12, select_by_weight( [ (1, str(randint(0, cpunr * 100))), (1, str(randf()) + "c") ] ) ), (1, select_equal(["0", "0c"])), (2, select_by_weight( [ (1, str(randint(cpunr * 100 + 1, sys.maxint))), (1, str(randf() * randint(1, sys.maxint)) + "c") ] ) ) ] ) )
def CpuGuarantee(): util = psutil.cpu_percent() cpunr = psutil.cpu_count() return ("cpu_guarantee", select_by_weight( [ (12, select_equal( [ str( randint(0, int(util * cpunr)) ), str( randf() * util ) + "c" ] ) ), (1, select_equal(["0", "0c"])), (2, select_equal( [ str( randint(int(util * cpunr), sys.maxint) ), str( randf() * randint(1, sys.maxint) ) ] ) ) ] ) )
def create_random_buffer(max_width, max_height): scale = randf() w = int(max(max_width * scale, 1)) h = int(max(max_height * scale, 1)) arr = [] red, green, blue = randf(), randf(), randf() for y in range(h): arr.append([]) for x in range(w): if randf() < 0.1: arr[y].append([red * 255.0, green * 255.0, blue * 255.0, 255]) else: arr[y].append([255, 255, 255, 0]) return Buffer.from_array(np.array(arr, dtype=np.uint8), buffer_type='i', drop_last_dim=True)
def update(self): # Update existing particles. for i, particle in self.particles.each(): particle.update() if particle.age >= particle.life: self.particles.kill(i) # Create new particles. for _ in range(3): angle = randf() * math.tau speed = randf() * 3 self.particles.insert( Particle( 64, 32, math.cos(angle) * speed, math.sin(angle) * speed, rand(10, 30), ))
def increment_score(self): points = int(250 * (2**(self.difficulty * 3))) state.score += points if state.score > state.high_score: state.has_set_high_score = True state.high_score = state.score self.score_particles.insert( TextParticle(W // 2 + 24, H // 2, f"{points} pts")) for _ in range(30): ang = randf() * math.tau ax = math.cos(ang) ay = math.sin(ang) v = 8.0 + randf() * 2.0 self.particles.insert( Particle(W // 2 + ax * v, H // 2 + ay * v, ax * v * 0.1, ay * v * 0.1)) self.cam_punch = 5 self.rotation_speed = -sgnz(self.rotation_speed) * ( 0.001 + randf() * self.difficulty * 0.05)
import random print (random.randint(0,9)) print (random.randf(0,15.0))
def update(self): self.difficulty = self.difficulty_timer / self.difficulty_duration if self.spawn_timer <= 0: self.spawn_timer = lerpi(self.start_spawn_interval, self.end_spawn_interval, self.difficulty) self.spawn_shape() else: self.spawn_timer -= 1 if self.difficulty_timer < self.difficulty_duration: self.difficulty_timer += 1 if self.cam_punch > 0: state.cam_x = -self.cam_punch + randf() * self.cam_punch * 2 state.cam_y = -self.cam_punch + randf() * self.cam_punch * 2 self.cam_punch -= 1 else: state.cam_x = 0 state.cam_y = 0 self.cam_punch = 0 score_diff = state.score - self.display_score self.display_score += math.ceil(score_diff / 8) player_seg = int(self.segments * self.player_ang / math.tau) scored = False died = False for i, shape in self.shapes.each(): if shape.depth <= shape.thickness: self.shapes.kill(i) scored = True elif shape.depth <= shape.thickness * 2: if player_seg == shape.segment: died = True shape.colour = 1 shape.depth -= self.speed if died: pyxel.stop() pyxel.play(3, 1) if state.scoreboard.check_highscores(state.score): state.has_set_high_score = True state.game.set_state(STATE_DEAD) return elif scored: self.increment_score() pyxel.play(3, 0) self.rotation += self.rotation_speed if self.rotation > math.tau: self.rotation -= math.tau right = sgn(btni(pyxel.KEY_RIGHT) + btni(pyxel.GAMEPAD_1_RIGHT)) left = sgn(btni(pyxel.KEY_LEFT) + btni(pyxel.GAMEPAD_1_LEFT)) self.player_ang = wrap_ang(self.player_ang + (right - left) * 0.1) for i, sp in self.score_particles.each(): sp.update() if sp.age >= sp.life: self.score_particles.kill(i) for i, p in self.particles.each(): p.update() if p.age >= p.life: self.particles.kill(i)
print("now sending data...") while True: # make a new random 8-channel sample; this is converted into a # pylsl.vectorf (the data type that is expected by push_sample) # three samples for Heart monitor: # 1. HR beats per minute (50-140 bpm) # 2. r-r interval in milliseconds (7 ms/beat - 20 ms/beat) # 3. heart rate variability (HRV, measures as std. dev., or pNN50, not sure of range) *** important *** # Muse EEG amy channels # 1. concentration score (normalized, 0.0 - 1.0) *** important *** # 2. mellow score (normalized, 0.0 - 1.0) # ... two more aggregate scores and a number of individual channels mysample = [randf(50.0, 140.0), randf(7.0, 20.0), rand()] muse_sample = [ rand(), rand(), rand(), rand(), rand(), rand(), rand(), rand(), rand(), rand(), rand(), rand(), rand(),
def __next__(self): i = randint(0, len(self.prob) - 1) return i if randf() < self.prob[i] else self.alias[i]
# Specify that the return type is an array of double with length # equal to the number of processes launched minus one. The rank0 # process just does communication, not processing. processArray.restype = ctypes.POINTER(ctypes.c_double * (nProcesses - 1)) print("[PYTHON] Creating matrices") # Generate a matrix for each rank except for # zero. Random floats between -10 and 10 are # chosen so tha precision is not lost on the # final sum value returned by the MPI processes. matrices = [] for i in range(nProcesses - 1): mat = [] for j in range(WIDTH * ROWS): mat.append(randf(-10.0, 10.0)) matrices.append(np.array(mat, np.float64)) print("[PYTHON] Sending matrices") # Send all of the matrices. # The primary_slave program will automatically # assign them to MPI processes. for i in matrices: sendMatrix(i) inputs = [] print("[PYTHON] Generating input arrays") # Generate a list of arrays to pass as inputs