def rectangle(a, b, color=(0, 0, 0)): t1 = ManualObject3D([ Triangle(V3(0, 0, 0), V3(a, 0, 0), V3(a, b, 0), color=Material(color)), Triangle(V3(0, 0, 0), V3(a, b, 0), V3(0, b, 0), color=Material(color)) ]) t1.move(V3(-a / 2, -b / 2, 0)) t1.from_init = V3() t1.refresh_normals() return t1
def __init__(self, track, nparts=10): self.track = track scene = parameters.scene # track.add_visual_rails() # arot, acol, amov = self.random_gen_arrows(nparts) # rotating, coloring = self.random_gen_globs(nparts) # # self.color_sign = 5 self.color = 1 self.material = Material((255, 0, 0)) self.material2 = Material((0, 255, 0)) for c in acol: c.set_color(self.material) for g in coloring: g.set_color(self.material2) def f(): self.color += self.color_sign if self.color >= 255: self.color = 255 self.color_sign *= -1 elif self.color <= 0: self.color = 0 self.color_sign *= -1 #refresh color for c in acol: self.material.col[1] = self.color self.material.m[1] = self.color / 0.2 self.material.M[1] = min(self.color * 1.1 + 100, 255) for c in arot: c.rotate_around_center_z(1) for c in amov: c.move(V3(0, 0, -1)) for g in rotating: g.rotate_around_center_z(3) for g in coloring: self.material2.col[1] = self.color self.material2.m[1] = self.color / 0.2 self.material2.M[1] = min(self.color * 1.1 + 100, 255) track.functions_things = f
def p_arrow(a, b, c, color=(0, 0, 0)): rect = p_rectangle(a, b, color) triangle = Path3D([V3(2 * c, b, 0), V3(0, b + 3 * c, 0), V3(-2 * c, b, 0)], True, Material(color)) triangle.move(V3(0, -b / 2, 0)) triangle.from_init = V3() triangle.filled = True return rect, triangle
def triangle(a, color=(0, 0, 0)): t1 = ManualObject3D([ Triangle(a * V3(-1, 0, 0), a * V3(1, 0, 0), a * V3(0, 1, 0), color=Material(color)) ]) t1.refresh_normals() return t1
def a_rectangle(a, b, color=(0, 0, 0), edges=None): points = [V3(0, 0, 0), V3(a, 0, 0), V3(a, b, 0), V3(0, b, 0)] area = Area3D(points, color=Material(color)) if edges: area.edges = V3(edges) area.move(V3(-a / 2, -b / 2, 0)) area.from_init = V3() area.refresh_triangle() return area
def p_rectangle(a, b, color=(0, 0, 0), edges=None, filled=True): points = [V3(0, 0, 0), V3(a, 0, 0), V3(a, b, 0), V3(0, b, 0)] area = Path3D(points, True, color=Material(color)) if edges: area.edges = V3(edges) area.move(V3(-a / 2, -b / 2, 0)) area.from_init = V3() area.filled = filled return area
def p_triangle(a, filled=True, color=(0, 0, 0)): ## a = V3(a,0,0) ## p1 = a.rotate_z(60) ## p2 = p1.rotate_z(60) ## p3 = p1.rotate_z(60) ## p = Path3D([p1,p2,p3], True, color) ## p.filled = filled ## return p p = Path3D([V3(-a, 0, 0), V3(a, 0), V3(0, a, 0)], True, Material(color)) p.filled = filled return p
def p_arrow_line(a, b, c, color=(0, 0, 0)): path = PathBuilder([V3(-a, 0, 0)]) path.go(2 * a, 0, 0) path.go(0, 0, b) path.go(c, 0, 0) path.go(-c - a, 0, c) #middle path.go(-c - a, 0, -c) path.go(c, 0, 0) path.go(0, 0, -b) p = Path3D(path.path, True, Material(color)) p.filled = False return p
def create_vessel(color): quality = parameters.CURRENT_QUALITY glass = Material((0, 0, 0), M=(120, 120, 120)) rest = Material(color) t, n, c = garage.generate_vessel(rest, glass) w = garage.random_wing(rest) v = vessel.Vessel(None, more_triangles=[]) #quality = power*friction #quality = turn+max_fuel power_rand = random.random() + 0.000001 power = parameters.MIN_POWER + power_rand * (parameters.MAX_POWER - parameters.MIN_POWER) friction = power_rand power *= parameters.ENGINE_POWER mass = parameters.MIN_MASS + random.random() * (parameters.MAX_MASS - parameters.MIN_MASS) turn = parameters.MIN_TURN + random.random() * (parameters.MAX_TURN - parameters.MIN_TURN) max_fuel = quality - turn max_fuel = parameters.MIN_FUEL + int( max_fuel * (parameters.MAX_FUEL - parameters.MIN_FUEL)) # v.tail = vessel.Part(t.triangles, turn, friction, mass) v.nose = vessel.Part(n.triangles, turn, friction, mass) v.cockpit = vessel.Part(c.triangles, turn, friction, mass) v.lwing = vessel.Part(w[0].triangles, turn / 2., friction / 2., mass / 2.) v.rwing = vessel.Part(w[1].triangles, turn / 2., friction / 2., mass / 2.) v.engine = vessel.Engine(max_fuel, power) v.engine.mass = mass v.engine.turn = turn v.engine.friction = friction # v.refresh_mesh() v.rotate_around_center_y(-90) v.compute_box3D() v.compute_dynamics() v.from_init_rot = V3() v.color = rest # return v
def add_visual_rails(self, color=(0, 0, 0)): for x in range(self.nx + 1): xrail = x * self.railw for y in range(self.ny + 1): yrail = y * self.railh p1 = parameters.scene.relative_to_cam(V3(xrail, yrail, 0)) p2 = parameters.scene.relative_to_cam( V3(xrail, yrail, parameters.VISURAIL_LENGTH)) path = Path3D([p1, p2], False, Material(color)) ## path = Path3D([V3(xrail,yrail,0),V3(xrail,yrail,30)],False,color) self.add_thing(path, 0, self.zfinish, parameters.VISURAIL_SPACE, parameters.VISURAIL_NMAX)
def init_game(hero): parameters.players = [ gamelogic.Player() for i in range(parameters.NPLAYERS - 1) ] hero_color = parameters.HERO_COLOR hero_player = gamelogic.Player(parameters.HERO_NAME, Material(hero_color)) hero_player.points = 0 parameters.player = hero_player parameters.players += [hero_player] if hero is None: hero = create_vessel(hero_color) hero.is_hero = True hero.mass /= 2. hero.compute_dynamics() hero.name = "Hero" #!! hero.attach_to_player(hero_player, reset_color=False)
def p_line(frompos, topos, color=(0, 0, 0)): p = Path3D([frompos, topos], False, Material(color)) p.filled = False return p
def p_disk(a, filled=True, color=(0, 0, 0), n=10): v0 = V3(a, 0, 0) path = [v0.rotate_z(angle) for angle in range(0, 360, 360 // n)] p = Path3D(path, True, Material(color)) p.filled = filled return p
def cube(a, color=(0, 0, 0)): cube = Object3D("cube_ascii.stl") cube.scale(a) cube.refresh_normals() cube.set_color(Material(color)) return cube
def __init__(self, title, ok_text, ranking, results=False, choosevessel=False): refresh_ranking() # light_pos = V3(0, 1000, -1000) light_m = V3(20, 20, 20) light_M = V3(200, 200, 200) self.light = light.Light(light_pos, light_m, light_M) self.viewport = pygame.Surface((400, int(parameters.H * 0.6))) self.viewport_color = (200, 200, 200) self.viewport.fill(self.viewport_color) self.viewport_rect = pygame.Rect((0, 0), self.viewport.get_size()) self.viewport_rect.centerx = parameters.W // 2 + 100 self.viewport_rect.centery = parameters.H // 2 self.cam = camera.Camera(self.viewport, fov=512, d=2, objs=[]) self.screen = thorpy.get_screen() self.displayed_vessel = None self.i = 0 # if results: ranking[0].points += 1 ranking[0].money += 300 + (parameters.NPLAYERS - ranking[0].ranking) * 100 ranking[2].points -= 1 ranking[2].money += 100 ranking[1].money += 200 if ranking[2].points < 0: ranking[2].points = 0 # self.trophy = None if choosevessel: self.e_players = [] def other_vessel(): self.vessels[0] = create_vessel(parameters.HERO_COLOR) self.vessels[0].set_pos(V3(0, -1 * 4.5, 20)) self.vessels[0].move(V3(0, 4, 0)) self.displayed_vessel = self.vessels[0] #replace self.ve new_ve = get_vessel_element(self.vessels[0]) self.e_bckgr.replace_element(self.ve, new_ve) thorpy.functions.refresh_current_menu() self.ve = new_ve self.e_bckgr.unblit_and_reblit() b = thorpy.make_button("Generate another vessel", other_vessel) c = thorpy.make_button("Done", thorpy.functions.quit_menu_func) self.e_players.append(b) self.e_players.append(c) from main import create_vessel self.vessels = [create_vessel(parameters.HERO_COLOR)] self.displayed_vessel = self.vessels[0].get_copy() self.ve = get_vessel_element(self.vessels[0]) self.e_players.append(self.ve) else: if results: self.e_players = [ p.get_element(str(i + 1) + ") ") for i, p in enumerate(ranking) ] else: self.e_players = [ p.get_element() for i, p in enumerate(ranking) ] self.vessels = [p.vessel.get_copy() for p in ranking] if results: import core3d from light import Material self.trophy = core3d.Object3D("trophy1.stl") self.trophy.set_color(Material((255, 215, 0))) ## self.trophy.set_color((255,255,0)) self.trophy.set_pos(V3(5., -0 * 4.5 - 0.2, 15)) self.trophy.rotate_around_center_z(90.) self.trophy.rotate_around_center_x(-65.) self.trophy.move(V3(0, 4, 0)) self.background = thorpy.load_image("background1.jpg") self.background = thorpy.get_resized_image( self.background, (parameters.W, parameters.H // 2), type_=max) self.e_bckgr = thorpy.Background.make(image=self.background, elements=self.e_players) # vw, vh = self.viewport_rect.size self.e_viewport_frame = thorpy.Element() painter = thorpy.painterstyle.ClassicFrame((vw + 3, vh + 3), color=self.viewport_color, pressed=True) self.e_viewport_frame.set_painter(painter) self.e_viewport_frame.finish() self.e_viewport_frame.set_center(self.viewport_rect.center) # reaction = thorpy.ConstantReaction(thorpy.THORPY_EVENT, self.refresh_display, {"id": thorpy.constants.EVENT_TIME}) self.e_bckgr.add_reaction(reaction) if not choosevessel: for i, v in enumerate(self.vessels): pos = self.e_players[i].get_fus_rect().center v.set_pos(V3(0, -i * 4.5, 20)) v.move(V3(0, 4, 0)) else: self.vessels[0].set_pos(V3(0, -1 * 4.5, 20)) self.vessels[0].move(V3(0, 4, 0)) # self.displayed_vessel.set_pos(V3(0, -1 * 4.5, 20)) self.displayed_vessel.move(V3(0, 4, 0)) # thorpy.store(self.e_bckgr, gap=40) for e in self.e_players: e.stick_to(self.viewport_rect, "left", "right", align=False) e.move((-5, 0)) self.e_title = get_etitle(title) if not choosevessel: self.e_ok = get_eok(ok_text) self.e_bckgr.add_elements( [self.e_viewport_frame, self.e_title, self.e_ok]) else: self.e_bckgr.add_elements([self.e_viewport_frame, self.e_title]) self.goback = False def return_garage(): self.derotate() self.goback = True thorpy.functions.quit_menu_func() if not results and not choosevessel: self.e_back = thorpy.make_button("Return to garage", return_garage) self.e_back.stick_to(self.e_ok, "left", "right") self.e_back.move((-20, 0)) self.e_bckgr.add_elements([self.e_back]) if not results: reaction = thorpy.Reaction(pygame.MOUSEMOTION, self.mousemotion) self.e_bckgr.add_reaction(reaction) m = thorpy.Menu(self.e_bckgr) m.play()
def init_scene(): ## random.seed(0) # gara = garage.Garage() gara.play() gara.derotate() # parameters.scene = Scene() scene = parameters.scene scene.cam = Camera(scene.screen, fov=512, d=2, objs=[]) scene.cam.set_aa(True) # light_pos = V3(0, 1000, -1000) light_m = V3(20, 20, 20) light_M = V3(200, 200, 200) light = Light(light_pos, light_m, light_M) scene.light = light ##hero = hero.get_splitted_copy(threshold=-2.5) scene.hero = parameters.player.vessel hero = scene.hero scene.objs.append(hero) #track nx = random.randint(3, 4) ny = random.randint(2, 4) print("nx,ny", nx, ny) lg = levelgen.LevelGenerator(parameters.ZFINISH, nx, ny) rw, rh = parameters.RAILW, parameters.RAILH possible_obstacles = [ primitivemeshes.p_rectangle(0.8 * rw, 0.8 * rh, (0, 0, 255), (0, 0, 0)) ] lg.random_gen(nparts=4, objects=possible_obstacles, min_density=0.1, max_density=0.8) track = scene.track for o in track.obstacles: if random.random() < 0.4: if random.random() < 0.5: o.rotation_x = random.randint( 2, 5) * (2 * random.randint(0, 1) - 1) else: o.rotation_y = random.randint( 2, 5) * (2 * random.randint(0, 1) - 1) o.obj.set_color(Material(parameters.COLOR_ROTATING)) if random.random() < 0.5: r = random.random() if r < 0.1: o.movement_x = 1 elif r < 0.2: o.movement_y = 1 elif r < 0.25: o.movement_x = 1 o.movement_y = 1 if o.movement_x or o.movement_y: o.obj.set_color(Material(parameters.COLOR_MOVING)) # deco = trackdecorator.Decorator(track, track.zfinish // 500) #500 # finish = primitivemeshes.p_rectangle(track.railw, track.railh, (0, 0, 0)) ## for pos in track.rail_centers(): for x in range(track.nx): for y in range(track.ny): pos = V3(track.rails[x, y].middlepos) pos.z = track.zfinish + 5 finish.set_pos(pos) if x % 2 == 0: if y % 2 == 0: color = (0, 0, 0) else: color = (255, 255, 255) else: if y % 2 == 0: color = (255, 255, 255) else: color = (0, 0, 0) finish.set_color(Material(random.choice(color))) scene.objs.append(finish.get_copy()) scene.track = track scene.opponents = [ create_vessel(random.choice(drawing.colors)) for i in range(2) ] scene.objs += scene.opponents ## fin = Object3D("finish.stl") ## triangles = [] ## for t in fin.triangles: ## isok = True ## for v in t.vertices(): ## if v.y >= 0: ## isok = False ## if isok: ## triangles.append(t) ## fin = ManualObject3D(triangles) ## fin.rotate_around_center_x(-90) ## fin.scale(30.) ## fin.set_color(Material((255,255,0))) ## fin.move(V3(0,40,track.zfinish)) ## track.things_objects.append(fin) ## scene.objs += [fin] # scene.refresh_cam() scene.players = [parameters.player] near = parameters.player.get_nearest_players() for i, o in enumerate(scene.opponents): player = near[i] scene.put_opponent_on_rail(o, i + 1, 0, 25) o.attach_to_player(player) scene.players.append(player) o.set_ia(100, 0.01) hero.reinit_orientation() hero.set_pos(parameters.HERO_POS) scene.put_hero_on_rail(0, 0) print("end main") scene.refresh_vessels() scene.hud.refresh_attributes() g = gamelogic.ShowRanking("Start list", "Go to race", scene.players) return scene, g.goback