def __init__(self, line_thickness=10, speed=5): self.line_thickness = line_thickness self.speed = speed self.score = 0 # Initiate variable and set starting positions # any future changes made within rectangles ball_x = int(window_width / 2 - self.line_thickness / 2) ball_y = int(window_height / 2 - self.line_thickness / 2) self.ball = Ball(ball_x, ball_y, self.line_thickness, self.line_thickness, self.speed) self.paddles = {} paddle_height = 50 paddle_width = self.line_thickness user_paddle_x = 20 computer_paddle_x = window_width - paddle_width - 20 # you self.paddles['user'] = Paddle(user_paddle_x, paddle_width, paddle_height) # computer self.paddles['computer'] = AutoPaddle(computer_paddle_x, paddle_width, paddle_height, self.ball, self.speed) self.scoreboard = Scoreboard(0)
class Game(): def __init__(self, line_thickness=10, speed=5): self.line_thickness = line_thickness self.speed = speed self.score = 0 # Initiate variable and set starting positions # any future changes made within rectangles ball_x = int(window_width / 2 - self.line_thickness / 2) ball_y = int(window_height / 2 - self.line_thickness / 2) self.ball = Ball(ball_x, ball_y, self.line_thickness, self.line_thickness, self.speed) self.paddles = {} paddle_height = 50 paddle_width = self.line_thickness user_paddle_x = 20 computer_paddle_x = window_width - paddle_width - 20 # you self.paddles['user'] = Paddle(user_paddle_x, paddle_width, paddle_height) # computer self.paddles['computer'] = AutoPaddle(computer_paddle_x, paddle_width, paddle_height, self.ball, self.speed) self.scoreboard = Scoreboard(0) # Draws the arena the game will be played in. def draw_arena(self): display_surf.fill((0, 0, 0)) # Draw outline of arena pygame.draw.rect(display_surf, WHITE, ((0, 0), (window_width, window_height)), self.line_thickness * 2) # Draw centre line pygame.draw.line(display_surf, WHITE, (int(window_width / 2), 0), (int(window_width / 2), window_height), int(self.line_thickness / 4)) def update(self): self.ball.move() self.paddles['computer'].move() if self.ball.hit_paddle(self.paddles['computer']): self.ball.bounce('x') elif self.ball.hit_paddle(self.paddles['user']): self.ball.bounce('x') self.score += 1 elif self.ball.pass_computer(): self.score += 5 elif self.ball.pass_player(): self.score = 0 self.draw_arena() self.ball.draw() self.paddles['user'].draw() self.paddles['computer'].draw() self.scoreboard.display(self.score)
def __init__(self, app_path: str): self.trivia_collector = TriviaCollector( os.path.join(app_path, 'assets')) # Change renderers here. All methods have to be the same self.renderer = Renderer() # RendererCurses() # Change keyInputHandler here. All methods have to be the same self.key_input_handler = KeyInputHandler( ) # KeyInputCurses(self.renderer._window) self.player = Player() self.scoreboard = Scoreboard()
class TriviaGame(): def __init__(self, app_path: str): self.trivia_collector = TriviaCollector( os.path.join(app_path, 'assets')) # Change renderers here. All methods have to be the same self.renderer = Renderer() # RendererCurses() # Change keyInputHandler here. All methods have to be the same self.key_input_handler = KeyInputHandler( ) # KeyInputCurses(self.renderer._window) self.player = Player() self.scoreboard = Scoreboard() def run(self): # get a random trivia and shuffle questions trivia = self.trivia_collector.GetRandomTrivia() random.shuffle(trivia['questions']) # Display each question with shaffled options for question in trivia['questions']: count, correct_index, options = self._MixOptions(question) self.renderer.DisplayQuestion(question['question'], options, count) self.renderer.DisplayUserInfo(self.player) question_start_time = time.time( ) # time to calculate score coefficient answer = self.key_input_handler.GetAnswer(count) if answer == correct_index: self.player.UpdateScore(30 - (time.time() - question_start_time)) self.renderer.DisplaySuccess() self.key_input_handler.PressAnyKey() elif answer == -1: self.renderer.DestroyRenderer() return else: self.renderer.DisplayCorrectAnswer(question['correct']) self.key_input_handler.PressAnyKey() # Display score and store nickname in dashboard self.renderer.DisplayFinish(self.player) self.player.nickname = self.key_input_handler.GetString() self.scoreboard.AddToScoreboard(self.player) self.renderer.DisplayScoreboard(self.scoreboard.GetTop3(), self.player) self.key_input_handler.PressAnyKey() self.renderer.DestroyRenderer() def _MixOptions(self, question: Dict): options = question['incorrect'].copy() options.append(question['correct']) random.shuffle(options) index = options.index(question['correct']) return len(options), index, options
def recommend(): channel = str(request.form.get('channel_id').encode(encoding='ascii', errors='ignore')) cat = str(request.form.get('category')) cat = cat.replace(' ', '_').replace('&', 'and') y = extract_data.get_specific_channel(channel) if y.empty: return render_template('index.html', error="Please input a real channel ID.") df = pd.read_csv('data/YT'+cat+'_data.csv') score = Scoreboard() score.fit(df, y) user = y['title'][0] results = score.most_similar(25) return render_template('recommend.html', results=results, user=user)
def run_game(): # 初始化游戏并创建一个屏幕对象 pygame.init() pygame.display.set_caption('Alien Invasion') ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) ship = Ship(ai_settings, screen) play_button = Button(ai_settings, screen, 'Play') bullets = Group() aliens = Group() gf.create_fleet(ai_settings, screen, aliens, ship) stats = GameStats(ai_settings) sb = Scoreboard(ai_settings, screen, stats) # 开始游戏的主循环 while True: gf.check_events(ai_settings, screen, ship, bullets, stats, play_button, aliens, sb) if not stats.game_over: ship.update() gf.update_bullets(ai_settings, screen, bullets, aliens, ship, stats, sb) gf.update_aliens(ai_settings, screen, aliens, bullets, ship, stats, sb) gf.update_screen(ai_settings, screen, ship, bullets, aliens, stats, play_button, sb)
def recommend(): channel = str( request.form.get('channel_id').encode(encoding='ascii', errors='ignore')) cat = str(request.form.get('category')) cat = cat.replace(' ', '_').replace('&', 'and') y = extract_data.get_specific_channel(channel) if y.empty: return render_template('index.html', error="Please input a real channel ID.") df = pd.read_csv('data/YT' + cat + '_data.csv') score = Scoreboard() score.fit(df, y) user = y['title'][0] results = score.most_similar(25) return render_template('recommend.html', results=results, user=user)
def run_game(): """The function opens the game and creates a screen""" # Initialize pygame, settings, and screen object. pygame.init() ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.display_width, ai_settings.display_height)) pygame.display.set_caption("Alien Invasion") # Make a rocket ship rocket_ship = RocketShip(screen, ai_settings) # Make a group to store bullets in. bullets = Group() # Make a group of Aliens and the fleet. aliens = Group() game_functions.create_fleet(ai_settings, screen, rocket_ship, aliens) # Create an instance to store game statistics.# Create an instance to store game statistics and create a scoreboard stats = GameStats(ai_settings) empty_ship = EmptyShip(ai_settings, screen) sb = Scoreboard(ai_settings, screen, stats) # Make the Play button. play_button = Button(ai_settings, screen) # Start the main loop for the game. while True: game_functions.check_events(ai_settings, screen, stats, sb, play_button, rocket_ship, aliens, bullets) if stats.game_active: rocket_ship.update() game_functions.update_bullets(ai_settings, screen, stats, sb, rocket_ship, aliens, bullets) game_functions.update_aliens(ai_settings, stats, sb, screen, rocket_ship, aliens, bullets) game_functions.update_screen(ai_settings, screen, stats, sb, rocket_ship, aliens, bullets, play_button, empty_ship)
def run_game(): """Initialize game, settings and create a screen object""" pygame.init() sounds = Sounds() ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption("Alien Invasion") # Make the play button. play_button = Button(ai_settings, screen, 'Play') # Make a ship, a group of aliens and a group of bullets ship = Ship(ai_settings, screen) aliens = Group() bullets = Group() # Create a fleet of aliens gf.create_fleet(ai_settings, screen, ship, aliens) # Create an instance to store the game statistics and scoreboard stats = GameStats(ai_settings) sb = Scoreboard(ai_settings, screen, stats) # Start the main loop for the game. while True: gf.check_events(ai_settings, screen, stats, sb, play_button, ship, sounds, aliens, bullets) if stats.game_active: ship.update() gf.update_bullets(ai_settings, screen, stats, sb, ship, sounds, aliens, bullets) gf.update_aliens(ai_settings, stats, sb, screen, ship, aliens, bullets) gf.update_screen(ai_settings, screen, sb, ship, aliens, bullets) else: gf.display_menu_screen(ai_settings, screen, play_button)
def scoreboard(): board = Scoreboard() if "LeeroyJenkins" in board._board: board._board["LeeroyJenkins"] = 0 return board
def run_game(): # 初始化背景设置 pygame.init() # 创建游戏设置类的实例 ai_settings = Settings() # 创建屏幕 screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) # 创建surface对象 pygame.display.set_caption("Alien Invasion") # 创建Play按钮 play_button = Button(screen, "Play") # 创建一艘飞船 ship = Ship(ai_settings, screen) # 创建一个用于存储子弹的编组 bullets = Group() # 创建一组外星人 aliens = Group() gf.create_fleet(ai_settings, screen, aliens) # 创建游戏角色 character = Character(screen) # 创建一个用于存储游戏统计信息的实例 stats = GameStats(ai_settings) # 创建记分牌 sb = Scoreboard(ai_settings, screen, stats) # 开始游戏的主循环 while True: # 监听事件 gf.check_events(ai_settings=ai_settings, screen=screen, stats=stats, play_button=play_button, sb=sb, ship=ship, aliens=aliens, bullets=bullets) if stats.game_active: # 根据标志修改飞船位置 ship.update() # 更新子弹 gf.update_bullets(ai_settings, screen, stats, sb, aliens, bullets) # 更新外星人 gf.update_aliens(ai_settings, stats, screen, sb, ship, aliens, bullets) # 更新屏幕 gf.update_screen(ai_settings=ai_settings, screen=screen, stats=stats, ship=ship, character=character, aliens=aliens, bullets=bullets, play_button=play_button, sb=sb)