示例#1
0
    def __init__(self, MANAGER, WINDOW):
        super().__init__(MANAGER, WINDOW)
        w_shape = loads(CONFIG.get('window', 'shape'))

        self.bg_img = pygame.image.load(CONFIG.get(
            'window', 'background_fp')).convert_alpha()
        self.bg_img = pygame.transform.scale(self.bg_img, w_shape)
示例#2
0
    def __init__(self, MANAGER, WINDOW):
        super().__init__(MANAGER, WINDOW)
        w_shape = loads(CONFIG.get('window', 'shape'))
        self.fps = CONFIG.getint('window', 'fps')

        # bg for background
        self.menu_bg = pygame.image.load(CONFIG.get(
            'window', 'background_fp')).convert_alpha()
        self.menu_bg = pygame.transform.scale(self.menu_bg, w_shape)
示例#3
0
    def __init__(self, MANAGER, WINDOW):
        super().__init__(MANAGER, WINDOW)
        w_shape = loads(CONFIG.get('window', 'shape'))
        fps = CONFIG.getint('window', 'fps')
        ''' Initialize graphics '''
        # Initializing in boids_instances.py caused errors
        bg_img = pygame.image.load(CONFIG.get(
            'window', 'background_fp')).convert_alpha()
        tuna_img = pygame.image.load('images/tuna.png').convert_alpha()
        gw_shark_img = pygame.image.load(
            'images/great_white_shark.png').convert_alpha()
        mine_img = pygame.image.load('images/mine.png').convert_alpha()

        bg_img = pygame.transform.scale(bg_img, w_shape)
        tuna_img = pygame.transform.scale(tuna_img, (70, 30))
        gw_shark_img = pygame.transform.scale(gw_shark_img, (200, 150))
        mine_img = pygame.transform.scale(mine_img, (90, 90))

        panel = pygame.Rect(w_shape[0] * 0.95, 0, w_shape[0] * 0.05,
                            w_shape[1])
        panel_border = pygame.Rect(w_shape[0] * 0.95, -10, w_shape[0] * 0.10,
                                   w_shape[1] * 1.1)
        '''''' '''''' '''''' '''''' ''

        img_list = [tuna_img, mine_img, gw_shark_img]

        # alter w_shape to fit simulation area
        w_shape = (int(w_shape[0] * 0.95), w_shape[1])
        simulation_area = pygame.Rect(0, 0, w_shape[0], w_shape[1])

        # Get the addresses of instance lists
        preys = boids_i.preys
        obstacles = boids_i.obstacles
        predators = boids_i.predators

        # Turn locals to attributes
        self.__dict__.update(locals())
        self.initialize()
示例#4
0
'''
Initializing instances with the purpose
the be used in the boids_state
'''
import pygame
from objects import interface
from main import CONFIG
from json import loads

from objects import tuna
from objects import uw_mine
from objects import shark

# pygame.init()
pygame.font.init()
WINDOW_SHAPE = loads(CONFIG.get('window', 'shape'))
font = CONFIG.get('general', 'font')
font_size = int(WINDOW_SHAPE[0] *
                CONFIG.getfloat('boids_state', 'font_window_ratio'))
