def __init__(self): self.directionX = 0 self.directionY = 0 pygame.sprite.Sprite.__init__(self) #Type of asteroid self.type_asteroid = random_randrange(7) if self.type_asteroid == const.BIG_ASTEROID1: self.image = const.SPRITE_B_AST1 elif self.type_asteroid == const.BIG_ASTEROID2: self.image = const.SPRITE_B_AST2 elif self.type_asteroid == const.BIG_ASTEROID3: self.image = const.SPRITE_B_AST3 elif self.type_asteroid == const.BIG_ASTEROID4: self.image = const.SPRITE_B_AST4 elif self.type_asteroid == const.BIG_ASTEROID5: self.image = const.SPRITE_B_AST5 elif self.type_asteroid == const.AN_ASTEROID1: self.image = const.SPRITE_A_AST1 elif self.type_asteroid == const.AN_ASTEROID2: self.image = const.SPRITE_A_AST2 screen = pygame.display.get_surface() self.area = screen.get_rect() self.starting_side = random_randrange(4) self.rect = self.image.get_rect() if self.starting_side == 0: self.location = [random_uniform(0, const.WINDOW_WIDTH - self.image.get_width() / 2 - 1 ), - self.image.get_height()/2 + 1] self.directionX = random_uniform(-1, 1) self.directionY = random_uniform(0.1, 1) elif self.starting_side == 1: self.location = [random_uniform(0, const.WINDOW_WIDTH - self.image.get_width() / 2 - 1), const.WINDOW_HEIGHT - self.image.get_height()/2 - 1] self.directionX = random_uniform(-1, 1) self.directionY = random_uniform(-0.1, -1) elif self.starting_side == 2: self.location = [-self.image.get_width() / 2 + 1, random_uniform(0, const.WINDOW_HEIGHT - self.image.get_height()/2 - 1)] self.directionX = random_uniform(0.1, 1) self.directionY = random_uniform(-1, 1) elif self.starting_side == 3: self.location = [const.WINDOW_WIDTH - self.image.get_width() / 2 - 1, random_uniform(0, const.WINDOW_HEIGHT - self.image.get_height()/2 - 1)] self.directionX = random_uniform(-0.1, -1) self.directionY = random_uniform(-1, 1) if -0.1 < self.directionX < 0.1: self.directionX = 0.1 * self.directionX / abs(self.directionX) if -0.1 < self.directionY < 0.1: self.directionY = 0.1 * self.directionY / abs(self.directionY)
def __init__(self): self.directionX = 0 self.directionY = 0 pygame.sprite.Sprite.__init__(self) #Type of asteroid self.type_asteroid = random_randrange(7) if self.type_asteroid == const.BIG_ASTEROID1: self.image = const.SPRITE_B_AST1 elif self.type_asteroid == const.BIG_ASTEROID2: self.image = const.SPRITE_B_AST2 elif self.type_asteroid == const.BIG_ASTEROID3: self.image = const.SPRITE_B_AST3 elif self.type_asteroid == const.BIG_ASTEROID4: self.image = const.SPRITE_B_AST4 elif self.type_asteroid == const.BIG_ASTEROID5: self.image = const.SPRITE_B_AST5 elif self.type_asteroid == const.AN_ASTEROID1: self.image = const.SPRITE_A_AST1 elif self.type_asteroid == const.AN_ASTEROID2: self.image = const.SPRITE_A_AST2 screen = pygame.display.get_surface() self.area = screen.get_rect() self.starting_side = random_randrange(4) if self.starting_side == 0: self.location = [random_uniform(0, const.WINDOW_WIDTH - self.image.get_width() / 2 - 1 ), - self.image.get_height()/2 + 1] self.directionX = random_uniform(-1, 1) self.directionY = random_uniform(0.1, 1) elif self.starting_side == 1: self.location = [random_uniform(0, const.WINDOW_WIDTH - self.image.get_width() / 2 - 1), const.WINDOW_HEIGHT - self.image.get_height()/2 - 1] self.directionX = random_uniform(-1, 1) self.directionY = random_uniform(-0.1, -1) elif self.starting_side == 2: self.location = [-self.image.get_width() / 2 + 1, random_uniform(0, const.WINDOW_HEIGHT - self.image.get_height()/2 - 1)] self.directionX = random_uniform(0.1, 1) self.directionY = random_uniform(-1, 1) elif self.starting_side == 3: self.location = [const.WINDOW_WIDTH - self.image.get_width() / 2 - 1, random_uniform(0, const.WINDOW_HEIGHT - self.image.get_height()/2 - 1)] self.directionX = random_uniform(-0.1, -1) self.directionY = random_uniform(-1, 1) if -0.1 < self.directionX < 0.1: self.directionX = 0.1 * self.directionX / abs(self.directionX) if -0.1 < self.directionY < 0.1: self.directionY = 0.1 * self.directionY / abs(self.directionY)
def process_keywords(self, keystack: Keystack, sentence: str) -> str: self.decomposition_result.clear() output = None for key in keystack: try: output = self.process_keyword(sentence, key) except ValueError as val_err: e = f"{str(val_err)} Keyword processing failed." logging.exception(e) raise KeywordProcessingFailedException(key, keystack, e) except ReassemblyRuleNotFoundException: raise if output: logging.debug(f"Output from key: {output}") break if not output: if self.context_memory: index = random_randrange(len(self.context_memory)) output = self.context_memory.pop(index) logging.debug(f"Output from memory: {output}") else: output = Reassembler.next_reassembly( PhraseMemory.keyword("xnone").decomposition_rules[0]) logging.debug(f"Output from xnone: {output}") return ' '.join(output)
def __init__(self): self.directionX = 0 self.directionY = 0 pygame.sprite.Sprite.__init__(self) #Type of asteroid self.type_asteroid = random_randrange(7) if self.type_asteroid == const.BIG_ASTEROID1: self.image = const.SPRITE_B_AST1 elif self.type_asteroid == const.BIG_ASTEROID2: self.image = const.SPRITE_B_AST2 elif self.type_asteroid == const.BIG_ASTEROID3: self.image = const.SPRITE_B_AST3 elif self.type_asteroid == const.BIG_ASTEROID4: self.image = const.SPRITE_B_AST4 elif self.type_asteroid == const.BIG_ASTEROID5: self.image = const.SPRITE_B_AST5 elif self.type_asteroid == const.AN_ASTEROID1: self.image = const.SPRITE_A_AST1 elif self.type_asteroid == const.AN_ASTEROID2: self.image = const.SPRITE_A_AST2 screen = pygame.display.get_surface() self.area = screen.get_rect() self.location = [ random_uniform(0, const.WINDOW_WIDTH - self.image.get_width()), 0 ] if self.location[0] + self.image.get_width() > const.WINDOW_WIDTH: self.directionX = random_uniform(-1, 1) else: self.directionX = random_uniform(-1, 2) if self.location[1] + self.image.get_height() > const.WINDOW_HEIGHT: self.directionY = random_uniform(-1, 1) elif self.location[ 1] + 1 < const.WINDOW_HEIGHT and self.directionX == 0: self.directionY = random_uniform(0, 2) else: self.directionY = random_uniform(0, 2)
def __init__(self): self.directionX = 0 self.directionY = 0 pygame.sprite.Sprite.__init__(self) #Type of asteroid self.type_asteroid = random_randrange(7) if self.type_asteroid == const.BIG_ASTEROID1: self.image = const.SPRITE_B_AST1 elif self.type_asteroid == const.BIG_ASTEROID2: self.image = const.SPRITE_B_AST2 elif self.type_asteroid == const.BIG_ASTEROID3: self.image = const.SPRITE_B_AST3 elif self.type_asteroid == const.BIG_ASTEROID4: self.image = const.SPRITE_B_AST4 elif self.type_asteroid == const.BIG_ASTEROID5: self.image = const.SPRITE_B_AST5 elif self.type_asteroid == const.AN_ASTEROID1: self.image = const.SPRITE_A_AST1 elif self.type_asteroid == const.AN_ASTEROID2: self.image = const.SPRITE_A_AST2 screen = pygame.display.get_surface() self.area = screen.get_rect() self.location = [random_uniform(0, const.WINDOW_WIDTH - self.image.get_width()) ,0] if self.location[0] + self.image.get_width() > const.WINDOW_WIDTH: self.directionX = random_uniform(-1, 1) else: self.directionX = random_uniform(-1, 2) if self.location[1] + self.image.get_height() > const.WINDOW_HEIGHT: self.directionY = random_uniform(-1, 1) elif self.location[1] + 1 < const.WINDOW_HEIGHT and self.directionX == 0: self.directionY = random_uniform(0, 2) else: self.directionY = random_uniform(0, 2)
def show_start_screen(): play = False #Title titleFont = pygame.font.Font(const.BASICFONT, 100) titleSurf = titleFont.render('Asteroids', True, const.WHITE, const.BGCOLOR) displacement = 1 state = 0 #Buttons StartButton = obj.Button(const.DARKGREEN, const.GREEN, (const.WINDOW_WIDTH // 2 - 50, const.WINDOW_HEIGHT * 2 // 3, 100, 40)) #Objects asteroid_list = [] battleShip = obj.Ship([const.WINDOW_WIDTH // 2 - 55, 2 * const.WINDOW_HEIGHT // 3 - const.SPRITE_SHIP.get_height(), 10, 10]) slow_ship = True pygame.key.set_repeat(1, 1) while not play: if random_randrange(25) == 0: asteroid_list.append( obj.Asteroid() ) const.DISPLAYSURF.fill(const.BGCOLOR) titleRect = titleSurf.get_rect() titleRect.center = (const.WINDOW_WIDTH / 2, const.WINDOW_HEIGHT / 2 + displacement) for i in asteroid_list: i.move() battleShip.move(slow_ship) const.DISPLAYSURF.blit(titleSurf, titleRect) for i in asteroid_list: const.DISPLAYSURF.blit(i.image, i.location) const.DISPLAYSURF.blit( pygame.transform.rotate(battleShip.image, battleShip.angle), battleShip.blit_location ) StartButton.Func('Start', const.WHITE, 20) #event loop for event in pygame.event.get(): if event.type == pygame.QUIT: func.terminate() elif event.type==pygame.MOUSEBUTTONDOWN: if StartButton.Press(): play = True pygame.display.update() const.FPSCLOCK.tick(15) if displacement > 10: state = 0 elif displacement < -10: state = 1 if state: displacement += 1 else: displacement -= 1 return play
def show_start_screen(): #Title titleFont = pygame.font.Font(const.BASICFONT, 100) titleSurf = titleFont.render('Asteroids', True, const.WHITE, const.BGCOLOR) displacement = 1 state = 0 #Objects asteroid_list = [] battleShip = obj.Ship( [const.WINDOW_WIDTH // 2 - 15, 2 * const.WINDOW_HEIGHT // 3, 10, 10]) slow_ship = True #Buttons StartButton = obj.Button( const.DARKGREEN, const.GREEN, (const.WINDOW_WIDTH // 2 - 50, const.WINDOW_HEIGHT * 2 // 3, 100, 40)) pygame.mixer.music.play() pygame.key.set_repeat(1, 1) while True: if random_randrange(25) == 0: asteroid_list.append(obj.Asteroid()) const.DISPLAYSURF.fill(const.BGCOLOR) titleRect = titleSurf.get_rect() titleRect.center = (const.WINDOW_WIDTH / 2, const.WINDOW_HEIGHT / 2 + displacement) for i in asteroid_list: i.move() battleShip.move(slow_ship) const.DISPLAYSURF.blit(titleSurf, titleRect) for i in asteroid_list: const.DISPLAYSURF.blit(i.image, i.location) const.DISPLAYSURF.blit( pygame.transform.rotate(battleShip.image, battleShip.angle), battleShip.blit_location) StartButton.Func('Start', const.WHITE, 20) #event loop for event in pygame.event.get(): if event.type == pygame.QUIT: func.terminate() elif event.type == pygame.MOUSEBUTTONDOWN: if StartButton.Press(): pass #start game elif event.type == pygame.KEYDOWN: slow_ship = False if event.key == pygame.K_LEFT: battleShip.angle += 3 elif event.key == pygame.K_RIGHT: battleShip.angle -= 3 elif event.key == pygame.K_DOWN: battleShip.acceleration = [ math_sin(math_pi * battleShip.angle / 180), math_cos(math_pi * battleShip.angle / 180) ] elif event.key == pygame.K_UP: battleShip.acceleration = [ -math_sin(math_pi * battleShip.angle / 180), -math_cos(math_pi * battleShip.angle / 180) ] elif event.type == pygame.KEYUP: slow_ship = True pygame.display.update() const.FPSCLOCK.tick(15) if displacement > 10: state = 0 elif displacement < -10: state = 1 if state: displacement += 1 else: displacement -= 1
def play(): #Objects asteroid_list = [] bullet_list = [] battleShip = obj.Ship((const.WINDOW_WIDTH // 2 - const.SPRITE_SHIP.get_width() //2 , const.WINDOW_HEIGHT // 2 - const.SPRITE_SHIP.get_height() // 2)) slow_ship = True const.DISPLAYSURF.fill(const.BGCOLOR) #clear the screen #Music settings pygame.mixer.music.set_volume(0.5) pygame.mixer.music.play() pygame.key.set_repeat(1, 1) while True: if random_randrange(25) == 0: asteroid_list.append( obj.Asteroid() ) const.DISPLAYSURF.fill(const.BGCOLOR) for i in asteroid_list: i.move() battleShip.move(slow_ship) for i in asteroid_list: const.DISPLAYSURF.blit(i.image, i.location) if len(bullet_list) != 0: for i in bullet_list: i.move(battleShip.image.get_width()) const.DISPLAYSURF.blit(i.image, i.location) const.DISPLAYSURF.blit( pygame.transform.rotate(battleShip.image, battleShip.angle), battleShip.blit_location ) #event loop for event in pygame.event.get(): if event.type == pygame.QUIT: func.terminate() elif event.type == pygame.KEYDOWN: slow_ship = False if event.key == pygame.K_LEFT: battleShip.angle += 3 elif event.key == pygame.K_RIGHT: battleShip.angle -= 3 elif event.key == pygame.K_DOWN: battleShip.acceleration = [math_sin(math_pi * battleShip.angle / 180), math_cos(math_pi * battleShip.angle / 180)] elif event.key == pygame.K_UP: battleShip.acceleration = [-math_sin(math_pi * battleShip.angle / 180), -math_cos(math_pi * battleShip.angle / 180)] elif event.type == pygame.KEYUP: if event.key == pygame.K_SPACE: bullet_list.append(obj.Bullet(battleShip.location[0] + const.SPRITE_SHIP.get_width() // 2 - const.SPRITE_BULLET.get_width() // 2, battleShip.location[1] + const.SPRITE_SHIP.get_height() // 4, battleShip.angle)) const.BLAST.play() else: slow_ship = True pygame.display.update() const.FPSCLOCK.tick(10)
def get_header(): quote = quote_list[random_randrange(0, len(quote_list))] return ('X-%s' % quote[0].replace(' ', '-'), quote[1])
def play(): #Objects asteroid_list = [] bullet_list = [] battleShip = obj.Ship((const.WINDOW_WIDTH // 2 - const.SPRITE_SHIP.get_width() //2 , const.WINDOW_HEIGHT // 2 - const.SPRITE_SHIP.get_height() // 2)) slow_ship = True list_explosion = [] lives = 2 lives_string = 'Lives: ' + str(lives) lives_font = pygame.font.Font(const.BASICFONT, 20) lives_surf = lives_font.render(lives_string, True, const.WHITE) lives_rect = lives_surf.get_rect() lives_rect.center = (lives_surf.get_width(), const.WINDOW_HEIGHT - lives_surf.get_height()) score = 0 score_string = 'Score: ' + str(score) score_font = pygame.font.Font(const.BASICFONT, 20) score_surf = score_font.render(score_string, True, const.WHITE) score_rect = score_surf.get_rect() score_rect.center = (const.WINDOW_WIDTH - score_surf.get_width(), const.WINDOW_HEIGHT - score_surf.get_height()) const.DISPLAYSURF.fill(const.BGCOLOR) #clear the screen #Music settings pygame.mixer.music.set_volume(0.2) pygame.mixer.music.play() while lives > 0: #Generating the asteroids if random_randrange(15) == 0: asteroid_list.append( obj.Asteroid() ) const.DISPLAYSURF.fill(const.BGCOLOR) const.DISPLAYSURF.blit(const.BG, (0,0)) for i in asteroid_list: i.move() if len(bullet_list) != 0: for i in bullet_list: if battleShip != 0: i.move(battleShip.image.get_width()) const.DISPLAYSURF.blit(i.image, i.location) for j in asteroid_list: if i.check_collision(j): asteroid_list.remove(j) bullet_list.remove(i) list_explosion.append(obj.Explosion(j)) score += 1 break if battleShip != 0: battleShip.move(slow_ship) const.DISPLAYSURF.blit( pygame.transform.rotate(battleShip.image, battleShip.angle), battleShip.blit_location ) for i in asteroid_list: if battleShip != 0: if battleShip.check_collision(i): pos_exp = battleShip.location battleShip = 0 lives -= 1 for j in range(len(asteroid_list)): asteroid_list.pop() for i in const.EXP_LIST: const.DISPLAYSURF.fill(const.BGCOLOR) const.DISPLAYSURF.blit(const.BG, (0,0)) const.DISPLAYSURF.blit(i, pos_exp) pygame.display.update() const.FPSCLOCK.tick(15) else: continue for i in asteroid_list: const.DISPLAYSURF.blit(i.image, i.location) score_string = 'Score: ' + str(score) score_surf = score_font.render(score_string, True, const.WHITE) const.DISPLAYSURF.blit(score_surf, score_rect) lives_string = 'Lives: ' + str(lives) lives_surf = lives_font.render(lives_string, True, const.WHITE) const.DISPLAYSURF.blit(lives_surf, lives_rect) if len(list_explosion) != 0: for i in list_explosion: i.draw() #event loop for event in pygame.event.get(): if event.type == pygame.QUIT: func.terminate() if battleShip == 0: battleShip = obj.Ship((const.WINDOW_WIDTH // 2 - const.SPRITE_SHIP.get_width() //2 , const.WINDOW_HEIGHT // 2 - const.SPRITE_SHIP.get_height() // 2)) else: list_key = [] list_key = pygame.key.get_pressed() if len(list_key) != 0: slow_ship = True if list_key[pygame.K_LEFT]: battleShip.angle += 10 slow_ship = False if list_key[pygame.K_RIGHT]: battleShip.angle -= 10 slow_ship = False if list_key[pygame.K_DOWN]: battleShip.acceleration = [math_sin(math_pi * battleShip.angle / 180), math_cos(math_pi * battleShip.angle / 180)] slow_ship = False if list_key[pygame.K_UP]: battleShip.acceleration = [-math_sin(math_pi * battleShip.angle / 180), -math_cos(math_pi * battleShip.angle / 180)] slow_ship = False if event.type == pygame.KEYUP: if event.key == pygame.K_SPACE: bullet_list.append(obj.Bullet(battleShip.location[0] + const.SPRITE_SHIP.get_width() // 2 - const.SPRITE_BULLET.get_width() // 2, battleShip.location[1] + const.SPRITE_SHIP.get_height() // 4, battleShip.angle)) const.BLAST.play() #delete the bullets that can't be seen on the screen for i in bullet_list: if i.location[0] < 0 or i.location[0] > const.WINDOW_WIDTH\ or i.location[1] < 0 or i.location[1] > const.WINDOW_HEIGHT: bullet_list.remove(i) #Same for asteroids for i in asteroid_list: if i.location[0] + i.image.get_width() < 0 or i.location[0] - i.image.get_width() > const.WINDOW_WIDTH\ or i.location[1] + i.image.get_height() < 0 or i.location[1] - i.image.get_height() > const.WINDOW_HEIGHT: asteroid_list.remove(i) for i in list_explosion: if i.size > 30: list_explosion.remove(i) pygame.display.update() const.FPSCLOCK.tick(15) pygame.mixer.music.stop() game_over()
def get_header(): quote = quote_list[random_randrange(0,len(quote_list))] return ('X-%s' % quote[0].replace(' ', '-'), quote[1])
def show_start_screen(): #Title titleFont = pygame.font.Font(const.BASICFONT, 100) titleSurf = titleFont.render('Asteroids', True, const.WHITE, const.BGCOLOR) displacement = 1 state = 0 #Objects asteroid_list = [] battleShip = obj.Ship([const.WINDOW_WIDTH // 2 - 15, 2 * const.WINDOW_HEIGHT // 3, 10, 10]) #Buttons StartButton = obj.Button(const.DARKGREEN, const.GREEN, (const.WINDOW_WIDTH // 2 - 50, const.WINDOW_HEIGHT * 2 // 3, 100, 40)) pygame.mixer.music.play() pygame.key.set_repeat(1, 1) while True: if random_randrange(25) == 0: asteroid_list.append( obj.Asteroid() ) const.DISPLAYSURF.fill(const.BGCOLOR) titleRect = titleSurf.get_rect() titleRect.center = (const.WINDOW_WIDTH / 2, const.WINDOW_HEIGHT / 2 + displacement) for i in asteroid_list: i.move() battleShip.move() const.DISPLAYSURF.blit(titleSurf, titleRect) for i in asteroid_list: const.DISPLAYSURF.blit(i.image, i.location) const.DISPLAYSURF.blit( pygame.transform.rotate(battleShip.image, battleShip.angle), battleShip.blit_location ) StartButton.Func('Start', const.WHITE, 20) #event loop for event in pygame.event.get(): if event.type == pygame.QUIT: func.terminate() elif event.type==pygame.MOUSEBUTTONDOWN: if StartButton.Press(): pass #start game elif event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: battleShip.angle += 3 elif event.key == pygame.K_RIGHT: battleShip.angle -= 3 elif event.key == pygame.K_DOWN: battleShip.acceleration = [math_sin(math_pi * battleShip.angle / 180), math_cos(math_pi * battleShip.angle / 180)] elif event.key == pygame.K_UP: battleShip.acceleration = [-math_sin(math_pi * battleShip.angle / 180), -math_cos(math_pi * battleShip.angle / 180)] pygame.display.update() const.FPSCLOCK.tick(15) if displacement > 10: state = 0 elif displacement < -10: state = 1 if state: displacement += 1 else: displacement -= 1