def mix(a, b, t): """ interpolate a -> b @ t Returns a vec4 Supports color names and pygame colors """ if isinstance(a, vec3): return glm.mix(a, b, t) # this works for vec4 as well return glm.mix(ncolor(a), ncolor(b), t)
def __init__(self, app, scene, pos: vec3, z_vel: float, **kwargs): super().__init__(app, scene, None, position=pos, velocity=Z * 1000, **kwargs) self._surface = None gcolor = self.scene.ground_color if gcolor: if "ROCK" not in self.app.cache: # self.color = ncolor("white") self._surface = pygame.Surface(ivec2(4)) # self.color = ncolor("white") self._surface.fill( pg_color(glm.mix(ncolor("black"), gcolor, 0.4))) # self._surface.fill((0,0,0)) self.app.cache["ROCK"] = self._surface # self.velocity = Z * 10000 + z_vel else: self._surface = self.app.cache["ROCK"]
def syncAngles(self, linkName, target, j0, j1, j2, minArea, maxArea, chassisSpace=False): target = np.clip(target, -1.0, +1.0) j2LocalPos = self.jointsLocalPos[linkName + "2"] if chassisSpace: target[0] = glm.mix(self.chassisSpaceMin[0], self.chassisSpaceMax[0], target[0] * 0.5 + 0.5) target[1] = glm.mix(self.chassisSpaceMin[1], self.chassisSpaceMax[1], target[1] * 0.5 + 0.5) target[2] = glm.mix(self.chassisSpaceMin[2], self.chassisSpaceMax[2], target[2] * 0.5 + 0.5) target = target - self.jointsLocalPos[linkName + "1"] else: target[0] = glm.mix(minArea[0], maxArea[0], target[0] * 0.5 + 0.5) target[1] = glm.mix(minArea[1], maxArea[1], target[1] * 0.5 + 0.5) target[2] = glm.mix(minArea[2], maxArea[2], target[2] * 0.5 + 0.5) angles = self.solveLeg(j2LocalPos, self.len1, self.len2, target) j0.newCurTarget = angles[0] j0.newCurTargetReachable = angles[3] j1.newCurTarget = angles[1] j1.newCurTargetReachable = angles[3] j2.newCurTarget = angles[2] j2.newCurTargetReachable = angles[3]
def change_logo_color(self, script): yield bigterm = self.bigterm while True: if self.scene.ground_color: break yield c = glm.mix( self.scene.ground_color, glm.mix(ncolor("white"), random_rgb(), random.random()), 0.2, ) r = 0 # rc = vec4() self.scene.play_sound("explosion.wav") while True: if r % 30 == 0: rc = random_rgb() s = "BUTTERFLY " for i in range(len(s)): # c = ncolor('purple') * i/len(s) + math.sin(r / 200 + i+r) ** 2 + .6 c = (ncolor("purple") * i / len(s) + ((math.sin(i + r) + 0.4) * script.dt) + 0.3) bigterm.write(s[i], (i - len(s) - 8, 1), c) if r > 15: s = "DESTROYERS " for i in range(len(s)): c = (self.scene.ground_color * i / len(s) + ((math.sin(i + r) + 4) * script.dt) + 0.3) bigterm.write(s[i], (i - len(s) - 3, 2), c) if r == 15: self.scene.play_sound("explosion.wav") yield script.sleep(0.1) r += 1
def update_mesh(self, mesh: MeshObject, time: float, dt: float): t = self._walk_time amount = self._walking head = glm.translate(glm.mat4(), glm.vec3(0, 0, 3)) head = glm.rotate(head, self._head_rotation / 180. * math.pi, glm.vec3(0, 0, 1)) mesh.part_transforms[0] = head d = -1 for i in range(2): rest_pos = glm.vec3(.4 * d, 0.1, 0) walk_pos = glm.vec3(.4 * d, .3 * math.sin(t + d * 1.57), 0) foot = glm.translate(glm.mat4(), glm.mix(rest_pos, walk_pos, amount)) foot = glm.scale(foot, glm.vec3(.3, .5, .3)) mesh.part_transforms[i + 1] = foot d = 1
def __call__(self, script): yield while self.alive: yield script.sleep(1 + random.random() * 4) to_player = self.scene.player.position - self.position if BUTTERFLY_MIN_SHOOT_DIST < glm.length(to_player): self.play_sound("squeak.wav") v = glm.mix(Z, to_player, 0.75) self.scene.add( Bullet( self.app, self.scene, self, self.position, v, speed=BULLET_SPEED * ENEMY_BULLET_FACTOR, ) )
def uniform_grid_volume_get_linear(data, coord, res, data_dim): if len(data) <= 0: return glm.vec4(0.0) # Clamp to nearly one to avoid issus c = glm.clamp(coord, 0.0, 0.9999) p = glm.floor(c * glm.vec3(res - 1)) cc = glm.fract(c * glm.vec3(res - 1)) # Get all samples v000 = get_vec4(data, index(p + glm.vec3(0, 0, 0), res) * data_dim, data_dim) v100 = get_vec4(data, index(p + glm.vec3(1, 0, 0), res) * data_dim, data_dim) v010 = get_vec4(data, index(p + glm.vec3(0, 1, 0), res) * data_dim, data_dim) v110 = get_vec4(data, index(p + glm.vec3(1, 1, 0), res) * data_dim, data_dim) v001 = get_vec4(data, index(p + glm.vec3(0, 0, 1), res) * data_dim, data_dim) v101 = get_vec4(data, index(p + glm.vec3(1, 0, 1), res) * data_dim, data_dim) v011 = get_vec4(data, index(p + glm.vec3(0, 1, 1), res) * data_dim, data_dim) v111 = get_vec4(data, index(p + glm.vec3(1, 1, 1), res) * data_dim, data_dim) # Interpolate x v00 = glm.mix(v000, v100, cc[0]) v01 = glm.mix(v010, v110, cc[0]) v10 = glm.mix(v001, v101, cc[0]) v11 = glm.mix(v011, v111, cc[0]) # Interpolate y v0 = glm.mix(v00, v01, cc[1]) v1 = glm.mix(v10, v11, cc[1]) # Interpolate z v = glm.mix(v0, v1, cc[2]) return v
def __init__(self, engine, sx, sy, length, height, seed): self.size = glm.vec3(sx * length, height, sy * length) self.tex_size = glm.vec2(sx, sy) self.texture = engine.graphics.get_texture('dirt') self.collider = physics_objects.Box((0, 0, 0), self.size) self.collider.get_height = self.get_height engine.physics.static.append(self.collider) self.positions = np.empty((sx * sy, 3), np.float32) normals = np.empty((sx * sy, 3), np.float32) texcoords = np.empty((sx * sy, 2), np.float32) colors = np.empty((sx * sy, 3), np.float32) indices = np.empty(6 * (sx - 1) * (sy - 1), np.uint32) # self.image = Image.new('L', (sx, sy), 'black') h = hash(seed) sample = glm.vec2(h, hash(h)) # Generate vertex positions for x in range(0, sx): for y in range(0, sy): pos = glm.vec3(x / float(sx), 0.0, y / float(sy)) for i in range(0, 4): pos.y += pow(1.0, -i) * noise2(pos.x + sample.x, pos.z + sample.y, i + 1) pos.y /= 4.0 pos.y = 0.5 * pos.y + 0.5 pos.y *= abs(pos.x-.5) + abs(pos.z-.5) # self.image.putpixel((x, y), int(pos.y * 255)) # pos.y = pos.x self.positions[y * sx + x] = list(pos) texcoords[y * sx + x] = [pos.x, pos.z] # image.close() # Specify indices and normals i = 0 for x in range(0, sx - 1): for y in range(0, sy - 1): indices[i] = y * sx + x indices[i + 1] = (y + 1) * sx + x indices[i + 2] = y * sx + x + 1 v0 = self.positions[indices[i]] v1 = self.positions[indices[i + 1]] v2 = self.positions[indices[i + 2]] normal = np.cross(v1 - v0, v2 - v0) normal /= np.linalg.norm(normal) normals[indices[i]] = normal normals[indices[i + 1]] = normal normals[indices[i + 2]] = normal i += 3 indices[i] = y * sx + (x + 1) indices[i + 1] = (y + 1) * sx + x indices[i + 2] = (y + 1) * sx + (x + 1) v0 = self.positions[indices[i]] v1 = self.positions[indices[i + 1]] v2 = self.positions[indices[i + 2]] normal = np.cross(v1 - v0, v2 - v0) normal /= np.linalg.norm(normal) normals[indices[i]] = normal normals[indices[i + 1]] = normal normals[indices[i + 2]] = normal i += 3 GRASS = glm.vec3(0.35, 0.65, 0.25) SNOW = glm.vec3(0.925, 0.93, 0.95) for x in range(0, sx): for y in range(0, sy): i = y * sx + x p = self.positions[i] normal = glm.vec3(float(normals[i, 0]), float(normals[i, 1]), float(normals[i, 2])) angle = max(glm.dot(normal, glm.vec3(0.0, 1.0, 0.0)), 0.0) h = float(p[1]) color = glm.mix(GRASS, SNOW * h, pow(angle, 10.0)) colors[i] = [color.x, color.y, color.z] self.mesh = mesh.Mesh(self.positions, normals, texcoords, colors, indices)
GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE ssao_program = program.create(*glsl.read("ssao")) ssao_blur_program = program.create(*glsl.read('ssao.vs', "ssao_blur.fs")) # generate sample kernel # ---------------------- ssaoKernel = [] for i in range(64): sample = glm.vec3(np.random.uniform((-1, -1, 0), (1, 1, 1), (3, ))) sample = glm.normalize(sample) sample *= np.random.uniform(0, 1) scale = i / 64 # scale samples s.t. they are more aligned to center of kernel scale = glm.mix(0.1, 1.0, scale * scale) sample *= scale ssaoKernel.append(sample) with program.use(ssao_program): for i, sample in enumerate(ssaoKernel): name = "samples[{}]".format(i) location = glGetUniformLocation(ssao_program, name) program.set_uniform(ssao_program, name, sample) noise_data = np.random.uniform((-1, -1, 0), (1, 1, 0), (4, 4, 3)) noise_texture = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, noise_texture) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, 4, 4, 0, GL_RGB, GL_FLOAT, noise_data) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
def __call__(self, script): yield scene = self.scene terminal = self.terminal stats = self.stats self.scene.music = "intermission.ogg" when = script.when # self.scene.sky_color = "#4c0b6b" # self.scene.ground_color = "#e08041" self.scene.stars() self.scene.cloudy() textdelay = 0.02 fade = [] fades = [ when.fade( 10, (0, 1), lambda t: scene.set_sky_color( glm.mix(ncolor("#4c0b6b"), ncolor("#e08041"), t)), ), when.fade( 10, (0, 1), lambda t: scene.set_ground_color( glm.mix(ncolor("darkgreen"), ncolor("yellow"), t)), lambda: fades.append( when.every( 0, lambda: scene.set_ground_color(scene.ground_color)) ), ), ] yield msg = [ ("Level " + str(stats.level), "COMPLETED"), ("Damage Done", int(stats.damage_done)), ("Damage Taken", int(stats.damage_taken)), ("Kills", int(stats.kills)), # ("Lives Remaining", stats.lives), None, ("Score", stats.score), ] for y, line in enumerate(msg): if line: scene.ensure_sound("message.wav") for x, m in enumerate(line[0]): terminal.write(m, (x + 1, y * 2 + 3), "white") # terminal.write(m[:x], (x + 1, y * 2 + 3), "white") # terminal.write(m[-1], (x + 1 + len(m) - 1, y * 2 + 3), "red") yield script.sleep(0.01) else: continue if isinstance(line[1], int): # total dd = 0 for val in range(0, line[1] + 1): terminal.write( str(val), (self.terminal.size.x - len(str(val)) - 1, y * 2 + 3), "white", ) dd += 0.6 / (val + 2) if dd > 1 / max(self.app.fps, 10): yield script.sleep(dd) dd = 0 else: yield script.sleep(0.1) terminal.write( str(line[1]), (self.terminal.size.x - len(str(line[1])) - 1, y * 2 + 3), "green", ) self.scene.play_sound("hit.wav") yield script.sleep(0.01) yield script.sleep(2) # while True: # terminal.write_center("Press any key to continue", 20, "green") # yield script.sleep(0.2) # if script.keys_down: # break # terminal.clear(20) # yield script.sleep(0.2) # if script.keys_down: # break self.stats.level += 1 self.app.state = "game"
def __call__(self, script): yield scene = self.scene terminal = self.terminal self.scene.music = "butterfly.ogg" when = script.when # self.scene.sky_color = "#4c0b6b" # self.scene.ground_color = "#e08041" self.scene.stars() self.scene.cloudy() textdelay = 0.02 fades = [ when.fade( 10, (0, 1), lambda t: scene.set_sky_color( glm.mix(ncolor("#4c0b6b"), ncolor("#e08041"), t)), ), when.fade( 10, (0, 1), lambda t: scene.set_ground_color( glm.mix(ncolor("darkgreen"), ncolor("yellow"), t)), lambda: fades.append( when.every( 0, lambda: scene.set_ground_color(scene.ground_color)) ), ), ] yield pages = [ [ "CREDITS", "", "flipcoder", " " + "Programming, Music, Sounds", "ddorn", " " + "Programming, Graphics", "MysteryCoder456", " " + "Programming", "Tamwile", " " + "Graphics", "Jtiai", " " + "Sounds", "", "Additional Assets: ", " opengameart.org/users/pitrizzo", ], [ "This game was created by PythonixCoders", "for PyWeek 29, a week-long Python game", "jam, where individuals or groups give", "themselves only one week to create a", "game.", "", "Participate next time at pyweek.org", ], ] for p, page in enumerate(pages): for y, line in enumerate(page): if line: scene.ensure_sound("message.wav") if p == 0: if line == "CREDITS": col = "white" elif not line.startswith(" "): col = "green" else: col = "white" else: col = "white" for x, m in enumerate(line): terminal.write(m, (x + 1, y + 1), col) # terminal.write(m[:x], (x + 1, y * 2 + 3), "white") # terminal.write(m[-1], (x + 1 + len(m) - 1, y * 2 + 3), "red") yield script.sleep(0.01) self.scene.play_sound("message.wav") else: continue delay = 0.1 yield script.sleep(4) self.terminal.clear() terminal.write_center("Thanks for Playing!!!", 10) while True: if script.keys_down: break yield script.sleep(0.1) self.app.state = None
def update_interpolation(self, delta_time): self.interpolated_position = glm.mix(glm.vec3(self.position), glm.vec3(self.old_position), self.step) self.step -= delta_time
def distance_to(self, point): d2 = self.obj2.distance_to(point) d1 = self.obj1.distance_to(point) h = glm.clamp(0.5 - 0.5 * (d1 + d2) / self.k, 0.0, 1.0) return glm.mix(d2, -d1, h) + self.k * h * (1.0 - h)
def update(self, deltaTime): # movement in camera coordinates movement = self._currentTransform.orientation * (self._movementInput * self.movementSpeedMod) if self.mode == 0: # rotate position around target # figure out where the old target is oldTarget = self._desiredTransform.position \ + self._desiredTransform.orientation * glm.vec3(0, 0, -1) * self._desiredTargetDistance # rotate the camera self._desiredTransform.orientation = glm.normalize( glm.angleAxis(self._rotationInput.x, self.worldUp) * self._desiredTransform.orientation * glm.angleAxis(self._rotationInput.y, glm.vec3(1, 0, 0))) # move so old target matches new target newTarget = self._desiredTransform.position \ + self._desiredTransform.orientation * glm.vec3(0, 0, -1) * self._desiredTargetDistance self._desiredTransform.position += oldTarget - newTarget # pan # zooming, make sure distance to the target does not become negative if self._desiredTargetDistance + self._movementInput.z > 0.01 * self.zoomSpeed: self._desiredTargetDistance += self._movementInput.z # now just apply movement self._desiredTransform.position += movement # interpolate # interpolate distance to target self._currentTargetDistance = glm.mix( self._currentTargetDistance, self._desiredTargetDistance, glm.pow(deltaTime, self.movementSmoothing)) # interpolate current target position desiredTarget = self._desiredTransform.position + self._desiredTransform.orientation * glm.vec3( 0, 0, -1) * self._desiredTargetDistance oldActualTarget = self._currentTransform.position + self._currentTransform.orientation * glm.vec3( 0, 0, -1) * self._currentTargetDistance oldActualTarget = glm.mix( oldActualTarget, desiredTarget, glm.pow(deltaTime, self.movementSmoothing)) # interpolate orientation self._currentTransform.orientation = glm.slerp( self._currentTransform.orientation, self._desiredTransform.orientation, glm.pow(deltaTime, self.rotationSmoothing)) # calculate proper position using difference that was created by rotation and moving the target newActualTarget = self._currentTransform.position + self._currentTransform.orientation * glm.vec3( 0, 0, -1) * self._currentTargetDistance self._currentTransform.position += oldActualTarget - newActualTarget elif self.mode == 1: # movement self._desiredTransform.position += movement # rotation self._desiredTransform.orientation = glm.normalize( glm.angleAxis(self._rotationInput.x, self.worldUp) * self._desiredTransform.orientation * glm.angleAxis(self._rotationInput.y, glm.vec3(1, 0, 0))) # interpolate between transform and desired transform self._currentTransform.position = glm.mix( self._currentTransform.position, self._desiredTransform.position, glm.pow(deltaTime, self.movementSmoothing)) self._currentTransform.orientation = glm.slerp( self._currentTransform.orientation, self._desiredTransform.orientation, glm.pow(deltaTime, self.rotationSmoothing)) self.modelMatrix = self._currentTransform.mat4() self.viewMatrix = glm.inverse(self.modelMatrix) self._rotationInput.x = 0 self._rotationInput.y = 0 self._movementInput.x = 0 self._movementInput.y = 0 self._movementInput.z = 0 self.movementSpeedMod = 1.0
def __call__(self, script): yield self.scene.scripts += self.change_logo_color when = script.when scene = self.scene terminal = self.terminal self.scene.music = "butterfly2.ogg" # self.scene.sky_color = "#4c0b6b" # self.scene.ground_color = "#e08041" # self.scene.stars() self.scene.cloudy() textdelay = 0.03 fades = [ when.fade( 10, (0, 1), lambda t: scene.set_sky_color_opt( glm.mix(ncolor("#4c0b6b"), ncolor("#e08041"), t)), ), when.fade( 10, (0, 1), lambda t: scene.set_ground_color_opt( glm.mix(ncolor("darkgreen"), ncolor("yellow"), t)), lambda: fades.append( when.every( 0, lambda: scene.set_ground_color_opt(scene. ground_color))), ), ] yield # self.scene.set_ground_color = "#e08041" # scene.sky_color = "black" self.scene.music = "butterfly2.ogg" # for i in range(len(msg)): # terminal.write(msg[i], (len(msg) / 2 - 1 + i, 1), self.scene.ground_color) # # scene.ensure_sound("type.wav") # yield script.sleep(0.002) # script.push(self.logo_color) # yield from self.change_logo_color(script) yield script.sleep(3) msg = [ "In the year 20XX, the butterfly", "overpopulation problem has", "obviously reached critical mass.", "The military has decided to intervene.", "Your mission is simple: defeat all the", "butterflies before the world ends.", "But look out for Big Butta, king of", "the butterflies.", ] for y, line in enumerate(msg): ty = y * 2 + 5 for x, m in enumerate(line): terminal.write(random_char(), (x + 2, ty), random_rgb()) cursor = (x + 2, ty) terminal.write(m, (x + 1, ty), "white") # scene.ensure_sound("type.wav") self.change_logo_color(script) # if not script.keys_down: # yield # else: yield script.sleep(textdelay) terminal.clear(cursor) when = script.when scene = self.scene terminal = self.terminal yield script.sleep(3) # while True: # terminal.write_center("Press any key to continue", 20, "green") # self.change_logo_color(script) # yield script.sleep(0.1) # if script.keys_down: # break # terminal.clear(20) # self.change_logo_color(script) # yield script.sleep(0.1) # if script.keys_down: # break terminal.clear() terminal.write_center("Loading...", 10) self.app.state = "game"