Code to randomly create a tower with up or down direction.
def create_random_tower(self): up_or_down = random.randint(1, config.TOWER_DOWN) if up_or_down == config.TOWER_UP: # this is cloud coming from up t = Tower(config.WIDTH, 0, config.TOWER_UP, self.screen) self.towers.add(t) else: # this is cloud coming from down t = Tower(config.WIDTH, 0, config.TOWER_DOWN, self.screen) t.y = config.HEIGHT - t.get_tower_height() self.towers.add(t)
config.py
TOWER_UP = 1 TOWER_DOWN = 2
tower.py
import pygame import random import config class Tower(pygame.sprite.Sprite): def __init__(self, x, y, mode, screen): pygame.sprite.Sprite.__init__(self) self.x = x self.y = y self.image = pygame.image.load('assets/Green-tower.png') self.speed = config.TOWER_SPEED self.screen = screen self.height = random.randint(150, 300) self.image = pygame.transform.scale(self.image, (self.image.get_width(), self.height)) self.mode = mode # 1 is up, 2 is down if self.mode == config.TOWER_UP: self.image = pygame.transform.flip(self.image, False, True) def update(self): # self.direction == 270 self.x -= self.speed self.screen.blit(self.image, (self.x, self.y)) def get_tower_height(self): return self.image.get_height()