def step_balls(self,ms): ms = min(ms,27.0) # avoid collision glitches when fps is low player = self.player dying = (self.mode == "dying") pr = player.getgeom('radius',0.49) ppos = player.pos for ball in self["balls"].contents: v = ball.velocity * ms bx,by,bz = pos = ball.pos newpos = v + pos r = ball.getgeom('radius',0.2) obstacles = self.level.obstacles_near(bx,by) for hc,hr,cell in obstacles: P = collision.collides(hc,hr,pos,r,v,collision.COLLIDE_REBOUND) if P: newpos, bv_times_ms = P vel = bv_times_ms * (1/ms) if ball.maxdestroy > 0 and not dying: points = self.hexfield.destroy(hc,hr) if points: sounds.play(points[1]) ball.maxdestroy -= 1 ball.duration -= 1000 self.inc_score(points[0]) if not ball.bounces: vel = ball.velocity else: vel *= 0.95 ball.velocity = vel break ball.pos = newpos if (ball.lethal and not dying and (ball.pos - ppos).length() < (r + pr)): self.player_die("{0} trauma".format(ball.__class__.__name__.lower()))
def __insert_new_apple(self): new_apple = Apple.get_new() for a in self.apples: if collides(a, new_apple): return False no_snake_field = True for snake in self.snakes: if snake.collides(new_apple): no_snake_field = False break if no_snake_field: self.apples.append(new_apple) return True return False
def __update_objects(self): dead_snakes = 0 for s in self.snakes: s.update_alive() if not s.is_alive(): dead_snakes = dead_snakes + 1 if dead_snakes == PLAYER_CFG.NUM_PLAYERS: self.is_game_over = True return for apple in self.apples: for s in self.snakes: if collides(apple, s.get_head()): s.grow() self.score += 5 self.apples.remove(apple) break for s in self.snakes: if s.is_alive(): s.update() if len(self.apples) < GAME_CFG.NUM_APPLES: while not self.__insert_new_apple(): pass
def step_monsters(self,ms): ms = min(ms,27.0) # avoid collision glitches when fps is low player = self.player dying = (self.mode == "dying") pr = player.getgeom('radius',0.49) ppos = Vec(player.pos) for mon in self["monsters"].contents: v = mon.velocity * ms mx,my,mz = pos = mon.pos newpos = v + pos r = mon.getgeom('radius',0.49) obstacles = self.level.obstacles_near(mx,my) collided = False for hc,hr,cell in obstacles: P = collision.collides(hc,hr,pos,r,v,collision.COLLIDE_REBOUND) if P: newpos, mv_times_ms = P velocity = mv_times_ms * (1/ms) mon.on_collision(None,newpos,velocity) collided = True break if not dying: for ball in self["balls"].contents: br = ball.getgeom("radius") if (ball.pos - newpos).length() < (r + br): mon.on_collision(ball,newpos,mon.velocity) collided = True break if mon._expired: return if not collided: mon.pos = newpos if not dying and (mon.pos - ppos).length() < (r + pr): mon.on_collision(player,newpos,(ppos - mon.pos)*0.01) self.player_die("{0} {1}".format( mon.harm_type, mon.__class__.__name__))
elif type(e) == sf.KeyEvent and e.pressed: if e.code is sf.Keyboard.ESCAPE: w.close() else: pass else: pass if breathing_clock.elapsed_time.seconds >= 10: # YOU DEAD w.close() if hugging: # Hugging handler for i in people: if collision.collides(player.global_bounds, i.global_bounds): people.remove(i) hug_sound.play() breathing_clock.restart() score += 10 else: pass hugging = False if randclock.elapsed_time.seconds >= randint(10, 20) and not taxi_vis: taxi_vis = True randclock.restart() if taxi_vis and refresh_clock.elapsed_time.seconds >= 1 and len(people) < 25: a = len(people) for i in range(0, randint(0, 5)):
def step_player(self,ms): ms = min(ms,35.0) # avoid collision glitches when fps is low player = self.player if self.keysdown: if self.first_person: a = self.player.angle strafe = 0 if pygletkey.Q in self.keysdown: strafe = a+90 elif pygletkey.E in self.keysdown: strafe = a-90 if pygletkey.A in self.keysdown: a += 5 if pygletkey.D in self.keysdown: a -= 5 theta = radians(a) if pygletkey.W in self.keysdown: v = Vec(cos(theta),sin(theta)) * ms * 0.01 elif pygletkey.S in self.keysdown: v = Vec(cos(theta),sin(theta)) * ms * -0.01 else: v = Vec(0,0) if strafe: A = radians(strafe) v += Vec(cos(A),sin(A)) * ms * 0.01 dx,dy,dz = v player.angle = a self.camera.look_from_spherical(30,a + 180,20,200) else: z = Vec(0,0) v = sum((self.movekeys.get(k,z) for k in self.keysdown),z).normalise() * ms * 0.01 dx,dy,dz = v if dx or dy: a = degrees(atan2(dy,dx)) player.angle = a if dx or dy: # See if player will collide with any hexagons px,py,pz = player.pos obstacles = self.level.obstacles_near(px,py) newpos = v + player.pos r = player.getgeom('radius',0.49) for hc,hr,cell in obstacles: P = collision.collides(hc,hr,player.pos,r,v,collision.COLLIDE_POSITION) if P: newpos = P break player.pos = newpos self.camera.look_at(tuple(player.pos)) # See if player has escaped phex = collision.nearest_neighbours(newpos.x,newpos.y,0).next() if phex == self.player_exit: sounds.play("fanfare") if self.levelnum > 0: levelnum = self.levelnum + 1 else: levelnum = self.levelnum - 1 level = self.find_level(levelnum) if level: self.exit_to(GameScreen,score=self.score,level=level, levelnum=levelnum,ammo=self.special_ammo, special=self.special_ball) else: self.exit_to(VictoryScreen,score=self.score) elif phex in self.level.powerups: bname = self.hexfield.collect(*phex) if bname: B = getattr(graphics,bname) if B == self.special_ball: self.special_ammo += B.ammo else: self.special_ball = B self.special_ammo = B.ammo sounds.play("chamber") self["hud"].set_ammo(self.special_ammo, self.special_ball.__name__) b = self[repr(phex)] if b: self["powerups"].remove(b)
def collides_with_body(self): for seg_idx in range(1, len(self.stack)): if collides(self.stack[0], self.stack[seg_idx]): return True return False
def collides(self, obj): # just to not place an apple under a snake's body for seg in self.stack: if collides(seg, obj): return True return False
y.move(sf.Vector2(player.position.x + random.randint(64, 128), player.position.y + random.randint(64, 128))) x = sf.Sprite(sf.Texture.from_file("eldritch1.png")) x.move(sf.Vector2(player.position.x + random.randint(64, 128), player.position.y + random.randint(64, 128))) z = sf.Sprite(sf.Texture.from_file("eldritch1.png")) z.move(sf.Vector2(player.position.x + random.randint(64, 128), player.position.y + random.randint(64, 128))) a = sf.Sprite(sf.Texture.from_file("eldritch1.png")) a.move(sf.Vector2(player.position.x + random.randint(64, 128), player.position.y + random.randint(64, 128))) enemies_list.append(x) enemies_list.append(y) enemies_list.append(z) enemies_list.append(a) if not spawning and spawn_clock.elapsed_time.seconds >= 5: spawning = True if collision.collides(goo[goo_num].global_bounds, player.global_bounds) and attacking: cthulhuhit.play() goo_hp -= 10 attacking = False spawning = False spawn_clock.restart() goo[goo_num].position = sf.Vector2(random.randint(0, 596), random.randint(0, 480-64)) for i in enemies_list: if collision.collides(i.global_bounds, player.global_bounds) and refresh_clock.elapsed_time.seconds > 1 and not attacking: hp -= 5 refresh_clock.restart() elif collision.collides(i.global_bounds, player.global_bounds) and attacking: enemies_list.remove(i) else: continue