def create_row_drops(ai_settings, screen, drops, row_number): drop = Drop(ai_settings, screen) number_drops_x = get_number_drops(ai_settings, drop.rect.width) for drop_number in range(number_drops_x): drop = Drop(ai_settings, screen) drop_width = drop.rect.width drop.x = drop_width + 2 * drop_width * drop_number drop.rect.x = drop.x drop.rect.y = 0 drops.add(drop)
def _create_steady_rain(self, row_number, drop_number): rain = Drop(self) rain_width, rain_height = rain.rect.size rain.x = rain_width * drop_number rain.rect.x = rain.x rain.rect.y = rain_height * row_number self.drops.add(rain)
def create_one(my_settings, screen, drops): #创建一行雨滴 drop = Drop(my_settings, screen) drop_width = drop.rect.width number_drops_x = get_number_drops_x(my_settings, drop_width) for drop_number in range(number_drops_x): create_one_drop(my_settings, screen, drops, drop_number)
def run_game(): """Run raindrop game.""" # Initialize game pygame.init() # Create window screen = pygame.display.set_mode((1200, 800)) # Add caption to window pygame.display.set_caption('Raindrop Game') # Create collection of Drop sprites drops = Group() # Create a single drop drop = Drop(screen) drop_width = drop.rect.width drop_height = drop.rect.height # Available room for drops width wise available_space_x = 1200 - (drop_width * 2) # How many stars can fit width-wise on screen number_drops = int(available_space_x / (2 * drop_width)) # Available space for rows of stars available_space_y = (800 - drop_height) number_rows = int(available_space_y / (2 * drop_height)) gf.create_raindrops(screen, number_rows, number_drops, drops) # Game loop while True: gf.check_events() gf.rain_drops(screen, drops) gf.update_screen(screen, drops)
def create_drop(ai_settings, screen, drops, drop_number): drop = Drop(ai_settings, screen) drop_width = drop.rect.width randomx = randint(-drop_width, drop_width) drop.x = drop_width + 2 * drop_width * drop_number + randomx drop.rect.top = -2 * drop.rect.height drop.rect.x = drop.x drops.add(drop)
def create_drop(d_settings, screen, drops, drop_number, row_number): """Создаёт каплю и размещает её в ряду.""" drop = Drop(d_settings, screen) drop_width = drop.rect.width drop.x = drop_width + 2 * drop_width * drop_number drop.rect.x = drop.x drop.rect.y = drop.rect.height + 2 * drop.rect.height * row_number drops.add(drop)
def create_drop(ai_settings, screen, drops, drop_number, row_number): """Создание капли и размещение её в ряду""" drop = Drop(ai_settings, screen) drop_width = drop.rect.width drop.x = drop_width + 2 * drop_width * drop_number drop.rect.x = drop.x drop.rect.y = drop.rect.height + 2 * drop.rect.height * row_number drops.add(drop)
def set(self, key, value, expiry=0, parent_key=None): print 'inside of set function' d = Drop(key=key) d.set(value, expiry) if parent_key: d.parent_key = parent_key self.reservoir[key] = d self.add_to_replication_replay_logs('SET', d) return True
def create_rain(ai_settings, screen, drops): """Создаёт дождь капель""" # Создание капли и вычисление количества капель в ряду drop = Drop(ai_settings, screen) number_drops_x = get_number_drops(ai_settings, drop.rect.width) number_rows = get_number_rows(ai_settings, drop.rect.height) # Создание капель for row_number in range(number_rows): for drop_number in range(number_drops_x): create_drop(ai_settings, screen, drops, drop_number, row_number)
def create_grid(rd_settings, screen, drops): """Create a full grid of drops.""" drop = Drop(rd_settings, screen) number_drops_x = get_number_drops_x(rd_settings, drop.rect.width) number_rows = get_number_rows(rd_settings, drop.rect.height) # Create the grid of drops. for row_number in range(number_rows): for drop_number in range(number_drops_x): create_drop(rd_settings, screen, drops, drop_number, row_number)
def create_raindrops(screen, number_rows, number_drops, drops): """Generate all raindrops.""" for row in range(number_rows): for drop_num in range(number_drops): # Create new instance of Drop drop = Drop(screen) drop_width = drop.rect.width drop.x = drop_width + (2 * drop_width * drop_num) drop.rect.x = drop.x drop.rect.y = drop.rect.height + (2 * drop.rect.height * row) drops.add(drop)
def create_drops(my_settings, screen, drops): #创建雨滴,等于先创建一个个实例 drop = Drop(my_settings, screen) drop_width = drop.rect.width drop_height = drop.rect.height number_drops_x = get_number_drops_x(my_settings, drop_width) number_drops_y = get_number_drops_y(my_settings, drop_height) for drop_rows in range(number_drops_y): for drop_number in range(number_drops_x): create_drop(my_settings, screen, drops, drop_number, drop_rows)
def create_fleet(d_settings, screen, drops): """Создаёт ряд капель.""" # Создание каплю и вычисление количества капель в ряду. drop = Drop(d_settings, screen) number_drops_x = get_number_drops_x(d_settings, drop.rect.width) number_rows = get_number_rows(d_settings, drop.rect.height) # Создание первого ряда капель. for row_number in range(number_rows): for drop_number in range(number_drops_x): # Создание капли и размещение ёё в ряду. create_drop(d_settings, screen, drops, drop_number, row_number)
def _create_rain(self): rain = Drop(self) rain_width, rain_height = rain.rect.size avalible_space_x = self.screen_width number_drop = avalible_space_x // (rain_width) avalible_space_y = self.screen_height number_rows = avalible_space_y // (rain_height) for row_number in range(number_rows): for drop_number in range(number_drop): self._create_steady_rain(row_number, drop_number)
def create_drop(rd_settings, screen, drops, drop_number, row_number): """ Create a drop, then set its position in a grid based upon drop # and row # parameters. Then, add the drop to the drop-group. """ drop = Drop(rd_settings, screen) drop_width = drop.rect.width drop_height = drop.rect.height # Calculate drop positions drop.rect.x = drop_width + 2 * drop_width * drop_number drop.rect.y = drop_height + 2 * drop_height * row_number drops.add(drop)
def run_rain(): """Initialize rain simulator, settings and loop""" pygame.init() ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption('Rain Simulation') drops = [Drop(ai_settings, screen)] while True: sf.check_events() pygame.mouse.set_visible(False) sf.update_fleet(ai_settings, screen, drops) sf.update_screen(ai_settings, screen, drops)
def update_edge_drops(d_settings, screen, drops, deleted_storage_drops): for drop in drops: if drop.rect.top >= 800: deleted_storage_drops.append(drop) drops.remove(drop) number_drops_x = get_number_drops_x(d_settings, drop.rect.width) if len(deleted_storage_drops) == number_drops_x: for drop_number in range(number_drops_x): # Создание капли и размещение ёё в ряду. drop = Drop(d_settings, screen) drop_width = drop.rect.width drop.x = drop_width + 2 * drop_width * drop_number drop.rect.x = drop.x drops.add(drop) del deleted_storage_drops[:] print(len(drops))
def get_operation_for_table(cnx, table_name): """ Valid operations: - truncate - erode_by_ - drop """ target = [] # The highest precedence erodes = Erode.get_all(cnx, table_name) for erode_operation in erodes: target.append(erode_operation) if Truncate.is_required(cnx, table_name): target.append(Truncate(cnx=cnx, table_name=table_name)) # The lowest precedence if Drop.is_required(cnx, table_name): target.append(Drop(cnx=cnx, table_name=table_name)) if Noop.is_required(cnx, table_name): target.append(Noop(cnx=cnx, table_name=table_name)) # This is costly to calculate, so we only do it if there is no previous operations if len(target) == 0: ops = get_parent_ops(cnx, table_name) for parent_operation in ops: target.append(parent_operation) # We can reduce all the operations to a subset of only the required. e.g. Drop + Truncate = Drop if len(target): target = reduce(add_operations, target) if not isinstance(target, tuple): target = (target, ) # We can make this a FailOperation if len(target) == 0: raise OperationError( "Error at: {}. Every table must have at least one operation". format(table_name)) return target
def run(): pygame.init() ai_settings = Settings() screen = pygame.display.set_mode((ai_settings.width, ai_settings.height)) pygame.display.set_caption("Purple Rain") drops = Group() for i in range(501): new_drop = Drop(screen, ai_settings) drops.add(new_drop) while True: for event in pygame.event.get(): ai_settings.check_events(event) screen.fill(ai_settings.bg_color) for d in drops: d.show() d.update() pygame.display.flip()
from umbrella import Umbrella pygame.init() win = pygame.display.set_mode((1200,600)) clock = pygame.time.Clock() fps = 60 RED = (20,0,0) NUM_OF_DROPS = 1200 pygame.display.set_caption('I only want to see you bathing in the purple rain') all_drops = [] for i in range(NUM_OF_DROPS): all_drops.append(Drop()) umb = Umbrella([i//2 for i in pygame.display.get_surface().get_size()]) show_umb = False run = True while run: clock.tick(fps) for i in range(NUM_OF_DROPS): all_drops[i].fall() all_drops[i].show(win) all_drops[i].check_umb(umb)
def create_one_drop(my_settings, screen, drops, drop_number): drop = Drop(my_settings, screen) drop.rect.x = 2 * drop.rect.width * drop_number drop.rect.y = 0 drops.add(drop)
def create_drop(my_settings, screen, drops, drop_number, drop_rows): drop = Drop(my_settings, screen) drop.rect.x = 2 * drop.rect.width * drop_number drop.y = drop.rect.height * drop_rows drop.rect.y = drop.y drops.add(drop)
def main(): """ Main: Displays Results of Q-Learning """ #Set max distance stop = 10000 play = True #Load trained Q-table q_table = loadtxt('q_table_final.csv', delimiter=',') while play: #Initialize pygame and window pygame.init() win = pygame.display.set_mode((500, 500)) pygame.display.set_caption('Make It Rain') #Set variables of man x = 210 y = 450 width = 30 height = 30 vel = 5 state_list = [] #Set variable of drops vel_rain = 1 drops = [] rate = 70 #Set loop variables run = True count = 0 reward = 0 while run: #Check if window was closed for event in pygame.event.get(): if event.type == pygame.QUIT: run = False play = False #Generate new drop depending of the set rate if count % rate == 0: size = random.randint(5, 20) new_drop = Drop(random.randint(0, 490), 0, size, size, vel_rain) drops.append(new_drop) count += 1 else: count += 1 #View enviroment & choose best action based on current view state vision = view(x, y, width, drops) action = np.argmax(q_table[vision]) #Take action if with bounderies if action == 0 and x > 0: x -= vel if action == 1 and x < 460: x += vel #Code for human play """ pygame.time.delay(5) keys = pygame.key.get_pressed() if keys[pygame.K_LEFT] and x > 0: x -= vel if keys[pygame.K_RIGHT] and x <470: x += vel if keys[pygame.K_UP] and y > 0: y -= vel if keys[pygame.K_DOWN] and y < 460: y += vel """ #Check for collision between man and rain drops for drop in drops: drop.fall() if (x + width >= drop.get_x() and x <= drop.get_x() + drop.get_width()) and ( y + height >= drop.get_y() and y <= drop.get_y() + drop.get_height()): for drop_stop in drops: drop_stop.set_velocity(0) game_on = False vel = 0 run = False break #Clear window. Redraw man. Redraw drops. Update display win.fill((0, 0, 0)) pygame.draw.rect(win, (255, 255, 255), (x, y, width, height)) for drop in drops: pygame.draw.rect(win, (0, 0, 255), (drop.get_x(), drop.get_y(), drop.get_width(), drop.get_height())) pygame.display.update() #Increment Reward reward += 1 #Stop game if it has reached finish if reward >= stop: run = False #Quit game and print reward print('You scored: ' + str(reward)) pygame.quit()
def _simulate_single_step(state): """Run a single step of the simulation on `state`, returning the next state.""" # Recover the important parts of our state. geo = state['geo'] points = state['points'] steps_completed = state['steps_completed'] settings = state['settings'] PLANE_SHAPE = settings['PLANE_SHAPE'] DROP_RADIUS = settings['DROP_RADIUS'] STEM_RADIUS = settings['STEM_RADIUS'] STEM_STICK_PROBABILITY = settings['STEM_STICK_PROBABILITY'] GROUND_STICK_PROBABILITY = settings['GROUND_STICK_PROBABILITY'] OLD_GENOME_BIAS = settings['OLD_GENOME_BIAS'] MELT_INTERVAL = settings['MELT_INTERVAL'] INTERACTIVE_MODE = settings['INTERACTIVE_MODE'] PERIODIC_BOUNDARY = settings['PERIODIC_BOUNDARY'] BOUNCE_DISTANCE = settings['BOUNCE_DISTANCE'] # We're constantly removing and adding to our kdtree, so rebalance it every # so often for efficiency. if len(points ) != 0 and steps_completed % 600 == 0 and geo.data is not None: geo = geo.rebalance() # In interactive fast mode we draw the state every so often. if settings['INTERACTIVE_FAST_MODE'] and len(points) != 0: if steps_completed % settings['INTERACTIVE_FAST_INTERVAL'] == 0: visualize_state(state) # Create a new drop in the plane. drop_coord = random_coord(PLANE_SHAPE) drop_artist = None if settings['INTERACTIVE_MODE']: drop_artist = visualize_drop_active(drop_coord, settings) # The drop can keep bouncing as long as it intersects a stem # and hasn't stuck yet. The bounce probability is determined # by bounce_probability(bounce_count). drop_stuck = False bounce_count = 0 while not drop_stuck: # Check intersection with any stem. drop_stem_intersect = False # Search the tree for any drop intersections. intersections = geo.search_nn_dist(drop_coord, (DROP_RADIUS + STEM_RADIUS) * (DROP_RADIUS + STEM_RADIUS)) if settings['PERIODIC_BOUNDARY']: phantom_drops = [(drop_coord[0] + 2, drop_coord[1]), (drop_coord[0] - 2, drop_coord[1]), (drop_coord[0], drop_coord[1] + 2), (drop_coord[0], drop_coord[1] - 2), (drop_coord[0] + 2, drop_coord[1] + 2), (drop_coord[0] + 2, drop_coord[1] - 2), (drop_coord[0] - 2, drop_coord[1] + 2), (drop_coord[0] - 2, drop_coord[1] - 2)] for phantom_drop in phantom_drops: intersections.extend( geo.search_nn_dist(phantom_drop, (DROP_RADIUS + STEM_RADIUS) * (DROP_RADIUS + STEM_RADIUS))) if intersections: # The drop has intersected with some other drops that are already there. # Find the drop that is the highest up. highest_point_height = None highest_point = None highest_point_id = None for node in intersections: point_id = node.ident point = points[point_id] assert node == point['coord'] intersection_height = point['height'] if highest_point_height is None or intersection_height > highest_point_height: highest_point_height = intersection_height highest_point = point highest_point_id = point_id drop_stem_intersect = True # Check if the drop bounces. if drop_stem_intersect and random_real() <= bounce_probability( bounce_count): assert highest_point is not None if settings['INTERACTIVE_MODE']: unvisualize_drop(drop_artist) # The drop bounces. bounce_count += 1 bounce_angle = random_theta() bounce_offset = polar_to_cartesian(BOUNCE_DISTANCE, bounce_angle) # For periodic boundary conditions we roll over from the edges of the boundary. if PERIODIC_BOUNDARY: assert PLANE_SHAPE == SQUARE drop_coord = [ highest_point['coord'][0] + bounce_offset[0], highest_point['coord'][1] + bounce_offset[1] ] if drop_coord[0] > 1: drop_coord[0] -= 2 if drop_coord[1] > 1: drop_coord[1] -= 2 if drop_coord[0] < -1: drop_coord[0] += 2 if drop_coord[1] < -1: drop_coord[1] += 2 drop_coord = tuple(drop_coord) else: drop_coord = (highest_point['coord'][0] + bounce_offset[0], highest_point['coord'][1] + bounce_offset[1]) if INTERACTIVE_MODE: drop_artist = visualize_drop_bounce(drop_coord) else: # The drop does not bounce. drop_stuck = True if drop_stem_intersect: # The drop has landed on top of an existing stem. Replace # the top of the stem with the new drop. if random_real() <= STEM_STICK_PROBABILITY: if INTERACTIVE_MODE: unvisualize_drop(drop_artist) drop_artist = visualize_drop(drop_coord) unvisualize_drop(highest_point['artist']) geo = geo.remove(highest_point['coord']) assert drop_coord is not None new_coord = ((OLD_GENOME_BIAS * highest_point['coord'][0] + drop_coord[0]) / (1 + OLD_GENOME_BIAS), (OLD_GENOME_BIAS * highest_point['coord'][1] + drop_coord[1]) / (1 + OLD_GENOME_BIAS)) geo.add(Drop(new_coord, ident=steps_completed)) new_height = highest_point['height'] + 1 + settings[ 'BOUNCE_HEIGHT_ADDITION'] points[steps_completed] = { 'height': new_height, 'artist': drop_artist, 'coord': new_coord } else: # The drop has landed outside of any stem. Simply add # it as a new stem. if random_real() <= GROUND_STICK_PROBABILITY: # Create a new stem for this drop. if INTERACTIVE_MODE: unvisualize_drop(drop_artist) drop_artist = visualize_drop(drop_coord) assert drop_coord is not None geo.add(Drop(drop_coord, ident=steps_completed)) points[steps_completed] = { 'height': 0, 'artist': drop_artist, 'coord': drop_coord } elif INTERACTIVE_MODE: unvisualize_drop(drop_artist) new_state = { 'points': points, 'geo': geo, 'steps_completed': steps_completed, 'settings': settings } if steps_completed % MELT_INTERVAL == 0: new_state = melt(new_state) return { 'points': new_state['points'], 'geo': new_state['geo'], 'steps_completed': new_state['steps_completed'] + 1, 'settings': settings }
def create_drops(ai_settings, screen, drops): drop = Drop(ai_settings, screen) number_drops_x = get_number_drops_x(ai_settings, drop.rect.width) #print(str(number_drops_x)) for drop_number in range(number_drops_x): create_drop(ai_settings, screen, drops, drop_number)
def spawnDrop(self): if randint(1, 2500) == 1: randx = randint(10, WIDTH - 30) self.dropList.append(Drop(20, 20, 0, 5, self.create_image(randx, MENUBARSIZE, image=self.drop_img, anchor="nw", tag="drop")))
def learn(): #Initialise learning variables & Q-table learning_rate = 0.1 DISCOUNT = 0.9 EPISODES = 75 q_table = np.random.uniform(low = 4000, high = 5000, size =(64, 3)) #Set max distance & reward sum, used to find average reward stop = 5000 reward_sum = 0 for i in range(EPISODES): #Remove display for training, initialise pygame os.environ['SDL_VIDEODRIVER'] = 'dummy' pygame.init() win = pygame.display.set_mode((500,500)) pygame.display.set_caption('Make It Rain') #Initialise variables of man x = 210 y = 450 width = 30 height = 30 vel = 5 #Initialise variables of drops rate = 100 vel_rain = 1 count = 0 drops = [] #Q-learning loop variables reward = 0 state_list = [] run = True while run: #Check to see if window has been closed for event in pygame.event.get(): if event.type == pygame.QUIT: run = False #Generate rain drops if count%rate == 0: size = random.randint(5,20) new_drop = Drop(random.randint(0,490), 0, size, size, vel_rain) drops.append(new_drop) count += 1 if rate > 75: rate -= 1 else: count += 1 #View enviroment & choose best action based on current view state & Q-table values vision = view(x,y,width,drops) action = np.argmax(q_table[vision]) #Set list of states ordered by the last time they were present if vision not in state_list: state_list.insert(0,vision) else: state_list.remove(vision) state_list.insert(0,vision) #Take action if action == 0 and x > 0: x -= vel if action == 1 and x <460: x += vel #Code for human play """ pygame.time.delay(5) keys = pygame.key.get_pressed() if keys[pygame.K_LEFT] and x > 0: x -= vel if keys[pygame.K_RIGHT] and x <470: x += vel if keys[pygame.K_UP] and y > 0: y -= vel if keys[pygame.K_DOWN] and y < 460: y += vel """ #Check for collision between man and rain drops for drop in drops: drop.fall() if (x + width >= drop.get_x() and x <= drop.get_x() + drop.get_width()) and (y + height >= drop.get_y() and y <= drop.get_y() + drop.get_height()): for drop_stop in drops: drop_stop.set_velocity(0) vel = 0 run = False break #Clear window. Redraw man. Redraw drops. Update display win.fill((0,0,0)) pygame.draw.rect(win, (255, 255, 255), (x,y,width,height)) for drop in drops: pygame.draw.rect(win, (0, 0, 255), (drop.get_x(),drop.get_y(),drop.get_width(),drop.get_height())) pygame.display.update() #Increment reward reward +=1 #Stop game if end has been reached if reward >= stop: run = False #pygame.quit() #discount variable this_discount = 1 #Loop through states of previous game, update q-table based on reward of previous game for state in state_list: q_table[state][np.argmax(q_table[state])] = (1-(learning_rate*this_discount))*(q_table[state][np.argmax(q_table[state])])+((learning_rate*this_discount)*reward) #Decrement discount this_discount = this_discount*DISCOUNT #Change learning rate based on average reward over 50 games if i%50==49: print("Average reward: " + str(reward_sum/50)) if (reward_sum/50)>2500 and (reward_sum/50) <= 3250: learning_rate = 0.08 elif (reward_sum/50)>3250 and (reward_sum/50) <= 4000: learning_rate = 0.04 elif (reward_sum/50)>4000 and (reward_sum/50) <= 4500: learning_rate = 0.03 elif (reward_sum/50) > 4500: learning_rate = 0.02 stop += 1000 elif (reward_sum/50) < 2500 and (reward_sum/50) >= 2000: learning_rate = 0.1 elif (reward_sum/50) < 2000 and (reward_sum/50) >= 1500: learning_rate = 0.15 elif (reward_sum/50) < 1500: learning_rate = 0.2 print("New learning rate: " + str(learning_rate)) reward_sum = 0 reward_sum+=reward else: reward_sum += reward #Export Q-table to CSV file q_table_final = asarray(q_table) savetxt('q_table_new.csv', q_table_final, delimiter=',')
def init_drop_objects(screenWidth, number=300): drops = [] for i in range(number): newDrop = Drop(screenWidth) drops.append(newDrop) return drops
import pygame, sys, random from drop import Drop pygame.init() size = width, height = 1024, 360 background = 0, 0, 0 screen = pygame.display.set_mode(size) droplets = [] for i in range(500): x = random.randint(0, width) y = random.randint(50, 200) * -1 grav = random.uniform(2, 3) scale = random.uniform(1, 4) droplets.append(Drop(i, x, y, grav, scale, screen)) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() screen.fill(background) for obj in droplets: obj.fall() obj.splash() pygame.display.update()
def drop_balls(self): for x in range(self.__num_of_balls): new_drop = Drop(x, self.__levels) new_drop.start_drop() self.__ball_drop.append(new_drop)