def main(): player_manager = PlayerManager("Los Angeles Lakers", "file.txt") player1 = Forward(23, "Lebron", "James", 201, 81, 2003, "forward", 1028, 690) player2 = Center(12, "Dwight", "Howard", 210, 90, 2002, "center", 1054, "Aggresive") player3 = Guard(10, "Rajon", "Rondo", 190, 76, 2004, "guard", 909, 1203) player_manager.add_player(player1) player_manager.add_player(player2) player_manager.add_player(player3) print_report(player_manager) player_manager.delete_player(12) player_manager.delete_player(10) print_report(player_manager) player1 = Forward(23, "Yeet", "James", 69, 81, 2003, "forward", 1028, 690) player_manager.update_player(player1) print_report(player_manager) print(player_manager.get_player(23))
def game(): level = Labyrinth() level.open_csv(constants.CSV_LEVEL, constants.NSPRITE) hero = Macgyver() hero.create(level.structure) guard = Guard() guard.create(level.structure) ether = Item() ether.create(level.ground_list, level.structure, 'E') thumb = Item() thumb.create(level.ground_list, level.structure, 'T') needle = Item() needle.create(level.ground_list, level.structure, 'N') screen = View() screen.load() screen.create(level.structure) return level, hero, guard, ether, thumb, needle, screen
def add_player(): """ Adds a player to the player manager """ content = request.json try: if content["player_type"] == "center": center = Center(content["player_id"], content["first_name"], content["last_name"], content["height"], content["weight"], content["year_drafted"], content["player_type"], content["num_rebounds"], content["play_type"]) player_manager.add_player(center) elif content["player_type"] == "forward": forward = Forward(content["player_id"], content["first_name"], content["last_name"], content["height"], content["weight"], content["year_drafted"], content["player_type"], content["num_shots_took"], content["num_shots_made"]) player_manager.add_player(forward) elif content["player_type"] == "guard": guard = Guard(content["player_id"], content["first_name"], content["last_name"], content["height"], content["weight"], content["year_drafted"], content["player_type"], content["num_steals"], content["num_assists"]) player_manager.add_player(guard) response = app.response_class(status=200, ) except ValueError as e: response = app.response_class(response=str(e), status=400) return response
def draw_maze(self): """Draws the self.layout's content on screen and creates different entities Each number in self.layout corresponds to an element of the maze. 0 = Free space to walk and place items 1 = Walls blocking the player 2 = items placed by the place_items function at random 3 = Guard : kills the player if he doesn't have all 3 items 4 = Exit : Enables the win screen 5 = Start position of player At each number (except 0) a sprite is created and added to its group """ # Size of the maze in height and width (both are equal) size = 15 # Counters to build the maze column = 0 row = 0 # size**2 because width*height = number of tiles for tile in range(size**2): # 1 = walls if self.layout[row + column * size] == 1: # We create an instance of Maze and add it to wall group wall = Maze(self, row*32, column*32) self.all_walls.add(wall) # 2 = Items, randomised with self.place_items() elif self.layout[row + column * size] == 2: asset_name = self.item_list.pop() item = Items(self, asset_name, row*32, column*32) self.all_items.add(item) # 3 = Guard elif self.layout[row + column * size] == 3: guard = Guard(self, row*32, column*32) self.all_guards.add(guard) # 4 = Exit elif self.layout[row + column * size] == 4: exit = Exit(self, row*32, column*32) self.all_exits.add(exit) # 5 = Player elif self.layout[row + column * size] == 5: self.player = Player(self, row*32, column*32) self.all_players.add(self.player) # Entrance addead beneath player entrance = Maze(self, row*32, column*32) entrance.image = pygame.image.load(ASSETS + "entrance.png") entrance.image = pygame.transform.scale( entrance.image, (32, 32)) self.all_walls.add(entrance) column += 1 if column > size - 1: column = 0 row += 1
def setUp(self): """ Sets up data and calls logPoint """ self.logPoint() self.player_manager = PlayerManager("Los Angeles Lakers", "file.txt") self.player1 = Guard(1, "Rajon", "Rondo", 190, 76, 2004, "guard", 909, 1203) self.player_manager.add_player(self.player1) self.assertIsNotNone(self.player_manager)
def __init__(self): self.number = Rule.numRules print Rule.numRules Rule.numRules += 1 print "making a rule" print Rule.numRules self.count = 0 self.index = 0 self.theGuard = Guard(self)
def test_update_valid_input(self): """ Tests the update method with valid inputs """ string = "3: Rajon Rondo is 190.00 cm tall, weighs 76.00 kg, drafted on 2004, has 909 steals and 1203 assists" self.assertEqual(string, self.player3.get_description(), "These two strings should be equal") self.player3 = Guard(3, "June", "Ka", 180, 81, 2019, 0, 0) self.player_manager.update_player(self.player3) string2 = "3: June Ka is 180.00 cm tall, weighs 81.00 kg, drafted on 2019, has 0 steals and 0 assists" self.assertEqual(string2, self.player3.get_description(), "These two strings should be equal")
def test_add_player_valid_input(self): """ Tests the add_player method with valid inputs """ self.assertEqual( 3, self.player_manager.get_players_stats().get_total_num_players(), "Team should have 3 players") player4 = Guard(7, "June", "Ka", 190, 76, 2004, 909, 1203) self.player_manager.add_player(player4) self.assertEqual( 4, self.player_manager.get_players_stats().get_total_num_players(), "Team should have 4 players")
def main(input_file): # Load input as events in sorted order (Event timestamp) events = [] with open(input_file) as file: for line in file: # TODO: Binary insert based off datetime instead of append e = Event(line) if (len(events) > 0): binary_insert(events, e) else: events.append(e) # Setup guards with shifts, and those shifts with events, in sorted order (Guard ID) guards = [] current_guard_id = 0 current_shift = Shift() for e in events: if e.type == 0: tmp_guard = Guard(e.GetGuardID()) if len(guards) > 0: guards[current_guard_id].add_shift(current_shift) current_shift = Shift() index = binary_search(guards, tmp_guard) if index == -1: current_guard_id = binary_insert(guards, tmp_guard) else: current_guard_id = index else: guards.append(tmp_guard) current_shift.add_event(e) # Find guard who slept most result_index = index_of_max([sum(g.minutes_asleep) for g in guards]) # Most slept minute msm = guards[result_index].get_most_slept_minute() # Part 1 Answer print("Guard #", guards[result_index].id, sep='') print("Slept most at minute", msm) print("Part 1 Answer:", guards[result_index].id * msm) print() # Part 2 Answer msm_count = [g.minutes_asleep[g.get_most_slept_minute()] for g in guards] result_index = index_of_max(msm_count) msm = guards[result_index].get_most_slept_minute() print("Guard #", guards[result_index].id, sep='') print("Slept most at minute ", msm, ', ', msm_count[result_index], ' times', sep='') print("Part 2 Answer:", guards[result_index].id * msm)
def setUp(self): """ Sets up data and calls logPoint """ self.logPoint() self.player_manager = PlayerManager("Los Angeles Lakers") self.player1 = Forward(1, "Lebron", "James", 201, 81, 2003, 1028, 690) self.player2 = Center(2, "Dwight", "Howard", 210, 90, 2002, 1054, "Aggresive") self.player3 = Guard(3, "Rajon", "Rondo", 190, 76, 2004, 909, 1203) self.player_manager.add_player(self.player1) self.player_manager.add_player(self.player2) self.player_manager.add_player(self.player3) self.assertIsNotNone(self.player_manager)
def _read_players_from_file(self): """ Reads players from file """ with open(self._filepath, 'r') as input_file: players = json.load(input_file) for json_data in players: type = json_data["player_type"] if type == "center": player_id = json_data["player_id"] first_name = json_data["first_name"] last_name = json_data["last_name"] height = json_data["height"] weight = json_data["weight"] year_drafted = json_data["year_drafted"] player_type = json_data["player_type"] num_rebounds = json_data["num_rebounds"] play_type = json_data["play_type"] player = Center(player_id, first_name, last_name, height, weight, year_drafted, player_type, num_rebounds, play_type) elif type == "forward": player_id = json_data["player_id"] first_name = json_data["first_name"] last_name = json_data["last_name"] height = json_data["height"] weight = json_data["weight"] year_drafted = json_data["year_drafted"] player_type = json_data["player_type"] num_shots_took = json_data["num_shots_took"] num_shots_made = json_data["num_shots_made"] player = Forward(player_id, first_name, last_name, height, weight, year_drafted, player_type, num_shots_took, num_shots_made) elif type == "guard": player_id = json_data["player_id"] first_name = json_data["first_name"] last_name = json_data["last_name"] height = json_data["height"] weight = json_data["weight"] year_drafted = json_data["year_drafted"] player_type = json_data["player_type"] num_steals = json_data["num_steals"] num_assists = json_data["num_assists"] player = Guard(player_id, first_name, last_name, height, weight, year_drafted, player_type, num_steals, num_assists) self._players.append(player) return self._players
def setUp(self): engine = create_engine('sqlite:///test_players.sqlite') # Create all the tables Base.metadata.create_all(engine) Base.metadata.bind = engine """ Sets up data and calls logPoint """ self.logPoint() self.player_manager = PlayerManager("test_players.sqlite") self.player1 = Forward(1, "Lebron", "James", 201, 81, 2003, "forward", 1028, 690) self.player2 = Center(2, "Dwight", "Howard", 210, 90, 2002, "guard", 1054, "Aggresive") self.player3 = Guard(3, "Rajon", "Rondo", 190, 76, 2004, "center", 909, 1203) self.player_manager.add_player(self.player1) self.player_manager.add_player(self.player2) self.player_manager.add_player(self.player3) self.assertIsNotNone(self.player_manager)
def get_all_guards(logs): guards = {} current_sleep_start = None current_guard = None for entry in logs: log = Log(entry) if log.log_type == LogTypes.NEW_GUARD: guard_id = log.get_guard_number_from_new_guard_message() if guard_id in guards: current_guard = guards[guard_id] else: new_guard = Guard(guard_id) guards[guard_id] = new_guard current_guard = new_guard elif log.log_type == LogTypes.FALL_ASLEEP: current_sleep_start = log.minute elif log.log_type == LogTypes.WAKE_UP: current_guard.add_sleep(int(current_sleep_start), int(log.minute)) return list(guards.values())
def init(self): margin = self.width * 0.10 pygame.font.init() self.score = 0 # Ship Ship.init(self.width) self.shipGroup = pygame.sprite.Group(Ship(self.width/2, \ self.height - margin)) col = self.width // 10 self.enemies = pygame.sprite.Group() # Boss Boss.init(self.width, 'images/boss.png') wB, hB = Boss.image.get_size() for i in range(3, 7): x = i * col + wB / 2 y = margin self.enemies.add(Boss(x, y)) # Guards Guard.init(self.width) for i in range(1, 9): for j in range(2, 4): x = i * col + wB / 2 + 1 # centered based on boss y = margin * j self.enemies.add(Guard(x, y)) # Grunts Grunt.init(self.width) for i in range(10): for j in range(4, 6): x = i * col + wB / 2 + 1 # centered based on boss y = margin * j self.enemies.add(Grunt(x, y)) self.bullets = pygame.sprite.Group()
def update_player(player_id): """ Updates an existing player""" content = request.json player = player_manager.get_player(player_id) if player == None: response = app.response_class(status=404) try: if content["player_type"] == "center": center = Center(content["player_id"], content["first_name"], content["last_name"], content["height"], content["weight"], content["year_drafted"], content["player_type"], content["num_rebounds"], content["play_type"]) player_manager.update_player(center) elif content["player_type"] == "forward": forward = Forward(content["player_id"], content["first_name"], content["last_name"], content["height"], content["weight"], content["year_drafted"], content["player_type"], content["num_shots_took"], content["num_shots_made"]) player_manager.update_player(forward) elif content["player_type"] == "guard": guard = Guard(content["player_id"], content["first_name"], content["last_name"], content["height"], content["weight"], content["year_drafted"], content["player_type"], content["num_steals"], content["num_assists"]) player_manager.update_player(guard) response = app.response_class(status=200) except ValueError as e: response = app.response_class(response=str(e), status=404) return response
def main(): # constants WIN_X = 640 WIN_Y = 480 FONT_SIZE = 16 S_U = Shadow.U # shadow unit size taken from Shadow class BASE_RESET_TICKS = 60 * 3 # 3 seconds to reset after being caught # pygame setup pygame.init() screen = pygame.display.set_mode((WIN_X, WIN_Y)) font = pygame.font.SysFont("Courier", FONT_SIZE, True) clock = pygame.time.Clock() # player data player = Player() # Shadow/Guard data for each level. These two lists need to be # of the same size, otherwise, there will be some extra data # that is unused in either list, and the game may not do level # transition properly. The lists for the individual levels' data # within the main list can be of any size. shadows = [[Shadow(0, 0, 1, 1), Shadow(WIN_X - S_U, WIN_Y - S_U, 1, 1), Shadow(100, 100, 8, 2), Shadow(275, 300, 8, 2)], [Shadow(0, 0, 1, 1), Shadow(100, 100, 1, 8), Shadow(200, 100, 8, 2), Shadow(500, 100, 2, 10), Shadow(WIN_X - S_U, WIN_Y - S_U, 1, 1)], [Shadow(0, 0, 1, 1), Shadow(WIN_X - S_U, WIN_Y - S_U, 1, 1), Shadow(150, 70, 2, 3), Shadow(360, 150, 2, 2), Shadow(450, 70, 2, 5), Shadow(150, 240, 2, 3), Shadow(300, 240, 2, 3), Shadow(450, 270, 2, 5), Shadow(170, 350, 5, 1)], [Shadow(0, 0, 1, 1), Shadow(WIN_X - S_U, WIN_Y - S_U, 1, 1), Shadow(85, 75, 2, 8), Shadow(180, 300, 7, 2), Shadow(225, 225, 2, 2), Shadow(290, 150, 2, 2), Shadow(370, 95, 2, 2), Shadow(450, 70, 5, 4), Shadow(565, 230, 2, 6)]] guards = [[Guard(500, 75, 500-250, 75+250), Guard(85, 400, 85+250, 400-250)], [Guard(400, 400, 250, 250), Guard(500, 200, 250, 450), Guard(100, 50, 100-75, 50+75)], [Guard(190, 50, 190+130, 50+130), Guard(330, 225, 330-150, 225+150), Guard(265, 410, 265+170, 410-170)], [Guard(60, 300, 60+100, 300+100), Guard(220, 160, 220+110, 160+110), Guard(500, 300, 500-220, 300-220), Guard(610, 215, 610-160, 215+160)]] # ---------------------------------------------------------- # metagame data ticksToReset = BASE_RESET_TICKS levelComplete = False levelFailed = False level = 0 NUM_LEVELS = len(shadows) levelDisplay = "Level: {0}/{1}".format(level + 1, NUM_LEVELS) status = "You are undetected." bg = pygame.image.load("bg.png").convert() while True: ## EVENTS ---------------------------------------------- nextEvent = pygame.event.poll() # quit game if nextEvent.type == pygame.QUIT: break # accept character movement if level is yet completed if not levelComplete and not levelFailed: keysDown = pygame.key.get_pressed() if keysDown[pygame.K_UP] and player.posY > 0: player.move("UP") if keysDown[pygame.K_DOWN] and player.posY < WIN_Y: player.move("DN") if keysDown[pygame.K_LEFT] and player.posX > 0: player.move("LT") if keysDown[pygame.K_RIGHT] and player.posX < WIN_X: player.move("RT") ## UPDATE ---------------------------------------------- # update player data based on new position, shadows, and guards player.update(shadows[level], guards[level]) # update guards for guard in guards[level]: guard.update() # check for necessary changes to metagame flags if player.getX() >= WIN_X - 16 and player.getY() >= WIN_Y - 16: levelComplete = True if player.isCaught(): levelFailed = True # update status text and counter if not levelComplete and not levelFailed: status = "You are undetected." elif levelComplete: if level + 1 < NUM_LEVELS: status = "Transition in {0}...".format(ticksToReset / 60 + 1) ticksToReset -= 1 else: status = "Game complete!" elif levelFailed: status = "Caught! Restart in {0}...".format(ticksToReset / 60 + 1) ticksToReset -= 1 # reset level if necessary if levelFailed and ticksToReset == 0: ticksToReset = BASE_RESET_TICKS levelFailed = False player.reset() for guard in guards[level]: guard.reset() # level transition if necessary if levelComplete and ticksToReset == 0: ticksToReset = BASE_RESET_TICKS levelComplete = False level += 1 levelDisplay = "Level: {0}/{1}".format(level + 1, NUM_LEVELS) status = "You are undetected." player.reset() ## RENDER ---------------------------------------------- # render background screen.blit(bg, (0,0)) # render player player.draw(screen) # render guards for guard in guards[level]: guard.draw(screen) # render shadows for shadow in shadows[level]: shadow.draw(screen) # render visibility display player.drawVisibility(screen) # display status text line1 = font.render(levelDisplay, True, (0, 0, 0)) line2 = font.render(status, True, (0, 0, 0)) screen.blit(line1, (10, WIN_Y - 10 - 2 * FONT_SIZE)) screen.blit(line2, (10, WIN_Y - 10 - FONT_SIZE)) pygame.display.flip() clock.tick(60) pygame.quit()
def init(self): pygame.font.init() self.state = "start" # Start state self.logo = pygame.transform.scale(pygame.image.load('images/galaga-logo.png').convert_alpha(), \ (int(self.height/2), int(self.width/2))) startFont = pygame.font.SysFont('emulogic', 15) self.startTxt = startFont.render("Press 'SPACE' to start.", False, \ (0,255,255)) self.displayTxt = True # Game state margin = self.width * 0.12 offset = self.width * 0.07 # For alien separation self.time = 0 self.score = 0 # For score text self.scoreFont = pygame.font.SysFont('emulogic', 15) self.text = self.scoreFont.render('Score ', False, (255,0,0), (0,0,0)) self.scoreTxt = self.scoreFont.render(str(self.score), False, (255,255,255), (0,0,0)) # Ship Ship.init(self.width) self.startX = self.width/2 self.startY = self.height - margin self.shipGroup = pygame.sprite.Group(Ship(self.startX, self.startY)) col = self.width//10 self.enemies = pygame.sprite.Group() # Boss Boss.init(self.width, 'images/boss.png') wB, hB = Boss.image.get_size() for i in range(3, 7): x = i * col + wB/2 y = margin self.enemies.add(Boss(x, y)) # Guards Guard.init(self.width) for i in range(1, 9): for j in range(2, 4): x = i * col + wB/2 + 1 # centered based on boss y = margin/2 + offset * j self.enemies.add(Guard(x, y)) # Grunts Grunt.init(self.width) for i in range(10): for j in range(4, 6): x = i * col + wB/2 + 1 # centered based on boss y = margin/2 + offset * j self.enemies.add(Grunt(x, y)) self.shipBullets = pygame.sprite.Group() self.enemyBullets = pygame.sprite.Group() stateFont = pygame.font.SysFont('emulogic', 20) self.gameOver = False # game over text self.gO = stateFont.render('GAME OVER', False, (255, 0, 0)) self.won = False # wining text self.win = stateFont.render('YOU WON', False, (0,255,255))
from guard import Guard if __name__ == "__main__": print(f'***EXAMPLE 1***') guard = Guard(name='Christopher') print(guard.greet()) print(guard.receive_bribe(10)) print(f'***EXAMPLE 2***') guard = Guard(salary=100) print(guard.greet()) print(guard.receive_bribe(95)) print(guard.receive_bribe(105)) print(f'***EXAMPLE 3***') guard = Guard(name='Christopher', salary=100) print(guard.greet()) print(guard.receive_bribe(80)) print(guard.receive_bribe(135))
def init(self): self.introMusic = pygame.mixer.Sound('sounds/stage-intro.ogg') self.introTimer = 7 self.playedIntro = False self.intro = True self.error = 3 if self.difficulty == "easy": self.error = 3 elif self.difficulty == "normal": self.error = 2 elif self.difficulty == "hard": self.error = 1 self.margin = self.width * 0.12 offset = self.width * 0.07 # For alien separation self.time = 0 self.score = 0 # For score text self.scoreFont = pygame.font.SysFont('emulogic', 15) self.text = self.scoreFont.render('Score ', False, (255, 0, 0), (0, 0, 0)) self.scoreTxt = self.scoreFont.render(str(self.score), False, (255, 255, 255), (0, 0, 0)) # Ship Ship.init(self.width) self.startX = self.width / 2 self.startY = self.height - self.margin self.shipGroup = pygame.sprite.Group(Ship(self.startX, self.startY)) # Ship lives imageSize = self.shipGroup.sprites()[0].size // 2 livesImage = pygame.transform.scale(\ pygame.image.load('images/ship-lives.png').convert_alpha(),\ (imageSize, imageSize)) self.shipLives = [] for i in range(self.shipGroup.sprites()[0].lives): self.shipLives.append(livesImage) self.deathTime = 0 # Explosions self.shipExpl = pygame.sprite.Group() self.enemyExpl = pygame.sprite.Group() self.shipDead = pygame.mixer.Sound('sounds/explosion.ogg') self.enemyDead = pygame.mixer.Sound('sounds/enemy-dead.ogg') self.canShoot = True self.divingSound = pygame.mixer.Sound('sounds/enemy-diving.ogg') self.respawn = True col = self.width // 10 self.enemies = pygame.sprite.Group() # Boss Boss.init(self.width, 'images/boss.png') wB, hB = Boss.image.get_size() for i in range(3, 7): x = i * col + wB / 2 y = self.margin self.enemies.add(Boss(x, y, self.difficulty)) # Guards Guard.init(self.width, 'images/guard.png') for i in range(1, 9): for j in range(2, 4): x = i * col + wB / 2 + 1 # centered based on boss y = self.margin / 2 + offset * j self.enemies.add(Guard(x, y, self.difficulty)) # Grunts Grunt.init(self.width, 'images/grunt.png') for i in range(10): for j in range(4, 6): x = i * col + wB / 2 + 1 # centered based on boss y = self.margin / 2 + offset * j self.enemies.add(Grunt(x, y, self.difficulty)) self.buckets = {} self.midBuckets = set() # middle of buckets for enemy in self.enemies: if isinstance(enemy, Grunt): enemyRect = enemy.rect x, y = enemyRect.topright cx = enemyRect.centerx self.buckets[x] = [] self.midBuckets.add(cx) self.midBuckets = sorted(list(self.midBuckets)) self.bucketsLst = sorted(self.buckets.keys()) # self.flap = False self.shipBullets = pygame.sprite.Group() self.shipSound = pygame.mixer.Sound('sounds/ship-shot.ogg') self.enemyBullets = pygame.sprite.Group() stateFont = pygame.font.SysFont('emulogic', 20) self.gameOver = False self.gOMusic = pygame.mixer.Sound('sounds/game-over.ogg') # game over text self.gO = stateFont.render('GAME OVER', False, (255, 0, 0)) self.won = False self.wonMusic = pygame.mixer.Sound('sounds/game-won.ogg') # wining text self.win = stateFont.render('YOU WON', False, (0, 255, 255)) self.playedMusic = False self.restartFont = pygame.font.SysFont('emulogic', 15) self.restartTxt = self.restartFont.render("Press 'R' to restart.", False, \ (255,0,0)) self.displayTxt = True