Best Python code snippet using yandex-tank
game.py
Source:game.py  
1# This is source-code for Battle on Tower game2import json3import math4import os  # Modules for develop5import random6import sys7import pygame as pg8class App:  # Main class for game9    def __init__(self):10        pg.init()11        pg.mixer.init()12        self.keys = {"rotate_left": pg.K_a,  # dictionaries with keys13                     "rotate_right": pg.K_d,14                     "shot": pg.K_SPACE,15                     "reload": pg.K_r}16        self.file_dir = os.path.dirname(__file__)  # file load optimization17        self.img_dir = os.path.join(self.file_dir)18        self.json_config_dir = os.path.join(self.file_dir)19        self.fonts_dir = os.path.join(self.file_dir)20        self.sounds_dir = os.path.join(self.file_dir)21        with open(os.path.join(self.json_config_dir,22                               'file_paths.json')) as file_paths:  # load json config with file paths23            self.data = json.load(file_paths)24        self.background_img = pg.image.load(25            os.path.join(self.img_dir,26                         random.choice(self.data["images"]["background"])))  # loading application files27        self.icon_img = pg.image.load(os.path.join(self.img_dir, self.data["images"]["icon"]))28        self.font_file = os.path.join(self.fonts_dir, self.data["fonts"][0])29        self.shot_sound_file = pg.mixer.Sound(os.path.join(self.sounds_dir, self.data["sounds"][0]))30        self.gameplay_sound_file = pg.mixer.Sound(os.path.join(self.sounds_dir, self.data["sounds"][1]))31        self.attack_sound_file = pg.mixer.Sound(os.path.join(self.sounds_dir, self.data["sounds"][2]))32        self.game_over_img = pg.image.load(os.path.join(self.img_dir, self.data["images"]["menu_imgs"]["game_over"]))33        self.button1_img = pg.image.load(os.path.join(self.img_dir, self.data["images"]["buttons"][0]))34        self.button2_img = pg.image.load(os.path.join(self.img_dir, self.data["images"]["buttons"][1]))35        self.gameplay_sound_file.play(-1)36        self.fps = 60  # main game params37        self.height, self.width = 1000, 60038        self.screen = pg.display.set_mode((self.height, self.width))39        pg.display.set_caption("Battle on Tower")40        pg.display.set_icon(self.icon_img)41        self.colors = [(0, 0, 0), (255, 255, 255), (255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (139, 69, 19)]42        self.clock = pg.time.Clock()43        self.tower_xp = 100  # common game object params44        self.ammo_count = 1045        self.kill_count = 046        self.arm_angle = 047        self.ray_x = 12048        self.ray_y = 12049        self.length = 40050        self.ray_angle = 051        self.is_shoot = True52        self.reloading = False53        self.game_over = False54        self.all_sprites = pg.sprite.Group()  # sprite groups55        self.bullets = pg.sprite.Group()56        self.enemies = pg.sprite.Group()57        self.sprites_init()58        self.game_cycle()59    def game_cycle(self):  # game cycle60        while True:61            self.clock.tick(self.fps)62            for event in pg.event.get():63                if event.type == pg.QUIT:64                    pg.quit()65                    sys.exit()66                if self.game_over:67                    self.button_connect(event)68            self.update_screen()69            self.key_pressed()70    def draw_simple_objects(self):  # drawing simple objects71        ground_img = pg.image.load(os.path.join(self.img_dir, self.data["images"]["ground"]))72        image = pg.image.load(os.path.join(self.img_dir, self.data["images"]["player"][1]))73        new_img = pg.transform.rotate(image, self.arm_angle)74        rect = new_img.get_rect(center=image.get_rect(topleft=(95, 150)).center)75        self.screen.blit(ground_img, (0, 500))76        self.screen.blit(new_img, rect)77        pg.draw.circle(self.screen, self.colors[2],78                       (self.ray_x + self.length * math.cos(self.ray_angle * math.pi / 180),79                        self.ray_y + self.length * math.sin(self.ray_angle * math.pi / 180)), 8, 2)80    def key_pressed(self):81        keys = pg.key.get_pressed()82        if keys[self.keys["rotate_right"]]:83            self.arm_angle -= 284            self.ray_angle += 285        if keys[self.keys["rotate_left"]]:86            self.arm_angle += 287            self.ray_angle -= 288    def sprites_init(self):  # initialisation sprites89        global tower, enemy_on_sky, enemy_on_ground90        player = Player(x=100, y=150, colors=self.colors, screen=self.screen, keys=self.keys,91                        bullets=self.bullets, ammo_count=self.ammo_count, is_shoot=self.is_shoot,92                        reloading=self.reloading, data=self.data, img_dir=self.img_dir, ray_angle=self.ray_angle,93                        enemies=self.enemies, shot_sound_file=self.shot_sound_file)94        tower = Tower(x=50, y=120, colors=self.colors, tower_xp=self.tower_xp, enemies=self.enemies, data=self.data,95                      img_dir=self.img_dir)96        for i in range(3):97            enemy_on_sky = EnemyOnSky(x=5000 + i * 300, y=random.randint(200, 500), colors=self.colors,98                                      bullets=self.bullets,99                                      enemies=self.enemies,100                                      all_sprites=self.all_sprites, data=self.data, img_dir=self.img_dir, anim_count=0,101                                      screen=self.screen, length=200, ray_angle=self.ray_angle, rot_angle=3,102                                      width=self.width)103            self.enemies.add(enemy_on_sky)104        for i in range(3):105            enemy_on_ground = EnemyOnGround(x=2000, y=430, colors=self.colors, bullets=self.bullets,106                                            enemies=self.enemies, all_sprites=self.all_sprites, data=self.data,107                                            img_dir=self.img_dir, anim_count=0)108            self.enemies.add(enemy_on_ground)109        self.all_sprites.add(tower, player)110    def update_screen(self):  # update canvas for screen111        self.screen.blit(self.background_img, (-2, 0))112        if not self.game_over:  # if game lost, objects will be deleted113            self.all_sprites.update()114            self.bullets.update()115            self.enemies.update()116            self.all_sprites.draw(self.screen)117            self.bullets.draw(self.screen)118            self.enemies.draw(self.screen)119            self.draw_simple_objects()120        self.collision_with_tower()121        self.ammo_counter()122        self.lose()123        pg.display.update()124    def text_render(self, text, font, size, color, x, y):  # this method responding rendering text125        font = pg.font.Font(font, size)126        text = font.render(text, True, color)127        self.screen.blit(text, (x, y))128    def collision_with_tower(self):  # collision with tower129        global tower130        self.text_render(f'XP: {str(round(self.tower_xp))}', self.font_file, 40, self.colors[2], 0, 0)131        collide = pg.sprite.spritecollide(tower, self.enemies, False)132        if collide and self.tower_xp > 0:133            self.tower_xp -= 0.05134        if self.tower_xp <= 0:135            self.game_over = True136    def ammo_counter(self):  # quantity counter for ammo, if player shoots, ammo count reduced by one number.137        keys = pg.key.get_pressed()138        self.text_render(f'Ammo: {str(round(self.ammo_count))}', self.font_file, 40, self.colors[-3], 400, 0)139        if keys[self.keys["shot"]] and self.is_shoot and self.ammo_count > 0:140            self.ammo_count -= 1141            self.is_shoot = False142        if not keys[self.keys["shot"]]:143            self.is_shoot = True144        if keys[self.keys["reload"]]:145            self.reloading = True146        if self.reloading and self.ammo_count <= 10:147            self.ammo_count += 0.02148            self.text_render('Reloading...', self.font_file, 60, self.colors[-2], 400, 100)149        if not keys[self.keys["reload"]]:150            self.reloading = False151    def lose(self):152        global btn1_rect, btn2_rect  # if player lost, text "Game Over" will be displayed.153        if self.game_over:154            self.gameplay_sound_file.stop()155            self.screen.blit(self.game_over_img, (0, 0))156            self.text_render("Game Over", self.font_file, 100, self.colors[3], 200, 200)157            btn1_rect = self.button1_img.get_rect(center=self.button1_img.get_rect(topleft=(200, 400)).center)158            btn2_rect = self.button2_img.get_rect(center=self.button2_img.get_rect(topleft=(600, 400)).center)159            self.screen.blit(self.button1_img, btn1_rect)160            self.screen.blit(self.button2_img, btn2_rect)161    def button_connect(self, event):162        global btn1_rect, btn2_rect163        if event.type == pg.MOUSEBUTTONUP:164            mouse_pos = pg.mouse.get_pos()165            if btn1_rect.collidepoint(mouse_pos):166                pg.quit()167                App()168            if btn2_rect.collidepoint(mouse_pos):169                pg.quit()170                sys.exit()171class Player(pg.sprite.Sprite):  # class for player172    def __init__(self, x, y, colors, screen, keys, bullets, ammo_count, is_shoot, reloading, data,173                 img_dir, ray_angle, enemies, shot_sound_file):174        super(Player, self).__init__()175        self.x = x176        self.y = y177        self.colors = colors178        self.screen = screen179        self.keys = keys180        self.bullets = bullets181        self.ammo_count = ammo_count182        self.is_shoot = is_shoot183        self.reloading = reloading184        self.data = data185        self.img_dir = img_dir186        self.ray_angle = ray_angle187        self.enemies = enemies188        self.shot_sound_file = shot_sound_file189        self.image = pg.image.load(os.path.join(self.img_dir, self.data["images"]["player"][0]))190        self.rect = self.image.get_rect(center=self.image.get_rect(topleft=(self.x, self.y)).center)191        self.attack = True  # variable which responsible for limit spawn bullets192    def update(self):  # this method responsible for update frame by frame193        self.key_pressed()194        self.ammo_counter_proc()195    def key_pressed(self):  # key pressing processing196        keys = pg.key.get_pressed()197        if keys[self.keys["shot"]]:198            self.shot()199            self.attack = False200        if not keys[self.keys["shot"]]:201            self.attack = True202        if keys[self.keys["rotate_right"]]:203            self.ray_angle += 2204        if keys[self.keys["rotate_left"]]:205            self.ray_angle -= 2206    def shot(self):  # this method responsible for spawn bullets207        if self.attack and self.ammo_count > 0:208            self.shot_sound_file.play()209            self.shot_sound_file.set_volume(0.5)210            bullet = Bullet(x=self.x, y=self.y, radius=5, colors=self.colors, speed=20, ray_angle=self.ray_angle,211                            keys=self.keys, enemies=self.enemies)212            self.bullets.add(bullet)213    def ammo_counter_proc(self):  # Reducing and reloading ammo214        keys = pg.key.get_pressed()215        if keys[self.keys["shot"]] and self.is_shoot and self.ammo_count > 0:216            self.ammo_count -= 1217            self.is_shoot = False218        if not keys[self.keys["shot"]]:219            self.is_shoot = True220        if keys[self.keys["reload"]]:221            self.reloading = True222        if self.reloading and self.ammo_count <= 10:223            self.ammo_count += 0.05224        if not keys[self.keys["reload"]]:225            self.reloading = False226class Bullet(pg.sprite.Sprite):  # class for bullets227    def __init__(self, x, y, speed, radius, colors, ray_angle, keys, enemies):228        super(Bullet, self).__init__()229        self.x = x230        self.y = y231        self.speed = speed232        self.colors = colors233        self.radius = radius234        self.ray_angle = ray_angle235        self.keys = keys236        self.enemies = enemies237        self.image = pg.Surface((self.radius * 2, self.radius * 2))238        self.image.set_colorkey(self.colors[0], False)239        pg.draw.circle(self.image, self.colors[-1], (self.radius, self.radius), self.radius)240        self.rect = pg.Rect(self.x, self.y, self.radius, self.radius)241        self.attack = True242    def update(self):243        self.force()244        self.key_pressed()245    def key_pressed(self):246        keys = pg.key.get_pressed()247        if keys[self.keys["rotate_right"]] and self.attack:248            self.ray_angle += 2249        if keys[self.keys["rotate_left"]] and self.attack:250            self.ray_angle -= 2251    def force(self):  # movement for ray direction252        self.rect.x += self.speed * math.cos(self.ray_angle * math.pi / 180)253        self.rect.y += self.speed * math.sin(self.ray_angle * math.pi / 180)254        self.attack = False255class EnemyOnGround(pg.sprite.Sprite):  # class for enemies(on ground)256    def __init__(self, x, y, colors, bullets, enemies, all_sprites, data, img_dir, anim_count):257        super(EnemyOnGround, self).__init__()258        self.x = x259        self.y = y260        self.colors = colors261        self.bullets = bullets262        self.enemies = enemies263        self.all_sprites = all_sprites264        self.data = data265        self.img_dir = img_dir266        self.anim_count = anim_count267        self.rand_keys = ["enemy", "enemy3"]268        self.rand = random.choice(self.rand_keys)269        self.image = pg.image.load(270            os.path.join(self.img_dir, self.data["images"][self.rand][0][0])).convert_alpha()271        self.rect = self.image.get_rect(center=self.image.get_rect(topleft=(self.x, self.y)).center)272        self.isRunning = True273        self.isPunched = False274        self.isDead = False275        self.enemy1 = False276        self.enemy2 = False277        self.speed = random.randint(2, 5)278    def update(self):279        self.run()280        self.collision()281        self.animation()282        self.enemy_select()283    def enemy_select(self):284        match self.rand:285            case "enemy":286                self.enemy1 = True287            case "enemy3":288                self.enemy2 = True289    def run(self):290        if self.rect.x <= 150:  # movement291            self.isRunning = False292        if self.isRunning and not self.isDead:293            self.rect.x -= self.speed294    def collision(self):  # collision with bullets and spawn for initial position295        collide1 = pg.sprite.spritecollide(self, self.bullets, False)296        collide2 = pg.sprite.spritecollide(self, self.all_sprites, False)297        if collide1 or self.rect.x < 0:298            self.isDead = True299            list(map(lambda x: x.kill(), self.bullets))300        if self.isDead and self.anim_count < 3:301            self.kill()302            enemy_on_ground = EnemyOnGround(x=2000, y=430, colors=self.colors, bullets=self.bullets,303                                            enemies=self.enemies, all_sprites=self.all_sprites, img_dir=self.img_dir,304                                            data=self.data, anim_count=0)305            self.enemies.add(enemy_on_ground)306        if collide2:307            self.isPunched = True308    def anim_count_increment(self, n=1):309        self.anim_count += n310        self.anim_count %= 60311    def animation(self):312        if self.enemy1:313            if self.isRunning:314                self.image = pg.image.load(315                    os.path.join(self.img_dir, self.data["images"]["enemy"][0][self.anim_count // 10])).convert_alpha()316                self.anim_count_increment(1)317            if self.isPunched:318                self.image = pg.image.load(319                    os.path.join(self.img_dir, self.data["images"]["enemy"][1][self.anim_count // 12])).convert_alpha()320                self.anim_count_increment(2)321            if self.isDead:322                self.image = pg.image.load(323                    os.path.join(self.img_dir, self.data["images"]["enemy"][2][self.anim_count // 20])).convert_alpha()324                self.anim_count_increment(1)325                self.rect.y = 450326        if self.enemy2:327            if self.isRunning:328                self.image = pg.image.load(329                    os.path.join(self.img_dir, self.data["images"]["enemy3"][1][self.anim_count // 15])).convert_alpha()330                self.anim_count_increment(1)331            if self.isPunched:332                self.image = pg.image.load(333                    os.path.join(self.img_dir, self.data["images"]["enemy3"][0][self.anim_count // 6])).convert_alpha()334                self.anim_count_increment(1)335            if self.isDead:336                self.image = pg.image.load(337                    os.path.join(self.img_dir, self.data["images"]["enemy3"][2][self.anim_count // 20])).convert_alpha()338                self.anim_count_increment(1)339                self.rect.y = 450340class EnemyOnSky(pg.sprite.Sprite):341    def __init__(self, x, y, colors, bullets, enemies, all_sprites, data, img_dir, anim_count, screen, length,342                 ray_angle, rot_angle, width):343        super(EnemyOnSky, self).__init__()344        self.x = x345        self.y = y346        self.y = y347        self.colors = colors348        self.bullets = bullets349        self.enemies = enemies350        self.all_sprites = all_sprites351        self.data = data352        self.img_dir = img_dir353        self.anim_count = anim_count354        self.screen = screen355        self.length = length356        self.ray_angle = ray_angle357        self.rot_angle = rot_angle358        self.width = width359        self.image = pg.image.load(os.path.join(self.img_dir, self.data["images"]["enemy2"][1][0])).convert_alpha()360        self.rect = self.image.get_rect(center=self.image.get_rect(topleft=(self.x, self.y)).center)361        self.speed = random.randint(2, 5)362        self.isFly = True363        self.isDead = False364        self.isAttack = False365    def update(self):366        self.fly()367        self.animation()368        self.collision()369    def fly(self):370        if self.isFly and not self.isAttack:371            self.ray_angle -= self.rot_angle372            if self.ray_angle < -60 or self.ray_angle > 60:373                self.rot_angle = -self.rot_angle374            self.rect.x -= self.speed * math.cos(self.ray_angle * math.pi / 180)375            self.rect.y -= self.speed * math.sin(self.ray_angle * math.pi / 180)376        if self.isDead:377            self.rect.y += 10378    def anim_count_increment(self, n=1):379        self.anim_count += n380        self.anim_count %= 60381    def animation(self):382        if self.isFly:383            self.image = pg.image.load(384                os.path.join(self.img_dir, self.data["images"]["enemy2"][1][self.anim_count // 15]))385            self.anim_count_increment(2)386        if self.isDead:387            self.image = pg.image.load(os.path.join(self.img_dir, self.data["images"]["enemy2"][0]))388    def collision(self):389        collide1 = pg.sprite.spritecollide(self, self.bullets, False)390        collide2 = pg.sprite.spritecollide(self, self.all_sprites, False)391        if collide1:392            self.isDead = True393            list(map(lambda x: x.kill(), self.bullets))394        if self.rect.y > 500 or self.rect.x < 0:395            self.kill()396            enemy_on_sky = EnemyOnSky(x=5000, y=random.randint(200, 500), colors=self.colors, bullets=self.bullets,397                                      enemies=self.enemies,398                                      all_sprites=self.all_sprites, data=self.data, img_dir=self.img_dir, anim_count=0,399                                      screen=self.screen, length=200, ray_angle=self.ray_angle, rot_angle=3,400                                      width=self.width)401            self.enemies.add(enemy_on_sky)402        if collide2:403            self.isAttack = True404class Tower(pg.sprite.Sprite):  # class for tower405    def __init__(self, x, y, colors, tower_xp, enemies, data, img_dir):406        super(Tower, self).__init__()407        self.x = x408        self.y = y409        self.colors = colors410        self.tower_xp = tower_xp411        self.enemies = enemies412        self.data = data413        self.img_dir = img_dir414        self.image = pg.image.load(os.path.join(self.img_dir, self.data["images"]["tower"]))...HUD.py
Source:HUD.py  
1import arcade2import math3import os45# Constants6SCREEN_WIDTH = 11527SCREEN_HEIGHT = 7008SCREEN_TITLE = "FILLERNAME"910CHARACTER_SCALING = 1.2511TILE_SCALING = 0.51213PLAYER_MOVEMENT_SPEED = 4.51415TOP_VIEWPORT_MARGIN = 25016BOTTOM_VIEWPORT_MARGIN = 501718GRID_PIXEL_SIZE = 641920BULLET_SPEED = 0.82122player_start_x = 57623player_start_y = 1282425AMMO_COUNT = 32627class MyGame(arcade.Window):2829    def __init__(self):30        super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)3132        self.wall_list = None33        self.player_list = None34        self.bullet_list = None35        self.player_bullet_list = None36        self.player_sprite = None37        self.thunder_list = None38        self.ammo_list = None39        self.finishing_line_list = None4041        self.physics_engine = None42        self.view_bottom = 043        self.frame_count = 044        self.shoot_delay = 045        self.ammo_count = AMMO_COUNT46        self.score = 047        self.level = 148        self.current_level = "Map_1.tmx"4950        arcade.set_background_color(arcade.csscolor.CORNFLOWER_BLUE)5152    def setup(self):53        self.player_list = arcade.SpriteList()54        self.wall_list = arcade.SpriteList(use_spatial_hash=True)55        self.plane_list = arcade.SpriteList(use_spatial_hash=True)56        self.bullet_list = arcade.SpriteList()57        self.player_bullet_list = arcade.SpriteList()58        self.thunder_list = arcade.SpriteList(use_spatial_hash=True)59        self.ammo_list = arcade.SpriteList()60        self.finishing_line_list = arcade.SpriteList(use_spatial_hash=True)6162        self.player_sprite = arcade.Sprite("Sprites\Player\player_0.png", CHARACTER_SCALING)6364        self.player_sprite.center_x = player_start_x65        self.player_sprite.center_y = player_start_y66        self.player_list.append(self.player_sprite)67        self.reset_map(self.current_level)6869    def on_draw(self):70        arcade.start_render()71        # Code to draw the screen goes here72        self.ammo_list.draw()73        self.wall_list.draw()74        self.player_list.draw()75        self.plane_list.draw()76        self.bullet_list.draw()77        self.player_bullet_list.draw()78        self.thunder_list.draw()79        self.finishing_line_list.draw()80        self.draw_bottom_hud()818283    def reset_map(self, new_map):84        map_name = "Maps\\" + new_map85        wall_layer_name = "CloudWall"86        enemy_layer_name = "EnemyPlane"87        thunder_layer_name = "ThunderCloud"88        ammo_layer_name = "Ammo"89        finishing_layer_name = "Finishing Line"90        my_map = arcade.tilemap.read_tmx(map_name)91        self.wall_list = arcade.tilemap.process_layer(map_object=my_map, layer_name=wall_layer_name,92                                                      scaling=TILE_SCALING * 2.25, use_spatial_hash=True)93        self.plane_list = arcade.tilemap.process_layer(my_map, enemy_layer_name, TILE_SCALING * 2.25)94        self.physics_engine = arcade.PhysicsEngineSimple(self.player_sprite, self.wall_list)95        self.thunder_list = arcade.tilemap.process_layer(my_map, thunder_layer_name, TILE_SCALING * 2.25,96                                                         use_spatial_hash=True)97        self.ammo_list = arcade.tilemap.process_layer(my_map, ammo_layer_name, TILE_SCALING * 2.25)98        self.finishing_line_list = arcade.tilemap.process_layer(map_object=my_map, layer_name=finishing_layer_name,99                                                                     scaling=TILE_SCALING * 2.25, use_spatial_hash=True)100101    def on_key_press(self, key, modifiers):102        if key == arcade.key.UP or key == arcade.key.W or key == arcade.key.SPACE:103            self.player_sprite.change_y = PLAYER_MOVEMENT_SPEED104        elif key == arcade.key.DOWN or key == arcade.key.S:105            self.player_sprite.change_y = -PLAYER_MOVEMENT_SPEED106        elif key == arcade.key.LEFT or key == arcade.key.A:107            self.player_sprite.change_x = -PLAYER_MOVEMENT_SPEED108        elif key == arcade.key.RIGHT or key == arcade.key.D:109            self.player_sprite.change_x = PLAYER_MOVEMENT_SPEED110111    def on_key_release(self, key, modifiers):112        if key == arcade.key.UP or key == arcade.key.W:113            self.player_sprite.change_y = 0114        elif key == arcade.key.DOWN or key == arcade.key.S:115            self.player_sprite.change_y = 0116        elif key == arcade.key.LEFT or key == arcade.key.A:117            self.player_sprite.change_x = 0118        elif key == arcade.key.RIGHT or key == arcade.key.D:119            self.player_sprite.change_x = 0120121    def on_mouse_press(self, x, y, button, modifiers):122        if self.shoot_delay <= 0 and self.ammo_count > 0:123            player_bullet = arcade.Sprite("Sprites\Player Bullet\Player_Bullet.png")124            player_bullet.change_y = BULLET_SPEED * 5125            player_bullet.center_x = self.player_sprite.center_x126            player_bullet.bottom = self.player_sprite.top127            self.player_bullet_list.append(player_bullet)128            self.shoot_delay = 60129            self.ammo_count -= 1130131    def draw_bottom_hud(self):132        ammoText = f"ammo: {self.ammo_count}"133        arcade.draw_text(ammoText, 10, 10 + self.view_bottom, arcade.color.BLACK, 18)134135        scoreText = f"score: {self.score}"136        arcade.draw_text(scoreText, 1020, 10 + self.view_bottom, arcade.color.BLACK, 18)137138    def on_update(self, delta_time):139        self.frame_count += 1140        self.physics_engine.update()141        self.shoot_delay -= 1142        changed = False143144        if arcade.check_for_collision_with_list(self.player_sprite,145                                                self.thunder_list) or arcade.check_for_collision_with_list(146                self.player_sprite, self.bullet_list):147            self.player_sprite.change_x = 0148            self.player_sprite.change_y = 0149            self.player_sprite.center_x = player_start_x150            self.player_sprite.center_y = player_start_y151            self.view_bottom = 0152            self.score = 0153            self.ammo_count = AMMO_COUNT154            self.reset_map(self.current_level)155            changed = True156157        if arcade.check_for_collision_with_list(self.player_sprite, self.finishing_line_list):158            self.player_sprite.change_x = 0159            self.player_sprite.change_y = 0160            self.player_sprite.center_x = player_start_x161            self.player_sprite.center_y = player_start_y162            self.view_bottom = 0163            self.score += self.ammo_count * 5164            self.ammo_count = AMMO_COUNT165            self.level +=1166            self.current_level = f"Map_{self.level}.tmx"167            self.reset_map(self.current_level)168            changed = True169170        clip_list = arcade.check_for_collision_with_list(self.player_sprite, self.ammo_list)171        for clip in clip_list:172            clip.remove_from_sprite_lists()173            self.ammo_count += 1174175        top_boundary = self.view_bottom + SCREEN_HEIGHT - TOP_VIEWPORT_MARGIN176        if self.player_sprite.top > top_boundary:177            self.view_bottom += self.player_sprite.top - top_boundary178            changed = True179180        bottom_boundary = self.view_bottom + BOTTOM_VIEWPORT_MARGIN181        if self.player_sprite.bottom < bottom_boundary:182            self.view_bottom -= bottom_boundary - self.player_sprite.bottom183            changed = True184185        if changed:186            self.view_bottom = int(self.view_bottom)187            arcade.set_viewport(0, SCREEN_WIDTH, self.view_bottom, SCREEN_HEIGHT + self.view_bottom)188189        for enemy in self.plane_list:190            start_x = enemy.center_x191            start_y = enemy.center_y192193            dest_x = self.player_sprite.center_x194            dest_y = self.player_sprite.center_y195196            x_diff = dest_x - start_x197            y_diff = dest_y - start_y198199            if math.sqrt((x_diff * x_diff) + (y_diff * y_diff)) < 700:200                angle = math.atan2(y_diff, x_diff)201202                enemy.angle = math.degrees(angle) + 90203204                if self.frame_count % 120 == 0:205                    bullet = arcade.Sprite("Sprites\Bullet\Bullet.png")206                    bullet.center_x = start_x207                    bullet.center_y = start_y208                    bullet.angle = math.degrees(angle) - 90209210                    bullet.change_x = math.cos(angle) * BULLET_SPEED * 5211                    bullet.change_y = math.sin(angle) * BULLET_SPEED * 5212213                    self.bullet_list.append(bullet)214215        for bullet in self.bullet_list:216            if math.sqrt(((self.player_sprite.center_x - bullet.center_x) * (217                    self.player_sprite.center_x - bullet.center_x)) + (218                                 (self.player_sprite.center_y - bullet.center_y) * (219                                 self.player_sprite.center_y - bullet.center_y))) > 800:220                bullet.remove_from_sprite_lists()221222            hit_list = arcade.check_for_collision_with_list(bullet, self.thunder_list)223            if len (hit_list) > 0:224                bullet.remove_from_sprite_lists()225            for thunder in hit_list:226                thunder.remove_from_sprite_lists()227228        self.bullet_list.update()229230        self.player_bullet_list.update()231        for player_bullet in self.player_bullet_list:232            hit_list = arcade.check_for_collision_with_list(player_bullet, self.plane_list)233            if len(hit_list) > 0:234                player_bullet.remove_from_sprite_lists()235236            for plane in hit_list:237                plane.remove_from_sprite_lists()238                self.score += 10239240241def main():242    window = MyGame()243    window.setup()244    arcade.run()245246247if __name__ == "__main__":
...ammo_img_generator.py
Source:ammo_img_generator.py  
1# ammo image generator2from mss import mss3import numpy as np4from PIL import Image5import os6from keyboard_input import PressKey, ReleaseKey7from mouse_input import click_mouse8import time9import cv210game_frame_x = 44811game_frame_y = 16712ammo_frame = {'left': game_frame_x+945, 'top': game_frame_y+746, 'width': 20, 'height': 17}13starting_ammo_count = 3014img_save_path = "./ammo_images_hud_scaling_0_5"15PressKey(0x38)16time.sleep(0.2)17PressKey(0x0F)18time.sleep(0.2)19ReleaseKey(0x0F)20time.sleep(0.2)21ReleaseKey(0x38)22for i in range(starting_ammo_count+1):23    ammo_count = starting_ammo_count - i24    # input("press enter to take image of ammo="+ str(ammo_count))25    with mss() as sct:26        ammo_img = np.array(sct.grab(ammo_frame))27        ammo_img = np.where(ammo_img == 255, np.uint8(255), np.uint8(0))28        im = Image.fromarray(ammo_img)29        im.save(os.path.join(img_save_path, "ammo_"+ str(ammo_count)+ ".png"))30        # im.save(os.path.join(img_save_path, "ammo_", str(ammo_count)), "png")31        # im.save("./ammo_images/test.png")32    click_mouse()33    time.sleep(0.2)34# with mss() as sct:35#     ammo_img = np.array(sct.grab(ammo_frame))36#     cv2.imshow("test", ammo_img)...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!!
