def shortestDistance(self, boid_x, boid_y): #circle around point, will need to do this for each point around the actual surface #calculate distance of point from each wall, find shortest one shortestDistance = 0.0 # | a1 | a2 | a3 | # | b1 | NA | b3 | # | c1 | c2 | c3 | if self.x - self.image.width / 2 * self.scale <= boid_x < self.x + self.image.width / 2 * self.scale: #a2 if boid_y > self.y + self.image.height / 2 * self.scale: shortestDistance = boid_y - ( self.image.height / 2 * self.scale + self.y) #c2 else: shortestDistance = ( self.y - self.image.height / 2 * self.scale) - boid_y #a1, b1, c1 elif boid_x < self.x - self.image.width / 2 * self.scale: #a1 if boid_y > self.y + self.image.height / 2 * self.scale: shortestDistance = util.distance( (boid_x, boid_y), (self.x - self.image.width / 2 * self.scale, self.y + self.image.height / 2 * self.scale)) #b1 elif self.y - self.image.height / 2 * self.scale <= boid_y <= self.y + self.image.height / 2 * self.scale: shortestDistance = self.x - self.image.width / 2 * self.scale - boid_x #c1 else: shortestDistance = util.distance( (boid_x, boid_y), (self.x - self.image.width / 2 * self.scale, self.y - self.image.height / 2 * self.scale)) #a3, b3, c3 else: #a3 if boid_y > self.y + self.image.height / 2 * self.scale: shortestDistance = util.distance( (boid_x, boid_y), (self.x + self.image.width / 2 * self.scale, self.y + self.image.height / 2 * self.scale)) #b3 elif self.y - self.image.height / 2 * self.scale <= boid_y <= self.y + self.image.height / 2 * self.scale: shortestDistance = boid_x - (self.x + self.image.width / 2 * self.scale) #c3 else: shortestDistance = util.distance( (boid_x, boid_y), (self.x + self.image.width / 2 * self.scale, self.y - self.image.height / 2 * self.scale)) return shortestDistance
def shortestDistance(self, boid_x, boid_y): shortestDistance = 0.0 # | a1 | a2 | a3 | # | b1 | NA | b3 | # | c1 | c2 | c3 | if self.x - self.image.width / 2 * self.scale <= boid_x < self.x + self.image.width / 2 * self.scale: #a2 if boid_y > self.y + self.image.height / 2 * self.scale: shortestDistance = boid_y - ( self.image.height / 2 * self.scale + self.y) #c2 else: shortestDistance = ( self.y - self.image.height / 2 * self.scale) - boid_y #a1, b1, c1 elif boid_x < self.x - self.image.width / 2 * self.scale: #a1 if boid_y > self.y + self.image.height / 2 * self.scale: shortestDistance = util.distance( (boid_x, boid_y), (self.x - self.image.width / 2 * self.scale, self.y + self.image.height / 2 * self.scale)) #b1 elif self.y - self.image.height / 2 * self.scale <= boid_y <= self.y + self.image.height / 2 * self.scale: shortestDistance = self.x - self.image.width / 2 * self.scale - boid_x #c1 else: shortestDistance = util.distance( (boid_x, boid_y), (self.x - self.image.width / 2 * self.scale, self.y - self.image.height / 2 * self.scale)) #a3, b3, c3 else: #a3 if boid_y > self.y + self.image.height / 2 * self.scale: shortestDistance = util.distance( (boid_x, boid_y), (self.x + self.image.width / 2 * self.scale, self.y + self.image.height / 2 * self.scale)) #b3 elif self.y - self.image.height / 2 * self.scale <= boid_y <= self.y + self.image.height / 2 * self.scale: shortestDistance = boid_x - (self.x + self.image.width / 2 * self.scale) #c3 else: shortestDistance = util.distance( (boid_x, boid_y), (self.x + self.image.width / 2 * self.scale, self.y - self.image.height / 2 * self.scale)) return shortestDistance
def on_mouse_press(self, x, y, button, modifiers): if button is pyglet.window.mouse.LEFT: if self.current is not None: if util.distance((x, y), self.position) < self.interaction_range: self.effect = True self.effect_x = x self.effect_y = y
def search(self, objects): """ Search for a target """ if self.time % 30 == 0: # self.search_radius += 10 # Faster, but less cool visually #for obj in [obj for obj in objects if obj.Type == 'matter']: #if self.clicked == True: self.inview = [self] for obj in objects: if abs(obj.x - self.x) < self.search_radius: if abs(obj.y - self.y) < self.search_radius: if util.distance( (obj.x, obj.y), (self.x, self.y)) < self.search_radius**2: self.inview.append(obj) if obj.Type == 'matter': self.search_radius = 20 self.target = obj return random.randint(-1, 0) else: return 0
def asteroids(num_asteroids, player_position, batch=None): """ Used to spawn the asteroids in valid locations. Valid locations are defined by not being too close to the player that there is a collision instantly but not being so far away as to be beyond the game window. Inputs ============================================================================ num_asteroids int; the number of asteroids to be spawned at this function call player_position vector; the x and y coordinates, measured in pixels, of where the player_ship is located batch pyglet batch; the name of the batch which the asteroids are meant to be a part of Outputs ============================================================================ asteroids list; a reference to all of the asteroid game objects that are present in the game window. """ asteroids = [] for i in range(num_asteroids): asteroid_x, asteroid_y = player_position while util.distance((asteroid_x, asteroid_y), player_position) < 100: asteroid_x = random.randint(0, 1000) asteroid_y = random.randint(0, 800) new_asteroid = asteroid.Asteroid(x=asteroid_x, y=asteroid_y, batch=batch) new_asteroid.rotation = random.randint(0, 360) new_asteroid.velocity_x = random.random() * 40 new_asteroid.velocity_y = random.random() * 40 asteroids.append(new_asteroid) return asteroids
def collides_with(self, other_object): if not self.reacts_to_bullets and other_object.is_bullet: return False if self.is_bullet and not other_object.reacts_to_bullets: return False collision_distance = self.image.width / 2 + other_object.image.width / 2 actual_distance = util.distance(self.position, other_object.position) return (actual_distance <= collision_distance)
def collides_with(self, other_object): if not self.reacts_to_bullets and other_object.is_bullet: return False if self.is_bullet and not other_object.reacts_to_bullets: return False collision_distance = self.image.width/2 + other_object.image.width/2 actual_distance = util.distance(self.position, other_object.position) return (actual_distance <= collision_distance)
def collides_with(self, other_object): '''使其在适当的情况下忽略子弹''' if not self.reacts_to_bullets and other_object.is_bullet: return False if self.is_bullet and not other_object.reacts_to_bullets: return False collision_distance = self.image.width * 0.5 * self.scale + other_object.image.width * 0.5 * other_object.scale actual_distance = util.distance(self.position, other_object.position) return (actual_distance <= collision_distance)
def asteroids(num_asteroids, player_position, batch=None): asteroids = [] for i in range(num_asteroids): asteroid_x, asteroid_y = player_position while distance((asteroid_x, asteroid_y), player_position) < 100: asteroid_x = random.randint(0, 800) asteroid_y = random.randint(0, 600) new_asteroid = asteroid.Asteroid(x=asteroid_x, y=asteroid_y, batch=batch) new_asteroid.rotation = random.randint(0, 360) new_asteroid.velocity_x = random.random() * 40 new_asteroid.velocity_y = random.random() * 40 asteroids.append(new_asteroid) return asteroids
def asteroids(num_asteroids, player_position, batch=None): asteroids = [] for i in range(num_asteroids): asteroid_x, asteroid_y = player_position while distance((asteroid_x, asteroid_y), player_position) < 100: asteroid_x = random.randint(0, 800) asteroid_y = random.randint(0, 600) new_asteroid = physicalobject.PhysicalObject(img=resources.asteroid_image, x=asteroid_x, y=asteroid_y, batch=batch) new_asteroid.rotation = random.randint(0, 360) new_asteroid.velocity_x = random.random()*40 new_asteroid.velocity_y = random.random()*40 asteroids.append(new_asteroid) return asteroids
def collides_with(self,other): actual_dist=util.distance((self.x,self.y),(other.x,other.y)) min_dist=((self.image.height/2)*math.sqrt(2))+((other.image.height/2)*math.sqrt(2)) #if collision detected, check edges of each for overlap if actual_dist<min_dist: if self.bottom<=other.top and self.top>=other.bottom and self.left<=other.right and self.right>=other.left: return True else: return False else: return False
def search(self): """ Search field of view for food target """ self.inview = [self] self.target = None self.search_radius = 100 for obj in (i[1] for i in self.objects): if abs(obj.x - self.x) < self.search_radius: if abs(obj.y - self.y) < self.search_radius: if util.distance((obj.x, obj.y), (self.x, self.y)) < self.search_radius**2: self.inview.append(obj) if obj.Type == 'food': self.target = obj
def collides_with(self, other_object): if not self.is_bullet and not other_object.is_bullet: return False if self.is_bullet: if not self.is_friendly and other_object.is_alien: return False if other_object.is_bullet: if not other_object.is_friendly and self.is_alien: return False collision_dist= self.image.width / 2 + other_object.image.width / 2 actual_dist = util.distance(self.position, other_object.position) return (actual_dist <= collision_dist)
def asteroids(num_asteroids, player_position, batch=None): asteroids = [] for i in range(num_asteroids): asteroid_x, asteroid_y = player_position while util.distance((asteroid_x, asteroid_y), player_position) < 100: asteroid_x = random.randint(0, 800) asteroid_y = random.randint(0, 600) new_asteroid = physicalobject.PhysicalObject( img=resources.asteroid_image, x=asteroid_x, y=asteroid_y, batch=batch) new_asteroid.rotation = random.randint(0, 360) new_asteroid.velocity_x = random.random()*40 new_asteroid.velocity_y = random.random()*40 asteroids.append(new_asteroid) return asteroids
def collides_with(self, other_object): if util.distance_x(self.position, other_object.position) <= 50: collision_distance = (self.image.width * 0.5 * self.scale + \ other_object.scale * other_object.image.width * 0.5) if util.distance_x(self.position, other_object.position) <= collision_distance: if util.distance_y( self.position, other_object.position) <= collision_distance: collision_distance_squared = collision_distance**2 actual_distance_squared = util.distance( self.position, other_object.position) return (actual_distance_squared <= collision_distance_squared) else: return False else: return False
def collides_with(self, other_object): """ Check if object collides with another object TODO: Optimize more precisely - consider grid-based approach """ if util.distance_x(self.position, other_object.position) <= 50: collision_distance = (self.image.width * 0.5 * self.scale + \ other_object.scale * other_object.image.width * 0.5) if util.distance_x(self.position, other_object.position) <= collision_distance: if util.distance_y(self.position, other_object.position) <= collision_distance: collision_distance_squared = collision_distance ** 2 actual_distance_squared = util.distance(self.position, other_object.position) if self.time % 10 == 0: return (actual_distance_squared <= collision_distance_squared) else: return False else: return False else: return False
def search(self, objects): """ Search for a target """ if self.time%30 == 0: # self.search_radius += 10 # Faster, but less cool visually #for obj in [obj for obj in objects if obj.Type == 'matter']: #if self.clicked == True: self.inview = [self] for obj in objects: if abs(obj.x - self.x) < self.search_radius: if abs(obj.y - self.y) < self.search_radius: if util.distance((obj.x, obj.y), (self.x, self.y)) < self.search_radius**2: self.inview.append(obj) if obj.Type == 'matter': self.search_radius = 20 self.target = obj return random.randint(-1,0) else: return 0
def collides_with(self, other_object): """ Used to determine if any two game objects are about to collide. If they are, then this function is used to initiate handling of that collision. Inputs ============================================================================== other_object game object; the other item in the game window that it is being determined whether there will be a collision between the object calling the function and this other_object Outputs ============================================================================== True / False boolean; true for if there will be a collision and false if there will not be. """ # Special conditions for collision between a bullet and the player if not self.reacts_to_bullets and other_object.is_bullet: return False if self.is_bullet and not other_object.reacts_to_bullets: return False collision_distance = self.image.width / 2 + other_object.image.width / 2 actual_distance = util.distance(self.position, other_object.position) return (actual_distance <= collision_distance)
def update(dt): global tokens_collected, max_tokens, level, level_timer, score, player_has_binder player_dead = False victory = False if hidden in game_objects: if util.distance((player.x, player.y), (hidden.x, hidden.y)) <= 150: hidden.found = True if level == 1: level_timer -= dt hud.time_label.text = 'Time:%s' % (str(int(level_timer))) if level_timer < 0: level += 1 clear_level() load_level(level) pyglet.clock.unschedule(update) if level_timer < 11.0: hud.time_label.color = (205, 17, 23, 255) for i in xrange(len(game_objects)): for j in xrange(i + 1, len(game_objects)): obj_1 = game_objects[i] obj_2 = game_objects[j] #make sure objects are not dead if not obj_1.dead and not obj_2.dead: if obj_1.__class__ is not obj_2.__class__: if obj_1.collides_with(obj_2) or obj_2.collides_with( obj_1): obj_1.handle_collision_with(obj_2) obj_2.handle_collision_with(obj_1) for obj in game_objects: obj.update(dt) #get rid of dead objects for to_remove in [obj for obj in game_objects if obj.dead]: #remove the object from the batch and the game_objects list to_remove.delete() game_objects.remove(to_remove) if to_remove == player: player_dead = True #Adjust the score if the dead item was worth points if isinstance(to_remove, btoken.Btoken): score += 5 hud.score_label.text = "Score:" + str(score) sound_fx.tokensound.play() player_has_binder = True if level == 1: if isinstance(to_remove, token.Token): tokens_collected += 1 #sound_fx.pop_sound.play() score += 20 hud.score_label.text = 'Score:' + str(score) if tokens_collected == max_tokens: victory = True sound_fx.level_clear_sound.play() if level == 2: if isinstance(to_remove, token.Token2): tokens_collected += 1 #sound_fx.pop_sound.play() score += 20 hud.score_label.text = 'Score:' + str(score) if tokens_collected == max_tokens: victory = True sound_fx.level_clear_sound.play() ''' #Adjust the score if the dead item was worth points if isinstance(to_remove,pwr_up.PwrUp): score+=to_remove.pt_value hud.score_label.text='Score:'+str(score) sound_fx.power_up_sound.play() avatar.process_power_up() #Adjust the score if the dead item was worth points if isinstance(to_remove,enemy.Enemy): sound_fx.pop_sound.play() score+=to_remove.pt_value score_label.text='Score:'+str(score) ''' #check for win/lose conditions if player_dead: #if len(player_lives)>0: pyglet.clock.unschedule(update) clear_level() load_level(level) #else: # hud.game_over_label.y=viewport.v_ctr elif victory: level += 1 #hud.level_label.text='Level:'+str(level) pyglet.clock.unschedule(update) clear_level() level_timer = 0 load_level(level)
def check_collision(self, other_obj): collision_distance = self.radius + other_obj.radius actual_distance = util.distance(self.position, other_obj.position) return True if actual_distance < collision_distance else False
def collides_with(self, other_object): collision_distance = self.image.width/2 + other_object.image.width/2 actual_distance = util.distance((self.x, self.y), (other_object.x, other_object.y)) return actual_distance <= collision_distance
def collides_with(self, other_object): collision_distance = self.image.width/2 + other_object.image.width/2 actual_distance = util.distance(self.position, other_object.position) return (actual_distance <= collision_distance)
def update(dt): global tokens_collected, max_tokens, level, score, player_lives player_dead=False victory=False if level==1: if hidden in game_objects: if util.distance((player.x,player.y),(hidden.x,hidden.y))<=150: hidden.found=True elif level==2: pass elif level==3: pass #look for collisions for i in xrange(len(game_objects)): for j in xrange(i+1,len(game_objects)): obj_1=game_objects[i] obj_2=game_objects[j] #make sure objects are not dead if not obj_1.dead and not obj_2.dead: if obj_1.__class__ is not obj_2.__class__: if obj_1.collides_with(obj_2) or obj_2.collides_with(obj_1): obj_1.handle_collision_with(obj_2) obj_2.handle_collision_with(obj_1) #call the update method of every game object for obj in game_objects: obj.update(dt) #get rid of dead objects for to_remove in [obj for obj in game_objects if obj.dead]: #remove the object from the batch and the game_objects list to_remove.delete() game_objects.remove(to_remove) if to_remove==player: player_dead=True #Adjust the score if the dead item was worth points if isinstance(to_remove,token.Token): tokens_collected+=1 sound_fx.pop_sound.play() score+=10 hud.score_label.text='Score:'+str(score) #if tokens_collected==max_tokens: # victory=True # sound_fx.level_clear_sound.play() #Adjust the score if the dead item was worth points if isinstance(to_remove,power_up.Power_up): player.process_power_up(to_remove) ## score+=to_remove.pt_value ## hud.score_label.text='Score:'+str(score) ## sound_fx.power_up_sound.play() #Adjust the score if the dead item was worth points if isinstance(to_remove,extra_life.Extra_life): player_lives+=1 hud.lives_label.text="Lives: %i"%(player_lives) ## score+=to_remove.pt_value ## hud.score_label.text='Score:'+str(score) ## sound_fx.power_up_sound.play() ## #Adjust the score if the dead item was worth points ## if isinstance(to_remove,enemy.Enemy): ## sound_fx.pop_sound.play() ## score+=to_remove.pt_value ## score_label.text='Score:'+str(score) #check for win/lose conditions if tokens_collected==max_tokens and\ util.distance((player.x,player.y),(hud.d.x,hud.d.y))<=25: victory=True if player_dead: player_lives-=1 hud.lives_label.text="Lives:%i"%(player_lives) pyglet.clock.unschedule(update) pyglet.clock.unschedule(player.revert) if player_lives>0: clear_level() load_level(level) else: hud.game_over_label.y=viewport.v_ctr elif victory: if level<3: level+=1 pyglet.clock.unschedule(player.revert) pyglet.clock.unschedule(update) clear_level() load_level(level) else: hud.win_label.y=viewport.v_ctr pyglet.clock.unschedule(update)