font_color_buttons = loads(CONFIG.get('boids_state', 'font_color_buttons'))
font_color_levers = loads(CONFIG.get('boids_state', 'font_color_levers'))
'''
Define prey, obstacle and predator list here since the lever_with_fishclass
levers needs the addresses 
'''
preys = []
obstacles = []
predators = []
'''
Mass slaughter button
'''
示例#5
0
class shark(general_fish.fish):
    eat_radius = CONFIG.getint('shark', 'eat_radius')
    obstacle_radius = CONFIG.getint('shark', 'obstacle_radius')
    hunt_radius = CONFIG.getint('shark', 'hunt_radius')

    f_hunt = CONFIG.getfloat('shark', 'f_hunt')
    f_repel_obstacle = CONFIG.getfloat('shark', 'f_repel_obstacle')
    f_repel_border = CONFIG.getfloat('shark', 'f_repel_border')

    def __init__(self, pos, img):
        super().__init__(pos, img)

    def draw(self, WINDOW):
        if (self.vel[0] > 0):
            new_image = pygame.transform.flip(self.img, 0, 1)
            new_image = pygame.transform.rotate(
                new_image,
                np.degrees(np.arctan2(*self.vel)) + 90)
        else:
            new_image = pygame.transform.rotate(
                self.img,
                np.degrees(np.arctan2(*self.vel)) + 90)

        img_center = new_image.get_rect(center=self.pos)
        WINDOW.blit(new_image, img_center)

    def percieve_obstacles(self, dist_vecs, dist_vecs_norm):
        obstacle_mask = (dist_vecs_norm <
                         self.obstacle_radius) & (dist_vecs_norm != 0)

        # dirs for directions (directions represented as normalized vectors)
        self.obstacles_dirs = dist_vecs[obstacle_mask] / dist_vecs_norm[
            obstacle_mask].reshape((-1, 1))

    def percieve_preys(self, dist_vecs, dist_vecs_norm):
        self.eat_mask = (dist_vecs_norm < self.eat_radius) & (dist_vecs_norm !=
                                                              0)
        far_mask = (dist_vecs_norm < self.hunt_radius) & (dist_vecs_norm != 0)
        self.prey_dirs = dist_vecs[far_mask] / dist_vecs_norm[far_mask].reshape(
            (-1, 1))

    def eat(self, preys):
        if (any(self.eat_mask)):
            # Flipped eat mask array to perserve
            # order in preys matrix
            for i in np.nonzero(self.eat_mask)[0][::-1]:
                if (i < len(preys)):
                    del (preys[i])

    def hunt(self):
        if (self.prey_dirs.shape[0]):
            self.recalc_vel.append(self.f_hunt * self.prey_dirs.mean(axis=0))
        else:
            self.vel = self.vel * 0.9

    def avoid_obstacles(self):
        if (self.obstacles_dirs.shape[0]):
            self.recalc_vel.append(self.f_repel_obstacle *
                                   self.obstacles_dirs.mean(axis=0))

    def stay_in_border(self, w_shape):
        if (self.pos[0] > w_shape[0] * 0.90):
            self.recalc_vel.append(np.array([-self.f_repel_border, 0]))
        if (self.pos[0] < w_shape[0] * 0.10):
            self.recalc_vel.append(np.array([self.f_repel_border, 0]))

        if (self.pos[1] > w_shape[1] * 0.90):
            self.recalc_vel.append(np.array([0, -self.f_repel_border]))
        if (self.pos[1] < w_shape[1] * 0.10):
            self.recalc_vel.append(np.array([0, self.f_repel_border]))

    def apply_behavior(self, state, i):
        '''
        Applies all methods necessary to give a complete behavior
        
        The method takes in that boids_state object as an input. 
        The boids state object carries dist_matrix and dist_matrix_norms 
        as instance attributes. The percieve methods are then feeded with 
        the appropriate slices of the matrices. An iterator value is 
        also passed to this to slice.

        [:state.num_preys] slices info about preys
        [state.num_preys:state.obstacle_index] slices info about obstacles
        [state.obstacle_index:] slices info about predators
        '''
        self.randval = state.randvals[i]

        offset_i = i + state.obstacle_index

        self.percieve_obstacles(
            dist_vecs=state.dist_matrix[offset_i]
            [state.num_preys:state.obstacle_index],
            dist_vecs_norm=state.dist_matrix_norms[offset_i]
            [state.num_preys:state.obstacle_index])

        self.percieve_preys(
            dist_vecs=state.dist_matrix[offset_i][:state.num_preys],
            dist_vecs_norm=state.dist_matrix_norms[offset_i][:state.num_preys])
        self.hunt()
        self.eat(state.preys)
        self.avoid_obstacles()
        self.stay_in_border(state.w_shape)
        self.motion()
