<s> from __future__ import division <EOL> import pygame <EOL> import random <EOL> from os import path <EOL> img_dir = path . join ( path . dirname ( __file__ ) , '<STR_LIT>' ) <EOL> sound_folder = path . join ( path . dirname ( __file__ ) , '<STR_LIT>' ) <EOL> WIDTH = <NUM_LIT> <EOL> HEIGHT = <NUM_LIT> <EOL> FPS = <NUM_LIT> <EOL> POWERUP_TIME = <NUM_LIT> <EOL> BAR_LENGTH = <NUM_LIT:100> <EOL> BAR_HEIGHT = <NUM_LIT:10> <EOL> WHITE = ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> ) <EOL> BLACK = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> RED = ( <NUM_LIT:255> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> GREEN = ( <NUM_LIT:0> , <NUM_LIT:255> , <NUM_LIT:0> ) <EOL> BLUE = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:255> ) <EOL> YELLOW = ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:0> ) <EOL> pygame . init ( ) <EOL> pygame . mixer . init ( ) <EOL> screen = pygame . display . set_mode ( ( WIDTH , HEIGHT ) ) <EOL> pygame . display . set_caption ( "<STR_LIT>" ) <EOL> clock = pygame . time . Clock ( ) <EOL> font_name = pygame . font . match_font ( '<STR_LIT>' ) <EOL> def main_menu ( ) : <EOL> global screen <EOL> menu_song = pygame . mixer . music . load ( path . join ( sound_folder , "<STR_LIT>" ) ) <EOL> pygame . mixer . music . play ( - <NUM_LIT:1> ) <EOL> title = pygame . image . load ( path . join ( img_dir , "<STR_LIT>" ) ) . convert ( ) <EOL> title = pygame . transform . scale ( title , ( WIDTH , HEIGHT ) , screen ) <EOL> screen . blit ( title , ( <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> pygame . display . update ( ) <EOL> while True : <EOL> ev = pygame . event . poll ( ) <EOL> if ev . type == pygame . KEYDOWN : <EOL> if ev . key == pygame . K_RETURN : <EOL> break <EOL> elif ev . key == pygame . K_q : <EOL> pygame . quit ( ) <EOL> quit ( ) <EOL> else : <EOL> draw_text ( screen , "<STR_LIT>" , <NUM_LIT:30> , WIDTH / <NUM_LIT:2> , HEIGHT / <NUM_LIT:2> ) <EOL> draw_text ( screen , "<STR_LIT>" , <NUM_LIT:30> , WIDTH / <NUM_LIT:2> , ( HEIGHT / <NUM_LIT:2> ) + <NUM_LIT> ) <EOL> pygame . display . update ( ) <EOL> ready = pygame . mixer . Sound ( path . join ( sound_folder , '<STR_LIT>' ) ) <EOL> ready . play ( ) <EOL> screen . fill ( BLACK ) <EOL> draw_text ( screen , "<STR_LIT>" , <NUM_LIT> , WIDTH / <NUM_LIT:2> , HEIGHT / <NUM_LIT:2> ) <EOL> pygame . display . update ( ) <EOL> def draw_text ( surf , text , size , x , y ) : <EOL> font = pygame . font . Font ( font_name , size ) <EOL> text_surface = font . render ( text , True , WHITE ) <EOL> text_rect = text_surface . get_rect ( ) <EOL> text_rect . midtop = ( x , y ) <EOL> surf . blit ( text_surface , text_rect ) <EOL> def draw_shield_bar ( surf , x , y , pct ) : <EOL> pct = max ( pct , <NUM_LIT:0> ) <EOL> fill = ( pct / <NUM_LIT:100> ) * BAR_LENGTH <EOL> outline_rect = pygame . Rect ( x , y , BAR_LENGTH , BAR_HEIGHT ) <EOL> fill_rect = pygame . Rect ( x , y , fill , BAR_HEIGHT ) <EOL> pygame . draw . rect ( surf , GREEN , fill_rect ) <EOL> pygame . draw . rect ( surf , WHITE , outline_rect , <NUM_LIT:2> ) <EOL> def draw_lives ( surf , x , y , lives , img ) : <EOL> for i in range ( lives ) : <EOL> img_rect = img . get_rect ( ) <EOL> img_rect . x = x + <NUM_LIT:30> * i <EOL> img_rect . y = y <EOL> surf . blit ( img , img_rect ) <EOL> def newmob ( ) : <EOL> mob_element = Mob ( ) <EOL> all_sprites . add ( mob_element ) <EOL> mobs . add ( mob_element ) <EOL> class Explosion ( pygame . sprite . Sprite ) : <EOL> def __init__ ( self , center , size ) : <EOL> pygame . sprite . Sprite . __init__ ( self ) <EOL> self . size = size <EOL> self . image = explosion_anim [ self . size ] [ <NUM_LIT:0> ] <EOL> self . rect = self . image . get_rect ( ) <EOL> self . rect . center = center <EOL> self . frame = <NUM_LIT:0> <EOL> self . last_update = pygame . time . get_ticks ( ) <EOL> self . frame_rate = <NUM_LIT> <EOL> def update ( self ) : <EOL> now = pygame . time . get_ticks ( ) <EOL> if now - self . last_update > self . frame_rate : <EOL> self . last_update = now <EOL> self . frame += <NUM_LIT:1> <EOL> if self . frame == len ( explosion_anim [ self . size ] ) : <EOL> self . kill ( ) <EOL> else : <EOL> center = self . rect . center <EOL> self . image = explosion_anim [ self . size ] [ self . frame ] <EOL> self . rect = self . image . get_rect ( ) <EOL> self . rect . center = center <EOL> class Player ( pygame . sprite . Sprite ) : <EOL> def __init__ ( self ) : <EOL> pygame . sprite . Sprite . __init__ ( self ) <EOL> self . image = pygame . transform . scale ( player_img , ( <NUM_LIT:50> , <NUM_LIT> ) ) <EOL> self . image . set_colorkey ( BLACK ) <EOL> self . rect = self . image . get_rect ( ) <EOL> self . radius = <NUM_LIT:20> <EOL> self . rect . centerx = WIDTH / <NUM_LIT:2> <EOL> self . rect . bottom = HEIGHT - <NUM_LIT:10> <EOL> self . speedx = <NUM_LIT:0> <EOL> self . shield = <NUM_LIT:100> <EOL> self . shoot_delay = <NUM_LIT> <EOL> self . last_shot = pygame . time . get_ticks ( ) <EOL> self . lives = <NUM_LIT:3> <EOL> self . hidden = False <EOL> self . hide_timer = pygame . time . get_ticks ( ) <EOL> self . power = <NUM_LIT:1> <EOL> self . power_timer = pygame . time . get_ticks ( ) <EOL> def update ( self ) : <EOL> if self . power >= <NUM_LIT:2> and pygame . time . get_ticks ( ) - self . power_time > POWERUP_TIME : <EOL> self . power -= <NUM_LIT:1> <EOL> self . power_time = pygame . time . get_ticks ( ) <EOL> if self . hidden and pygame . time . get_ticks ( ) - self . hide_timer > <NUM_LIT:1000> : <EOL> self . hidden = False <EOL> self . rect . centerx = WIDTH / <NUM_LIT:2> <EOL> self . rect . bottom = HEIGHT - <NUM_LIT:30> <EOL> self . speedx = <NUM_LIT:0> <EOL> keystate = pygame . key . get_pressed ( ) <EOL> if keystate [ pygame . K_LEFT ] : <EOL> self . speedx = - <NUM_LIT:5> <EOL> elif keystate [ pygame . K_RIGHT ] : <EOL> self . speedx = <NUM_LIT:5> <EOL> if keystate [ pygame . K_SPACE ] : <EOL> self . shoot ( ) <EOL> if self . rect . right > WIDTH : <EOL> self . rect . right = WIDTH <EOL> if self . rect . left < <NUM_LIT:0> : <EOL> self . rect . left = <NUM_LIT:0> <EOL> self . rect . x += self . speedx <EOL> def shoot ( self ) : <EOL> now = pygame . time . get_ticks ( ) <EOL> if now - self . last_shot > self . shoot_delay : <EOL> self . last_shot = now <EOL> if self . power == <NUM_LIT:1> : <EOL> bullet = Bullet ( self . rect . centerx , self . rect . top ) <EOL> all_sprites . add ( bullet ) <EOL> bullets . add ( bullet ) <EOL> shooting_sound . play ( ) <EOL> if self . power == <NUM_LIT:2> : <EOL> bullet1 = Bullet ( self . rect . left , self . rect . centery ) <EOL> bullet2 = Bullet ( self . rect . right , self . rect . centery ) <EOL> all_sprites . add ( bullet1 ) <EOL> all_sprites . add ( bullet2 ) <EOL> bullets . add ( bullet1 ) <EOL> bullets . add ( bullet2 ) <EOL> shooting_sound . play ( ) <EOL> """<STR_LIT>""" <EOL> if self . power >= <NUM_LIT:3> : <EOL> bullet1 = Bullet ( self . rect . left , self . rect . centery ) <EOL> bullet2 = Bullet ( self . rect . right , self . rect . centery ) <EOL> missile1 = Missile ( self . rect . centerx , self . rect . top ) <EOL> all_sprites . add ( bullet1 ) <EOL> all_sprites . add ( bullet2 ) <EOL> all_sprites . add ( missile1 ) <EOL> bullets . add ( bullet1 ) <EOL> bullets . add ( bullet2 ) <EOL> bullets . add ( missile1 ) <EOL> shooting_sound . play ( ) <EOL> missile_sound . play ( ) <EOL> def powerup ( self ) : <EOL> self . power += <NUM_LIT:1> <EOL> self . power_time = pygame . time . get_ticks ( ) <EOL> def hide ( self ) : <EOL> self . hidden = True <EOL> self . hide_timer = pygame . time . get_ticks ( ) <EOL> self . rect . center = ( WIDTH / <NUM_LIT:2> , HEIGHT + <NUM_LIT:200> ) <EOL> class Mob ( pygame . sprite . Sprite ) : <EOL> def __init__ ( self ) : <EOL> pygame . sprite . Sprite . __init__ ( self ) <EOL> self . image_orig = random . choice ( meteor_images ) <EOL> self . image_orig . set_colorkey ( BLACK ) <EOL> self . image = self . image_orig . copy ( ) <EOL> self . rect = self . image . get_rect ( ) <EOL> self . radius = int ( self . rect . width * <NUM_LIT> / <NUM_LIT:2> ) <EOL> self . rect . x = random . randrange ( <NUM_LIT:0> , WIDTH - self . rect . width ) <EOL> self . rect . y = random . randrange ( - <NUM_LIT> , - <NUM_LIT:100> ) <EOL> self . speedy = random . randrange ( <NUM_LIT:5> , <NUM_LIT:20> ) <EOL> self . speedx = random . randrange ( - <NUM_LIT:3> , <NUM_LIT:3> ) <EOL> self . rotation = <NUM_LIT:0> <EOL> self . rotation_speed = random . randrange ( - <NUM_LIT:8> , <NUM_LIT:8> ) <EOL> self . last_update = pygame . time . get_ticks ( ) <EOL> def rotate ( self ) : <EOL> time_now = pygame . time . get_ticks ( ) <EOL> if time_now - self . last_update > <NUM_LIT:50> : <EOL> self . last_update = time_now <EOL> self . rotation = ( self . rotation + self . rotation_speed ) % <NUM_LIT> <EOL> new_image = pygame . transform . rotate ( self . image_orig , self . rotation ) <EOL> old_center = self . rect . center <EOL> self . image = new_image <EOL> self . rect = self . image . get_rect ( ) <EOL> self . rect . center = old_center <EOL> def update ( self ) : <EOL> self . rotate ( ) <EOL> self . rect . x += self . speedx <EOL> self . rect . y += self . speedy <EOL> if ( self . rect . top > HEIGHT + <NUM_LIT:10> ) or ( self . rect . left < - <NUM_LIT> ) or ( self . rect . right > WIDTH + <NUM_LIT:20> ) : <EOL> self . rect . x = random . randrange ( <NUM_LIT:0> , WIDTH - self . rect . width ) <EOL> self . rect . y = random . randrange ( - <NUM_LIT:100> , - <NUM_LIT> ) <EOL> self . speedy = random . randrange ( <NUM_LIT:1> , <NUM_LIT:8> ) <EOL> class Pow ( pygame . sprite . Sprite ) : <EOL> def __init__ ( self , center ) : <EOL> pygame . sprite . Sprite . __init__ ( self ) <EOL> self . type = random . choice ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . image = powerup_images [ self . type ] <EOL> self . image . set_colorkey ( BLACK ) <EOL> self . rect = self . image . get_rect ( ) <EOL> self . rect . center = center <EOL> self . speedy = <NUM_LIT:2> <EOL> def update ( self ) : <EOL> """<STR_LIT>""" <EOL> self . rect . y += self . speedy <EOL> if self . rect . top > HEIGHT : <EOL> self . kill ( ) <EOL> class Bullet ( pygame . sprite . Sprite ) : <EOL> def __init__ ( self , x , y ) : <EOL> pygame . sprite . Sprite . __init__ ( self ) <EOL> self . image = bullet_img <EOL> self . image . set_colorkey ( BLACK ) <EOL> self . rect = self . image . get_rect ( ) <EOL> self . rect . bottom = y <EOL> self . rect . centerx = x <EOL> self . speedy = - <NUM_LIT:10> <EOL> def update ( self ) : <EOL> """<STR_LIT>""" <EOL> self . rect . y += self . speedy <EOL> if self . rect . bottom < <NUM_LIT:0> : <EOL> self . kill ( ) <EOL> class Missile ( pygame . sprite . Sprite ) : <EOL> def __init__ ( self , x , y ) : <EOL> pygame . sprite . Sprite . __init__ ( self ) <EOL> self . image = missile_img <EOL> self . image . set_colorkey ( BLACK ) <EOL> self . rect = self . image . get_rect ( ) <EOL> self . rect . bottom = y <EOL> self . rect . centerx = x <EOL> self . speedy = - <NUM_LIT:10> <EOL> def update ( self ) : <EOL> """<STR_LIT>""" <EOL> self . rect . y += self . speedy <EOL> if self . rect . bottom < <NUM_LIT:0> : <EOL> self . kill ( ) <EOL> background = pygame . image . load ( path . join ( img_dir , '<STR_LIT>' ) ) . convert ( ) <EOL> background_rect = background . get_rect ( ) <EOL> player_img = pygame . image . load ( path . join ( img_dir , '<STR_LIT>' ) ) . convert ( ) <EOL> player_mini_img = pygame . transform . scale ( player_img , ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> player_mini_img . set_colorkey ( BLACK ) <EOL> bullet_img = pygame . image . load ( path . join ( img_dir , '<STR_LIT>' ) ) . convert ( ) <EOL> missile_img = pygame . image . load ( path . join ( img_dir , '<STR_LIT>' ) ) . convert_alpha ( ) <EOL> meteor_images = [ ] <EOL> meteor_list = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> for image in meteor_list : <EOL> meteor_images . append ( pygame . image . load ( path . join ( img_dir , image ) ) . convert ( ) ) <EOL> explosion_anim = { } <EOL> explosion_anim [ '<STR_LIT>' ] = [ ] <EOL> explosion_anim [ '<STR_LIT>' ] = [ ] <EOL> explosion_anim [ '<STR_LIT>' ] = [ ] <EOL> for i in range ( <NUM_LIT:9> ) : <EOL> filename = '<STR_LIT>' . format ( i ) <EOL> img = pygame . image . load ( path . join ( img_dir , filename ) ) . convert ( ) <EOL> img . set_colorkey ( BLACK ) <EOL> img_lg = pygame . transform . scale ( img , ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> explosion_anim [ '<STR_LIT>' ] . append ( img_lg ) <EOL> img_sm = pygame . transform . scale ( img , ( <NUM_LIT:32> , <NUM_LIT:32> ) ) <EOL> explosion_anim [ '<STR_LIT>' ] . append ( img_sm ) <EOL> filename = '<STR_LIT>' . format ( i ) <EOL> img = pygame . image . load ( path . join ( img_dir , filename ) ) . convert ( ) <EOL> img . set_colorkey ( BLACK ) <EOL> explosion_anim [ '<STR_LIT>' ] . append ( img ) <EOL> powerup_images = { } <EOL> powerup_images [ '<STR_LIT>' ] = pygame . image . load ( path . join ( img_dir , '<STR_LIT>' ) ) . convert ( ) <EOL> powerup_images [ '<STR_LIT>' ] = pygame . image . load ( path . join ( img_dir , '<STR_LIT>' ) ) . convert ( ) <EOL> shooting_sound = pygame . mixer . Sound ( path . join ( sound_folder , '<STR_LIT>' ) ) <EOL> missile_sound = pygame . mixer . Sound ( path . join ( sound_folder , '<STR_LIT>' ) ) <EOL> expl_sounds = [ ] <EOL> for sound in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> expl_sounds . append ( pygame . mixer . Sound ( path . join ( sound_folder , sound ) ) ) <EOL> pygame . mixer . music . set_volume ( <NUM_LIT> ) <EOL> player_die_sound = pygame . mixer . Sound ( path . join ( sound_folder , '<STR_LIT>' ) ) <EOL> all_sprites = pygame . sprite . Group ( ) <EOL> player = Player ( ) <EOL> all_sprites . add ( player ) <EOL> mobs = pygame . sprite . Group ( ) <EOL> for i in range ( <NUM_LIT:8> ) : <EOL> newmob ( ) <EOL> bullets = pygame . sprite . Group ( ) <EOL> powerups = pygame . sprite . Group ( ) <EOL> score = <NUM_LIT:0> <EOL> running = True <EOL> menu_display = True <EOL> while running : <EOL> if menu_display : <EOL> main_menu ( ) <EOL> pygame . time . wait ( <NUM_LIT> ) <EOL> pygame . mixer . music . stop ( ) <EOL> pygame . mixer . music . load ( path . join ( sound_folder , '<STR_LIT>' ) ) <EOL> pygame . mixer . music . play ( - <NUM_LIT:1> ) <EOL> menu_display = False <EOL> clock . tick ( FPS ) <EOL> for event in pygame . event . get ( ) : <EOL> if event . type == pygame . QUIT : <EOL> running = False <EOL> if event . type == pygame . KEYDOWN : <EOL> if event . key == pygame . K_ESCAPE : <EOL> running = False <EOL> all_sprites . update ( ) <EOL> hits = pygame . sprite . groupcollide ( mobs , bullets , True , True ) <EOL> for hit in hits : <EOL> score += <NUM_LIT:50> - hit . radius <EOL> random . choice ( expl_sounds ) . play ( ) <EOL> expl = Explosion ( hit . rect . center , '<STR_LIT>' ) <EOL> all_sprites . add ( expl ) <EOL> if random . random ( ) > <NUM_LIT> : <EOL> pow = Pow ( hit . rect . center ) <EOL> all_sprites . add ( pow ) <EOL> powerups . add ( pow ) <EOL> newmob ( ) <EOL> hits = pygame . sprite . spritecollide ( player , mobs , True , pygame . sprite . collide_circle ) <EOL> for hit in hits : <EOL> player . shield -= hit . radius * <NUM_LIT:2> <EOL> expl = Explosion ( hit . rect . center , '<STR_LIT>' ) <EOL> all_sprites . add ( expl ) <EOL> newmob ( ) <EOL> if player . shield <= <NUM_LIT:0> : <EOL> player_die_sound . play ( ) <EOL> death_explosion = Explosion ( player . rect . center , '<STR_LIT>' ) <EOL> all_sprites . add ( death_explosion ) <EOL> player . hide ( ) <EOL> player . lives -= <NUM_LIT:1> <EOL> player . shield = <NUM_LIT:100> <EOL> hits = pygame . sprite . spritecollide ( player , powerups , True ) <EOL> for hit in hits : <EOL> if hit . type == '<STR_LIT>' : <EOL> player . shield += random . randrange ( <NUM_LIT:10> , <NUM_LIT:30> ) <EOL> if player . shield >= <NUM_LIT:100> : <EOL> player . shield = <NUM_LIT:100> <EOL> if hit . type == '<STR_LIT>' : <EOL> player . powerup ( ) <EOL> if player . lives == <NUM_LIT:0> and not death_explosion . alive ( ) : <EOL> running = False <EOL> screen . fill ( BLACK ) <EOL> screen . blit ( background , background_rect ) <EOL> all_sprites . draw ( screen ) <EOL> draw_text ( screen , str ( score ) , <NUM_LIT> , WIDTH / <NUM_LIT:2> , <NUM_LIT:10> ) <EOL> draw_shield_bar ( screen , <NUM_LIT:5> , <NUM_LIT:5> , player . shield ) <EOL> draw_lives ( screen , WIDTH - <NUM_LIT:100> , <NUM_LIT:5> , player . lives , player_mini_img ) <EOL> pygame . display . flip ( ) <EOL> pygame . quit ( ) </s>
