def game_loop(): # put the fps meter into this textbox globs.fpsbox = TextBox("IBMPlexSans-Regular.ttf", size=30, color=Vec(0, 0, 0), pos=Vec(5, 5, 0.9)) globs.textboxes.append(globs.fpsbox) dt_count, dt_sum = 0, 0 # tally up the dt's dt = 1 # first value of dt while True: # render, and compute how much time it took t0 = glfw.get_time() listen.dispatch("on_frame", dt) listen.dispatch("on_render") dt_render = glfw.get_time() - t0 # wait a bit to match the fps rate if needed _, dt_wait = yield from listen.wait(max(globs.spf - dt_render, 0)) dt = dt_wait + dt_render # put the average fps into the textbox dt_count += 1 dt_sum += dt if dt_sum >= 0.5: # every half second globs.fpsbox.text = str(int(dt_count / dt_sum * 10) / 10) + "fps" dt_count, dt_sum = 0, 0
def next_level_loop(): while True: yield from listen.event("next_level") globs.play_disabled = True yield from listen.wait(0.1) if globs.level_idx + 1 < len(globs.levels): globs.selected_color = 0 globs.level_idx += 1 print("Loading level", globs.level_idx + 1, "of", len(globs.levels)) polys = globs.levels[globs.level_idx]() idxs = list(range(len(polys))) np.random.shuffle(idxs) globs.polygons = [] for i in idxs: yield from listen.wait(2 / len(polys)) globs.polygons.append(polys[i]) globs.polygons = polys # to reset the order globs.play_disabled = False listen.dispatch("update_hud") else: globs.level_idx = -1 listen.dispatch("ending_screen")
def game_loop(): dt = 1 while True: # render, and compute how much time it took t0 = glfw.get_time() listen.dispatch("on_frame", dt) listen.dispatch("on_render") dt_render = glfw.get_time() - t0 # wait a bit to match the fps rate if needed _, dt_wait = yield from listen.wait(max(globs.spf - dt_render, 0)) dt = dt_wait + dt_render
def reset_level_loop(): while True: yield from listen.event("reset_level") globs.play_disabled = True polys = globs.levels[globs.level_idx]() idxs = list(range(len(polys))) np.random.shuffle(idxs) for i in idxs: if globs.polygons[i].color != polys[i].color: yield from listen.wait(2 / len(polys)) globs.polygons[i].color = polys[i].color globs.selected_color = 0 listen.dispatch("update_hud") globs.play_disabled = False
def play_loop(): while True: _, button, action, mods = yield from listen.on_mouse_button( globs.window) if globs.play_disabled: continue if button != glfw.MOUSE_BUTTON_LEFT: continue if action != glfw.PRESS: continue x, y = glfw.get_cursor_pos(globs.window) pt = mouse_coords(x, y) # Ignore clicks that hit the HUD clicked_hud = None for polygon in globs.hud_polygons: if is_point_in_poly(pt, polygon): clicked_hud = polygon break if clicked_hud is not None: continue # Look for clicks that hit the puzzle found = None for x in range(globs.polydata["nx"]): if found: break for y in range(globs.polydata["ny"]): delta = globs.polydata["origin"].xy delta += x * globs.polydata["unit_dx"] delta += y * globs.polydata["unit_dy"] for polygon in globs.polygons: if is_point_in_poly(pt - delta, polygon): found = polygon break assert found is not None """ def tmp_recolor(neighbor): prvcolor = neighbor.color neighbor.color = Vec(0,0,0) yield from listen.wait(0.2) neighbor.color = prvcolor yield from listen.wait(0.2) neighbor.color = Vec(0,0,0) yield from listen.wait(0.2) neighbor.color = prvcolor # debugging tool for figuring out the neighbors of a polygon for neighbor in found.neighbors: listen.launch(tmp_recolor(neighbor)) continue """ polygons_to_update = [found] while True: done = True for polygon in polygons_to_update: for neighbor in polygon.neighbors: if neighbor in polygons_to_update: continue if polygon.color == neighbor.color: polygons_to_update.append(neighbor) done = False if done: break # update the polygons for polygon in polygons_to_update: polygon.color = globs.polydata["colors"][globs.selected_color] # check if all the colors on the map are the same done = True for polygon in globs.polygons: if polygon.color != globs.polydata["colors"][globs.selected_color]: done = False break globs.move_count -= 1 if done: print("Level complete!") globs.bgcolor = globs.polydata["colors"][globs.selected_color] listen.dispatch("next_level") else: globs.selected_color += 1 globs.selected_color %= len(globs.polydata["colors"]) if globs.move_count == 0: listen.dispatch("reset_level") listen.dispatch("update_hud")
def title_screen(): globs.textboxes = [] title_box = TextBox(font_loc + "Vitreous-Black.ttf", size=80, color=Vec(0.0, 0.0, 0.0), pos=Vec(120, 525, -0.2)) title_box.text = "Tesseland" globs.textboxes.append(title_box) name_boxes = [ TextBox(font_loc + "Vitreous-Medium.ttf", size=30, color=Vec(0, 0, 0), pos=Vec(150, 425 - 50 * k, -0.2)) for k in range(3) ] name_boxes[0].text = "Triangle Madness by Sarah Asano" name_boxes[1].text = "Polygon Panic by Chris Couste" name_boxes[2].text = "Rectange Ruckus by Patrick Rall" globs.textboxes += name_boxes click_to_begin = TextBox(font_loc + "Vitreous-Medium.ttf", size=30, color=Vec(0, 0, 0), pos=Vec(150, 150, -0.2)) click_to_begin.text = "Click to begin" globs.textboxes.append(click_to_begin) globs.hud_polygons = [] globs.hud_polygons.append( Polygon(Vec(0, 1, 1), [Vec(0, 3), Vec(0, 8), Vec(5, 8)])) globs.hud_polygons.append( Polygon(Vec(1, 1, 1), [Vec(2, 5), Vec(5, 8), Vec(8, 8), Vec(8, 5)])) globs.hud_polygons.append( Polygon( Vec(1, 0.5, 0), [Vec(0, 0), Vec(0, 3), Vec(2, 5), Vec(3, 5), Vec(8, 0)])) globs.hud_polygons.append( Polygon(Vec(0.5, 1.0, 0), [Vec(3, 5), Vec(8, 0), Vec(8, 5)])) while True: _, button, action, mods = yield from listen.on_mouse_button( globs.window) x, y = glfw.get_cursor_pos(globs.window) pt = mouse_coords(x, y) for poly in globs.hud_polygons: if is_point_in_poly(pt, poly): globs.hud_polygons.remove(poly) break if len(globs.hud_polygons) == 0: globs.textboxes = [] listen.dispatch("next_level") break
def ending_screen(): while True: yield from listen.event("ending_screen") globs.textboxes = [] polygons = [] polygons.append( Polygon(colors["peru"], [Vec(0, 3), Vec(0, 8), Vec(5, 8)])) polygons.append( Polygon(colors["mistyrose"], [Vec(2, 5), Vec(5, 8), Vec(8, 8), Vec(8, 5)])) polygons.append( Polygon(colors["lightsteelblue"], [Vec(0, 0), Vec(0, 3), Vec(2, 5), Vec(3, 5), Vec(8, 0)])) polygons.append( Polygon(colors["mediumaquamarine"], [Vec(3, 5), Vec(8, 0), Vec(8, 5)])) yield from listen.wait(0.4) globs.hud_polygons = [] for poly in polygons: globs.hud_polygons.append(poly) yield from listen.wait(0.2) thank_you = TextBox(font_loc + "Vitreous-Medium.ttf", size=30, color=Vec(0, 0, 0), pos=Vec(125, 600, -0.2)) thank_you.text = "Thank you for playing" globs.textboxes.append(thank_you) title_box = TextBox(font_loc + "Vitreous-Black.ttf", size=80, color=Vec(0.0, 0.0, 0.0), pos=Vec(120, 525, -0.2)) title_box.text = "Tesseland" globs.textboxes.append(title_box) name_boxes = [ TextBox(font_loc + "Vitreous-Medium.ttf", size=30, color=Vec(0, 0, 0), pos=Vec(150, 325 - 50 * k, -0.2)) for k in range(5) ] name_boxes[0].text = "Special guest level by Billy Rieger" name_boxes[1].text = "Special thanks to our playtesters" name_boxes[2].text = "Levi Walker" name_boxes[3].text = "Jayashree Srinivasan" name_boxes[4].text = "Stella Wang" globs.textboxes += name_boxes globs.bgcolor = Vec(0.0, 0.0, 0) while True: _, button, action, mods = yield from listen.on_mouse_button( globs.window) x, y = glfw.get_cursor_pos(globs.window) pt = mouse_coords(x, y) for poly in globs.hud_polygons: if is_point_in_poly(pt, poly): globs.hud_polygons.remove(poly) break if len(globs.hud_polygons) == 0: globs.textboxes = [] listen.dispatch("next_level") break