示例#6
0
 def __init__(self, WINDOW, MANAGER):
     super().__init__(WINDOW, MANAGER)
     self.fps = CONFIG.getint('window', 'fps')
示例#7
0
import pygame 
import numpy as np 
from main import CONFIG
n_strong_f = CONFIG.getfloat('particles_state','nuclear_strong_force')
n_weak_f = CONFIG.getfloat('particles_state', 'nuclear_weak_force')
drag_f = CONFIG.getfloat('particles_state', 'drag_force')

'''
I wrote most of this code early on and didn't really comment. 
Now I dont really get it, so I wont bother commenting
'''

class particle:
    def __init__(self, pos):
        pos = np.array(pos)
        vel = np.zeros((2))
        acc = np.zeros((2))
        x0 = np.array([20,20])
        friends = []
        friends_dist_vecs = []
        self.__dict__.update(locals())
    

    def draw(self, WINDOW):
        pygame.draw.circle(
            WINDOW,
            (255,255,255),
            self.pos.astype('int'),
            5
        )
    
示例#8
0
class tuna(general_fish.fish):
    repel_radius = CONFIG.getint('tuna', 'repel_radius')
    attract_radius = CONFIG.getint('tuna', 'attract_radius')
    obstacle_radius = CONFIG.getint('tuna', 'obstacle_radius')
    foe_radius = CONFIG.getint('tuna', 'foe_radius')

    f_attract_friends = CONFIG.getfloat('tuna', 'f_attract_friends')

    f_repel_friends = CONFIG.getfloat('tuna', 'f_repel_friends')
    f_repel_obstacles = CONFIG.getfloat('tuna', 'f_repel_obstacles')
    f_repel_foes = CONFIG.getfloat('tuna', 'f_repel_foes')

    def __init__(self, pos, img):
        super().__init__(pos, img)

    def draw(self, WINDOW):
        '''
        Calculates orientation and blits instance to 
        WINDOW.
        '''

        # The in
        if (self.vel[0] < 0):
            blit_image = pygame.transform.flip(self.img, 0, 1)
            blit_image = pygame.transform.rotate(
                blit_image,
                np.degrees(np.arctan2(*self.vel)) - 90)
        else:
            blit_image = pygame.transform.rotate(
                self.img,
                np.degrees(np.arctan2(*self.vel)) - 90)
        # Get a new rect with the center of the old rect.
        img_center = blit_image.get_rect(center=self.pos)
        WINDOW.blit(blit_image, img_center)

    def percieve_friends(self, dist_vecs, dist_vecs_norm, vel_vecs):
        '''
        Slices dist_vecs and dist_vecs_norms with respect to all tunas
        to get directions to all tunas
        '''
        close_mask = (dist_vecs_norm < self.repel_radius) & (dist_vecs_norm !=
                                                             0)
        medium_mask = (dist_vecs_norm < self.attract_radius) & (dist_vecs_norm
                                                                != 0)
        '''
        dirs for directions (directions represented as unit vectors)
        '''
        self.friends_close_dirs = dist_vecs[close_mask] / dist_vecs_norm[
            close_mask].reshape((-1, 1))
        self.friends_medium_dirs = dist_vecs[medium_mask] / dist_vecs_norm[
            medium_mask].reshape((-1, 1))

        self.friends_vels = vel_vecs[medium_mask]

    def percieve_obstacles(self, dist_vecs, dist_vecs_norm):
        '''
        Slices dist_vecs and dist_vecs_norms with respect to obstacles
        to get directions to all obstacles
        '''
        medium_mask = (dist_vecs_norm < self.obstacle_radius) & (dist_vecs_norm
                                                                 != 0)
        self.obstacles_dirs = dist_vecs[medium_mask] / dist_vecs_norm[
            medium_mask].reshape((-1, 1))

    def percieve_foes(self, dist_vecs, dist_vecs_norm):
        '''
        Slices dist_vecs and dist_vecs_norms with respect to foes
        to get directions to all foes (sharks)
        '''
        far_mask = (dist_vecs_norm < self.foe_radius) & (dist_vecs_norm != 0)
        self.foes_dirs = dist_vecs[far_mask] / dist_vecs_norm[far_mask].reshape(
            (-1, 1))

    def attraction(self):
        '''
        Cohesion rule
        '''
        if (self.friends_medium_dirs.shape[0]):
            self.recalc_vel.append(self.f_attract_friends *
                                   self.friends_medium_dirs.mean(axis=0))

    def repulsion(self):
        '''
        Separation rule
        '''

        # Force norm from tunas within inner tuna radius
        if (self.friends_close_dirs.shape[0]):
            self.recalc_vel.append(self.f_repel_friends *
                                   self.friends_close_dirs.mean(axis=0))

        # Force norm from obstacle within obstacle radius
        if (self.obstacles_dirs.shape[0]):
            self.recalc_vel.append(self.f_repel_obstacles *
                                   self.obstacles_dirs.mean(axis=0))

        # Force norm from sharks within foe radius
        if (self.foes_dirs.shape[0]):
            self.recalc_vel.append(self.f_repel_foes *
                                   self.foes_dirs.mean(axis=0))

    def match_vel(self):
        '''
        Alignment rule
        '''
        if (self.friends_medium_dirs.shape[0] > 1):
            self.recalc_vel.append(
                self.friends_vels.mean(axis=0) * 0.20 + self.vel * 0.80 -
                self.vel)

    def apply_behavior(self, state, i):
        '''
        Applies all methods necessary to give a complete behavior
        
        The method takes in that boids_state object as an input. 
        The boids state object carries dist_matrix and dist_matrix_norms 
        as instance attributes. The percieve methods are then feeded with 
        the appropriate slices of the matrices. An iterator value is 
        also passed to this to slice.

        [:state.num_preys] slices info about preys
        [state.num_preys:state.obstacle_index] slices info about obstacles
        [state.obstacle_index:] slices info about predators
        '''
        self.percieve_friends(
            dist_vecs=state.dist_matrix[i][:state.num_preys],
            dist_vecs_norm=state.dist_matrix_norms[i][:state.num_preys],
            vel_vecs=state.vel_vecs[:state.num_preys])

        self.percieve_obstacles(dist_vecs=state.dist_matrix[i]
                                [state.num_preys:state.obstacle_index],
                                dist_vecs_norm=state.dist_matrix_norms[i]
                                [state.num_preys:state.obstacle_index])

        self.percieve_foes(
            dist_vecs=state.dist_matrix[i][state.obstacle_index:],
            dist_vecs_norm=state.dist_matrix_norms[i][state.obstacle_index:])

        self.attraction()
        self.repulsion()
        self.match_vel()
        self.wrap_x(state.w_shape)
        self.avoid_y_borders(state.w_shape)
        self.randval = state.randvals[i]
        self.motion()
示例#9
0
    def __init__(self, MANAGER, WINDOW, POOL, QUEUE):
        super().__init__(MANAGER, WINDOW)
        fps = CONFIG.getint('window', 'fps')
        particles_list = []

        self.__dict__.update(locals())
示例#10
0
'''
Initializing instances with the purpose
the be used in the menu_state
'''
import pygame
from objects import interface
from main import CONFIG
from json import loads
from objects import tuna

pygame.font.init()
WINDOW_SHAPE = loads(CONFIG.get('window', 'shape'))
font = CONFIG.get('general', 'font')
text_x_pos = WINDOW_SHAPE[0] * CONFIG.getfloat('menu_state', 'text_x_ratio')
font_size = int(WINDOW_SHAPE[0] *
                CONFIG.getfloat('menu_state', 'font_window_ratio'))
font_color = loads(CONFIG.get('menu_state', 'font_color'))
'''
text_list are for text objects without any 
further functionality
'''
text_list = [
    interface.text_float(
        text='Boids & Particle Physics Simulator',
        pos=(text_x_pos, WINDOW_SHAPE[1] * 0.05),
        color=font_color,
        font=font,
        font_size=int(font_size * 1.8),
    ),
]
'''