Best Python code snippet using ATX
thomas'_main.py
Source:thomas'_main.py  
1import arcade2import math3import random4WIDTH = 8405HEIGHT = 5806class Bullet(arcade.Sprite):7    def update(self):8        # Barrier checks (Image must completely disappear from screen)9        if self.center_x - self.width > WIDTH:10            self.kill()11        elif self.center_x + self.width < 0:12            self.kill()13        elif self.center_y - self.height > HEIGHT:14            self.kill()15        elif self.center_y + self.height < 0:16            self.kill()17        self.center_x += self.change_x18        self.center_y += self.change_y19class Bolder(arcade.Sprite):20    def update(self):21        # Border checks22        if self.center_x - self.width / 2 < 0:23            self.change_x *= -124        elif self.center_x + self.width / 2 > WIDTH:25            self.change_x *= -126        elif self.center_y + self.height / 2 > HEIGHT:27            self.change_y *= -128        elif self.center_y - self.height / 2 < 0:29            self.change_y *= -130        self.center_y += self.change_y31        self.center_x += self.change_x32class Player(arcade.Sprite):33    def update(self):34        self.center_x += self.change_x35        self.center_y += self.change_y36        # Border checks37        if self.center_x - self.width / 2 < 0:38            self.center_x = self.width / 239            self.change_x = 040        elif self.center_x + self.width / 2 > WIDTH:41            self.center_x = WIDTH - self.width / 242            self.change_x = 043        if self.center_y + self.height / 2 > HEIGHT:44            self.center_y = HEIGHT - self.height / 245            self.change_y = 046        elif self.center_y - self.height / 2 < 0:47            self.center_y = self.height / 248            self.change_y = 049def bolder_split(bolder,game_info): # Bolder splits into smaller pieces50    # Get info51    bolder.kill()52    x = bolder.center_x53    y = bolder.center_y54    scale = bolder.scale55    # Make new bolders56    for i in range(random.randint(2, 5)):57        new_scale = scale * 0.558        # Check to see if bolder is too small59        if new_scale >= 0.05:60            skin = random.choice(game_info.bolder_skins)61            bolder = Bolder(skin, new_scale, center_x=x,center_y=y)62            bolder.change_x = random.uniform(-5,5)63            bolder.change_y = random.uniform(-5,5)64            game_info.bolder_list.append(bolder)65class Game():66    def __init__(self, key_map, frame_count, game_score, bolder_list, bolder_skins, player, bullet_list):67        self.key_map = key_map68        self.frame_count = frame_count69        self.game_score = game_score70        self.bolder_list = bolder_list71        self.bolder_skins = bolder_skins72        self.player = player73        self.bullet_list = bullet_list74    def update(self):75        # Increment score and frames76        self.frame_count += 177        if self.frame_count % 20 == 0:78            self.game_score += 179        # Bolder creation80        if self.frame_count % 280 == 0:81            x = random.randint(100, WIDTH - 100)82            y = random.randint(100, HEIGHT - 100)83            bolder = Bolder('Image Folder/spaceMeteors_001.png', 0.2, center_x=x, center_y=y)84            bolder.change_x = random.uniform(-5, 5 + 1)85            bolder.change_y = random.uniform(-5, 5 + 1)86            self.bolder_list.append(bolder)87        # Check for movement speeds/directions88        max_speed = 489        if self.key_map['up pressed'] == True:90            if abs(self.player.change_y + 0.5) < max_speed:91                self.player.change_y += .592        elif self.key_map['down pressed'] == True:93            if abs(self.player.change_y - 0.5) < max_speed:94                self.player.change_y -= .595        if self.key_map['right pressed'] == True:96            if abs(self.player.change_x + 0.5) < max_speed:97                self.player.change_x += .598        elif self.key_map['left pressed'] == True:99            if abs(self.player.change_x - 0.5) < max_speed:100                self.player.change_x -= .5101        self.player.update()102        # Collision between bullet and bolder103        for bullet in self.bullet_list:104            bullet_bolder_collision = arcade.check_for_collision_with_list(bullet, self.bolder_list)105            for bolder in bullet_bolder_collision:106                bullet.kill()107                bolder_split(bolder,self)108        # Collision between player and bolder109        for bolder in self.bolder_list:110            player_bolder_collision = arcade.check_for_collision(self.player, bolder)111            if player_bolder_collision == True:112                return 'Contact'113        self.bullet_list.update()114        self.bolder_list.update()115    def on_draw(self):116        # Score117        arcade.draw_text('Score: {}'.format(self.game_score), WIDTH - 150, 25, arcade.color.WHITE, 22)118        # Player119        self.player.draw()120        # Bullets121        self.bullet_list.draw()122        # Bolders123        self.bolder_list.draw()124    def on_key_press(self,key):125        if key == arcade.key.W:126            self.key_map['up pressed'] = True127        if key == arcade.key.S:128            self.key_map['down pressed'] = True129        if key == arcade.key.A:130            self.key_map['left pressed'] = True131        if key == arcade.key.D:132            self.key_map['right pressed'] = True133    def on_key_release(self,key):134        if key == arcade.key.W:135            self.key_map['up pressed'] = False136        if key == arcade.key.S:137            self.key_map['down pressed'] = False138        if key == arcade.key.A:139            self.key_map['left pressed'] = False140        if key == arcade.key.D:141            self.key_map['right pressed'] = False142    def on_mouse_press(self,x,y):143        # CREATE BULLET144        mouse_x = x145        mouse_y = y146        # Setup trig ratios147        x_diff = mouse_x - self.player.center_x148        y_diff = mouse_y - self.player.center_y149        angle = math.atan2(y_diff, x_diff)150        bullet_angle = math.degrees(angle) - 90151        bullet_speed = 14152        d_x = math.cos(angle) * bullet_speed153        d_y = math.sin(angle) * bullet_speed154        # Finished product155        bullet = Bullet('Image Folder/Bullet.png', 0.6, center_x=self.player.center_x, center_y=self.player.center_y)156        bullet.change_x = d_x157        bullet.change_y = d_y158        bullet.angle = bullet_angle159        self.bullet_list.append(bullet)160    def on_mouse_motion(self,x, y):161        # Gether info162        mouse_x = x163        mouse_y = y164        # Setup trig ratios165        x_diff = mouse_x - self.player.center_x166        y_diff = mouse_y - self.player.center_y167        angle = math.atan2(y_diff, x_diff)168        new_angle = math.degrees(angle) - 90169        self.player.angle = new_angle170class Button():171    def __init__(self, x, y, w, h, default_color, hover_color, current_color, text):172        self.x = x173        self.y = y174        self.w = w175        self.h = h176        self.default_color = default_color177        self.hover_color = hover_color178        self.current_color = current_color179        self.text = text180    def draw(self):181        arcade.draw_rectangle_filled(self.x, self.y, self.w, self.h, self.current_color)182        arcade.draw_text(self.text,self.x,self.y,arcade.color.BLACK, 15, anchor_x='center', anchor_y='center')183    def on_hover(self,mouse_x,mouse_y):184        if self.x - self.w / 2 <= mouse_x <= self.x + self.w / 2 and self.y - self.h / 2 <= mouse_y <= self.y + self.h / 2:185            self.current_color = self.hover_color186        else:187            self.current_color = self.default_color188    def on_click(self, mouse_x, mouse_y):189        if self.x - self.w / 2 <= mouse_x <= self.x + self.w / 2 and self.y - self.h / 2 <= mouse_y <= self.y + self.h / 2:190            return True191        return False192class Menu():193    def __init__(self): # Why do we still need __init__194        global instructions_option, game_option, credits_option195        default_color = (240, 63, 63)196        hover_color = (204, 255, 229)197        current_color = default_color198        instructions_option = Button(WIDTH / 2, HEIGHT / 2 + 100, 100, 35,199                            default_color, hover_color, current_color, 'Instructions')200        game_option = Button(WIDTH / 2, HEIGHT / 2, 100, 35,201                            default_color, hover_color, current_color, 'Game')202        credits_option = Button(WIDTH / 2, HEIGHT / 2 - 100, 100, 35,203                            default_color, hover_color, current_color, 'Credits')204    def draw(self):205        instructions_option.draw()206        game_option.draw()207        credits_option.draw()208        arcade.draw_text('Laser Quest', WIDTH / 2,HEIGHT / 2 + 225,arcade.color.BLACK, 45,209                         align='center', anchor_y='center', anchor_x='center')210    def on_hover(self, mouse_x, mouse_y):211        instructions_option.on_hover(mouse_x, mouse_y)212        game_option.on_hover(mouse_x, mouse_y)213        credits_option.on_hover(mouse_x, mouse_y)214    def on_click(self,mouse_x,mouse_y):215        # Is the click registered onto a box?216        if instructions_option.on_click(mouse_x,mouse_y) == True:217            return 'instructions'218        elif game_option.on_click(mouse_x,mouse_y) == True:219            return 'game'220        elif credits_option.on_click(mouse_x, mouse_y) == True:221            return 'credits'222        # None are clicked223        return False224class Instructions():225    def __init__(self):226        default_color = (240, 63, 63)227        hover_color = (204, 255, 229)228        current_color = default_color229        global back_option230        back_option = Button(75, 50, 75, 50, default_color, hover_color, current_color, 'Back')231    def draw(self):232        # Set up instructions txt233        arcade.draw_text('Instructions', WIDTH/2, HEIGHT/2 + 200, arcade.color.BLACK, 35,234                         anchor_x='center', anchor_y='center', align='center')235        arcade.draw_text('Movement \n\n W,A,S,D to move.', WIDTH/2, HEIGHT/2 + 100, arcade.color.ORANGE, 25,236                         anchor_x='center', anchor_y='center', align='center')237        arcade.draw_text('Shooting \n\n Left click to shoot.', WIDTH/2, HEIGHT/2, arcade.color.RED, 25,238                         anchor_x='center', anchor_y='center', align='center')239        arcade.draw_text('Aiming \n\n Cursor to aim.', WIDTH/2, HEIGHT/2 - 100, arcade.color.GRAPE, 25,240                         anchor_x='center', anchor_y='center', align='center')241        # Set up back button242        back_option.draw()243    def on_click(self,mouse_x,mouse_y):244        # Box click checks245        if back_option.on_click(mouse_x,mouse_y) == True:246            return 'back'247        return False248    def on_hover(self, mouse_x, mouse_y):249        back_option.on_hover(mouse_x, mouse_y)250class Exitscreen():251    def __init__(self):252        default_color = (240, 63, 63)253        hover_color = (204, 255, 229)254        current_color = default_color255        global back_option256        back_option = Button(75, 50, 75, 50, default_color, hover_color, current_color, 'Back')257    def draw(self,score):258        # Print exitscreen txt259        arcade.draw_text('You Died \n\n Your Score: {}'.format(score), WIDTH / 2, HEIGHT / 2, arcade.color.BLACK, 40,260                         align='center', anchor_x='center', anchor_y='center')261        # Set up back button262        back_option.draw()263    def on_click(self, mouse_x, mouse_y):264        # Box click checks265        if back_option.on_click(mouse_x, mouse_y) == True:266            return 'back'267        return False268    def on_hover(self, mouse_x, mouse_y):269        back_option.on_hover(mouse_x, mouse_y)270class Credits():271    def __init__(self):272        default_color = (240, 63, 63)273        hover_color = (204, 255, 229)274        current_color = default_color275        global back_option276        back_option = Button(75, 50, 75, 50, default_color, hover_color, current_color, 'Back')277    def draw(self):278        # Set up credits txt279        arcade.draw_text('This game is so far made entirely by Thomas Wu', WIDTH / 2, HEIGHT / 2, arcade.color.BLACK,280                         22, anchor_y='center', anchor_x='center', align='center')281        # Set up back button282        back_option.draw()283    def on_click(self,mouse_x,mouse_y):284        # Box click checks285        if back_option.on_click(mouse_x,mouse_y) == True:286            return 'back'287        return False288    def on_hover(self, mouse_x, mouse_y):289        back_option.on_hover(mouse_x, mouse_y)290def update(delta_time):291    global exitscreen_bool, game_bool292    if game_bool == True:293        # Check for contact294        if game.update() == 'Contact':295            exitscreen_bool = True296            game_bool = False297def on_draw():298    arcade.start_render()299    if main_menu_bool == True:300        main_menu.draw()301    elif instructions_bool == True:302        instructions.draw()303    elif credits_bool == True:304        credits.draw()305    elif game_bool == True:306        game.on_draw()307    elif exitscreen_bool == True:308        exitscreen.draw(game.game_score)309def on_key_press(key, modifiers):310    if game_bool == True:311        game.on_key_press(key)312def on_key_release(key, modifiers):313    if game_bool == True:314        game.on_key_release(key)315def on_mouse_press(x, y, button, modifiers):316    global main_menu_bool, instructions_bool, game_bool, credits_bool, exitscreen_bool317    if main_menu_bool == True:318        click_result = main_menu.on_click(x,y)319        if click_result != False:320            main_menu_bool = False321            if click_result == 'instructions':322                instructions_bool = True323            elif click_result == 'game':324                game_bool = True325            else:   # Credits box was clicked326                credits_bool = True327    elif instructions_bool == True:328        click_result = instructions.on_click(x, y)329        if click_result != False:330            # Go back to main menu331            instructions_bool = False332            main_menu_bool = True333    elif credits_bool == True:334        click_result = credits.on_click(x, y)335        if click_result != False:336            # Go back to main menu337            credits_bool = False338            main_menu_bool = True339    elif exitscreen_bool == True:340        click_result = exitscreen.on_click(x, y)341        if click_result != False:342            # Go back to main menu343            exitscreen_bool = False344            main_menu_bool = True345            # Restart all game logic346            # Bolder list347            game.bolder_list = arcade.SpriteList()348            # Initial bolder creation (default bolders in the game)349            for i in range(3):350                skin = random.choice(game.bolder_skins)351                #Set up bolder position (Make sure not to spawn on player)352                x_range = [[100, int(WIDTH / 2 - game.player.width)], [int(WIDTH / 2 + game.player.width), WIDTH - 100]]353                y_range = [[100, int(HEIGHT / 2 - game.player.height)],354                           [int(HEIGHT / 2 + game.player.height), HEIGHT - 100]]355                x = random.randint(*random.choice(x_range))356                y = random.randint(*random.choice(y_range))357                scale = random.uniform(0.15, 0.23)358                bolder = Bolder(skin, scale, center_x=x, center_y=y)359                bolder.change_x = random.uniform(-5, 5 + 1)360                bolder.change_y = random.uniform(-5, 5 + 1)361                game.bolder_list.append(bolder)362            # Player movements363            game.player.change_x = 0364            game.player.change_y = 0365            game.player.center_x = WIDTH / 2366            game.player.center_y = HEIGHT / 2367            game.key_map = {'up pressed': False, 'down pressed': False, 'right pressed': False, 'left pressed': False}368            # Bullets369            game.bullet_list = arcade.SpriteList()370            game.game_score = 0371            game.frame_count = 0372    elif game_bool == True:373        game.on_mouse_press(x, y)374def on_mouse_motion(x,y,dx,dy):375    if main_menu_bool == True:376        main_menu.on_hover(x, y)377    elif instructions_bool == True:378        instructions.on_hover(x,y)379    elif credits_bool == True:380        credits.on_hover(x, y)381    elif game_bool == True:382        game.on_mouse_motion(x, y)383    elif exitscreen_bool == True:384        exitscreen.on_hover(x, y)385def setup():386    arcade.open_window(WIDTH, HEIGHT, 'Laser Quest')387    arcade.set_background_color(arcade.color.GREEN_YELLOW)388    arcade.schedule(update, 1/60)389    # Override arcade window methods390    window = arcade.get_window()391    window.on_draw = on_draw392    window.on_key_press = on_key_press393    window.on_key_release = on_key_release394    window.on_mouse_motion = on_mouse_motion395    window.on_mouse_press = on_mouse_press396    global main_menu397    main_menu = Menu()398    global instructions399    instructions = Instructions()400    global credits401    credits = Credits()402    global exitscreen403    exitscreen = Exitscreen()404    global main_menu_bool,instructions_bool, credits_bool, game_bool, exitscreen_bool405    main_menu_bool = True406    instructions_bool = False407    game_bool = False408    credits_bool = False409    exitscreen_bool = False410    # Info about the actual gameplay411    # Player and player movement412    player = Player('Image Folder/Space_ship.png', 0.40, center_x=WIDTH / 2, center_y=HEIGHT / 2)413    player.change_x = 0414    player.change_y = 0415    key_map = {'up pressed': False, 'down pressed': False, 'right pressed': False, 'left pressed': False}416    # Bolders417    bolder_skins = [418        'Image Folder/spaceMeteors_001.png',419        'Image Folder/spaceMeteors_002.png',420        'Image Folder/spaceMeteors_003.png',421        'Image Folder/spaceMeteors_004.png'422    ]423    bolder_list = arcade.SpriteList()424    # Initial bolder creation (default bolders in the game)425    for i in range(3):426        skin = random.choice(bolder_skins)427        # Set up bolder position (Make sure not to spawn on player)428        x_range = [[100, int(WIDTH / 2 - player.width)], [int(WIDTH / 2 + player.width), WIDTH - 100]]429        y_range = [[100, int(HEIGHT / 2 - player.height)],430                   [int(HEIGHT / 2 + player.height), HEIGHT - 100]]431        x = random.randint(*random.choice(x_range))432        y = random.randint(*random.choice(y_range))433        scale = random.uniform(0.15, 0.23)434        bolder = Bolder(skin, scale, center_x=x, center_y=y)435        bolder.change_x = random.uniform(-5, 5 + 1)436        bolder.change_y = random.uniform(-5, 5 + 1)437        bolder_list.append(bolder)438    # Bullets439    bullet_list = arcade.SpriteList()440    # Score data441    frame_count = 0442    game_score = 0443    global game444    game = Game(key_map, frame_count, game_score, bolder_list, bolder_skins, player, bullet_list)445    arcade.run()446if __name__ == '__main__':...carlaUtils.py
Source:carlaUtils.py  
1# coding:utf-82# Type: Public3import numpy as np4import common.Math as  cMath5import math6class CarlrUtils(object):7    Author = "BaoChuan Wang"8    AllowImport = False9    @staticmethod10    def get_direction_vector_series_and_car_to_next_waypoint_ratio(11            carla_engine,12            start_waypoint_xy_array,13            target_waypoint_xy_array,14            draw_in_UE=False15    ):16        '''17        éç¨äºWaypointsTargetç¯å¢çstateæ±å18        # 以ä¸ä»£ç ä½ä¸ºåè19        è·å¾è½¦è¾æè¿çè·¯å¾ç¹,以忥䏿¥n个路å¾ç¹(ç®åæ¹ä¸ºæå两个路å¾ç¹,ä¸ä¼éç车è¾ä½ç½®æ´æ°!),ç¶åè¿åä¸è¿ä¸¤ä¸ªè·¯å¾ç¹ç¸å
³çåæ°,æ:20        1.车è¾å°ä¸¤ä¸ªwaypointsçä¸ç¹è·ç¦»21        2.waypointæ¹åè§22        3.车è¾å°waypointä¸ç¹æ¹åè§23        4.è½¦è¾æ¬èº«æ¹åè§24        #  å¦å¤è¿æ ·è·åwaypoints宿¶æ´æ°çæ¹æ³æ¯ä¸åéç,产ççrewardsä¸å¯¹actionè¿ç»25        # åæ¥çæ¹æ³æ¯è½¦è¾è·åæè¿çwaypointç¶åæ±å¾ä¸ä¸ä¸ªwaypoints,ç°å¨æ¹æä¸å¼å§å°±ç¡®å®waypoints26        å ä¸ºä½¿ç¨è·åæè¿waypointsçæ¹æ³å¯è½ä¼äº§çåé27        åæ¥çæ¹æ³ä»£ç :28        # # è·å¾è½¦è¾çä¸ä¸¤ä¸ªwaypointsçxyåæ 29        # next_center_waypoints = self.engine.map.get_waypoint(30        #     # location31        #     self.engine.vehicle.get_location()32        # )33        # # è·å¾æ¥ä¸æ¥5mçä½ä¸ºä¸ä¸ä¸ªè·¯å¾ç¹34        # next_next_center_waypoints = next_center_waypoints.next(5)[0]35        #36        # waypoint_list =((37        #            next_center_waypoints.transform.location.x,38        #            next_center_waypoints.transform.location.y39        #        ), (40        #            next_next_center_waypoints.transform.location.x,41        #            next_next_center_waypoints.transform.location.y42        #        ))43        #44        # # å¨carlaä¸ç»å¶è·¯å¾ç¹45        # self.engine.draw_waypoint_list(46        #     [next_center_waypoints,next_next_center_waypoints],life_time=1)47        #48        # return waypoint_list49        # 注æç¹:50        å ä¸ºæç»è®¡ç®çæ¶åæ¯éè¦ä¸¤ä¸ªwaypointæ¥å¾å°å车è¾çè·ç¦»51        以å 车è¾å°waypointsä¸å¿ç¹çæ¹å å 两个waypointsæ¹å ç夹è§52        æä»¥ä¸å®è¦ä¿è¯waypointsä¸å¿ç¹å¨è½¦è¾åæ¹(å¦åå°±ä¼åé)53        éè¦ä¿è¯Waypointsçé´éè¶³å¤å¤§å³å¯!ä¹å¯ä»¥è¿éåç¹æ¶ååé¢ä¸¤ä¸ªç¹è䏿¯ä¸ä¸ªç¹!54        # è¿éçä»£ç æ¯æ±å¾è·ç¦»è½¦è¾æè¿çç¹,ç¶åå¾ä¸æ¾3个ç¹,ç°å¨æ´æ°æä¸å¼å§æå®çç¹!55        # # æ±å¾æè¿çwaypointsçindex,ç¶åæ¾ä¸ä¸ä¸ª!妿å°äºwaypointsçæ«ç«¯?56        # distance = np.sqrt(57        #     np.sum(np.square(self.car_waypoints_xy_array - np.array([self.engine.vehicle.get_location().x,58        #                                                              self.engine.vehicle.get_location().y])), axis=1))59        #60        # # print(distance)61        # # æå¤§çindex62        # index_max = distance.shape[0] - 163        # # æ¾å°è·ç¦»æè¿çwaypointsçindex64        # index = int(np.argmin(distance))65        #66        #67        # index = index_max - 168        #69        # # è¿éç¹åå¾ååä¸ç¹å¿70        # next_point_index = index + 371        # if next_point_index > index_max: next_point_index = index_max72        # if draw_in_UE:73        #     # ä½åºä¸¤ä¸ªwaypointsç线段74        #     start = self.car_waypoints_list[index]75        #     end = self.car_waypoints_list[next_point_index]76        #     self.engine.draw_line(start, end, life_time=1, color=(0, 255, 0))77        # center_point = (self.car_waypoints_xy_array[index, :].reshape(-1) +78        #                 self.car_waypoints_xy_array[next_point_index, :].reshape(-1)) / 279        '''80        # 车è¾ä½ç½®81        vehicle_location = carla_engine.vehicle.get_location()82        car_point = np.array([vehicle_location.x, vehicle_location.y])83        if draw_in_UE:84            # waypointä¸ç¹85            center_point = (start_waypoint_xy_array + target_waypoint_xy_array) / 286            center_point_transform = carla_engine.make_transform(87                x=center_point[0],88                y=center_point[1],89                z=vehicle_location.z90            )91            carla_engine.draw_point_xyz(center_point[0], center_point[1], carla_engine.vehicle.get_location().z + 0.25,92                                        color=(0, 255, 255), thickness=0.1)93            carla_engine.draw_line_location(94                vehicle_location,95                center_point_transform.location,96                life_time=1, color=(0, 0, 255)97            )98        # waypointsçå使¹ååé99        way_unit_direction = target_waypoint_xy_array - start_waypoint_xy_array100        way_unit_direction /= np.linalg.norm(way_unit_direction, 2)101        # 车è¾å°ä¸å¿ç¹çå使¹ååé102        car_to_way_unit_direction = (target_waypoint_xy_array - car_point)103        car_to_way_unit_direction /= np.linalg.norm(car_to_way_unit_direction, 2)104        # è½¦è¾æ¬èº«çå使¹ååé105        car_unit_direction = carla_engine.vehicle.get_transform().get_forward_vector()106        car_unit_direction = np.array([car_unit_direction.x, car_unit_direction.y])107        # 车è¾å°targetç¹åæ»è·¯ç¨çæ¯å¼108        total_distance = np.linalg.norm(target_waypoint_xy_array - start_waypoint_xy_array, 2)109        now_distance = np.linalg.norm(target_waypoint_xy_array - car_point, 2)110        car_to_target_distance_ratio = now_distance / total_distance111        # 车è¾çyawè§åº¦112        car_yaw = math.radians(carla_engine.vehicle_yaw)113        # å¢å :ç¸å¯¹äºè½¦è¾åæ çç®æ waypointçxåy114        target_xy_array_relate_to_car = cMath.convert_point_into_relative_coordinate(115            target_waypoint_xy_array,116            car_point,117            original_yaw_radius=car_yaw)118        return way_unit_direction, car_to_way_unit_direction, car_unit_direction, car_to_target_distance_ratio, target_xy_array_relate_to_car119    @staticmethod120    def get_car_target_waypoints(engine, vehicle, n_waypoint=2, waypoint_spacing=15, draw_waypoints=True):121        if n_waypoint < 2:122            raise ValueError("At least 2 waypoints will return!")123        # List<Waypoints>124        car_waypoints_list = []125        # Array2D126        car_waypoints_xy_array = None127        # List<List>128        car_waypoints_xy_list = []129        # èµ·å§çç¹130        next_center_waypoints = engine.map.get_waypoint(vehicle.get_location())131        # 车è¾çèµ·ç¹132        start_waypoint_xy_array = np.array([next_center_waypoints.transform.location.x,133                                            next_center_waypoints.transform.location.y])134        car_waypoints_list.append(next_center_waypoints)135        car_waypoints_xy_list.append([next_center_waypoints.transform.location.x,136                                      next_center_waypoints.transform.location.y])137        if n_waypoint == 2:138            next_center_waypoints = next_center_waypoints.next(waypoint_spacing)[0]139            car_waypoints_list.append(next_center_waypoints)140            car_waypoints_xy_list.append([next_center_waypoints.transform.location.x,141                                          next_center_waypoints.transform.location.y])142        else:143            for i in range(n_waypoint - 1):144                next_center_waypoints = next_center_waypoints.next(waypoint_spacing)[0]145                car_waypoints_list.append(next_center_waypoints)146                car_waypoints_xy_list.append([next_center_waypoints.transform.location.x,147                                              next_center_waypoints.transform.location.y])148        car_waypoints_xy_array = np.array(car_waypoints_xy_list)149        # ç»ç¹150        target_waypoint_xy_array = np.array([next_center_waypoints.transform.location.x,151                                             next_center_waypoints.transform.location.y])152        # ç»å¶è·¯å¾ç¹153        if draw_waypoints:154            engine.draw_waypoint_list(car_waypoints_list, life_time=99999)155        return car_waypoints_list, car_waypoints_xy_list, car_waypoints_xy_array, target_waypoint_xy_array156    @staticmethod157    def get_velocity_accel_relative_to_car_and_their_scalar(engine):158        velocity_vector = engine.get_velocity()159        velocity_to_car_x, velocity_to_car_y = cMath.convert_point_into_relative_coordinate(160            target_xy=[velocity_vector.x, velocity_vector.y],161            original_xy=[0, 0],162            original_yaw_radius=math.radians(engine.vehicle_yaw))163        velocity = engine.get_velocity_scalar()164        accel_vector = engine.get_accel()165        accel_to_car_x, accel_to_car_y = cMath.convert_point_into_relative_coordinate(166            target_xy=[accel_vector.x, accel_vector.y],167            original_xy=[0, 0],168            original_yaw_radius=math.radians(engine.vehicle_yaw))169        accel = engine.get_velocity_scalar()...temp1.py
Source:temp1.py  
1#2# Simple example scene for a 2D simulation3# Simulation of a buoyant smoke density plume4#5from manta import *6#import manta7from PIL import Image8im = Image.open("/home/tushar/manta_mikey_1/manta/scenes/mine/november_24/templates_krueger/his/temp_gray.png")9pix = im.load()10x,y=im.size11image_perlin_1()12image_load_1()13from time import sleep14# solver params15res = 12816res_x = res17res_y = res18gs = vec3(res_x,res_y,1)19s = Solver(name='main', gridSize = gs, dim=2)20s.timestep = 1.021timings = Timings()22# prepare grids23flags = s.create(FlagGrid)24#flag_test = s.create(FlagGrid)25vel = s.create(MACGrid)26#vel_test = s.create(MACGrid)27density = s.create(RealGrid)28pressure = s.create(RealGrid)29#This is a dummy grid created for the velocity-only visualization in the GUI.Velocity is MAC Grid30velocity_only = s.create(RealGrid)31#This is the pressure just after CG solve32pressure_before = s.create(RealGrid)33pressure_modification_elliptical_gaussian = s.create(RealGrid)34flags.initDomain()35flags.fillGrid()36if (GUI):37	gui = Gui()38	gui.show()39	gui.pause()40#This is the source shape41source = s.create(Cylinder, center=gs*vec3(0.5,0.5,0.5), radius=res*0.48, z=gs*vec3(0, 0.48, 0))42# This is for elliptical gaussian43splat_center_ellipse = gs*vec3(0.5,0.5,0.5)44splat_center_ellipse_rounded = vec3(0,0,0)45splat_sigma_x = 16.046splat_sigma_y = 2.047splat_radius_x = (res*0.5) - 4.048splat_radius_y = (res*0.25) - 1.049velocity_center = vec3(0.0,0.0,0.0)50#	This is the noise with central mean value as 151source.applyRNG_binary_NoiseToGrid_seeded(grid=density, value=1, fraction_white=0.20, res_x = res_x, res_y = res_y)52#main loop53for t in range(400):54	setInflowBcs(vel, dire='y', value=vec3(0.0,0.3,0.0))55	advectSemiLagrange_time_fraction(flags=flags, vel=vel, grid=density, order=2, time_fraction = 1.0)56	advectSemiLagrange_time_fraction(flags=flags, vel=vel, grid=vel, order=2, time_fraction = 1.0)57	setWallBcs(flags=flags, vel=vel)    58	setInflowBcs(vel, dire='y', value=vec3(0.0,0.3,0.0))59#	splat_center_ellipse_rounded.x = int(splat_center_ellipse.x+0.5)60#	splat_center_ellipse_rounded.y = int(splat_center_ellipse.y+0.5)61#	splat_center_ellipse_rounded.z = int(splat_center_ellipse.z+0.5)62#	velocity_center = vel.get(splat_center_ellipse_rounded)63#	print "velocity_center.x = ",velocity_center.x64#	print "velocity_center.y = ",velocity_center.y65#	print "velocity_center.z = ",velocity_center.z66#	This is elliptical Gaussian67	splat_center_ellipse = (splat_center_ellipse)+((s.timestep)*(velocity_center))68	print "splat_center_ellipse.x = ",splat_center_ellipse.x69	print "splat_center_ellipse.y = ",splat_center_ellipse.y70	print "splat_center_ellipse.z = ",splat_center_ellipse.z71	splat_center_ellipse_rounded.x = int(splat_center_ellipse.x+0.5)72	splat_center_ellipse_rounded.y = int(splat_center_ellipse.y+0.5)73	splat_center_ellipse_rounded.z = int(splat_center_ellipse.z+0.5)74	create_pressure_modifier_elliptical_gaussian_with_multiplier(pressure_modification_elliptical_gaussian , splat_radius_x , splat_radius_y , splat_center_ellipse_rounded , splat_sigma_x , splat_sigma_y , multiplier = 10.0, lower_factor = 1.0, higher_factor = 1.0)75#	time range of application of pressure modification76	t1 = 577	t2 = 9578	if (t==(t1-2)):79		gui.pause()80	if (t==(t2-2)):81		gui.pause()82	if (t>t1)and(t<t2):83#		This is velocity correction84		solvePressure3_part2(flags=flags, vel=vel, pressure=pressure_modification_elliptical_gaussian)85		setWallBcs(flags=flags, vel=vel)86		setInflowBcs(vel, dire='y', value=vec3(0.0,0.3,0.0))87	advectSemiLagrange_time_fraction(flags=flags, vel=vel, grid=vel, order=2, time_fraction = 0.5)88	setWallBcs(flags=flags, vel=vel)    89	setInflowBcs(vel, dire='y', value=vec3(0.0,0.3,0.0))90#	splat_center_ellipse_rounded.x = int(splat_center_ellipse.x+0.5)91#	splat_center_ellipse_rounded.y = int(splat_center_ellipse.y+0.5)92#	splat_center_ellipse_rounded.z = int(splat_center_ellipse.z+0.5)93#	velocity_center = vel.get(splat_center_ellipse_rounded)94#	print "velocity_center.x = ",velocity_center.x95#	print "velocity_center.y = ",velocity_center.y96#	print "velocity_center.z = ",velocity_center.z97	solvePressure3_part1(flags=flags, vel=vel, pressure=pressure, openBound='Y')98	solvePressure3_part2(flags=flags, vel=vel, pressure=pressure)99	setWallBcs(flags=flags, vel=vel)100	setInflowBcs(vel, dire='y', value=vec3(0.0,0.3,0.0))101	splat_center_ellipse_rounded.x = int(splat_center_ellipse.x+0.5)102	splat_center_ellipse_rounded.y = int(splat_center_ellipse.y+0.5)103	splat_center_ellipse_rounded.z = int(splat_center_ellipse.z+0.5)104	velocity_center = vel.get(splat_center_ellipse_rounded)105	print "velocity_center.x = ",velocity_center.x106	print "velocity_center.y = ",velocity_center.y107	print "velocity_center.z = ",velocity_center.z108	timings.display()109	s.step()...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
