def run_game(): pygame.init() ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption("Alien Invasion") play_button = Button(ai_settings, screen, "Let's Go") stats = GameStats(ai_settings) sb = Scoreboard(ai_settings, screen, stats) bg_color = (ai_settings.bg_color) ship = Ship(ai_settings, screen) bullets = Group() aliens = Group() gf.create_fleet(ai_settings, screen, ship, aliens) #background = pygame.image.load("bg.bmp") while True: gf.check_events(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets) if stats.game_active: ship.update() gf.update_bullets(ai_settings, screen, stats, sb, ship, aliens, bullets) gf.update_aliens(ai_settings, stats, screen, sb, ship, aliens, bullets) gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, play_button)
def run_game(): pygame.init() pygame.mixer.init() pygame.mixer.pre_init(44100, 16, 2, 4096) # frequency, size, channels, buffersize pygame.mixer.init() ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screen_height, ai_settings.screen_width)) pygame.display.set_caption("ALIEN INVASION") play_button = Button(ai_settings, screen, "Play") stats = GameStats(ai_settings) sb = Scoreboard(ai_settings, screen, stats) arrow = Arrow(ai_settings, screen) bg_color = (100, 230, 200) num_of_arrows = Group() num_of_balloons = Group() gf.create_fleet(ai_settings, screen, arrow, num_of_balloons) pygame.mixer.music.load('sounds/bgmmusic.wav') pygame.mixer.music.play(-1) while True: gf.check_events(ai_settings, screen, stats, sb, play_button, arrow, num_of_arrows, num_of_balloons) if stats.game_active: arrow.update() num_of_arrows.update() gf.update_each_arrows(ai_settings, screen, stats, sb, play_button, arrow, num_of_balloons, num_of_arrows) gf.update_balloons(ai_settings, stats, sb, screen, arrow, num_of_balloons, num_of_arrows) gf.update_screen(ai_settings, screen, stats, sb, arrow, num_of_arrows, num_of_balloons, play_button)
def game(): pygame.init() #Initialize Pygame pygame.mixer.pre_init(44100, -16, 2, 2048) # Reduce Lagging for the Music pygame.mixer.init() #Initialize Mixer for Background Music pygame.mixer.music.load('bgm.wav') #Load the BGM File pygame.mixer.music.play(-1) #Play the BGM Infinitely screen=pygame.display.set_mode((500,650)) #Set the Pygame Window pygame.display.set_caption("STARHEAD EXTERMINATOR") #Set the Window Caption #Call All the Classes txt = "DONT SHOOT THE GREEN ONE" button = Button(screen,txt) stats = Stats() gun = Gun(screen) enemy = Enemy(screen,stats) host = Hostage(screen,stats) score = Score(screen, stats) enemies = Group() hostage = Group() #Start the Game Loop while True: gf.firstscreen(screen,button,stats,gun,enemies,hostage,score) #First Display of the Game if stats.game_active: #Start when the Player Click the Button gf.gametrue(screen,stats,gun,enemies,hostage,score) #Update and Behaviour of Objects in the Game gf.update_screen(screen,gun,enemies,hostage,button,stats) #Update the Screen and Flip
def run_game(): pygame.init() ai_settings=settings.Settings() screen = pygame.display.set_mode( (ai_settings.screen_width,ai_settings.screen_height)) pygame.display.set_caption("外星人入侵") play_button=Button(ai_settings,screen,'Play') ship=Ship(screen,ai_settings) bullets=Group() aliens=Group() gf.create_fleet(ai_settings,screen,ship,aliens) stats=GameStats(ai_settings) sb=Scoreboard(ai_settings,screen,stats) while True: gf.check_events(ai_settings,screen,stats,play_button,ship,sb,aliens,bullets) if stats.game_active: ship.update() gf.update_bullets(ai_settings,screen,stats,sb,ship,aliens,bullets) gf.update_aliens(ai_settings,stats,screen,ship,sb,aliens,bullets) gf.update_screen(ai_settings,screen,sb, ship,bullets,aliens,stats,play_button)
def run_game(): # Initialize game and create a screen object. pygame.init() ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption("Alien Invasion") play_button = Button(ai_settings, screen, "Play") stats = GameStats(ai_settings) sb = Scoreboard(ai_settings, screen, stats) ship = Ship(ai_settings, screen) bullets = Group() aliens = Group() alien = Alien(ai_settings, screen) gf.create_fleet(ai_settings, screen, ship, aliens) # Start the main loop for the game. while True: gf.check_events(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets) if stats.game_active: ship.update() gf.update_bullets(ai_settings, screen, stats, sb, ship, aliens, bullets) gf.update_aliens(ai_settings, screen, stats, sb, ship, aliens, bullets) gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, play_button) for bullet in bullets.copy(): if bullet.rect.bottom <= 0: bullets.remove(bullet)
def run_game(): pygame.init() ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption("Alien Invasion") ship = Ship(ai_settings, screen) bullets = Group() while True: gf.check_events(ai_settings,screen,ship,bullets) ship.update() bullets.update() gf.update_screen(ai_settings, screen, ship, bullets)
def run_game(): # Initialize game and create a screen object. pygame.init() game_set = Settings() #Making an instance of settings class screen = pygame.display.set_mode((game_set.screen_width, game_set.screen_height)) # Here screen has been created pygame.display.set_caption("Alien Ship Fight Game") ship_set = Ship(game_set, screen) #Making an instance of Ship class #Creating an instance to store the game_statistics game_stats = GameStatistics(game_set) # # Create an instance to store game statistics and create a scoreboard. sb = ScoreBoard(game_set,screen,game_stats) #Make a group to store bullet bullets = Group() play_button = Button(game_set,screen, "Play") ''' Creating Rows of Aliens To create a row, first create an empty group called aliens in alien_invasion.py to hold all of our aliens, and then call a function in game_functions.py to create a fleet. ''' aliens = Group() # Create the fleet of aliens. gf.create_fleet(game_set,screen,ship_set,aliens) # Set the background color #bg_color = (246, 221, 204) # Start the main loop for the game. while True: # Watch for keyboard and mouse events. gf.check_events(game_set,screen, ship_set, bullets, play_button, game_stats, aliens, sb) if game_stats.game_active: ship_set.update() gf.update_bullets(bullets, aliens, game_set,screen,ship_set, game_stats, sb ) #In the main while loop we will update the position of each alien as well gf.update_aliens(game_set,aliens , ship_set, game_stats, bullets, screen, sb) gf.update_screen(game_set, screen, ship_set, aliens, bullets, game_stats, play_button, sb)
def run_game(): pygame.init() game_settings = Settings() message = input("Start Game:") screen = pygame.display.set_mode(game_settings.screen_size) pygame.display.set_caption("Monster Attack") #hero = Hero(screen) #bullets = Group() play_button = Play_button(screen, message) while 1: gf.check_events(hero, bullets, game_settings, screen, play_button) gf.update_screen(game_settings, screen, hero, bullets, play_button) if game_settings.game_active: hero.update() bullets.update() for bullet in bullets: if bullet.rect.bottom <= 0: bullets.remove(bullet) if len(bullets) > 15: bullets.remove(bullet)
def run_game(): pygame.init() ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption("Alien Invasion") play_button = Button(ai_settings, screen, "Play") stats = Gamestats(ai_settings) ship = Ship(ai_settings, screen) alien = Alien(ai_settings, screen) bullets = Group() aliens = Group() gf.create_fleet(ai_settings, screen, ship, aliens) while True: gf.check_events(ai_settings, screen, stats, play_button, ship, bullets) if stats.game_active: ship.update() gf.update_bullets(ai_settings, screen, ship, aliens, bullets) gf.update_aliens(ai_settings, stats, screen, ship, aliens, bullets) gf.update_screen(ai_settings, screen, stats, ship, aliens, bullets, play_button) bullets.update()
def run_game(): pygame.init() pygame.mixer.init() normalsiren = pygame.mixer.music.load("sound/normalsiren.mp3") runningsiren = pygame.mixer.music.load("sound/runningsiren.mp3") normalsirenplaying = False runningsirenplaying = False #load the settings ai_settings = Settings() # Set up the screen screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption("Pacman Portal") screen_rect = screen.get_rect() # Timer for ghosts to run away timer = pygame.time.Clock() timer_started = True clock_ticker = 0 menuRect = pygame.Rect(0, 0, ai_settings.screen_width, ai_settings.screen_height) # Show buttons play_button = Button(ai_settings, screen, "Play", (600, 400)) high_scores_button = Button(ai_settings, screen, "High Scores", (screen_rect.centerx, 460)) back_button = Button(ai_settings, screen, "Back", (screen_rect.centerx, 700)) # Open the high scores list high_scoresfile = open("high_scores.txt", "r") high_scores = HighScores(screen) # Set up the maze maze = [["X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X"], ["X", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", "X", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", "X"], ["X", ".", "X", "X", "X", "X", ".", "X", "X", "X", "X", "X", ".", "X", ".", "X", "X", "X", "X", "X", ".", "X", "X", "X", "X", ".", "X"], ["X", "o", "X", "X", "X", "X", ".", "X", "X", "X", "X", "X", ".", "X", ".", "X", "X", "X", "X", "X", ".", "X", "X", "X", "X", "o", "X"], ["X", ".", "X", "X", "X", "X", ".", "X", "X", "X", "X", "X", ".", "X", ".", "X", "X", "X", "X", "X", ".", "X", "X", "X", "X", ".", "X"], ["X", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", "X"], ["X", ".", "X", "X", "X", "X", ".", "X", "X", ".", "X", "X", "X", "X", "X", "X", "X", ".", "X", "X", ".", "X", "X", "X", "X", ".", "X"], ["X", ".", "X", "X", "X", "X", ".", "X", "X", ".", "X", "X", "X", "X", "X", "X", "X", ".", "X", "X", ".", "X", "X", "X", "X", ".", "X"], ["X", ".", ".", ".", ".", ".", ".", "X", "X", ".", ".", ".", ".", "X", ".", ".", ".", ".", "X", "X", ".", ".", ".", ".", ".", ".", "X"], ["X", "X", "X", "X", "X", "X", ".", "X", "X", "X", "X", "X", "_", "X", "_", "X", "X", "X", "X", "X", ".", "X", "X", "X", "X", "X", "X"], ["X", "X", "X", "X", "X", "X", ".", "X", "_", "_", "_", "_", "_", "B", "_", "_", "_", "_", "_", "X", ".", "X", "X", "X", "X", "X", "X"], ["X", "X", "X", "X", "X", "X", ".", "X", "_", "X", "X", "X", "X", "_", "X", "X", "X", "X", "_", "X", ".", "X", "X", "X", "X", "X", "X"], ["X", "X", "X", "X", "X", "X", ".", "X", "_", "X", "_", "_", "_", "_", "_", "_", "_", "X", "_", "X", ".", "X", "X", "X", "X", "X", "X"], ["X", "_", "_", "_", "_", "_", ".", "_", "_", "X", "_", "I", "_", "_", "_", "P", "_", "X", "_", "_", ".", "_", "_", "_", "_", "_", "X"], ["X", "X", "X", "X", "X", "X", ".", "X", "_", "X", "_", "_", "_", "C", "_", "_", "_", "X", "_", "X", ".", "X", "X", "X", "X", "X", "X"], ["X", "X", "X", "X", "X", "X", ".", "X", "_", "X", "X", "X", "X", "X", "X", "X", "X", "X", "_", "X", ".", "X", "X", "X", "X", "X", "X"], ["X", "X", "X", "X", "X", "X", ".", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", ".", "X", "X", "X", "X", "X", "X"], ["X", "X", "X", "X", "X", "X", ".", "X", "_", "X", "X", "X", "X", "X", "X", "X", "X", "X", "_", "X", ".", "X", "X", "X", "X", "X", "X"], ["X", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", "X", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", "X"], ["X", ".", "X", "X", "X", "X", ".", "X", "X", "X", "X", "X", ".", "X", ".", "X", "X", "X", "X", "X", ".", "X", "X", "X", "X", ".", "X"], ["X", "o", ".", ".", "X", "X", ".", "X", "X", "X", "X", "X", ".", "X", ".", "X", "X", "X", "X", "X", ".", "X", "X", ".", ".", "o", "X"], ["X", "X", "X", ".", "X", "X", ".", ".", ".", ".", ".", ".", ".", "M", ".", ".", ".", ".", ".", ".", ".", "X", "X", ".", "X", "X", "X"], ["X", "X", "X", ".", "X", "X", ".", "X", ".", "X", "X", "X", "X", "X", "X", "X", "X", "X", ".", "X", ".", "X", "X", ".", "X", "X", "X"], ["X", ".", ".", ".", ".", ".", ".", "X", ".", ".", ".", ".", ".", "X", ".", ".", ".", ".", ".", "X", ".", ".", ".", ".", ".", ".", "X"], ["X", ".", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", ".", "X", ".", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", ".", "X"], ["X", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", "X"], ["X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X"], ] # Set up the stats stats = GameStats(ai_settings) sb = Scoreboard(ai_settings = ai_settings, screen = screen, stats = stats) walls = Group() points = Group() pills = Group() ghosts = Group() redportalbullets = Group() blueportalbullets = Group() redportals = Group() blueportals = Group() deathanimations = Group() fruits = Group() # Initialize menu objects menupacman = MenuPacMan(ai_settings=ai_settings, screen=screen) menupacman.reset_position() menupowerpill = powerPill(screen = screen, pos_x = 32, pos_y = 20) menuinky = MenuInky(ai_settings = ai_settings, screen = screen) menublinky = MenuBlinky(ai_settings = ai_settings, screen = screen) menupinky = MenuPinky(ai_settings = ai_settings, screen = screen) menuclyde = MenuClyde(ai_settings = ai_settings, screen = screen) # Initialize Pacman player = PacMan(ai_settings = ai_settings, screen = screen) player.reset_position() # Initialize Ghosts blinky = Blinky(ai_settings=ai_settings, screen=screen, maze=maze, player=player, ghosts = ghosts) blinky.reset_position() inky = Inky(ai_settings=ai_settings, screen=screen, maze=maze, player=player, ghosts=ghosts) inky.reset_position() pinky = Pinky(ai_settings=ai_settings, screen=screen, maze=maze, player=player, ghosts=ghosts) inky.reset_position() clyde = Clyde(ai_settings=ai_settings, screen=screen, maze=maze, player=player, ghosts=ghosts) clyde.reset_position() # Initialize Portals redportal = RedPortal(ai_settings = ai_settings, screen = screen) blueportal = BluePortal(ai_settings=ai_settings, screen=screen) # Initialize the game # gf.initialize_game(ai_settings = ai_settings, screen = screen, walls = walls, points = points, pills = pills, stats = stats, sb = sb, player = player, maze = maze, blinky = blinky, inky = inky, pinky = pinky, clyde = clyde, redportal = redportal, blueportal = blueportal) # Game Loop while(True): gf.check_events(ai_settings = ai_settings, screen = screen, stats = stats, sb = sb, player = player, blinky = blinky, inky = inky, pinky = pinky, clyde = clyde, maze = maze, walls = walls, points = points, pills = pills, redportalbullets = redportalbullets, blueportalbullets = blueportalbullets, redportal = redportal, blueportal = blueportal, play_button = play_button, back_button = back_button, high_scores_button = high_scores_button) gf.update_screen(ai_settings = ai_settings, screen = screen, player = player, walls = walls , points = points, pills = pills, fruits = fruits, stats = stats, sb = sb, maze = maze, blinky = blinky, inky = inky, pinky = pinky, clyde = clyde, ghosts = ghosts, redportalbullets = redportalbullets, blueportalbullets = blueportalbullets, redportal = redportal, blueportal = blueportal, deathanimations = deathanimations, play_button = play_button, back_button = back_button, high_scores_button = high_scores_button, menupacman = menupacman, menupowerpill = menupowerpill, menuinky = menuinky, menublinky = menublinky, menupinky = menupinky, menuclyde = menuclyde, timer = timer, timer_started = timer_started, clock_ticker = clock_ticker, normalsiren = normalsiren, runningsiren = runningsiren)
import pygame import gamefunctions as gf from time import sleep from settings import Settings from cells import Cell #--------------------------------------------------------------------------------------------------- cw_settings = Settings() pygame.init() #Creation of screen and stuff. screen = gf.set_screen(cw_settings) cells = Cell(cw_settings) if __name__ == '__main__': '''Initialize the game''' while True: '''Loop for the game action.''' gf.check_events(cw_settings, cells) gf.cover_screen(cw_settings, screen) gf.update_screen(cw_settings, screen, cells) sleep(cw_settings.timeSleep)
def run_game(self): settings = Settings() screen = pygame.display.set_mode( (settings.screen_width, settings.screen_height)) scoreboard_1 = ScoreBoard(screen, settings, PlayerSide.LEFT) scoreboard_2 = ScoreBoard(screen, settings, PlayerSide.RIGHT) screen_rect = screen.get_rect() player_one = Player(settings, screen, PlayerSide.LEFT) player_two = Player(settings, screen, PlayerSide.RIGHT) ball = Ball(screen, settings, PlayerSide.LEFT, (screen_rect.centerx, screen_rect.centery), player_one, player_two) box_top = screen_rect.centerx - 25, screen_rect.top + 150, 50, 50 box_mid = screen_rect.centerx - 25, screen_rect.centery - 25, 50, 50 box_down = screen_rect.centerx - 25, screen_rect.top + 500, 50, 50 box_1 = SupriseBox(screen, box_top, (50, 50, 50)) box_2 = SupriseBox(screen, box_mid, (100, 0, 0)) box_3 = SupriseBox(screen, box_down, (0, 0, 100)) while self.run: if self.state: player_one.update_player(settings, PlayerSide.LEFT, game) player_two.update_player(settings, PlayerSide.RIGHT, game) gf.check_event(player_one, player_two, ball, game, scoreboard_1, scoreboard_2, box_1, box_2, box_3) play_button = Button(screen, ResetQuit.RESTART) play_button.prep_msg('Left Player: [SPACE]') restart_button = Button(screen, ResetQuit.RESTART) restart_button.prep_msg('Press ' R' for Restart') quit_button = Button(screen, ResetQuit.QUIT) quit_button.prep_msg("Press 'Q' to Quit") scoreboard_1.score(PlayerSide.LEFT) scoreboard_2.score(PlayerSide.RIGHT) ball.ball_position(GameState.START, player_one, player_two, self.servis, settings, game) gf.update_start_screen(screen, settings, player_one, player_two, ball, play_button, restart_button, quit_button, game, scoreboard_1, scoreboard_2) else: gf.check_event(player_one, player_two, ball, game, scoreboard_1, scoreboard_2, box_1, box_2, box_3) gf.check_score(ball, screen, game, scoreboard_1, scoreboard_2, settings, player_one, player_two) gf.box_timer(box_1, box_2, box_3) player_one.update_player(settings, PlayerSide.LEFT, game) player_two.update_player(settings, PlayerSide.RIGHT, game) player_one.update_player_height(settings, PlayerSide.LEFT) player_two.update_player_height(settings, PlayerSide.RIGHT) gf.check_collision_player(ball, PlayerSide.RIGHT, player_two, player_one, settings) gf.check_collision_player(ball, PlayerSide.LEFT, player_two, player_one, settings) gf.check_collision_down_up(ball, screen) gf.check_suprise(settings, ball, box_1, box_2, box_3) scoreboard_1.score(PlayerSide.LEFT) scoreboard_2.score(PlayerSide.RIGHT) ball.ball_position(GameState.INGAME, player_one, player_two, self.servis, settings, game) gf.update_screen(screen, settings, player_one, player_two, ball, scoreboard_1, scoreboard_2, game, box_1, box_2, box_3)