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(): # 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() 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 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(): pygame.mixer.pre_init(44100, -16, 2, 4096) pygame.init() screen = pygame.display.set_mode((448, 224)) pygame.display.set_caption("Super Mario Bros") # sound = Sound() mario = Mario(screen) enemies = Group() enemies.add(Goomba(y=c.GROUND_HEIGHT, x=150, direction=c.LEFT)) powerups = Group() enemies.add(Goomba(y=c.GROUND_HEIGHT, x=150, direction=c.LEFT)) enemies.add(Koopa(y=c.GROUND_HEIGHT, x=170)) enemies.add(Goomba(y=c.GROUND_HEIGHT, x=190, direction=c.LEFT)) enemies.add(Goomba(y=c.GROUND_HEIGHT, x=210, direction=c.LEFT)) enemies.add(Goomba(y=c.GROUND_HEIGHT, x=240, direction=c.LEFT)) enemies.add(Koopa(y=280, x=200, winged=True)) enemies.add(PirahnaPlant(y=280, x=100)) enemies.add(CheepCheep(y=50, x=100)) enemies.add(Podaboo(x=200)) enemies.add(FireBar(x=150, y=150)) enemies.add(Blooper(x=200, y=100, mario=mario)) enemies.add(FireBar(x=150, y=150)) enemies.add(FireBar(x=150, y=150, radius=20)) enemies.add(FireBar(x=150, y=150, radius=30)) enemies.add(FireBar(x=150, y=150, radius=40)) enemies.add(FireBar(x=150, y=150, radius=50)) enemies.add(FireBar(x=150, y=150, radius=60)) enemies.add(FireBar(x=150, y=150, radius=70)) enemies.add(Blooper(x=200, y=100, mario=mario)) powerups.add(Mushroom(x=20)) powerups.add(Flower(x=250)) powerups.add(Life(x=50)) powerups.add(Star(x=10)) background = Background(screen) background.setTiles([[ "images/1-1/tile000.png", "images/1-1/tile001.png", "images/1-1/tile002.png", "images/1-1/tile003.png", "images/1-1/tile004.png", "images/1-1/tile005.png", "images/1-1/tile006.png", "images/1-1/tile007.png" ]]) background.scroll(0, 0) while True: gf.check_events(mario) mario.update() gf.update_enemies(mario, enemies, powerups) gf.draw_screen(screen, mario, enemies, powerups, background)
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(): #main game function pygame.init() running = -1 screen = pygame.display.set_mode((800, 800)) pygame.display.set_caption("Pong!") screen.fill((255, 255, 255)) paddle_one = Paddle(screen, 50, 300) paddle_two = Paddle(screen, 750, 300) score_one = 0 score_two = 0 bb = Ball(screen) while running == -1: screen.fill((255, 255, 255)) pygame.font.init() myfont = pygame.font.SysFont('Comic Sans MS', 70) secondfont = pygame.font.SysFont('Comic Sans MS', 50) titlesurface = myfont.render('Welcome to Pong', False, (0, 0, 0)) screen.blit(titlesurface, (200, 200)) Infosurface = secondfont.render( 'Use Up/Down and W/S to move the paddle', False, (0, 0, 0)) Infoosurface = secondfont.render('Don\'t let your opponent score! ', False, (0, 0, 0)) screen.blit(Infosurface, (100, 400)) screen.blit(Infoosurface, (200, 500)) clicksurface = secondfont.render('click any key to continue', False, (0, 0, 0)) screen.blit(clicksurface, (200, 600)) pygame.display.update() for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: running += 1 while running == 0: screen.fill((255, 255, 255)) scorefont = pygame.font.SysFont('Comic Sans MS', 50) score_one_screen = scorefont.render(str(score_one), False, (0, 0, 0)) screen.blit(score_one_screen, (100, 50)) score_two_screen = scorefont.render(str(score_two), False, (0, 0, 0)) screen.blit(score_two_screen, (700, 50)) if gamefunctions.check_lose(bb) == 1: score_two += 1 bb.reset_ball() elif gamefunctions.check_lose(bb) == 2: score_one += 1 bb.reset_ball() if gamefunctions.check_win(score_one, score_two) == 1: running += 1 elif gamefunctions.check_win(score_one, score_two) == 2: running += 2 gamefunctions.check_events(paddle_one, paddle_two) bb.collided_left(paddle_one) bb.collided_right(paddle_two) paddle_one.update() paddle_one.move_paddle() paddle_two.update() paddle_two.move_paddle() bb.update() bb.draw_ball() pygame.display.update() while running == 1: screen.fill((255, 255, 255)) winfont = pygame.font.SysFont('Comic Sans MS', 100) win_screen = scorefont.render("Player One Wins!", False, (0, 0, 0)) screen.blit(win_screen, (300, 300)) pygame.display.update() clicksurface = secondfont.render('click any key to play again', False, (0, 0, 0)) screen.blit(clicksurface, (200, 600)) pygame.display.update() for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() while running == 2: screen.fill((255, 255, 255)) winfont = pygame.font.SysFont('Comic Sans MS', 100) win_screen = scorefont.render("Player Two Wins!", False, (0, 0, 0)) screen.blit(win_screen, (300, 300)) pygame.display.update() clicksurface = secondfont.render('click any key to play again', False, (0, 0, 0)) screen.blit(clicksurface, (200, 600)) pygame.display.update() for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit()