def shoot(self, nave): if keyboard.key_pressed("space") and self.can_shoot: bullet = Sprite("./assets/images/bullet.png") bullet.set_position(nave.x + nave.width / 2, nave.y) self.bullet_array.append(bullet) self.can_shoot = False if self.can_shoot == False: self.cooldown -= self.window.delta_time() * GVar.BULLET_COOLDOWN if self.cooldown <= 0: self.can_shoot = True self.cooldown = 100
def spawn(self, alien_spawn_adress): level_control = open(alien_spawn_adress, "r") line = level_control.readline() lin = 0 while lin < 7: for col in range(len(line)): if line[col] == "1": alien = Sprite("./assets/images/alien.png") alien.set_position( 265 + col * (alien.width + 10), GVar.ENEMY_SPAWN_HEIGHT + lin * (alien.height + 10)) self.enemy_mtx.append(alien) lin += 1 line = level_control.readline()
class Spaceship(object): def __init__(self, window): self.window = window self.spaceship = Sprite("./assets/images/spaceship.png") self.velocity = 0 self.__set_pos() def run(self): self.__draw() self.move(self.velocity) def move(self, velocity): if keyboard.key_pressed("left"): self.velocity = -GVar.SPACESHIP_VELOCITY elif keyboard.key_pressed("right"): self.velocity = GVar.SPACESHIP_VELOCITY else: self.velocity = 0 if self.spaceship.x >= (GVar.WIDTH - self.spaceship.width): self.spaceship.set_position(GVar.WIDTH - self.spaceship.width - 1, GVar.HEIGHT - self.spaceship.height - 10) if self.spaceship.x <= 0: self.spaceship.set_position(0.5, GVar.HEIGHT - self.spaceship.height - 10) self.spaceship.x += velocity * self.window.delta_time() def __draw(self): self.spaceship.draw() def __set_pos(self): self.spaceship.set_position(GVar.WIDTH/2 - self.spaceship.width/2, GVar.HEIGHT - self.spaceship.height - 10)
def __init__(self, window): self.window = window self.play = Sprite("./assets/images/play.png") self.difc = Sprite("./assets/images/difc.png") self.rank = Sprite("./assets/images/rank.png") self.quit = Sprite("./assets/images/quit.png") self.mouse = Mouse() self.__set_pos() self.__draw()
def shoot(self): if self.can_shoot == True and self.set_cooldown == False: self.bullet_cooldown = 100 + randint(0, 50) self.set_cooldown = True if self.can_shoot: alien = self.enemy_mtx[randint(0, len(self.enemy_mtx) - 1)] bullet = Sprite("./assets/images/bullet.png") bullet.set_position(alien.x + alien.width / 2, alien.y + alien.height) self.enemy__bullet_mtx.append(bullet) self.can_shoot = False self.set_cooldown = False if self.can_shoot == False: self.bullet_cooldown -= self.window.delta_time( ) * GVar.ENEMY_BULLET_COOLDOWN if self.bullet_cooldown < 0: self.can_shoot = True
class Menu(object): def __init__(self, window): self.window = window self.play = Sprite("./assets/images/play.png") self.difc = Sprite("./assets/images/difc.png") self.rank = Sprite("./assets/images/rank.png") self.quit = Sprite("./assets/images/quit.png") self.mouse = Mouse() self.__set_pos() self.__draw() def run(self): if self.mouse.is_over_object( self.play) and self.mouse.is_button_pressed(1): GVar.STATE = 1 if self.mouse.is_over_object( self.difc) and self.mouse.is_button_pressed(1): GVar.STATE = 2 if self.mouse.is_over_object( self.rank) and self.mouse.is_button_pressed(1): GVar.STATE = 3 if self.mouse.is_over_object( self.quit) and self.mouse.is_button_pressed(1): GVar.STATE = 4 self.__draw() def __draw(self): self.play.draw() self.difc.draw() self.rank.draw() self.quit.draw() def __set_pos(self): button_distance = (GVar.HEIGHT - 4 * self.play.height) / 5 self.play.set_position(GVar.WIDTH / 2 - self.play.width / 2, button_distance) self.difc.set_position(GVar.WIDTH / 2 - self.play.width / 2, 2 * button_distance + self.play.height) self.rank.set_position(GVar.WIDTH / 2 - self.play.width / 2, 3 * button_distance + 2 * self.play.height) self.quit.set_position(GVar.WIDTH / 2 - self.play.width / 2, 4 * button_distance + 3 * self.play.height)
def __init__(self, window): self.window = window self.easy_button = Sprite("./assets/images/easy.png") self.norm_button = Sprite("./assets/images/norm.png") self.hard_button = Sprite("./assets/images/hard.png") self.__set_pos()
class Difficulty(object): def __init__(self, window): self.window = window self.easy_button = Sprite("./assets/images/easy.png") self.norm_button = Sprite("./assets/images/norm.png") self.hard_button = Sprite("./assets/images/hard.png") self.__set_pos() def run(self): self.__draw() self.set_difficulty() def set_difficulty(self): if mouse.is_over_object(self.easy_button) and mouse.is_button_pressed(1): GVar.DIFC = 1 * GVar.DIFC_MULTIPLIER GVar.DIFC_CHOSEN = True GVar.STATE = 1 if mouse.is_over_object(self.norm_button) and mouse.is_button_pressed(1): GVar.DIFC = 2 * GVar.DIFC_MULTIPLIER GVar.DIFC_CHOSEN = True GVar.STATE = 1 if mouse.is_over_object(self.hard_button) and mouse.is_button_pressed(1): GVar.DIFC = 3 * GVar.DIFC_MULTIPLIER GVar.DIFC_CHOSEN = True GVar.STATE = 1 def __draw(self): self.easy_button.draw() self.norm_button.draw() self.hard_button.draw() def __set_pos(self): button_distance = (GVar.HEIGHT - 3 * self.easy_button.height)/4 self.easy_button.set_position(GVar.WIDTH/2 - self.easy_button.width/2, button_distance) self.norm_button.set_position(GVar.WIDTH/2 - self.easy_button.width/2, self.easy_button.height + 2 * button_distance) self.hard_button.set_position(GVar.WIDTH/2 - self.easy_button.width/2, 2 * self.easy_button.height + 3 * button_distance)
def __init__(self, window): self.window = window self.spaceship = Sprite("./assets/images/spaceship.png") self.velocity = 0 self.__set_pos()