Advertisement
diliupg

Space Gladiator

Aug 25th, 2014
546
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 108.97 KB | None | 0 0
  1.                 #-------------- Space Gladiator ----------------#
  2.                 # Concept & Design      : Diliup Gabadamudalige #
  3.                 # Coding Commenced      : 2013-08-03            #
  4.                 # Coding Completed      : 2013-11-08            #
  5.                 #-----------------------------------------------#
  6. # This game was created as a part of my Python learning process which started in March 2013.
  7. # All coding done during spare time away from teaching Audio Engineering, Piano, Violin and Guitar.
  8. # you can download the same code with all the resources at : http://soft.diliupg.com/
  9. import pygame
  10. import pygame._view
  11. from pygame.locals import *
  12. import sys
  13. import os
  14. import math
  15. import time
  16. import random
  17. import copy
  18. import cPickle
  19.  
  20. wincon=pygame.image.load("resources\grfx\dg.bmp")
  21. pygame.display.set_icon(wincon)
  22. pygame.display.set_caption('SPACE GLADIATOR-EMorphus Productions 2013')
  23. #initialize pygame, load the 1st. music track and start playing while the game\
  24. # loads in the background
  25. clock = pygame.time.Clock()
  26. pygame.mixer.pre_init(44100, 16, 2, 4096) # setup mixer to avoid sound lag
  27. pygame.init()
  28. if sys.platform == 'win32' or sys.platform == 'win64':
  29.     os.environ['SDL_VIDEO_CENTERED'] = '2'# center of screen
  30. Dlist=pygame.display.list_modes(0,FULLSCREEN)
  31. Swidth=Dlist[0][0]-24
  32. Sheight=Dlist[0][1]-50
  33. screenmode=0
  34. screen = pygame.display.set_mode((Swidth, Sheight),screenmode,0)
  35. pygame.mouse.set_visible(False)
  36. startmusic="resources\snd\SGOpeningtheme.ogg"
  37. pygame.mixer.set_num_channels(64)
  38. pygame.mixer.music.load(startmusic)
  39. pygame.mixer.music.set_volume(0.3)
  40. pygame.mixer.music.play(-1)
  41. font = pygame.font.Font("resources\data\prometheanb.ttf", 22)
  42. text = font.render("Game Loading. Please wait....", 2, (150,150,250))
  43. screen.blit(text,[Swidth/2-text.get_width()/2,Sheight/2-text.get_height()])
  44. pygame.display.update()
  45.  
  46. #------------------------------------------------------------------------------#
  47. #                        A L L  T H E  F U N C T I O N S                       #
  48. #------------------------------------------------------------------------------#
  49. def update_all(text1,text2,text3):
  50.     global life
  51.     for i in xrange (Hero.lives):
  52.         screen.blit(life,(controls.Swidth-(25*(i+1)),2))
  53.     if Hero.life>0 and Hero.lives>0 and Hero.status=="N":
  54.         Hero.update()
  55.  
  56.     for g in controls.globelist:
  57.         g.update()
  58.  
  59.     for e in controls.Alien1: # 1st WAVE of ALIENS
  60.         if e.status=="M":
  61.             e.follow(Hero,7)
  62.             e.update()
  63.  
  64.     if controls.enemystate=='boss on' and boss1.status<>"E":
  65.         boss1.follow(Hero,8)
  66.         boss1.update()
  67.  
  68.     if controls.enemystate=='MLA2' and Mothership1.status=="M":
  69.         Mothership1.mship_update(Mothership1.heading)
  70.  
  71.     for e in controls.Alien2: # 1st WAVE of ALIENS
  72.         if e.status=="M":
  73.             e.follow(Hero,8)
  74.             e.update()
  75.  
  76.     for e in controls.FinalAliens: # 1st WAVE of ALIENS
  77.         if e.status=="M":
  78.             e.follow(Hero,9)
  79.             e.update()
  80.  
  81.     for a in asteroids:
  82.         a.update()
  83.  
  84.  
  85.     update_score(score,scorestr,realscore)
  86.     update_game_text(text1,text2,text3)
  87.     update_screen()
  88.     star_field()
  89. def update_screen():
  90.     pygame.display.update()
  91.     screen.fill((0,0,0))
  92.  
  93. def update_score(score,scorestr,realscore):
  94.     if score[6]>=10: score[6]=0; score[5]+=1
  95.     if score[5]>=10: score[5]=0; score[4]+=1
  96.     if score[4]>=10: score[4]=0; score[3]+=1
  97.     if score[3]>=10: score[3]=0; score[2]+=1
  98.     if score[2]>=10: score[2]=0; score[1]+=1
  99.     if score[1]>=10: score[1]=0; score[0]+=1
  100.     for w in score: scorestr+=str(w)
  101.     realscore=int(scorestr)
  102.  
  103. def update_game_text(text1,text2,text3):
  104.     pygame.draw.rect(screen, (128,128,128), [2, controls.Sheight-(152)\
  105.                                          , 14,152],3)# SHIELD meter border
  106.     pygame.draw.rect(screen, (128,128,128), [18, controls.Sheight-(152)\
  107.                                          , 14,152],3)#GUN TEMP meter border
  108.     if colourvals[0]<0: colourvals[0]=1
  109.     pygame.draw.rect(screen, (255,colourvals[0],0),\
  110.                      [controls.Swidth-controls.Swidth+4,\
  111.                       controls.Sheight-Hero.life/3, 10,295],0)# SHIELD amount
  112.     pygame.draw.rect(screen, (255,colourvals[1],0),\
  113.                      [controls.Swidth-controls.Swidth+20,\
  114.                       controls.Sheight-Hero.guntemp*2+4, 10,295],0)# GUN TEMPERATURE
  115.     font = pygame.font.Font("resources\data\prometheanb.ttf", 22)
  116.     screen.blit(text1, [2,0])# print text->score, colourvals[1]
  117.     text = font.render(scorestr, 2, (150,150,250))
  118.     screen.blit(text, [84,1])# print score
  119.     screen.blit(text2, [1,controls.Sheight-108])# SHIELD
  120.     screen.blit(text3, [18,controls.Sheight-100])# GUN TEMP
  121.  
  122.     if Hero.bonus[2]=="M":
  123.         screen.blit(text4,(controls.Swidth/2-text4.get_width()/2,5))
  124.         Hero.bonus[3]+=1
  125.         if Hero.bonus[3]>=100:
  126.             Hero.bonus[3]=0
  127.             Hero.bonus[2]="S"
  128.             shipbonus.play()
  129.     if Hero.bombs[0]>0 and controls.CBomb==False:
  130.         font = pygame.font.Font("resources\data\prometheanb.ttf", 22)
  131.         bombamount=font.render('BOMBS '+str(Hero.bombs[0])+\
  132.                                ' : '+'READY TO FIRE! (B)', 2,[200,0,200])
  133.         screen.blit(bombamount,((controls.Swidth-bombamount.get_width())/2,\
  134.                            controls.Sheight-25))
  135.     if Hero.bombs[1]=="R" and controls.CBomb==False:
  136.         R=random.randrange(100,250,25)
  137.     if controls.CBomb=="C":
  138.         R=random.randrange(100,250,25)
  139.         font = pygame.font.Font("resources\data\prometheanb.ttf", 20)
  140.         text7=font.render("CLUSTER BOMB READY! 'C' TO LAUNCH",\
  141.                           2,[R,R,R])
  142.         screen.blit(text7,(controls.Swidth/2-text7.get_width()/2,\
  143.                            controls.Sheight-25))
  144.  
  145. def initialize_stars():
  146.     global stars
  147.     controls.stars=[]
  148.     for x in xrange (500):
  149.         controls.stars.append([(int(controls.Swidth*random.random())),\
  150.                       (int(controls.Sheight*random.random()))])
  151. def star_field():
  152.     for w in xrange(len(controls.stars)):
  153.         x=random.randrange(100,255,2)
  154.         color1=(x,x,255)
  155.         pygame.draw.line(screen, color1, [controls.stars[w][0],\
  156.                                           controls.stars[w][1]],\
  157.                          [controls.stars[w][0],controls.stars[w][1]], 1)
  158.         controls.stars[w][1]+=starvel[0]
  159.         if controls.stars[w][1]>=controls.Sheight:
  160.             controls.stars[w][1]=0
  161.             controls.stars[w][0]=int(controls.Swidth*random.random())
  162.  
  163. def show_instructions():
  164.     font = pygame.font.Font("resources\data\prometheanb.ttf", 18)
  165.     while controls.instructions ==True:
  166.         x=60;y=80
  167.         for t in xrange(len(instructions)):
  168.             a=str(instructions[t])
  169.             text = font.render(a, 0, [200,200,200])
  170.             screen.blit(text,(x,y))
  171.             y+=36
  172.         for event in pygame.event.get():
  173.             if event.type==KEYDOWN:
  174.                 controls.instructions=False
  175.         star_field()
  176.         update_screen()
  177.  
  178. def player_instructions(akuru,time):
  179.     color=[0,0,0]; x=0
  180.     c=0
  181.     length=(len(akuru))
  182.     t=0
  183.     font = pygame.font.Font("resources\data\prometheanb.ttf", 30)
  184.     while controls.instructions==True:
  185.         pygame.event.pump()
  186.         keys = pygame.key.get_pressed()
  187.         if keys[K_ESCAPE] or keys[K_BACKSPACE] or keys[K_RETURN]:
  188.                 controls.instructions=False
  189.         a=str(akuru[t])
  190.         for c in xrange(254):
  191.             text = font.render(a, 0, color)
  192.             textlength=text.get_width()
  193.             screen.blit(text,(controls.Swidth/2-textlength/2,controls.Sheight/2))
  194.             pygame.time.wait(time)
  195.             y=1 if c<127 else -1
  196.             x+=y
  197.             x=0 if x<0 else x
  198.             color[0]=color[1]=color[2]=x
  199.             update_screen()
  200.             pygame.event.pump()
  201.             keys = pygame.key.get_pressed()
  202.             if keys[K_ESCAPE] or keys[K_BACKSPACE] or keys[K_RETURN]:
  203.                 controls.instructions=False
  204.         t+=1
  205.         if t==length:
  206.             controls.instructions=False
  207. def save_game():
  208.     yref=yy=(Sheight/4)+100
  209.     slotno=1
  210.     while controls.savegame==True:
  211.         timestamp=time.asctime(time.localtime())[4:]
  212.         x=controls.Swidth/3
  213.         y=controls.Sheight/4
  214.         font = pygame.font.Font("resources\data\prometheanb.ttf", 20)
  215.         empty='<      - e m p t y -     >'
  216.         warning='save slot not empty.  S to overwrite'
  217.         warntext=font.render(warning,0,[200,0,0])
  218.         text=['Save Game','select slot and hit ENTER to SAVE game.',\
  219.               'BACKSPACE to exit without saving.']
  220.         for i in xrange(len(text)):
  221.             itext=font.render(text[i],0,[100,100,255])
  222.             screen.blit(itext,(controls.Swidth/2-itext.get_width()/2,\
  223.                               y-itext.get_height()))
  224.             y+=(itext.get_height()*2)
  225.         if len(savelist[slotno-1])>0:
  226.             screen.blit(warntext,(controls.Swidth/2-\
  227.                                   warntext.get_width()/2,y-itext.get_height()))
  228.  
  229.         screen.fill([30,30,30],[0,y,controls.Swidth,y],0)
  230.         font = pygame.font.Font("resources\data\prometheanb.ttf", 16)
  231.         cursor=font.render("<<<",0,[100,100,255])
  232.         for i in xrange(len(savelist)): # print the save list from disk file
  233.             if savelist[i]==[]:
  234.                 text=font.render(str(i+1)+". "+empty,0,[200,200,200])
  235.             else:
  236.                 text=font.render(str(i+1)+'. '+str(savelist[i][0]),0,[200,200,200])
  237.             screen.blit(text,(controls.Swidth/2-text.get_width()/2,y))
  238.             y+=text.get_height()*2
  239.         screen.blit(cursor,(controls.Swidth-controls.Swidth/2.55,yy))
  240.  
  241.         for event in pygame.event.get():
  242.             if event.type==KEYDOWN:
  243.                 if event.key==K_DOWN:
  244.                     yy+=25
  245.                     slotno+=1
  246.                 elif event.key==K_UP:
  247.                     yy-=25
  248.                     slotno-=1
  249.  
  250.                 if yy>=yref+250:
  251.                     yy=yref
  252.                     slotno=1
  253.                 if yy<yref:
  254.                     yy=yref+225
  255.                     slotno=10
  256.  
  257.                 if event.key==K_RETURN:
  258.                     savegamedata=vars(save_game_data()) # dict
  259.                     print savegamedata
  260.                     newsave=[timestamp,savegamedata] # make the save file
  261.                     savelist[slotno-1]=newsave # put into slot selected
  262.                     f=open('resources\data\saves.sg','wb')
  263.                     cPickle.dump(savelist,f) # write the file to disk
  264.                     f.close()
  265.                     controls.savegame=False
  266.  
  267.                 if event.key==K_BACKSPACE:
  268.                     controls.savegame=False
  269.  
  270.         star_field()
  271.         update_screen()
  272.  
  273. def load_game():
  274.     yref=yy=(Sheight/4)+100
  275.     slotno=1
  276.     while controls.loadgame==True:
  277.         timestamp=time.asctime(time.localtime())[4:]
  278.         x=controls.Swidth/3
  279.         y=controls.Sheight/4
  280.         font = pygame.font.Font("resources\data\prometheanb.ttf", 20)
  281.         empty='<      - e m p t y -     >'
  282.         warning='slot empty. nothing to load'
  283.         warntext=font.render(warning,0,[200,0,0])
  284.         text=['Load Game','select slot and hit HNTER to LOAD game.',\
  285.               'BACKSPACE to exit without loading.']
  286.         for i in xrange(len(text)):
  287.             itext=font.render(text[i],0,[100,100,255])
  288.             screen.blit(itext,(controls.Swidth/2-itext.get_width()/2,\
  289.                               y-itext.get_height()))
  290.             y+=(itext.get_height()*2)
  291.         if len(savelist[slotno-1])==0:
  292.             screen.blit(warntext,(controls.Swidth/2-\
  293.                                   warntext.get_width()/2,y-itext.get_height()))
  294.  
  295.         screen.fill([30,30,30],[0,y,controls.Swidth,250],0)
  296.         font = pygame.font.Font("resources\data\prometheanb.ttf", 16)
  297.         cursor=font.render("<<<",0,[100,100,255])
  298.         for i in xrange(len(savelist)): # print the save list from disk file
  299.             if savelist[i]==[]:
  300.                 text=font.render(str(i+1)+". "+empty,0,[200,200,200])
  301.             else:
  302.                 text=font.render(str(i+1)+'. '+str(savelist[i][0]),0,[200,200,200])
  303.             screen.blit(text,(controls.Swidth/2-text.get_width()/2,y))
  304.             y+=25
  305.         screen.blit(cursor,(controls.Swidth-controls.Swidth/2.55,yy))
  306.  
  307.         for event in pygame.event.get():
  308.             if event.type==KEYDOWN:
  309.                 if event.key==K_DOWN:
  310.                     yy+=25
  311.                     slotno+=1
  312.                 elif event.key==K_UP:
  313.                     yy-=25
  314.                     slotno-=1
  315.  
  316.                 if yy>=yref+250:
  317.                     yy=yref
  318.                     slotno=1
  319.                 if yy<yref:
  320.                     yy=yref+225
  321.                     slotno=10
  322.  
  323.                 if event.key==K_RETURN and savelist[slotno-1]!=[]:
  324.                     # Get highscores from disk n close file
  325.                     f=open('resources\data\saves.sg','rb')
  326.                     loadlist=cPickle.load(f)
  327.                     controls.loadgamedata=loadlist[slotno-1][1]
  328.                     # call the routine to load the game data to the vazriables
  329.                     set_game_data(controls.loadgamedata)
  330.                     f.close()
  331.                     controls.loadgame=False
  332.  
  333.                 if event.key==K_BACKSPACE:
  334.                     controls.loadgame=False
  335.  
  336.         star_field()
  337.         update_screen()
  338.  
  339. def set_game_data(info):
  340.     global score
  341.     controls.globes=info['globes']
  342.     controls.PUcount=info['PUcount']
  343.     controls.invincibility=info['invincibility']
  344.     controls.TRIPLEgun=info['TRIPLEgun']
  345.     controls.TRIPLEguntime=info['TRIPLEguntime']
  346.     controls.CBombflag=info['CBombflag']
  347.     controls.CBomb=info['CBomb']
  348.     controls.enemystate=info['enemystate']
  349.     controls.gamestate=info['gamestate']
  350.     controls.asteroidcount=info['asteroidcount']
  351.     controls.collected=info['collected']
  352.     controls.gamecounter=info['gamecounter']
  353.     controls.no_of_enemies=info['no_of_enemies']
  354.     controls.enemycount=info['enemycount']
  355.     controls.music=info['music']
  356.     controls.rank=info['rank']
  357.     score=info['score']
  358.     Hero.lives=info['Herolives']
  359.     Hero.life=info['Herolife']
  360.     Hero.nodamage=info['Heronodamage']
  361.    
  362.     # if player had collected any globes before saving the game, reduce it from
  363.     # the amount to be spawned
  364.     if controls.globes=='Galive':
  365.         globeamount=controls.globequantity[0]-controls.collected
  366.         controls.globelist=[]
  367.         spawn_globes(Gglobe, 0.3,10,globeamount)
  368.  
  369.     if controls.globes=='Ralive':
  370.         globeamount=controls.globequantity[1]-controls.collected
  371.         controls.globelist=[]
  372.         spawn_globes(Rglobe, 0.4,15,globeamount)
  373.    
  374.     if controls.globes=='Oalive':
  375.         if controls.asteroidswitch=="OFF":
  376.             controls.asteroidswitch="ON"
  377.         globeamount=controls.globequantity[2]-controls.collected
  378.         controls.globelist=[]
  379.         spawn_globes(Oglobe, 0.5,20,globeamount)
  380.        
  381.     if controls.globes=='Palive':
  382.         globeamount=controls.globequantity[3]-controls.collected
  383.         controls.globelist=[]
  384.         spawn_globes(Pglobe, 0.6,25,globeamount)
  385.  
  386.     if controls.gamestate=="Level 2":
  387.         boss1.radar=controls.Swidth/2-100
  388.         boss1.mag=40 # shooting delay
  389.         controls.enemystate="boss on"
  390.         boss1.status='M'
  391.  
  392.     if controls.gamestate=="Level 3":
  393.         controls.asteroidswitch="OFF"
  394.         controls.globes='OFF'
  395.         Mothership1.status="M"
  396.         controls.globelist=[]
  397.         Hero.BMag=[]
  398.         Hero.bombs=[0,"D"]
  399.         controls.counter=0
  400.         controls.no_of_enemies=12
  401.         controls.spendtime=0
  402.         boss1.status='boss1 dest'
  403.  
  404.     if controls.gamestate=="Level 4":
  405.         controls.enemystate="REST 2"
  406.         controls.asteroidswitch="ON"
  407.         controls.asteroidcount=0
  408.         controls.asteroidlimit=6
  409.         controls.asteroidamount=100
  410.         controls.spendtime=0
  411.    
  412. def hall_of_fame_message():
  413.     x=controls.Swidth/4
  414.     y=300
  415.     font = pygame.font.Font("resources\data\prometheanb.ttf", 22)
  416.     akuru=['CONGRATULATIONS! You qualify to enter ',\
  417.            'the Space Gladiator Hall of Fame.','Please enter your name.']
  418.  
  419.     for t in xrange(len(akuru)):
  420.         a=str(akuru[t])
  421.         text = font.render(a, 0, [175,175,175])
  422.         screen.blit(text,(controls.Swidth/2-text.get_width()/2,y))
  423.         y+=text.get_height()
  424.     return y # y position to print name as it is typed
  425.  
  426. def random_xyheading(): #generates a random number outside 0 and Swidth
  427.     x=int(random.randrange(-100,100,1))
  428.     y=int(random.randrange(50,(controls.Sheight-50),1))
  429.     h=int(random.randrange(-45,45,1)) # heading
  430.     if x>=-50:
  431.         x+=controls.Swidth+50
  432.         h=h+180
  433.     return x,y,h
  434.  
  435. def Asteroid_spawn():
  436.     radius=controls.Sheight-100
  437.     x=int(radius*math.cos(controls.asterseed)+controls.Swidth/2)
  438.     y=int(radius*math.sin(controls.asterseed)+controls.Sheight/2)
  439.     h=random.randrange(-180,180,1) # heading
  440.     controls.asterseed=(controls.asterseed+40)%180
  441.     return x,y,h
  442.  
  443. def spawn_globes(globeimage,gspeed,size,quantity):
  444.     for w in xrange (quantity):
  445.         '''image,x,y,scale,no of items spawned'''
  446.         globe=Collectibles(globeimage,controls.Swidth*random.random(),\
  447.                            controls.Sheight*random.random(),\
  448.                           size,quantity)
  449.         globe.speed=gspeed
  450.         controls.globelist.append(globe)
  451.     return controls.globelist
  452.  
  453. def add_and_close_file():
  454.     cPickle.dump(highscores,f)
  455.     f.close()
  456.  
  457. def game_over():
  458.     pygame.mixer.music.stop()
  459.     pygame.quit()
  460.     sys.exit()
  461.  
  462. def really_quit():
  463.     controls.quitgame=True
  464.     while controls.quitgame==True:
  465.         font = pygame.font.Font("resources\data\prometheanb.ttf", 25)
  466.         y=controls.Sheight/2
  467.         for i in xrange(len(confirm)):
  468.             text=font.render(str(confirm[i]),2,[10,10,250])
  469.             screen.blit(text,(controls.Swidth/2-text.get_width()/2,y))
  470.             y+=text.get_height()
  471.         for event in pygame.event.get():
  472.             if event.type==KEYDOWN:
  473.                 if event.key==K_y:
  474.                     game_over()
  475.  
  476.                 elif event.key!=K_y:
  477.                     controls.quitgame=False
  478.         star_field()
  479.         update_screen()
  480.  
  481. #------------------------------------------------------------------------------#
  482. #                          A L L  T H E  C L A S S E S                         #
  483. #------------------------------------------------------------------------------#
  484.  
  485. #The Master class with common attributes and methods for all classes
  486. class Master_elements(object):
  487.     def __init__(self,x,y,scale,status,frrate,frsize):
  488.         self.x=x
  489.         self.y=y
  490.         self.scale=scale
  491.         self.status=status
  492.         self.frsize=frsize
  493.         self.frrate=frrate
  494.         self.heading=0 #in degrees
  495.         self.frame=0 # when exploding
  496.         self.rectarea=pygame.Rect(self.x-self.scale/2,\
  497.                                   (controls.Sheight-self.y)-self.scale/2,\
  498.                                   self.scale,self.scale)
  499.         self.centerx=self.rectarea.centerx
  500.         self.centery=self.rectarea.centery
  501.     def posxy(self,x,y):
  502.         if self.x>controls.Swidth:
  503.             self.x=0
  504.         elif self.x<0 :
  505.             self.x=controls.Swidth
  506.         else:
  507.             self.x=x
  508.         if self.y<0:
  509.             self.y=controls.Sheight
  510.         elif self.y>controls.Sheight:
  511.             self.y=0
  512.         else:
  513.             self.y=y
  514.     def setHeading(self,angle):
  515.         self.heading=angle%360
  516.  
  517.     def getrectarea(self):
  518.         self.rectarea=pygame.Rect(self.x-self.scale/2,\
  519.                                   (controls.Sheight-self.y)-self.scale/2,\
  520.                                   self.scale,self.scale)
  521.         self.centerx=self.rectarea.centerx
  522.         self.centery=self.rectarea.centery
  523.  
  524. class HeroObject(Master_elements):
  525.     def __init__(self,scale,x,y,status,lives,\
  526.                  bonus,bombs,frrate,frsize):
  527.         Master_elements.__init__(self,x,y,scale,status,frrate,frsize)
  528.         self.image1=pygame.transform.scale(images.Heroa,(self.scale,self.scale))# normal
  529.         self.image2=pygame.transform.scale(images.Herob,(self.scale,self.scale))# thrust
  530.         self.image3=images.Heroc # explode
  531.         self.image4=images.Herod # teleport
  532.         self.speedx=0
  533.         self.speedy=0
  534.         self.thrust=0.02
  535.         self.speedlimit=6
  536.         self.engines=False
  537.         self.firerate=0
  538.         self.maxfirerate=4
  539.         self.life=450 # initial value 450
  540.         self.lives=lives
  541.         self.reinit=100# if Hero dies respawn after reinit becomes 0
  542.         self.ssx=0 # sprite sheet pox
  543.         self.ssy=0 # sprite sheet posy
  544.         self.guntemp=0 # when max value can't fire gun!
  545.         self.nodamage=500# time invincible after being spawned 250*2=500
  546.         self.bonus=bonus # ["one",20,"S",0]
  547.         self.shieldradius=22
  548.         self.shieldcolour=[250,250,250]
  549.         self.shieldcolour2=255
  550.         self.CMag=[] # Center Magazine
  551.         self.LMag=[] # Left Magazine
  552.         self.RMag=[] # Right magazine
  553.         self.BMag=[] # Bomb Magazine
  554.         self.CBMag=[]# Cluster Bomb Mag
  555.         self.bombs=bombs
  556.  
  557.     def move(self,speedx,speedy):
  558.         self.x+=speedx
  559.         self.y+=speedy
  560.         self.posxy(self.x,self.y)
  561.     def teleport(self):
  562.         if (self.frame<16):
  563.             self.ssx=(self.frame%4)*100
  564.             self.ssy=math.floor(self.frame/4)*100
  565.         else:
  566.             self.status="N"
  567.             self.x=controls.Swidth*random.random()
  568.             self.y=controls.Sheight*random.random()
  569.             self.speedx*=-Hero.thrust
  570.             self.speedy*=-Hero.thrust
  571.             self.frame=0
  572.         screen.blit(self.image4,(self.x-self.scale,(controls.Sheight-self.y)-\
  573.                                  self.scale),Rect((self.ssx,self.ssy),\
  574.                                                     (100,100)))
  575.         self.frame+=1
  576.     def explode(self):
  577.         if self.frame < self.frrate:
  578.             self.ssx=(self.frame%self.frrate)*self.frsize
  579.             self.ssy=math.floor(self.frame/self.frrate)*self.frsize
  580.         else:
  581.             self.status="S"
  582.         screen.blit(self.image3,(self.x-self.scale/2,\
  583.                                  (controls.Sheight-self.y)-\
  584.                                  self.scale/2),Rect((self.ssx,self.ssy),\
  585.                                                     (self.frsize,self.frsize)))
  586.         self.frame+=1
  587.     def update(self):
  588.         if self.engines==True:
  589.             rot_image = pygame.transform.rotate(self.image2,self.heading)
  590.             rot_rect=self.image2.get_rect().copy()
  591.         else:
  592.             rot_image = pygame.transform.rotate(self.image1,self.heading)
  593.             rot_rect=self.image1.get_rect().copy()
  594.         rot_rect.center = rot_image.get_rect().center
  595.         rot_image = rot_image.subsurface(rot_rect).copy()
  596.         screen.blit(rot_image,(self.x-self.scale/2,controls.Sheight\
  597.                                -self.y-self.scale/2))
  598.  
  599. class Gun():
  600.     def __init__(self,image1,image2,speed,status,scale,life,kind,frrate,frsize):
  601.         self.x=0
  602.         self.y=0
  603.         self.scale=scale
  604.         self.life=life
  605.         self.heading=0
  606.         self.rectarea=pygame.Rect(self.x-self.scale/2,\
  607.                                   (controls.Sheight-self.y)-self.scale/2,\
  608.                                   self.scale,self.scale)
  609.         self.image1=pygame.transform.scale(image1, (self.scale,self.scale))
  610.         self.image2=image2
  611.         self.speed=speed
  612.         self.status =status #S=Standby, F=Fired, E=exploding, D=Destroyed
  613.         self.ssx=0
  614.         self.ssy=0
  615.         self.frame=0
  616.         self.frrate=frrate
  617.         self.frsize=frsize
  618. # AB-alien bullet HB-Hero bullet HM=Hero missile BM=boss missile
  619.         self.kind=kind
  620.     def fire(self,parent):
  621.         self.x=parent.x+(parent.scale/2)*math.cos(math.radians(parent.heading))
  622.         self.y=parent.y+(parent.scale/2)*math.sin(math.radians(parent.heading))
  623.         self.heading=parent.heading
  624.         self.status="F"
  625.     def fireR(self,parent):
  626.         self.x=parent.x+(parent.scale/2)*math.cos(math.radians\
  627.                                                   (parent.heading-30))
  628.         self.y=parent.y+(parent.scale/2)*math.sin(math.radians\
  629.                                                   (parent.heading-30))
  630.         self.heading=parent.heading
  631.         self.status="F"
  632.     def fireL(self,parent):
  633.         self.x=parent.x+(parent.scale/2)*math.cos(math.radians\
  634.                                                   (parent.heading+30))
  635.         self.y=parent.y+(parent.scale/2)*math.sin(math.radians\
  636.                                                   (parent.heading+30))
  637.         self.heading=parent.heading
  638.         self.status="F"
  639.  
  640.     def fireBombCluster(self,parent,angle,h):
  641.         self.x=parent.x-(parent.scale/2)*math.cos(math.radians\
  642.                                                   (parent.heading-angle))
  643.         self.y=parent.y-(parent.scale/2)*math.sin(math.radians\
  644.                                                   (parent.heading-angle))
  645.         self.heading=h
  646.         self.status="CF"
  647.  
  648.     def explode(self):
  649.         self.status="E"
  650.     def next_image_frame(self):
  651.         self.frame+=1
  652.         if self.frame < self.frrate*self.frrate:
  653.             self.ssx=(self.frame%self.frrate)*self.frsize
  654.             self.ssy=math.floor(self.frame/self.frrate)*self.frsize
  655.         else:
  656.             self.status="D"
  657.             self.frame=0
  658.             self.ssx=0
  659.             self.ssy=0
  660.     def update(self):
  661.         if self.kind!="HM":
  662.             self.life-=1
  663.         if self.kind=="HM" and self.status=="CF":
  664.             self.life-=1
  665.             if self.x>controls.Swidth or self.x<0 or \
  666.                self.y>controls.Sheight or self.y<0:
  667.                 self.life=0
  668.         if self.life<=0:
  669.             self.status="D"
  670.         if self.status=="F" or self.status=="CF":
  671.             if self.status=='CF': # bomb fired so start reducing speed
  672.                 self.speed-=0.1
  673.                 if self.speed<=0:
  674.                     self.speed=0
  675.             self.x+=self.speed*math.cos(math.radians(self.heading))
  676.             self.y+=self.speed*math.sin(math.radians(self.heading))
  677.             if((self.x<0 or self.x > controls.Swidth) \
  678.                and (self.y < 0 or self.y > controls.Sheight)):
  679.                 self.status="D"
  680.         if self.status=="E":
  681.             self.next_image_frame()
  682.         if self.status=="F" or self.status=="CF":
  683.             self.rotate()
  684.             screen.blit(self.new_image,(self.x,\
  685.                                      controls.Sheight-self.y),\
  686.                         Rect((self.ssx,self.ssy),(100,100)))
  687.         if self.status=="E":
  688.             screen.blit(self.image2,(self.x-self.frsize/2,\
  689.                                      controls.Sheight-self.y-self.frsize/2),\
  690.                         Rect((self.ssx,self.ssy),(self.frsize,self.frsize)))
  691.     def getrectarea(self):
  692.         if self.kind=="BM":
  693.             self.rectarea=pygame.Rect(self.x+10-self.scale/2,\
  694.                           (controls.Sheight+10-self.y)-self.scale/2,\
  695.                           self.scale-8,self.scale-8)
  696.         elif self.kind=="HM" and controls.CBomb=="F":
  697.             self.rectarea=pygame.Rect(self.x,\
  698.                                       (controls.Sheight-self.y),\
  699.                                       self.scale,self.scale)
  700.         elif self.kind=="HM" and controls.CBomb=="E":
  701.             self.rectarea=pygame.Rect(self.x-self.frsize/2,\
  702.                                       (controls.Sheight-self.y)-self.frsize/2,\
  703.                                       self.frsize,self.frsize)
  704.         else:
  705.             self.rectarea=pygame.Rect(self.x-self.scale/2,\
  706.                                       (controls.Sheight-self.y)-self.scale/2,\
  707.                                       self.scale,self.scale)
  708.  
  709.     def rotate(self):
  710.         rot_image = pygame.transform.rotate(self.image1,self.heading)
  711.         rot_rect=self.image1.get_rect().copy()
  712.         rot_rect.center = rot_image.get_rect().center
  713.         self.new_image = rot_image.subsurface(rot_rect).copy()
  714.  
  715. class Collectibles():
  716.     def __init__(self,image,x,y,scale,noofitems):
  717.         self.x=x
  718.         self.y=y
  719.         self.scale=scale
  720.         self.status="N"# normal when collected becomes "C"
  721.         self.heading=controls.Swidth*random.random()
  722.         self.image=image
  723.         self.image=pygame.transform.scale(image,(self.scale,self.scale))
  724.         self.speed=0.3
  725.         self.noofitems=noofitems
  726.         self.rectarea=pygame.Rect(self.x-self.scale/2,\
  727.                                   (controls.Sheight-self.y)-self.scale/2,\
  728.                                   self.scale,self.scale)
  729.     def update(self):
  730.         self.x+=self.speed*math.cos(math.radians(self.heading))
  731.         self.y+=self.speed*math.sin(math.radians(self.heading))
  732.         """accelerate the collectibles when the Hero gets close"""
  733.         if abs(Hero.x-self.x)<75 and abs(Hero.y-self.y)<100:
  734.             self.x+=self.speed*math.cos(math.radians(self.heading))*\
  735.                      (4*random.random())
  736.             self.y+=self.speed*math.sin(math.radians(self.heading))*\
  737.                      (4*random.random())
  738.         if self.x<0: self.x=controls.Swidth
  739.         if self.x>controls.Swidth: self.x=0
  740.         if self.y<0: self.y=controls.Sheight
  741.         if self.y>controls.Sheight: self.y=0
  742.         screen.blit(self.image,(self.x-self.scale/2,controls.Sheight\
  743.                                -self.y-self.scale/2))
  744.     def getrectarea(self):
  745.         self.rectarea=pygame.Rect(self.x-self.scale/2,\
  746.                                   (controls.Sheight-self.y)-self.scale/2,\
  747.                                   self.scale,self.scale)
  748.  
  749. class Asteroids():
  750.     """image,x,y,life,scale,width,xfr,frsize,noframes,status"""
  751.     def __init__(self,image,image2,x,y,life,width,frrate,\
  752.                  frsize,noframes,kind,status):
  753.         self.x=x
  754.         self.y=y
  755.         self.scale=frsize
  756.         self.kind=kind # L-Large, M-Medium, S-Small
  757.         self.status=status #  M-moving in space,  E-explode,  D- destroyed
  758.         self.heading=360*random.random()
  759.         self.frame=0
  760.         self.frrate=frrate
  761.         self.frsize=frsize
  762.         """rect smaller than actual size cause of asteroid shape"""
  763.         self.life=life
  764.         self.image=image
  765.         self.expimage=image2
  766.         self.width=width
  767.         self.noframes=noframes
  768.         self.speed=0.6
  769.         self.speedlimit=self.speed
  770.         self.ssx=0
  771.         self.ssy=0
  772.         self.framecount=0
  773.         self.rectarea=pygame.Rect(self.x-self.scale/3,\
  774.                                   (controls.Sheight-self.y)-self.scale/3,\
  775.                                   self.scale-self.scale/2.5,\
  776.                                   self.scale-self.scale/2.5)
  777.         self.centerx=self.rectarea.centerx
  778.         self.centery=self.rectarea.centery
  779.  
  780.     def update(self):
  781.         """only the number of frames on the x axis is needed"""
  782.         if self.frame<self.noframes:
  783.             self.ssx=(self.frame%self.frrate)*self.frsize
  784.             self.ssy=math.floor(self.frame/self.frrate)*self.frsize
  785.         self.frame+=1
  786.  
  787.         if self.status=="M" and self.frame==self.noframes:
  788.             self.frame=0
  789.             self.ssx=0
  790.             self.xxy=0
  791.  
  792.         if self.status=="M":
  793.             self.x+=self.speed*math.cos(math.radians(self.heading))
  794.             self.y+=self.speed*math.sin(math.radians(self.heading))
  795.  
  796.         if self.y<-self.frsize: self.y=controls.Sheight
  797.         if self.y>controls.Sheight: self.y=0
  798.         if self.x<-self.frsize: self.x=controls.Swidth
  799.         if self.x>controls.Swidth: self.x=0
  800.         if self.status=="M":
  801.             screen.blit(self.image,(self.x-self.scale/2,\
  802.                                     controls.Sheight-self.y-self.scale/2),Rect\
  803.                         ((self.ssx,self.ssy),(self.scale,self.scale)))
  804.         if self.status=="E":
  805.             screen.blit(self.expimage,(self.x-self.scale/2,\
  806.                                        controls.Sheight-self.y-self.scale/2),\
  807.                         Rect((self.ssx,self.ssy),(self.scale,self.scale)))
  808.             if self.frame>=self.noframes:
  809.                 self.status="D"
  810.  
  811.     def getrectarea(self):
  812.         """get the rect a little smaller than the actual size"""
  813.         self.rectarea=pygame.Rect(self.x-self.scale/3,\
  814.                                   (controls.Sheight-self.y)-self.scale/3,\
  815.                                   self.scale-self.scale/2.5,\
  816.                                   self.scale-self.scale/2.5)
  817.  
  818. class EnemyObject(Master_elements):
  819.     def __init__(self,image,image2,scale,x,y,life,firerate,frrate,\
  820.                  frsize,status):
  821.         """status-"M" move "E"-explode "D"-destroy"""
  822.         Master_elements.__init__(self,x,y,scale,status,frrate,frsize)
  823.         self.life=life
  824.         self.image=pygame.transform.scale(image,(self.scale,self.scale))
  825.         self.image2=image2
  826.         """enemy starts with status 'M' becomes 'E'-life=0 'D'=destroyed"""
  827.         self.a=0.04 # accelleration
  828.         self.fixeda=0.04
  829.         self.firerate=firerate
  830.         self.radar=0 # how close the enemy must be to detect opponent
  831.         self.Mag=[] # The gun bullet magazine
  832.         self.BMag=[] # The bomb mag if any
  833.         """state= flag to show style of movement,counter,accel save loc"""
  834.         self.state='normal'
  835.         self.counter=0
  836.         # safe area for same type collisions
  837.         self.safearea=self.scale/2
  838.  
  839.     def follow(self,target,speed):
  840.         self_rect=pygame.Rect(self.x-self.scale/2,\
  841.                               controls.Sheight-self.y-self.scale/2,\
  842.                               self.scale,self.scale)
  843.         target_rect=pygame.Rect(target.x-target.scale/2,\
  844.                                 controls.Sheight-target.y-target.scale/2,\
  845.                                 target.scale,target.scale)
  846.         # follow Hero only if close than radar
  847.         if abs(self.centerx-Hero.centerx)<self.radar and \
  848.            abs(self.centery-Hero.centery)<self.radar and self.state=='normal':
  849.             delta_x=target.x-self.x
  850.             delta_y=target.y-self.y
  851.             self.setHeading(math.degrees(math.atan2(delta_y,delta_x)))
  852.             self.move(speed)
  853.         else:
  854.             self.heading=self.heading
  855.             self.move(speed)
  856.  
  857.     def move(self,speed):
  858.         new_x=self.x+speed*math.cos(math.radians(self.heading))*\
  859.                constant*self.a
  860.         new_y=self.y+speed*math.sin(math.radians(self.heading))*\
  861.                constant*self.a
  862.         self.ai_behaviour()
  863.         self.posxy(new_x,new_y)
  864.  
  865.     def ai_behaviour(self):
  866.         if self.state=='turn':
  867.             self.counter=0
  868.             self.state='away' # move away from hero
  869.  
  870.         # dont get too close to the Hero
  871.         if abs(self.centerx-Hero.centerx)<=((self.scale)+(Hero.scale)) and\
  872.            abs(self.centery-Hero.centery)<=((self.scale)+(Hero.scale)):
  873.             self.state='away'
  874.             self.counter=0
  875.  
  876.         if self.state=='away' and self.counter<80:
  877.             if self.counter<20: #turn till this value
  878.                 self.heading+=2
  879.                 self.a=0.06 # move away faster than approaching
  880.             self.counter+=1
  881.  
  882.         if self.counter>=80: # reach limit of  moving away
  883.             self.state='normal'
  884.             self.a=self.fixeda
  885.             self.counter=0
  886.             self.heading=self.heading+30
  887.  
  888.     def explode(self):
  889.         if (self.frame <=(self.frrate*self.frrate)):
  890.             self.ssx=(self.frame%self.frrate)*self.frsize
  891.             self.ssy=math.floor(self.frame/self.frrate)*self.frsize
  892.             screen.blit(self.image2,(self.x-self.frsize/2,controls.Sheight-\
  893.                                      self.y-self.frsize/2),Rect\
  894.                         ((self.ssx,self.ssy),(self.frsize,self.frsize)))
  895.         else:
  896.             self.status="D"
  897.         self.frame+=1
  898.  
  899.     def update(self):
  900.         rot_image = pygame.transform.rotate(self.image,self.heading)
  901.         rot_rect=self.image.get_rect().copy()
  902.         rot_rect.center = rot_image.get_rect().center
  903.         rot_image = rot_image.subsurface(rot_rect).copy()
  904.         screen.blit(rot_image,(self.x-self.scale/2,controls.Sheight-\
  905.                                self.y-self.scale/2))
  906.         self.draw_healthbar()
  907.  
  908.     def mship_update(self,heading): # use for rotating mothership while moving
  909.         self.x+=self.speed*math.cos(math.radians(self.heading))
  910.         self.y+=self.speed*math.sin(math.radians(self.heading))
  911.         rot_image = pygame.transform.rotate(self.image,self.heading)
  912.         rot_rect=self.image.get_rect().copy()
  913.         rot_rect.center = rot_image.get_rect().center
  914.         rot_image = rot_image.subsurface(rot_rect).copy()
  915.         screen.blit(rot_image,(self.x-self.scale/2,controls.Sheight\
  916.                                -self.y-self.scale/2))
  917.         self.draw_healthbar()
  918.  
  919. # Draw the Alien Health Bar
  920.     def draw_healthbar(self):
  921.         pygame.draw.rect(screen,(255,0,0),[self.x-self.scale/2\
  922.                                    ,controls.Sheight-self.y-self.scale/2,\
  923.                                            self.life ,3],0)
  924. class Load_images():
  925.     def __init__(self):
  926.         self.Heroa=pygame.image.load\
  927.                     ("resources\grfx\HeroTF.png").convert_alpha()
  928.         self.Herob=pygame.image.load\
  929.                     ("resources\grfx\HeroTT.png").convert_alpha()
  930.         self.Heroc=pygame.image.load\
  931.                     ("resources\grfx\Hexplosion.png").convert_alpha()
  932.         self.Herod=pygame.image.load\
  933.                     ("resources\grfx\Teleport.png").convert_alpha()
  934.        
  935. class Game_controls():
  936.     def __init__(self):
  937.         self.Swidth=Swidth
  938.         self.Sheight=Sheight
  939.         self.globelist=[] # list for globes
  940.         self.globes='Green'
  941.         """globes come on when the flag shows the color"""
  942.         self.globequantity=[100,75,50,15] # 100 Green,75 Red,50 Orange,15 Purple
  943.         self.PUcount=[1,1,1]    # counter-invincibility, guntytpe,CBomb
  944.         self.invincibility=False
  945.         self.TRIPLEgun=False
  946.         self.TRIPLEguntime=0
  947.         self.CBombflag=False
  948.         self.CBomb=False # Cluster bomb False,True,fired-F,exploding-E,over-D
  949.         self.CBangle=0 # the cluster bomb dispersion angle increase by 36
  950.         self.enemystate='boss off'
  951.         self.gamestate='initial'#(initial,Level 1,Level 2 etc.)
  952.         self.pause=True
  953.         self.instructions=False
  954.         self.titlewidth=0
  955.         self.titleheight=0
  956.         self.halloffame=False
  957.         self.asteroidcount=0    # if equal to asteroid amount no more spawned
  958.         self.asteroidlimit=4# minimum no of asteroids in list. 5 at start
  959.         self.asteroidamount=75# total amount of large asteroids spawned
  960.         self.asteroidswitch="OFF" # first time on is with Orange globes
  961.         self.asterseed=int(random.randrange(0,360,36))
  962.         self.collected=0        # amount of globes collected
  963.         self.gamespeed=250
  964.         self.gamecounter=0
  965.         self.no_of_enemies=0
  966.         self.enemycount=0 # count each alienwave
  967.         self.enemy_limit=25  # enemy limit on screen
  968.         self.LreduceA1=6     # life reduce Alien1 life-24
  969.         self.LreduceA2=8     # life reduce Alien2 life-24
  970.         self.LreduceB=0.4   # life reduce Boss  life-100
  971.         self.LreduceM=0.2  # life reduce Mothership life-100
  972.         self.LreduceFA=0.2 # life reduce FinalAliens life-50
  973.         self.FAamount=6
  974.         self.spendtime=0 # a counter to do nothing
  975.         self.glt_sec=clock.tick()/1000 # game looptime in seconds
  976.         self.name=''            # hall of fame name
  977.         self.music='theme'
  978.         self.stars=[] # the star field co ordinates list
  979.         self.Alien1=[]    # amount of enemies
  980.         self.Alien2=[]
  981.         self.FinalAliens=[] # the final Alien attack ships-2
  982.         self.AlienMag=[]
  983.         self.Alien2Mag=[]
  984.         self.Alien3Mag=[]
  985.         self.BossBombMag=[]
  986.         self.musicfader=.5
  987.         self.counter=0 # a counter to do waiting
  988.         self.savegame=False
  989.         self.loadgame=False
  990.         self.loadgamedata=[] # load game data here and distribute to variables
  991.         self.rank="CADET"
  992.         self.quitgame=False
  993.        
  994. class save_game_data():
  995.     def __init__(self):
  996.         self.globes=controls.globes
  997.         self.PUcount=controls.PUcount
  998.         self.invincibility=controls.invincibility
  999.         self.TRIPLEgun=controls.TRIPLEgun
  1000.         self.TRIPLEguntime=controls.TRIPLEguntime
  1001.         self.CBombflag=controls.CBombflag
  1002.         self.CBomb=controls.CBomb
  1003.         self.enemystate=controls.enemystate
  1004.         self.gamestate=controls.gamestate
  1005.         self.asteroidcount=controls.asteroidcount
  1006.         self.collected=controls.collected
  1007.         self.gamecounter=controls.gamecounter
  1008.         self.no_of_enemies=controls.no_of_enemies
  1009.         self.enemycount=controls.enemycount
  1010.         self.music=controls.music
  1011.         self.rank=controls.rank
  1012.         self.score=score
  1013.         self.Herolives=Hero.lives
  1014.         self.Herolife=Hero.life
  1015.         self.Heronodamage=Hero.nodamage
  1016.  
  1017. controls=Game_controls() # initialize all game controls
  1018. screen = pygame.display.set_mode\
  1019.          ((controls.Swidth, controls.Sheight),screenmode,0) #display screen
  1020. colourvals=[225,250] # colour grad,temp. color
  1021. color1=(255,255,255)
  1022. color2=(170,170,170)
  1023. color3=(100,100,100)
  1024. red=[255,0,0]
  1025.  
  1026. # no font specified-NONE
  1027. font = pygame.font.Font("resources\data\prometheanb.ttf", 22)
  1028. text1 = font.render("Score:", 2, (150,150,250))
  1029. font = pygame.font.Font("resources\data\prometheanb.ttf", 18)
  1030. text = font.render("S H I E L D", 2, [190,0,0])
  1031. text2=pygame.transform.rotozoom(text,90,1)
  1032. text = font.render("GUN TEMP.", 2,[200,200,200])
  1033. text3=pygame.transform.rotozoom(text,90,1)
  1034. font = pygame.font.Font("resources\data\prometheanb.ttf", 22)
  1035. text4= font.render("ALIEN SHIP BONUS-1000!", 2,[255,55,35])
  1036. text5= font.render("BOMBS: ", 2,[200,0,200])
  1037.  
  1038. scorestr=""
  1039. score=[0,0,0,0,0,0,0] #mil,hunthous, tenthous,thous,huns,tens,ones
  1040. scorestr= "".join(str(i) for i in score)
  1041. ##for w in score: scorestr+=str(w)
  1042. realscore=int(scorestr)
  1043. asteroids=[]
  1044. starvel=[0.1,0.1,0.05] # velocity of the different star layers
  1045. initialize_stars()
  1046.  
  1047. f=open('resources\data\scores.sg','rb')# Get highscores from disk n close file
  1048. highscores=cPickle.load(f)
  1049. f.close()
  1050.  
  1051. f=open('resources\data\saves.sg','rb')# Get save list from disk n close file
  1052. savelist=cPickle.load(f)
  1053. f.close()
  1054.  
  1055.  
  1056. intro=['special thanks to','Shanika Amarasoma and Jonathan Stockwell',\
  1057.        'Hero Space Ship graphics','Kim Lathrope','other Space Ship graphics',\
  1058.        'millionthvector.blogspot.com','Game concept, design, coding,',\
  1059.        'music composed and performed by','Diliup Gabadamudalige',\
  1060.        ' ','somewhere in the Galaxy','this could be happening.....']
  1061.  
  1062. instructions=['You are searching for rare elements in the far corners of the ',\
  1063. 'Galaxy. You have found a system abundant with rare energy globes.',\
  1064. 'Collect as much as you can while fighting the guardian Krogg ships.',\
  1065. 'The purple energy globes can be converted into Plasma bombs which',\
  1066. 'can be used to defend your ship from the Main Fighter Ship.',\
  1067. 'Spacebar-Lazer Fire      "B"-Fire Bombs      "T"-Teleport',\
  1068. 'Teleporting causes shield depletion. Continous firing will heat your',\
  1069. 'gun and overheating will prevent you from further firing.',\
  1070. '"UP"-thrust     "DOWN"-brake     "LEFT"/"RIGHT"-to rotate',\
  1071. '"P" for High Scores and pause game.',\
  1072. '"Esc" to quit game','Good Luck!',' ',\
  1073. 'Hit a key to get back to the game']
  1074.  
  1075. confirm=['Do you really want to quit?','Y to Quit',\
  1076.              'any other key to get back to the game']
  1077.  
  1078. level3=['Just as you thought it was over','and start breathing again,',\
  1079.         'your Radar picks up something','heading your way....']
  1080.  
  1081. level4=["The Kroggs who consider themselves","Masters of the Universe,",\
  1082.         "are not pleased by your daring actions.",\
  1083.         "So now it's time to meet them.","personally.."]
  1084.  
  1085. playerwins=["Congratulations!","You completed the game.",\
  1086.             'you are hereby','A  S P A C E  G L A D I A T O R !']
  1087. #------------------------------------------------------------------------------#
  1088. #                                load the sound                                #
  1089. #------------------------------------------------------------------------------#
  1090.  
  1091. Heromove=pygame.mixer.Sound('resources\snd\Thrust.ogg')
  1092. Herofire=pygame.mixer.Sound('resources\snd\LASER1.WAV')
  1093. Herofire2=pygame.mixer.Sound('resources\snd\atarifire.WAV')
  1094. enemyexp=pygame.mixer.Sound('resources\snd\enemyexplode.WAV')
  1095. alienshoot=pygame.mixer.Sound('resources\snd\photon.wav')
  1096. warn=pygame.mixer.Sound('resources\snd\warn1.ogg')
  1097. Heroexp=pygame.mixer.Sound('resources\snd\Heroexplode.wav')
  1098. collect=pygame.mixer.Sound('resources\snd\hiblip.wav')
  1099. teleportsound=pygame.mixer.Sound('resources\snd\synthfall.wav')
  1100. extralife=pygame.mixer.Sound('resources\snd\extra_life.wav')
  1101. shipbonus=pygame.mixer.Sound('resources\snd\asbp.wav')
  1102. bosscanfire=pygame.mixer.Sound('resources\snd\podipipireema.wav')
  1103. Herofirebomb=pygame.mixer.Sound('resources\snd\HBlaunch.wav')
  1104. hkollidea=pygame.mixer.Sound('resources\snd\Kollide.wav')
  1105. astexplode=pygame.mixer.Sound('resources\snd\globexplod.wav')
  1106. bossexplode=pygame.mixer.Sound('resources\snd\B1exp.wav')
  1107. bossmove=pygame.mixer.Sound('resources\snd\badboss1.wav')
  1108. invince=pygame.mixer.Sound('resources\snd\invincecollect.ogg')
  1109. alien1music="resources\snd\SGAlien1theme.ogg"
  1110. alien2music="resources\snd\SGAlien2theme.ogg"
  1111. boss1music="resources\snd\SGBoss1theme.ogg"
  1112.  
  1113. Herofirech= pygame.mixer.Channel(0)
  1114. Herothrustch=pygame.mixer.Channel(1)
  1115. bosschannel=pygame.mixer.Channel(2)
  1116. collectch=pygame.mixer.Channel(3)
  1117. aliench=pygame.mixer.Channel(4)
  1118. pygame.mixer.set_reserved(0)
  1119. pygame.mixer.set_reserved(1)
  1120. pygame.mixer.set_reserved(2)
  1121. pygame.mixer.set_reserved(3)
  1122. pygame.mixer.set_reserved(4)
  1123. Heromove.set_volume(.2); Herofire.set_volume(.2)
  1124. enemyexp.set_volume(.5); alienshoot.set_volume(0.06)
  1125. teleportsound.set_volume(.3); shipbonus.set_volume(0.9)
  1126. bosscanfire.set_volume(0.5); warn.set_volume(0.02)
  1127. hkollidea.set_volume(0.4) ;Heroexp.set_volume(0.5)
  1128. astexplode.set_volume(0.3);bossexplode.set_volume(.5)
  1129. extralife.set_volume(0.2); invince.set_volume(0.02)
  1130.  
  1131. #------------------------------------------------------------------------------#
  1132. #                              load the Graphics                               #
  1133. #------------------------------------------------------------------------------#
  1134.  
  1135. # Enemy types
  1136. alien1=pygame.image.load("resources\grfx\AlienANew.png").convert_alpha()
  1137. alien2=pygame.image.load("resources\grfx\AlienBNew.png").convert_alpha()
  1138. bossA=pygame.image.load("resources\grfx\BossA.png").convert_alpha()
  1139. bossB=pygame.image.load("resources\grfx\FAlien.png").convert_alpha()
  1140. AMSHIP1=pygame.image.load("resources\grfx\mothership1.png").convert_alpha()
  1141. # Bullets and Bombs
  1142. Herobullet=pygame.image.load("resources\grfx\lazerH.png").convert_alpha()
  1143. alienbullet=pygame.image.load("resources\grfx\lazerA.png").convert_alpha()
  1144. Herobomb=pygame.image.load("resources\grfx\item4.png").convert_alpha()
  1145. bossbomb1=pygame.image.load("resources\grfx\BOSBOM.png").convert_alpha()
  1146. # Bomb & Bullet xplosions
  1147. alxplode=pygame.image.load("resources\grfx\explosiondg002.png").convert_alpha()#alien
  1148. al2xplode=pygame.image.load("resources\grfx\AL2EX.png").convert_alpha()#alien
  1149. bsxplode=pygame.image.load("resources\grfx\Boss1explode.png").convert_alpha()#boss
  1150. BAexpld=pygame.image.load("resources\grfx\BaddestAlienexp.png").convert_alpha()#last Alien
  1151. Abltxpld=pygame.image.load("resources\grfx\Abx.png").convert_alpha()#Ablt
  1152. Hbltxpld=pygame.image.load("resources\grfx\Hbx.png").convert_alpha()#Hblt
  1153. HBxpld=pygame.image.load("resources\grfx\explosiondg003.png").convert_alpha()#HBOMB
  1154. BBxpld=pygame.image.load("resources\grfx\explosiondg004.png").convert_alpha()#BOSBOMB
  1155. AMS1EXP=pygame.image.load("resources\grfx\mothershipexp.png").convert_alpha()
  1156. # Different collectibles
  1157. Gglobe=pygame.image.load("resources\grfx\item1.png").convert_alpha()
  1158. Rglobe=pygame.image.load("resources\grfx\item2.png").convert_alpha()
  1159. Oglobe=pygame.image.load("resources\grfx\item3.png").convert_alpha()
  1160. Pglobe=pygame.image.load("resources\grfx\item4.png").convert_alpha()
  1161. clusterbomb=pygame.image.load("resources\grfx\item5.png").convert_alpha()
  1162. Bglobe=pygame.image.load("resources\grfx\item6.png").convert_alpha()
  1163. Mglobe=pygame.image.load("resources\grfx\item7.png").convert_alpha()
  1164. CBombexp=pygame.image.load("resources\grfx\clusterbombex.png").convert_alpha()
  1165. CBombe=pygame.image.load("resources\grfx\item8.png").convert_alpha()
  1166. # Asteroids
  1167. meteor=pygame.image.load("resources\grfx\BIGasters.png").convert_alpha()
  1168. meteor2=pygame.image.load("resources\grfx\SMALLasters.png").convert_alpha()
  1169. meteor3=pygame.image.load("resources\grfx\TINYasters.png").convert_alpha()
  1170. metex1=pygame.image.load("resources\grfx\AEXL.png").convert_alpha()
  1171. metex2=pygame.image.load("resources\grfx\AEXM.png").convert_alpha()
  1172. metex3=pygame.image.load("resources\grfx\AEXS.png").convert_alpha()
  1173.  
  1174. title=pygame.image.load("resources\grfx\SGtitle2.png").convert_alpha()
  1175. prana=pygame.image.load("resources\grfx\HeroTF.png").convert_alpha()
  1176. podiprana=pygame.transform.scale(prana,(25,25))
  1177. life=pygame.transform.rotate(podiprana, 90)
  1178.  
  1179. images=Load_images()
  1180. #------------------------------------------------------------------------------#
  1181. #                          Prepare Game Characters                             #
  1182. #------------------------------------------------------------------------------#
  1183.  
  1184. # image1 image2 speed status size,life,kind,firerate, framesize
  1185. Hbullet=Gun(Herobullet,Hbltxpld,9,"S",9,45,"HB",4,25)# Hero BULLET
  1186. Abullet=Gun(alienbullet,Abltxpld,8.3,"S",9,40,"AB",4,25)# ALIEN BULLET
  1187. HBOMB=Gun(Herobomb,HBxpld,8,"S",25,100,"HM",6,100)# Hero BOMB
  1188. BBOMB=Gun(bossbomb1,BBxpld,7,"S",20,100,"BM",6,50)# BOSS BOMB
  1189. CBOMB=Gun(CBombe,CBombexp,6,"S",20,250,"HM",10,250)# CBOMB
  1190.  
  1191. #1-scale, 2-posx,3-posy,4-status,5-lives,6-bonus\
  1192. #(7-extra life,enemycount,bonus,bombs),8-frrate,-frsize,9-bonus message
  1193. Hero =HeroObject(46,controls.Swidth/2,controls.Sheight/2,"N",6,\
  1194.                  ["one",20,"S",0],[0,"D"],5,64)
  1195. #new attribute created for the clusterbomb in the class Gun
  1196. CBOMB.explode=100 # cluster bomb will explode when this turn 0
  1197.  
  1198. # BOSS1 initialized
  1199. #image,image2(expld),scale,x,y,life,firerate,frame rate,frame size,status
  1200. bosx=-100
  1201. bosy=random.randrange(50,controls.Sheight-100)
  1202. # create boss1 off screen but passive-P
  1203. boss1=EnemyObject(bossA,bsxplode,80,bosx,bosy,100,30,8,400,"P")
  1204. mx=random.randrange(50,100,5)
  1205. Mothership1=EnemyObject(AMSHIP1,AMS1EXP,110,mx,0,100.0,0,10,250,"P")
  1206. Mothership1.heading=90
  1207. Mothership1.speed=2
  1208. controls.musicfader=pygame.mixer.music.get_volume()
  1209. Hero.setHeading(90)
  1210. controls.instructions=True
  1211. player_instructions(intro,10)
  1212.  
  1213. #------------------------------------------------------------------------------#
  1214. #                             MAIN GAME LOOP                                   #
  1215. #------------------------------------------------------------------------------#
  1216.  
  1217.  
  1218. #------------------------------------------------------------------------------#
  1219. #                           LEVEL 1  THE ENERGY GLOBES                         #                                     #
  1220. #------------------------------------------------------------------------------#
  1221. while True:
  1222.     timestamp=time.asctime(time.localtime())[4:]
  1223.  
  1224.     Hero.getrectarea()
  1225.     # reduce the size of the Hero rect by 10 pixels
  1226.     Hero.rectarea=Hero.rectarea.inflate(-10,-10)
  1227.     controls.gamecounter+=1
  1228.     controls.glt_sec=clock.tick(50)/1000.0# time taken to run game loop in seconds
  1229. ##    controls.timegone+=controls.glt_sec
  1230.     constant=controls.gamespeed*controls.glt_sec
  1231.     scorestr= "".join(str(i) for i in score)
  1232.     realscore=int(scorestr)
  1233. #bonus 1000 for every 20 alien ships
  1234.     if controls.enemycount>=Hero.bonus[1]:
  1235.         score[3]+=1
  1236.         Hero.bonus[1]+=20
  1237.         Hero.bonus[2]="M"
  1238. # extra life for every 20000 points up to max 60000 and 3 lives
  1239.     if Hero.bonus[0]=="one" and realscore>=20000:
  1240.         Hero.lives+=1
  1241.         extralife.play()
  1242.         Hero.bonus[0]="two"
  1243.     elif Hero.bonus[0]=="two" and realscore>=40000:
  1244.         Hero.lives+=1
  1245.         extralife.play()
  1246.         Hero.bonus[0]="three"
  1247.     elif Hero.bonus[0]=="three" and realscore>=60000:
  1248.         Hero.lives+=1
  1249.         extralife.play()
  1250.         Hero.bonus[0]="four"
  1251. # short period where Hero has no damage from enemy fire
  1252.     Hero.nodamage-=1
  1253.     if Hero.nodamage<=0:
  1254.         Hero.nodamage=0
  1255.         Hero.shieldcolour=[250,250,250]
  1256. # increase number of Alien1s by one every 750 increase of score
  1257.     if controls.globes<>'Palive' and realscore>750 and\
  1258.        controls.gamestate=="Level 1":
  1259.         controls.no_of_enemies=realscore//750
  1260.     if controls.no_of_enemies>controls.enemy_limit:
  1261.         controls.no_of_enemies=controls.enemy_limit
  1262.  
  1263.     pygame.event.pump()
  1264.     keys = pygame.key.get_pressed()
  1265.     if keys[K_ESCAPE]:
  1266.         really_quit()
  1267.        
  1268.     for event in pygame.event.get():
  1269.         if event.type==KEYDOWN:
  1270.             # PAUSE GAME,high scores and instructions
  1271.             if event.key==K_p:
  1272.                 controls.pause=not controls.pause
  1273.  
  1274.             if event.key==K_t:              # TELEPORT
  1275.                 teleportsound.play()
  1276.                 Hero.status="T"
  1277.  
  1278.             if event.key==K_f and screenmode==0: # toggle fullscreen on/off
  1279.                 screenmode=FULLSCREEN
  1280.                 screen = pygame.display.set_mode((Swidth, Sheight),screenmode,0)
  1281.             elif event.key==K_f and screenmode==FULLSCREEN:
  1282.                 screenmode=0
  1283.                 screen = pygame.display.set_mode((Swidth, Sheight),screenmode,0)
  1284.  
  1285. # bomb count and launch status D-dormant R-ready to launch L-launched
  1286.             if controls.globes=='Palive' and Hero.bombs[0]>0 \
  1287.                and Hero.status!="E"\
  1288.                and Hero.bombs[1]=="D" and event.key==K_b :
  1289.                 Hero.bombs[1]="R" # get bomb ready
  1290.                 Hero.bombs[0]-=1
  1291.                 Hero.BMag.append(copy.copy(HBOMB))
  1292.                 Herofirebomb.play()
  1293.                 Hero.bombs[1]="L"
  1294.                 Hero.BMag[-1].fire(Hero)     # BOMB FIRED
  1295.  
  1296.             if controls.CBomb=='C' and event.key==K_b:
  1297.                 Herofirebomb.play()
  1298.                 for CB in Hero.CBMag:
  1299.                     # Cluster Bombs FIRED
  1300.                     controls.CBangle+=1
  1301.                     CB.fireBombCluster(Hero,\
  1302.                                        ( controls.CBangle*36),\
  1303.                                        ( controls.CBangle*36))
  1304.                     CB.status='CF'
  1305.                     controls.CBomb="F"
  1306.  
  1307.     if Hero.status=="T":
  1308.         Hero.teleport()
  1309.         Hero.life-=1
  1310.     # MOVE THE Hero
  1311.     if keys[K_LEFT]:
  1312.         Hero.setHeading(Hero.heading+4)
  1313.     if keys[K_RIGHT]:
  1314.         Hero.setHeading(Hero.heading-4)
  1315.     if keys[K_UP]: # accellerate!
  1316.         Herothrustch.play(Heromove)
  1317.         Hero.engines=True
  1318.         Hero.speedx+=Hero.thrust*math.cos(math.radians(Hero.heading))*constant
  1319.         Hero.speedy+=Hero.thrust*math.sin(math.radians(Hero.heading))*constant
  1320.         if Hero.speedx>=Hero.speedlimit: Hero.speedx=Hero.speedlimit
  1321.         if Hero.speedx<=-Hero.speedlimit: Hero.speedx=-Hero.speedlimit
  1322.         if Hero.speedy>=Hero.speedlimit: Hero.speedy=Hero.speedlimit
  1323.         if Hero.speedy<=-Hero.speedlimit: Hero.speedy=-Hero.speedlimit
  1324.     else:
  1325.         Heromove.stop()
  1326.         Hero.engines=False
  1327.         Hero.speedx-=0
  1328.         Hero.speedy-=0
  1329.     if keys[K_DOWN]: #brakes! :-)
  1330.         Hero.speedx*=0.97
  1331.         Hero.speedy*=0.97
  1332.  
  1333.     if Hero.life>0:
  1334.         Hero.move(Hero.speedx, Hero.speedy)
  1335.         Hero.life+=.04 # slow buildup of shield
  1336.     if Hero.life>=450:
  1337.         Hero.life=450
  1338.     if Hero.life<=0 and Hero.status=="N":
  1339.         Hero.life=0
  1340.         Heroexp.play()
  1341.         Hero.status="E"
  1342.     if Hero.status=="E":
  1343.         Hero.explode()
  1344.         for e in controls.Alien1: # when Hero explode enemy moves away
  1345.             e.follow(e,4)
  1346.             e.firerate=30
  1347.             e.update()
  1348.     if Hero.status=="S":
  1349.         Hero.reinit-=1
  1350.     if Hero.life<40 and Hero.life>0: # warn that shield is low
  1351.         warn.play()
  1352.     if Hero.reinit<=0:
  1353.         Hero.lives-=1
  1354.     # Hero IS RE-SPAWNED
  1355.         tempHerobombs=Hero.bombs
  1356.         tempHeroCBMag=Hero.CBMag
  1357.         Hero =HeroObject(45,controls.Swidth/2,\
  1358.                          controls.Sheight/2,"N",Hero.lives,\
  1359.                          Hero.bonus,Hero.bombs, Hero.frrate, Hero.frsize)
  1360.         Hero.setHeading(90)
  1361.         Hero.CBMag=tempHeroCBMag
  1362.         Hero.bombs=tempHerobombs
  1363.         for e in controls.Alien1:
  1364.             e.firerate=30
  1365.         if controls.globes=='Palive':
  1366.             boss1.firerate=30
  1367. # draw the Hero ship shield
  1368.     if Hero.nodamage>0:
  1369.         pygame.draw.circle(screen, (Hero.shieldcolour[2]*random.random(),\
  1370.                                     Hero.shieldcolour[1]*random.random(),\
  1371.                                     Hero.shieldcolour[0]*random.random()), \
  1372.                            (int(Hero.x),int(controls.Sheight-Hero.y)),Hero.\
  1373.                            shieldradius, 0)
  1374.         Hero.shieldcolour[0]-=1
  1375.         for x in xrange(2):
  1376.             if Hero.shieldcolour[x]<=0:
  1377.                 Hero.shieldcolour[x]=0
  1378.                 Hero.shieldcolour[x+1]-=1
  1379.         if Hero.shieldcolour[2]<=0:
  1380.             Hero.shieldcolour[2]=0
  1381.         Hero.shieldradius+=1
  1382.         if Hero.shieldradius>28:
  1383.             Hero.shieldradius=18
  1384.  
  1385. # HERO SHOOT
  1386.     if keys[K_SPACE] and Hero.guntemp<=74 and Hero.status=="N":
  1387.         Hero.firerate-=1 # fire the gun
  1388.         if Hero.firerate<=0 and controls.TRIPLEguntime<=0:
  1389.             Hero.CMag.append(copy.copy(Hbullet))
  1390.             Herofirech.play(Herofire)
  1391.             Hero.guntemp+=0.4 # GUN TEMPERATURE increases
  1392.             colourvals[1]-=10 # GUN TEMP. COLOUR changes
  1393.             if colourvals[1]<1:
  1394.                 colourvals[1]=1
  1395.             Hero.CMag[-1].fire(Hero)
  1396.             Hero.firerate=Hero.maxfirerate
  1397. # IF HERO CATCHES TRIPLE GUN BOOSTER PORT AND STARBOARD GUNS ACTIVATE
  1398.         elif Hero.firerate<=0 and controls.TRIPLEguntime>0:
  1399.             Hero.firerate=Hero.maxfirerate
  1400.             Hero.CMag.append(copy.copy(Hbullet))
  1401.             Hero.LMag.append(copy.copy(Hbullet))
  1402.             Hero.RMag.append(copy.copy(Hbullet))
  1403.             Herofirech.play(Herofire)
  1404.             Herofirech.play(Herofire2)
  1405.             Herofirech.play(Herofire)
  1406.             Hero.guntemp+=0.3 # GUN TEMPERATURE increases
  1407.             colourvals[1]-=20 # GUN TEMP. COLOUR changes
  1408.             if colourvals[1]<1:
  1409.                 colourvals[1]=1
  1410.             Hero.CMag[-1].fire(Hero)
  1411.             Hero.LMag[-1].fireL(Hero)
  1412.             Hero.RMag[-1].fireR(Hero)
  1413.             controls.TRIPLEguntime-=1
  1414.             if controls.TRIPLEguntime==0:
  1415.                 controls.TRIPLEguntime=0
  1416. # HERO GUN COOLS WHEN NOT FIRING
  1417.     if not keys[K_SPACE]:
  1418.         Hero.guntemp-=.2
  1419.         if Hero.guntemp<0:
  1420.             Hero.guntemp=0
  1421.         colourvals[1]+=5
  1422.         if colourvals[1]>100:
  1423.             colourvals[1]=100
  1424. #------------------------------------------------------------------------------#
  1425. #                               THE ASTEROIDS                                  #
  1426. #------------------------------------------------------------------------------#
  1427.     #image,image2,x,y,life,width,xfr,frsize,noframes.
  1428.     #...status(L-Large, M-Medium, S=Small),
  1429.     #status-M-moving, E-explode, D-destroyed
  1430.     if controls.asteroidswitch=="ON":
  1431.         if len(asteroids)<controls.asteroidlimit and \
  1432.            controls.asteroidcount<=controls.asteroidamount:
  1433.             x,y,h=Asteroid_spawn()
  1434.             asteroid=Asteroids(meteor,metex1,x,y,5,1024,8,128,60,"L","M")
  1435.             asteroid.heading=h
  1436.             asteroids.append(asteroid)
  1437. #------------------------------------------------------------------------------#
  1438. #                                   THE POWERUPS                               #
  1439. #------------------------------------------------------------------------------#
  1440.  
  1441. #THE INVINCIBILITY POWERUP
  1442.     if realscore>15000 and Hero.nodamage<=0 and controls.invincibility==False:
  1443.         controls.PUcount[0]+=1
  1444.     if controls.PUcount[0]%500==0 and controls.invincibility==False:
  1445.         controls.PUcount[0]+=1
  1446.         controls.invincibility=True
  1447.         # image,x,y,scale,no of items spawned
  1448.         invincibility=Collectibles(Bglobe,controls.Swidth*random.random(),\
  1449.                            controls.Sheight*random.random(),35,1)
  1450.         invincibility.speed=0.7
  1451.         invincibility.life=500
  1452.     if controls.invincibility==True and invincibility.life<=0:
  1453.         controls.invincibility=False
  1454.         controls.PUcount[0]=1 # reset the invincibilty counter
  1455.  
  1456. # THE TRIPLE GUN POWERUP
  1457.     if controls.TRIPLEgun==False and controls.gamestate<>"Level 1" \
  1458.        and controls.gamestate<>"Level 2":
  1459.         controls.PUcount[1]+=1
  1460.     if controls.PUcount[1]%750==0 and controls.TRIPLEgun==False:
  1461.         controls.PUcount[1]+=1
  1462.         controls.TRIPLEgun=True
  1463.         # image,x,y,scale,no of items spawned
  1464.         TRIPLEgun=Collectibles(Mglobe,controls.Swidth*random.random(),\
  1465.                            controls.Sheight*random.random(),45,1)
  1466.         TRIPLEgun.speed=0.7
  1467.         TRIPLEgun.life=500
  1468.     if controls.TRIPLEgun==True and TRIPLEgun.life<=0:
  1469.         controls.TRIPLEgun=False
  1470.         controls.PUcount[1]=1 # reset the TRIPLEgun counter
  1471.  
  1472. # THE CBOMB (Cluster Bomb) GENERATOR
  1473.     if controls.gamestate!='Level 2' and realscore>=25000\
  1474.        and controls.CBomb==False:
  1475.         controls.PUcount[2]+=1
  1476.     if controls.PUcount[2]%1000==0 and controls.CBomb==False:
  1477.         controls.PUcount[2]+=1
  1478.         controls.CBomb=True
  1479.         # image,x,y,scale,no of items spawned
  1480.         CBombpup=Collectibles(clusterbomb,controls.Swidth*random.random(),\
  1481.                            controls.Sheight*random.random(),50,1)
  1482.         CBombpup.speed=0.7
  1483.         CBombpup.life=300
  1484.     '''if CBomb NOT caught while alive reset flags to launch again'''
  1485. #------------------------------------------------------------------------------#
  1486. #                             THE COLLECTIBLES                                 #
  1487. #------------------------------------------------------------------------------#
  1488.     if controls.globes=='Green':
  1489.         # call function with globe image and speed
  1490.         spawn_globes(Gglobe, 0.3,10,controls.globequantity[0])
  1491.         controls.globes='Galive'
  1492.  
  1493.     if controls.globes=='Galive' and \
  1494.        controls.collected==controls.globequantity[0]:
  1495.         controls.collected=0
  1496.         controls.globes='Red'
  1497.  
  1498.     if controls.globes=='Red':
  1499.         controls.globelist=[]
  1500.         spawn_globes(Rglobe, 0.4,15,controls.globequantity[1])
  1501.         controls.collected=0
  1502.         controls.globes='Ralive'
  1503.  
  1504.     if controls.globes=='Ralive' and \
  1505.        controls.collected==controls.globequantity[1]:
  1506.         controls.collected=0
  1507.         controls.globes='Orange'
  1508.  
  1509.     if controls.globes=='Orange':
  1510.         if controls.asteroidswitch=="OFF":
  1511.             controls.asteroidswitch="ON" # bring on asteroids 1st time level 1
  1512.         controls.globelist=[]
  1513.         spawn_globes(Oglobe, 0.5,20,controls.globequantity[2])
  1514.         controls.collected=0
  1515.         controls.globes='Oalive'
  1516.  
  1517.     if controls.globes=='Oalive' and \
  1518.        controls.collected==controls.globequantity[2]:
  1519.         controls.collected=0
  1520.         controls.globes='Purple'
  1521.  
  1522.     if controls.globes=='Purple':
  1523.         controls.globelist=[]
  1524.         spawn_globes(Pglobe, 0.6,25,controls.globequantity[3])
  1525.         controls.collected=0
  1526.         controls.globes='Palive'
  1527. #------------------------------------------------------------------------------#
  1528. #                            THE 1ST. ALIEN WAVE                               #
  1529. #------------------------------------------------------------------------------#
  1530.  
  1531.     #image,image2(expld),scale,x,y,life,firerate,frame rate,frame size,status
  1532.     if len(controls.Alien1)<controls.no_of_enemies \
  1533.        and controls.gamecounter%300==0 and controls.globes!='Palive' \
  1534.        and controls.gamestate=='Level 1':
  1535.         x,y,h=random_xyheading()
  1536.         badET=EnemyObject(alien1,alxplode,35,30+x,y,30,30,6,100,"M")
  1537.         badET.heading=h
  1538.         badET.radar=220
  1539.         controls.Alien1.append(badET)
  1540.     if len(controls.Alien1)>0 and controls.music=='theme':
  1541.         pygame.mixer.music.load(alien1music)
  1542.         pygame.mixer.music.set_volume(0.3)
  1543.         pygame.mixer.music.play(-1)
  1544.         controls.music='level1alien1'
  1545.  
  1546.  
  1547. #------------------------------------------------------------------------------#
  1548. #                      L E V E L  2 THE ALIEN OVERLORD                         #
  1549. #------------------------------------------------------------------------------#
  1550.  
  1551.     if controls.globes=='Palive' and controls.enemystate=="boss off"\
  1552.        and controls.gamestate=='Level 1':
  1553.         boss1.radar=controls.Swidth/2-100
  1554.         boss1.mag=40 # shooting delay
  1555.         controls.enemystate="boss on"
  1556.         boss1.status='M'
  1557.         controls.gamestate="Level 2"
  1558.     if controls.enemystate=='boss on' and controls.music<>'boss music':
  1559.         pygame.mixer.music.load(boss1music)
  1560.         pygame.mixer.music.set_volume(0.3)
  1561.         pygame.mixer.music.play(-1)
  1562.         controls.music='boss music'
  1563.  
  1564.     if controls.enemystate=='boss on' and boss1.status<>"E":
  1565.         boss1.getrectarea()
  1566.         # make the boss rect 20 pixels smaller
  1567.         boss1.rectarea=boss1.rectarea.inflate(-20,-20)
  1568. ##        pygame.draw.rect(screen, (0,255,0), boss1.rectarea,2)
  1569.         boss1.firerate-=1
  1570.         if boss1.firerate==0 and Hero.life>0:
  1571.             controls.BossBombMag.append(copy.copy(BBOMB))
  1572.             # fire only if Hero  gets closer tham this
  1573.             if abs(Hero.x-boss1.x)<boss1.radar and\
  1574.                abs(Hero.y-boss1.y)<boss1.radar:
  1575.                 controls.BossBombMag[-1].fire(boss1)
  1576.                 bosschannel.play(bosscanfire)
  1577.             boss1.firerate=30# slow down the alien fire rate
  1578.         boss1.mag-=1
  1579.         if boss1.life>0:
  1580.             boss1.life+=0.01 # self healing ability of boss
  1581.         if boss1.life>=100:
  1582.             boss1.life=100
  1583.         if boss1.mag<=-50:
  1584.             boss1.mag=10
  1585.         if boss1.life<=0:
  1586.             boss1.life=0
  1587.             boss1.status="E"
  1588.             bosschannel.play(bossexplode)
  1589.             score[3]+=5
  1590.     if boss1.status=="E":
  1591.         boss1.explode()
  1592.  
  1593.     if boss1.status=='D' and controls.spendtime<=100:
  1594.         controls.spendtime+=1
  1595.         controls.enemystate='REST'
  1596.  
  1597.     if boss1.status=='D' and controls.spendtime>=100:
  1598.         controls.gamestate="Level 3"
  1599.         controls.asteroidswitch="OFF" # no asteroids in level 3
  1600.         controls.globes='OFF'
  1601.         Mothership1.status="M"
  1602.         controls.globelist=[]
  1603.         Hero.BMag=[]
  1604.         Hero.bombs=[0,"D"]
  1605.         controls.counter=0
  1606.         controls.no_of_enemies=12 # second round of aliens
  1607.         controls.spendtime=0
  1608.         boss1.status='boss1 dest'
  1609.         player_instructions(level3,8) # describe level 3 to player
  1610.  
  1611. #------------------------------------------------------------------------------#
  1612. #                         L E V E L  3 THE MOTHERSHIP                          #                              #
  1613. #------------------------------------------------------------------------------#
  1614. # MSA-Mother Ship Active   MLA2- Mothership Launch Alien2
  1615.  
  1616. #                                ALIEN WAVE 2                                  #
  1617.     if controls.enemystate=='REST' and controls.counter <25:
  1618.         controls.counter+=1 # rest a while
  1619.  
  1620.     if controls.enemystate=='REST' and controls.counter >=25:
  1621.         controls.counter=0
  1622.         controls.enemystate='MLA2'
  1623.  
  1624.     if controls.enemystate=='MLA2' and Mothership1.status=="M":
  1625.         Mothership1.getrectarea()
  1626.         Mothership1.heading-=5
  1627.         Mothership1.y+=1
  1628.         controls.counter+=1
  1629. #------------------------------------------------------------------------------#
  1630. #                      L E V E L  3 THE 2nd.ALIEN WAVE                         #                              #
  1631. #------------------------------------------------------------------------------#
  1632.  
  1633.         if controls.counter>=25:
  1634.             if len(controls.Alien2)<controls.no_of_enemies: # 10 alien2's
  1635. ##image,image2(expld),scale,x,y,life,firerate,frame rate,frame size,status
  1636.                 anotherbadET=EnemyObject(alien2,al2xplode,44,Mothership1.x,\
  1637.                                   Mothership1.y,32,25,6,100,"M")
  1638.                 anotherbadET.heading=random.randrange(0,360,10)
  1639.                 anotherbadET.radar=280
  1640.                 controls.Alien2.append(anotherbadET)
  1641.             controls.counter=0
  1642.  
  1643.     if Mothership1.y>=controls.Sheight+Mothership1.scale and\
  1644.        Mothership1.status<>"D" and len(controls.Alien2)<=controls.no_of_enemies:
  1645.         mx=random.randrange(75,controls.Swidth-75,10)
  1646.         Mothership1=EnemyObject(AMSHIP1,AMS1EXP,110,mx,0,Mothership1.life,\
  1647.                                 0,10,250,"M")
  1648.         Mothership1.speed=2
  1649.         controls.enemystate=='MSA'
  1650.     if len(controls.Alien2)<=5 and controls.enemystate=='MSA' \
  1651.        and Mothership1.status=="M":
  1652.         controls.enemystate='MLA2'
  1653.  
  1654.     if len(controls.Alien2)>0 and controls.music=='boss music':
  1655.         pygame.mixer.music.load(alien2music)
  1656.         pygame.mixer.music.set_volume(0.3)
  1657.         pygame.mixer.music.play(-1)
  1658.         controls.music='level2alien2'
  1659.  
  1660.     if Mothership1.life<=0 and Mothership1.status=="M":
  1661.         Mothership1.life=0
  1662.         Mothership1.status="E"
  1663.         score[3]+=5
  1664.     if Mothership1.status=="E":
  1665.         Mothership1.explode()
  1666.  
  1667.     if Mothership1.status=="D":
  1668.         Mothership1.status="DESTROYED"
  1669.         Mothership1.x=-4000
  1670.         Mothership1.y=-4000
  1671.  
  1672.     if Mothership1.status=="DESTROYED" and \
  1673.        len(controls.Alien2)==0 and controls.gamestate=="Level 3":
  1674.         controls.spendtime+=1
  1675.  
  1676.     if Mothership1.status=="DESTROYED" and controls.spendtime>=200:
  1677.         controls.enemystate="REST 2"
  1678.         controls.asteroidswitch="ON" # bring on more asteroids for level 3
  1679.         controls.asteroidcount=0 # respawn asteroids for this level with more
  1680.         controls.asteroidlimit=6 # should be 6
  1681.         controls.asteroidamount=100 # level 3 - double the amount of asteroids
  1682.         controls.gamestate="Level 4"
  1683.         controls.spendtime=0
  1684.         player_instructions(level4,7) # describe level 4 to player
  1685.  
  1686. #------------------------------------------------------------------------------#
  1687. #                           L E V E L  4 THE FINAL BATTLE                      #
  1688. #------------------------------------------------------------------------------#
  1689. # the asteroids are activated again and the FinalAien crafts are deployed
  1690.  
  1691.     if controls.gamestate=="Level 4" and controls.enemystate=="REST 2":
  1692.         for a in xrange(controls.FAamount): # 6 Final Aliens
  1693.             x,y,h=random_xyheading()
  1694.     #image,image2(expld),scale,x,y,life,firerate,frame rate,frame size,status
  1695.             BaddestET=EnemyObject(bossB,BAexpld,50,30+x,y,50,30,8,400,"M")
  1696.             BaddestET.heading=h
  1697.             BaddestET.radar=controls.Sheight/2
  1698.             controls.FinalAliens.append(BaddestET)
  1699.         controls.enemystate="FINAL BATTLE"
  1700.         controls.enemycount=0
  1701.  
  1702. #------------------------------------------------------------------------------#
  1703. #                           Fire the Alien Guns                                #
  1704. #------------------------------------------------------------------------------#
  1705.  
  1706.     for e in controls.Alien1:
  1707.         e.firerate-=1
  1708.         if e.firerate<=0 and Hero.life>0 and e.state=="normal":
  1709.             controls.AlienMag.append(copy.copy(Abullet))
  1710.             # fire only if Hero gets closer than this
  1711.             if abs(Hero.x-e.x)<e.radar and abs(Hero.y-e.y)<e.radar:
  1712.                 controls.AlienMag[-1].fire(e)
  1713.                 alienshoot.play()
  1714.             e.firerate=8# slow down the alien fire rate
  1715.  
  1716.     for e in controls.Alien2:
  1717.         e.firerate-=1
  1718.         if e.firerate<=0 and Hero.life>0 and e.state=="normal":
  1719.             controls.Alien2Mag.append(copy.copy(Abullet))
  1720.             if abs(Hero.x-e.x)<e.radar and abs(Hero.y-e.y)<e.radar:
  1721.                 controls.Alien2Mag[-1].fire(e)
  1722.                 alienshoot.play()
  1723.             e.firerate=7
  1724.  
  1725.     for e in controls.FinalAliens:
  1726.         e.firerate-=1
  1727.         if e.firerate<=0 and Hero.life>0 and e.state=="normal":
  1728.             controls.Alien2Mag.append(copy.copy(Abullet))
  1729.             if abs(Hero.x-e.x)<e.radar and abs(Hero.y-e.y)<e.radar:
  1730.                 controls.Alien2Mag[-1].fire(e)
  1731.                 alienshoot.play()
  1732.             e.firerate=7
  1733.  
  1734. #-----------------------------------------------------------------------------#
  1735. #                                 COLLISIONS                                  #
  1736. #-----------------------------------------------------------------------------#
  1737.     alien1INF_rects=[]
  1738.     alien1DEF_rects=[]
  1739.     alien2INF_rects=[]
  1740.     alien2DEF_rects=[]
  1741.     FalienINF_rects=[]
  1742.     FalienDEF_rects=[]
  1743.     globe_rects=[]
  1744.     asteroids_rects=[]
  1745.     asterINF_rects=[]
  1746.     font = pygame.font.Font("resources\data\prometheanb.ttf", 10)
  1747. #------------------------------------------------------------------------------#
  1748. #                       HERO COLLIDE WITH ALIEN SHIPS                          #
  1749. #------------------------------------------------------------------------------#
  1750.  
  1751. #------------------------------------------------------------------------------#
  1752. #                             THE 1ST. ALIEN WAVE                              #
  1753. #------------------------------------------------------------------------------#
  1754.  
  1755.     for e in controls.Alien1: # 1st WAVE of ALIENS
  1756.         e.getrectarea()
  1757.         """get DEFLATED rects as enemy surface is not a perfect square and the
  1758.        sprite rect will be large"""
  1759.         DEFrect=e.rectarea.inflate(-e.safearea, -e.safearea)
  1760.         alien1DEF_rects.append(DEFrect)
  1761.         """ get INFLATED rect list for same type collisions (STC)"""
  1762.         INFrect=e.rectarea.inflate(e.safearea,e.safearea)
  1763.         alien1INF_rects.append(INFrect)
  1764. ##        pygame.draw.rect(screen, (255,0,255), INFrect,2) # draw INFLATED rect
  1765.         Alcollide=Hero.rectarea.collidelist(alien1DEF_rects)
  1766.         if not Alcollide==-1 and Hero.nodamage<=0 and\
  1767.            Hero.life>0:
  1768.             Hero.life-=.4
  1769.             hkollidea.play()
  1770.         else:
  1771.             hkollidea.stop()
  1772.         if not Alcollide==-1 and Hero.nodamage>0:
  1773.             controls.Alien1[Alcollide].life-=1
  1774.  
  1775. #------------------------------------------------------------------------------#
  1776. #                       ALIEN1 colliding with each other                       #
  1777. #------------------------------------------------------------------------------#
  1778.     for i, e in enumerate(alien1INF_rects):
  1779.         A1INFlist=e.collidelistall(alien1INF_rects)
  1780.         for j in A1INFlist:
  1781.             if j==i:
  1782.                 continue # this prevents self collision
  1783.             dx=controls.Alien1[i].centerx-controls.Alien1[j].centerx
  1784.             dy=controls.Alien1[i].centery-controls.Alien1[j].centery
  1785.             distance=math.hypot(dx,dy)
  1786.             heading=.5*math.pi+(math.atan2(dy,dx))
  1787.             if distance<=controls.Alien1[j].safearea+\
  1788.                controls.Alien1[i].safearea:
  1789.                 controls.Alien1[i].heading+=2
  1790.                 controls.Alien1[i].x-=math.sin(heading)
  1791.                 controls.Alien1[i].y+=math.cos(heading)
  1792.                 controls.Alien1[j].heading-=2
  1793.                 controls.Alien1[j].x+=math.sin(heading)
  1794.                 controls.Alien1[j].y-=math.cos(heading)
  1795.             if distance<=controls.Alien1[j].scale/2+controls.Alien1[i].scale/2:
  1796.                 controls.Alien1[i].x-=1
  1797.                 controls.Alien1[j].x-=1
  1798.  
  1799. #------------------------------------------------------------------------------#
  1800. #                             THE 2ND. ALIEN WAVE                              #
  1801. #------------------------------------------------------------------------------#
  1802.  
  1803.     for e in controls.Alien2: # 2nd WAVE of ALIENS
  1804.         e.getrectarea()
  1805.         DEFrect=e.rectarea.inflate(-e.safearea, -e.safearea)
  1806.         alien2DEF_rects.append(DEFrect)
  1807.         INFrect=e.rectarea.inflate(e.safearea,e.safearea)
  1808.         alien2INF_rects.append(INFrect)
  1809.         Al2collide=Hero.rectarea.collidelist(alien2DEF_rects)
  1810.         if not Al2collide==-1 and Hero.nodamage<=0 and\
  1811.            Hero.life>0:
  1812.             Hero.life-=.2
  1813.             hkollidea.play()
  1814.         else:
  1815.             hkollidea.stop()
  1816.         if not Al2collide==-1 and Hero.nodamage>0:
  1817.             controls.Alien2[Al2collide].life-=1
  1818.  
  1819. #------------------------------------------------------------------------------#
  1820. #                       ALIEN2 colliding with each other                       #
  1821. #------------------------------------------------------------------------------#
  1822.  
  1823.     for i, e in enumerate(alien2INF_rects):
  1824.         A2INFlist=e.collidelistall(alien2INF_rects)
  1825.         for j in A2INFlist:
  1826.             if j==i:
  1827.                 continue
  1828.             dx=controls.Alien2[i].centerx-controls.Alien2[j].centerx
  1829.             dy=controls.Alien2[i].centery-controls.Alien2[j].centery
  1830.             distance=math.hypot(dx,dy)
  1831.             heading=.5*math.pi+(math.atan2(dy,dx))
  1832.             if distance<=controls.Alien2[j].safearea+controls.Alien2[i].safearea:
  1833.                 controls.Alien2[i].heading+=2
  1834.                 controls.Alien2[i].x-=math.sin(heading)
  1835.                 controls.Alien2[i].y+=math.cos(heading)
  1836.                 controls.Alien2[j].heading-=2
  1837.                 controls.Alien2[j].x+=math.sin(heading)
  1838.                 controls.Alien2[j].y-=math.cos(heading)
  1839.             if distance<=controls.Alien2[j].scale/2+controls.Alien2[i].scale/2:
  1840.                 controls.Alien2[i].x-=1
  1841.                 controls.Alien2[j].x-=1
  1842.  
  1843. #------------------------------------------------------------------------------#
  1844. #                              THE FINAL ALIENS                                #
  1845. #------------------------------------------------------------------------------#
  1846.  
  1847.     for e in controls.FinalAliens: # final WAVE of ALIENS
  1848.         e.getrectarea()
  1849.         DEFrect=e.rectarea.inflate(-e.safearea,-e.safearea)
  1850.         FalienDEF_rects.append(e.rectarea)
  1851.         INFrect=e.rectarea.inflate(e.safearea,e.safearea)
  1852.         FalienINF_rects.append(INFrect)
  1853.         Al2collide=Hero.rectarea.collidelist(FalienDEF_rects)
  1854.  
  1855.         if not Al2collide==-1 and Hero.nodamage<=0 and\
  1856.            Hero.life>0:
  1857.             Hero.life-=.5
  1858.             hkollidea.play()
  1859.         else:
  1860.             hkollidea.stop()
  1861.  
  1862. # Final Aliens not effected by Hero shield
  1863.  
  1864. #------------------------------------------------------------------------------#
  1865. #                      FinalAliens colliding with each other                       #
  1866. #------------------------------------------------------------------------------#
  1867.  
  1868.     for i, e in enumerate(FalienINF_rects):
  1869.         FAINFlist=e.collidelistall(FalienINF_rects)
  1870.         for j in FAINFlist:
  1871.             if j==i:
  1872.                 continue
  1873.             dx=controls.FinalAliens[i].centerx-controls.FinalAliens[j].centerx
  1874.             dy=controls.FinalAliens[i].centery-controls.FinalAliens[j].centery
  1875.             distance=math.hypot(dx,dy)
  1876.             heading=.5*math.pi+(math.atan2(dy,dx))
  1877.             if distance<=controls.FinalAliens[j].safearea+\
  1878.                controls.FinalAliens[i].safearea:
  1879.                 controls.FinalAliens[i].heading+=2
  1880.                 controls.FinalAliens[i].x-=math.sin(heading)
  1881.                 controls.FinalAliens[i].y+=math.cos(heading)
  1882.                 controls.FinalAliens[j].heading-=2
  1883.                 controls.FinalAliens[j].x+=math.sin(heading)
  1884.                 controls.FinalAliens[j].y-=math.cos(heading)
  1885.             if distance<=controls.FinalAliens[j].scale/2+\
  1886.                controls.FinalAliens[i].scale/2:
  1887. # if two Final Aliens collide with eachother, teleport one elsewhere
  1888.                 controls.FinalAliens[i].x=random.randrange\
  1889.                                            (10,controls.Swidth-100)
  1890.                 controls.FinalAliens[i].y=random.randrange\
  1891.                                            (10,controls.Sheight-100)
  1892.  
  1893.     if Hero.rectarea.colliderect(boss1.rectarea):
  1894.         Hero.life-=6
  1895.  
  1896. #------------------------------------------------------------------------------#
  1897. #                    ASTEROIDS COLLIDE WITH ALIENS AND HERO                    #
  1898. #------------------------------------------------------------------------------#
  1899.     for a in asteroids:
  1900.         if a.status=="M":
  1901.             a.getrectarea()
  1902.             asteroids_rects.append(a.rectarea)
  1903.             INFrect=a.rectarea.inflate(5,5)
  1904.             asterINF_rects.append(INFrect)
  1905.             Ascollide=Hero.rectarea.collidelist(asteroids_rects)
  1906.  
  1907.             if not Ascollide==-1 and Hero.nodamage<=0 \
  1908.                and Hero.life>0 and a.life>0:
  1909. # find which type of asteroid hit the Hero and reduce life accordingly
  1910.                 if asteroids[Ascollide].kind=="L":
  1911.                     Hero.life-=20
  1912.                 elif asteroids[Ascollide].kind=="M":
  1913.                     Hero.life-=10
  1914.                 else:
  1915.                     Hero.life-=5
  1916.  
  1917.             if not Ascollide==-1 and Hero.nodamage>0 and a.life>0:
  1918.                 asteroids[Ascollide].life=0
  1919.             Alcollisions=a.rectarea.collidelist(alien1DEF_rects)
  1920.             Al2collisions=a.rectarea.collidelist(alien2DEF_rects)
  1921.  
  1922.             if not Alcollisions==-1:
  1923.                 controls.Alien1[Alcollisions].life-=.2
  1924.                 if controls.Alien1[Alcollisions].life<=0:
  1925.                     controls.Alien1[Alcollisions].life=0
  1926.  
  1927.             if not Al2collisions==-1:
  1928.                 controls.Alien2[Al2collisions].life-=.2
  1929.                 if controls.Alien2[Al2collisions].life<=0:
  1930.                     controls.Alien2[Al2collisions].life=0
  1931.  
  1932. #------------------------------------------------------------------------------#
  1933. #                      Asteroids collide with each other                       #
  1934. #------------------------------------------------------------------------------#
  1935.  
  1936.     for i, a in enumerate(asterINF_rects):
  1937.         if asteroids[i].status=="M":
  1938.             asterbumplist=a.collidelistall(asterINF_rects)
  1939.             for j in asterbumplist:
  1940.                 if j==i:
  1941.                     continue
  1942. ##               if asteroids[j].rectarea.colliderect(asteroids[i].rectarea):
  1943.                 dx=asteroids[i].centerx-asteroids[j].centerx
  1944.                 dy=asteroids[i].centery-asteroids[j].centery
  1945.                 distance=math.hypot(dx,dy)
  1946. ##                if distance<=asteroids[j].scale+asteroids[i].scale:
  1947.                 angle=0.5*math.pi+(math.atan2(dy,dx))
  1948.                 asteroids[i].heading=(angle-asteroids[i].heading)%360
  1949.                 asteroids[j].heading-=(angle-asteroids[j].heading)%360
  1950.                 asteroids[i].life-=0.01
  1951.  
  1952. #------------------------------------------------------------------------------#
  1953. #                           A L L  E N E M Y  G U N S                          #
  1954. #------------------------------------------------------------------------------#
  1955.  
  1956. # AB-alien bullet HB-Hero bullet HM=Hero missile BM=boss missile
  1957.     for b in controls.AlienMag:
  1958.         b.update()
  1959.         b.getrectarea()
  1960.  
  1961.         if b.rectarea.colliderect(Hero.rectarea)and Hero.nodamage<=0 \
  1962.            and b.kind=="AB" and b.status=="F":
  1963.             Hero.life-=4
  1964.             b.status="E"
  1965.  
  1966.         if b.status=="D":
  1967.             try:
  1968.                 controls.AlienMag.remove(b) #REMOVE SPENT HBULLET
  1969.             except:
  1970.                 pass
  1971.  
  1972.     for b in controls.Alien2Mag:
  1973.         b.update()
  1974.         b.getrectarea()
  1975.  
  1976.         if b.rectarea.colliderect(Hero.rectarea)and Hero.nodamage<=0 \
  1977.            and b.kind=="AB" and b.status=="F":
  1978.             Hero.life-=2
  1979.             b.status="E"
  1980.  
  1981.         if b.status=="D":
  1982.             try:
  1983.                 controls.Alien2Mag.remove(b) #REMOVE SPENT HBULLET
  1984.             except:
  1985.                 pass
  1986.  
  1987.     for b in controls.Alien3Mag:
  1988.         b.update()
  1989.         b.getrectarea()
  1990.  
  1991.         if b.rectarea.colliderect(Hero.rectarea)and Hero.nodamage<=0 \
  1992.            and b.kind=="AB" and b.status=="F":
  1993.             Hero.life-=5
  1994.             b.status="E"
  1995.  
  1996.         if b.status=="D":
  1997.             try:
  1998.                 controls.Alien3Mag.remove(b) #REMOVE SPENT HBULLET
  1999.             except:
  2000.                 pass
  2001.  
  2002.     for b in controls.BossBombMag:
  2003.         b.update()
  2004.         b.getrectarea()
  2005.  
  2006.         if b.rectarea.colliderect(Hero.rectarea)and Hero.nodamage<=0 \
  2007.              and Hero.life>0 and b.kind=="BM" and b.status=='F':
  2008.             b.status="E"
  2009.             Hero.life-=20
  2010.  
  2011.         if b.status=="D":
  2012.             try:
  2013.                 controls.AlienMag.remove(b) #REMOVE SPENT HBULLET
  2014.             except:
  2015.                 pass
  2016.  
  2017. #------------------------------------------------------------------------------#
  2018. #                             COLLECT ALL POWERUPS                             #
  2019. #------------------------------------------------------------------------------#
  2020.  
  2021. #INVINCIBILITY POWERUP
  2022.     if controls.invincibility==True and invincibility.life>0:
  2023.         invincibility.life-=1
  2024.         invincibility.update()
  2025.         invincibility.getrectarea()
  2026. ##        pygame.draw.rect(screen, (0,255,0), invincibility.rectarea,2)
  2027.  
  2028.         if invincibility.rectarea.colliderect(Hero.rectarea):
  2029.             collectch.play(invince)
  2030.             invincibility.life=0 # type and life
  2031.             controls.PUcount[0]=0
  2032.             score[4]+=5
  2033.             Hero.nodamage=500
  2034.  
  2035. #TRIPLEGUN POWERUP
  2036.     if controls.TRIPLEgun==True and TRIPLEgun.life>0:
  2037.         TRIPLEgun.life-=1
  2038.         TRIPLEgun.update()
  2039.         TRIPLEgun.getrectarea()
  2040.  
  2041.         if TRIPLEgun.rectarea.colliderect(Hero.rectarea):
  2042.             collectch.play(invince)
  2043.             TRIPLEgun.life=0 # type and life
  2044.             controls.PUcount[1]=0
  2045.             score[4]+=5
  2046.             controls.TRIPLEguntime=150
  2047.  
  2048. #CBOMB POWERUP
  2049.     if controls.CBomb==True and CBombpup.life>0:
  2050.         CBombpup.life-=1
  2051.         CBombpup.update()
  2052.         CBombpup.getrectarea()
  2053.  
  2054.         if CBombpup.rectarea.colliderect(Hero.rectarea):
  2055.             collectch.play(invince)
  2056.             CBombpup.life=0 # type and life
  2057.             score[3]+=1
  2058.             controls.CBomb='C'  # CBomb caught by Hero
  2059.             for a in xrange(10): # now add 10 cluster bombs
  2060.                 Hero.CBMag.append(copy.copy(CBOMB))
  2061.  
  2062.     if controls.CBomb==True and CBombpup.life<=0:
  2063.         controls.CBomb=False
  2064.         controls.PUcount[2]=1 # reset the Cluster bomb counter
  2065.  
  2066. #THE ENERGY GLOBES
  2067.  
  2068.     for g in controls.globelist:
  2069.         g.getrectarea()
  2070.  
  2071.         if g.rectarea.colliderect(Hero.rectarea):
  2072.             g.status="C"
  2073.             collectch.play(collect)
  2074.             if controls.globes=='Galive':
  2075.                 score[5]+=5
  2076.             elif controls.globes=='Ralive':
  2077.                 score[5]+=7
  2078.                 score[6]+=5
  2079.             elif controls.globes=='Oalive':
  2080.                 score[4]+=1
  2081.             elif controls.globes=='Palive':
  2082.                 score[4]+=1
  2083.                 score[5]+=5
  2084.                 Hero.bombs[0]+=1 # bomb material added to list
  2085.             controls.collected+=1
  2086.         if g.status=="C":
  2087.             try:
  2088.                 controls.globelist.remove(g)
  2089.             except:
  2090.                 pass
  2091. #------------------------------------------------------------------------------#
  2092. #                              HERO GUN CONTROLS                               #
  2093. #------------------------------------------------------------------------------#
  2094.  
  2095.     for b in Hero.CMag: # MAIN GUN
  2096.         b.update()
  2097.         b.getrectarea()
  2098.         Alcollisions= b.rectarea.collidelist(alien1DEF_rects)
  2099.         Al2collisions=b.rectarea.collidelist(alien2DEF_rects)
  2100.         Al3collisions=b.rectarea.collidelist(FalienDEF_rects)
  2101.         Ascollisions=b.rectarea.collidelist(asteroids_rects)
  2102.  
  2103.         # MOTHERSHIP1 COLLISION CHECK
  2104.         if b.rectarea.colliderect(Mothership1.rectarea) and\
  2105.            Mothership1.status=="M" and b.status=="F":
  2106.             b.status="E"
  2107.             score[5]+=5
  2108.             Mothership1.life-=controls.LreduceM
  2109.  
  2110.         # BOSS1 COLLISION CHECK
  2111.         if boss1.status=='M' and b.rectarea.colliderect(boss1.rectarea)\
  2112.            and b.status=="F":
  2113.             b.status="E"
  2114.             score[4]+=4
  2115.             boss1.life-=controls.LreduceB
  2116.  
  2117.         # ALIEN1 COLLISION CHECK
  2118.         if not Alcollisions==-1 and b.status=="F" and \
  2119.            controls.Alien1[Alcollisions].status=="M":
  2120.             controls.Alien1[Alcollisions].life-=controls.LreduceA1
  2121.  
  2122.             if controls.Alien1[Alcollisions].state<>'turn':
  2123.                 controls.Alien1[Alcollisions].state='turn'
  2124.  
  2125.             if controls.Alien1[Alcollisions].life<=0:
  2126.                 controls.enemycount+=1
  2127.                 score[4]+=2
  2128.             b.status="E"
  2129.  
  2130.         # ALIEN2 COLLISION CHECK
  2131.         if not Al2collisions==-1 and b.status=="F" and \
  2132.            controls.Alien2[Al2collisions].status=="M":
  2133.             controls.Alien2[Al2collisions].life-=controls.LreduceA2
  2134.  
  2135.             if controls.Alien2[Al2collisions].state<>'turn':
  2136.                 controls.Alien2[Al2collisions].state='turn'
  2137.  
  2138.             if controls.Alien2[Al2collisions].life<=0:
  2139.                 controls.enemycount+=1
  2140.                 score[4]+=2
  2141.             b.status="E"
  2142.  
  2143.         # FINALALIEN COLLISION CHECK
  2144.         if not Al3collisions==-1 and b.status=="F" and \
  2145.            controls.FinalAliens[Al3collisions].status=="M":
  2146.             controls.FinalAliens[Al3collisions].life-=controls.LreduceFA
  2147.             score[5]+=5
  2148.  
  2149.             if controls.FinalAliens[Al3collisions].state<>'turn':
  2150.                 controls.FinalAliens[Al3collisions].state='turn'
  2151.             b.status="E"
  2152.  
  2153.         # ASTEROID COLLISION CHECK
  2154.         if not Ascollisions==-1 and b.status=="F" and \
  2155.            asteroids[Ascollisions].life>0:
  2156.             asteroids[Ascollisions].life-=1
  2157.             if asteroids[Ascollisions].life<=0:
  2158.                 controls.asteroidcount+=1
  2159.             if asteroids[Ascollisions].kind=="L":
  2160.                 score[4]+=1
  2161.             if asteroids[Ascollisions].kind=="M":
  2162.                 score[4]+=2
  2163.                 score[5]+=1
  2164.             if asteroids[Ascollisions].kind=="S":
  2165.                 score[4]+=4
  2166.             b.status="E"
  2167.  
  2168.         if b.status=="D":
  2169.             try:
  2170.                 Hero.CMag.remove(b) #REMOVE SPENT HBULLET
  2171.             except:
  2172.                 pass
  2173.  
  2174.     for b in Hero.LMag: # STARBOARD (LEFT) GUN
  2175.         b.update()
  2176.         b.getrectarea()
  2177.         Alcollisions= b.rectarea.collidelist(alien1DEF_rects)
  2178.         Al2collisions= b.rectarea.collidelist(alien2DEF_rects)
  2179.         Al3collisions=b.rectarea.collidelist(FalienDEF_rects)
  2180.         Ascollisions=b.rectarea.collidelist(asteroids_rects)
  2181.  
  2182.         # MOTHERSHIP1 COLLISION CHECK
  2183.         if b.rectarea.colliderect(Mothership1.rectarea) and\
  2184.            Mothership1.status=="M":
  2185.             b.status="E"
  2186.             score[5]+=1
  2187.             Mothership1.life-=controls.LreduceM/4
  2188.  
  2189.         # ALIEN2 COLLISION CHECK
  2190.         if not Al2collisions==-1 and b.status=="F" and \
  2191.            controls.Alien2[Al2collisions].status=="M":
  2192.             controls.Alien2[Al2collisions].life-=controls.LreduceA2
  2193.             if controls.Alien2[Al2collisions].state<>'turn':
  2194.                 controls.Alien2[Al2collisions].state='turn'
  2195.             if controls.Alien2[Al2collisions].life<=0:
  2196.                 controls.enemycount+=1
  2197.             b.status="E"
  2198.  
  2199. ##        # FINALALIEN COLLISION CHECK
  2200.         if not Al3collisions==-1 and b.status=="F" and \
  2201.            controls.FinalAliens[Al3collisions].status=="M":
  2202.             controls.FinalAliens[Al3collisions].life-=controls.LreduceFA
  2203.             score[5]+=1
  2204.  
  2205.             if controls.FinalAliens[Al3collisions].state<>'turn':
  2206.                 controls.FinalAliens[Al3collisions].state='turn'
  2207.             b.status="E"
  2208.  
  2209.         # ASTEROID COLLISION CHECK
  2210.         if not Ascollisions==-1 and b.status=="F" and \
  2211.            asteroids[Ascollisions].life>0:
  2212.             asteroids[Ascollisions].life-=1 #
  2213.             b.status="E"
  2214.  
  2215.         if b.status=="D":
  2216.             try:
  2217.                 Hero.LMag.remove(b) #REMOVE SPENT HBULLET
  2218.             except:
  2219.                 pass
  2220.  
  2221.     for b in Hero.RMag: # PORT (RIGHT) GUN
  2222.         b.update()
  2223.         b.getrectarea()
  2224.         Alcollisions= b.rectarea.collidelist(alien1DEF_rects)
  2225.         Al2collisions= b.rectarea.collidelist(alien2DEF_rects)
  2226.         Al3collisions=b.rectarea.collidelist(FalienDEF_rects)
  2227.         Ascollisions=b.rectarea.collidelist(asteroids_rects)
  2228.  
  2229.         # MOTHERSHIP1 COLLISION CHECK
  2230.         if b.rectarea.colliderect(Mothership1.rectarea) and\
  2231.            Mothership1.status=="M":
  2232.             b.status="E"
  2233.             score[5]+=1
  2234.             Mothership1.life-=controls.LreduceM/4
  2235.  
  2236.         # ALIEN2 COLLISION CHECK
  2237.         if not Al2collisions==-1 and b.status=="F" and \
  2238.            controls.Alien2[Al2collisions].status=="M":
  2239.             controls.Alien2[Al2collisions].life-=controls.LreduceA2
  2240.             if controls.Alien2[Alcollisions].state<>'turn':
  2241.                 controls.Alien2[Alcollisions].state='turn'
  2242.             b.status="E"
  2243.  
  2244.         # FINALALIEN COLLISION CHECK
  2245.         if not Al3collisions==-1 and b.status=="F" and \
  2246.            controls.FinalAliens[Al3collisions].status=="M":
  2247.             controls.FinalAliens[Al3collisions].life-=controls.LreduceFA
  2248.             score[5]+=1
  2249.             if controls.FinalAliens[Al3collisions].state<>'turn':
  2250.                 controls.FinalAliens[Al3collisions].state='turn'
  2251.             b.status="E"
  2252.  
  2253.         # ASTEROID COLLISION CHECK
  2254.         if not Ascollisions==-1 and b.status=="F" and \
  2255.            asteroids[Ascollisions].life>0:
  2256.             asteroids[Ascollisions].life-=1 #
  2257.             b.status="E"
  2258.  
  2259.         if b.status=="D":
  2260.             try:
  2261.                 Hero.RMag.remove(b) #REMOVE SPENT HBULLET
  2262.             except:
  2263.                 pass
  2264.  
  2265.     for b in Hero.BMag: # bomb collisions
  2266.         b.update()
  2267.         b.getrectarea()
  2268.         #pygame.draw.rect(screen, (0,255,0), b.rectarea,1)
  2269.         if b.rectarea.colliderect(boss1.rectarea) and b.status=="F":
  2270.             score[4]+=5
  2271.             boss1.life-=10
  2272.             b.status="E"
  2273.             Hero.bombs[1]="D"
  2274.         if b.status=="D":
  2275.             try:
  2276.                 Hero.BMag.remove(b) #REMOVE HBOMB
  2277.                 Hero.bombs[1]="D"
  2278.             except:
  2279.                 pass
  2280.  
  2281.     for b in Hero.CBMag: # CLUSTER BOMB COLLISIONS
  2282.         b.update()
  2283.         b.getrectarea()
  2284.         Alcollisions= b.rectarea.collidelist(alien1INF_rects)
  2285.         Al2collisions= b.rectarea.collidelist(alien2INF_rects)
  2286.         Al3collisions=b.rectarea.collidelist(FalienDEF_rects)
  2287.         Ascollisions=b.rectarea.collidelist(asteroids_rects)
  2288.  
  2289.         if b.speed==0:
  2290.             b.explode-=1
  2291.         if b.explode==0:
  2292.             b.status="E"
  2293.             controls.CBomb="E"
  2294.         # ALIEN COLLISION CHECK
  2295.         if not Alcollisions==-1 and b.status=="E" and \
  2296.            controls.Alien1[Alcollisions].status=="M":
  2297.             controls.Alien1[Alcollisions].life=0
  2298.             controls.enemycount+=1
  2299.             score[4]+=2
  2300.             score[5]+=5
  2301.         # ALIEN2 COLLISION CHECK
  2302.         if not Al2collisions==-1 and b.status=="E" and \
  2303.            controls.Alien2[Al2collisions].status=="M":
  2304.             controls.Alien2[Al2collisions].life=0
  2305.             controls.enemycount+=1
  2306.             score[4]+=2
  2307.             score[5]+=5
  2308.  
  2309.         # FINAL ALIENS COLLISION CHECK
  2310.         if not Al3collisions==-1 and b.status=="E" and \
  2311.            controls.FinalAliens[Al3collisions].status=="M":
  2312.             controls.FinalAliens[Al3collisions].life-=controls.LreduceFA
  2313.             score[4]+=2
  2314.             score[5]+=5
  2315.  
  2316.         # ASTEROID COLLISION CHECK
  2317.         if not Ascollisions==-1 and b.status=="E" and \
  2318.            asteroids[Ascollisions].life>0:
  2319.             asteroids[Ascollisions].life=0
  2320.             score[4]+=1
  2321.         if b.status=="D":
  2322.             try:
  2323.                 Hero.CBMag.remove(b) #REMOVE HBOMB
  2324.             except:
  2325.                 pass
  2326.         if len(Hero.CBMag)<=0 and controls.CBomb=="E":
  2327.             controls.PUcount[2]=1
  2328.             controls.CBomb=False
  2329.  
  2330. #------------------------------------------------------------------------------#
  2331. #                    REMOVE DESTORYED ALIENS AND ASTEROIDS                     #
  2332. #------------------------------------------------------------------------------#
  2333.  
  2334.     for e in controls.Alien1:
  2335.         if e.life<=0 and e.status=="M": # ALIEN LIFE 0 SO EXPLODE
  2336.             enemyexp.play()
  2337.             e.status="E"
  2338.         if e.status=="E":
  2339.             e.explode()
  2340.         if e.status=="D":
  2341.             try:
  2342.                 controls.Alien1.remove(e) #REMOVE DESTROYED ALIEN
  2343.             except:
  2344.                 pass
  2345.  
  2346.     for e in controls.Alien2:
  2347.         if e.life<=0 and e.status=="M": # ALIEN LIFE 0 SO EXPLODE
  2348.             enemyexp.play()
  2349.             e.status="E"
  2350.         if e.status=="E":
  2351.             e.explode()
  2352.         if e.status=="D":
  2353.             try:
  2354.                 controls.Alien2.remove(e) #REMOVE DESTROYED ALIEN
  2355.             except:
  2356.                 pass
  2357.  
  2358.     for e in controls.FinalAliens:
  2359.         if e.life<=0 and e.status=="M": # ALIEN LIFE 0 SO EXPLODE
  2360.             enemyexp.play()
  2361.             e.status="E"
  2362.         if e.status=="E":
  2363.             e.explode()
  2364.         if e.status=="D":
  2365.             score[3]+=2
  2366.             controls.enemycount+=1
  2367.             try:
  2368.                 controls.FinalAliens.remove(e) #REMOVE DESTROYED ALIEN
  2369.             except:
  2370.                 pass
  2371.  
  2372.     for a in asteroids:
  2373.         if a.life<=0 and a.kind=="L" and a.status=="M":
  2374.             astexplode.play()
  2375.             x=a.x+40; y=a.y+40
  2376.             asteroidA=Asteroids(meteor2,metex2,x,y,5,512,8,64,60,"M","M")
  2377.             asteroidA.speed=1.8
  2378.             asteroidA.speedlimit=asteroidA.speed
  2379.             asteroidA.heading+=90
  2380.             asteroidA.life=4
  2381.             x=a.x-40; y=a.y-40
  2382.             asteroidB=Asteroids(meteor2,metex2,x,y,5,512,8,64,60,"M","M")
  2383.             asteroidB.speed=1.5
  2384.             asteroidB.speedlimit=asteroidB.speed
  2385.             asteroidB.heading-=90
  2386.             a.status="E"
  2387.             asteroids.append(asteroidA)
  2388.             asteroids.append(asteroidB)
  2389.         if a.life<=0 and a.kind=="M" and a.status=="M":
  2390.             astexplode.play()
  2391.             x=a.x+20; y=a.y+20
  2392.             asteroidA=Asteroids(meteor3,metex3,x,y,2,128,8,32,60,"S","M")
  2393.             asteroidA.speed=2.5
  2394.             asteroidA.speedlimit=asteroidA.speed
  2395.             asteroidA.heading+=90
  2396.             asteroidA.life=2
  2397.             x=a.x-20; y=a.y-20
  2398.             asteroidB=Asteroids(meteor3,metex3,x,y,2,128,8,32,60,"S","M")
  2399.             asteroidB.speed=3.5
  2400.             asteroidB.speedlimit=asteroidB.speed
  2401.             asteroidB.heading-=90
  2402.             a.status="E"
  2403.             asteroids.append(asteroidA)
  2404.             asteroids.append(asteroidB)
  2405.         if a.life<=0 and a.kind=="S" and a.status=="M":
  2406.             astexplode.play()
  2407.             a.status="E"
  2408.         if a.status=="D":
  2409.             try:
  2410.                 asteroids.remove(a) #REMOVE EXPLODED ASTEROID FROM GAME
  2411.             except:
  2412.                 pass
  2413.  
  2414.     if Hero.life<=0:
  2415.         Hero.life=0
  2416.  
  2417.     if controls.enemystate=="FINAL BATTLE" \
  2418.        and controls.enemycount==controls.FAamount:
  2419.         controls.gamestate="player wins"
  2420.         player_instructions(playerwins,10)
  2421.         Hero.lives=0
  2422.  
  2423. #------------------------------------------------------------------------------#
  2424. #                    START, INSTRUCTIONS AND PAUSE SCREENS                     #
  2425. #------------------------------------------------------------------------------#
  2426.  
  2427.     while controls.pause==True :
  2428.         scorepos=55
  2429.         font = pygame.font.Font("resources\data\prometheanb.ttf", 30)
  2430.         text = font.render("Hall Of Fame", 1, (70,70,255))
  2431.         textpos1 = text.get_rect()
  2432.         x=controls.Swidth/2-(textpos1[2]/2)
  2433.         screen.blit(text,(x,textpos1[3]/2))
  2434.         font = pygame.font.Font("resources\data\prometheanb.ttf", 15)
  2435.         text = font.render("----------------------------", 1,(80,80,255) )
  2436.         textpos2 = text.get_rect()
  2437.         screen.blit(text,(x,25+textpos2[3]/2))
  2438.         font = pygame.font.Font("resources\data\prometheanb.ttf", 14)
  2439.         for t in highscores:
  2440.             text = font.render(t[1], 1, (90,90,255)) # player name
  2441.             textpos3= text.get_rect()
  2442.             y=x-60
  2443.             screen.blit(text,(y,scorepos-textpos3[3]/2))
  2444.             text = font.render(str(t[0]), 1, (90,90,255)) # player high score
  2445.             textpos4 = text.get_rect()
  2446.             screen.blit(text,(y+100,scorepos-textpos4[3]/2))
  2447.             text = font.render(str(t[2]), 1, (90,90,255)) # player rank
  2448.             textpos5 = text.get_rect()
  2449.             screen.blit(text,(y+200,scorepos-textpos5[3]/2))
  2450.             scorepos+=18
  2451.         # Instructions
  2452.         font = pygame.font.Font("resources\data\prometheanb.ttf", 20)
  2453.         text = font.render("I-Instructions", 0, (128,128,128))
  2454.         textpos = text.get_rect()
  2455.         x=textpos[3]
  2456.         screen.blit(text,(controls.Swidth/2-textpos[2]/2,\
  2457.                                              controls.Sheight-x))
  2458.         a=random.randrange(0,100)
  2459.         b=random.randrange(0,175)
  2460.         c=random.randrange(0,255)
  2461.         if controls.gamestate=="initial":
  2462.             text = font.render("P - to start game", 1,\
  2463.                                [40,40,b])
  2464.         else:
  2465.             text = font.render("Game Paused... P - to continue!",\
  2466.                                1, [60,60,a])
  2467.         textpos = text.get_rect()
  2468.         y=x+textpos[3]+10
  2469.         screen.blit(text,(controls.Swidth/2-textpos[2]/2,controls.Sheight-y))
  2470.  
  2471.         text = font.render("Music Volume : "+str(int(controls.musicfader*100))\
  2472.                             +" -> inc <- dec",1, [50,50,c])
  2473.         textpos = text.get_rect()
  2474.         z=y+textpos[3]+10
  2475.         screen.blit(text,(controls.Swidth/2-textpos[2]/2,\
  2476.                           controls.Sheight-z))
  2477.  
  2478.         text = font.render("F - FULLSCREEN or WINDOWED mode",0, (128,128,128))
  2479.         textpos = text.get_rect()
  2480.         zz=z+textpos[3]+10
  2481.         screen.blit(text,(controls.Swidth/2-textpos[2]/2,controls.Sheight-zz))
  2482.  
  2483.         text = font.render("S - Save Game    L - Load Game",0, (128,128,128))
  2484.         textpos = text.get_rect()
  2485.         za=zz+textpos[3]+10
  2486.         screen.blit(text,(controls.Swidth/2-textpos[2]/2,controls.Sheight-za))
  2487.  
  2488.         # animate the game title
  2489.         controls.titlewidth+=8
  2490.         controls.titleheight+=1
  2491.         if controls.titlewidth>=800:
  2492.             controls.titlewidth=800; controls.titleheight=100
  2493.         p=pygame.transform.scale(title, (controls.titlewidth,\
  2494.                                          controls.titleheight))
  2495.         w=p.get_rect(); x=(controls.Sheight-w[3])-150; y=x-3
  2496.         screen.blit(p,((controls.Swidth-w[2])/2,random.randrange(y,x)))
  2497.         star_field()
  2498.         update_screen()
  2499.  
  2500.         #get key input
  2501.         for event in pygame.event.get():
  2502.             if event.type==KEYDOWN:
  2503.                 if event.key==K_p :
  2504.                     if controls.gamestate=="initial":
  2505.                         controls.gamestate="Level 1"
  2506.                         pygame.event.clear()
  2507.                     controls.pause=not controls.pause
  2508.                 Hero.setHeading(90)
  2509.                 if event.key==K_i:
  2510.                     controls.instructions=True
  2511.                     show_instructions()
  2512.                 if event.key==K_RIGHT:              # Music vol+
  2513.                         controls.musicfader+=0.01
  2514.                 if event.key==K_LEFT:              # Music vol-
  2515.                         controls.musicfader-=0.01
  2516.                 if controls.musicfader<=0:
  2517.                     controls.musicfader=0
  2518.                 if controls.musicfader>=1:
  2519.                     controls.musicfader=1
  2520.                 pygame.mixer.music.set_volume(controls.musicfader)
  2521.                 if event.key==K_f and screenmode==0: # toggle fullscreen on/off
  2522.                     screenmode=FULLSCREEN
  2523.                     screen = pygame.display.set_mode((Swidth, Sheight),\
  2524.                                                      screenmode,0)
  2525.                 elif event.key==K_f and screenmode==FULLSCREEN:
  2526.                     screenmode=0
  2527.                     screen = pygame.display.set_mode((Swidth, Sheight),\
  2528.                                                      screenmode,0)
  2529.                 if event.key==K_s:
  2530.                     controls.savegame=True
  2531.  
  2532.                 if event.key==K_l:
  2533.                     controls.loadgame=True
  2534.  
  2535.         if controls.savegame==True:
  2536.             save_game()
  2537.  
  2538.         if controls.loadgame==True:
  2539.             load_game()
  2540.  
  2541.     update_all(text1,text2,text3)
  2542.  
  2543. #------------------------------------------------------------------------------#
  2544. #                        G A  M  E    O  V  E  R                               #
  2545. #                            replay or quit                                    #
  2546. #------------------------------------------------------------------------------#
  2547.     controls.name=''
  2548.     f=open('resources\data\scores.sg','wb')
  2549.     while Hero.lives<=0 and controls.halloffame==False:
  2550.         font = pygame.font.Font("resources\data\prometheanb.ttf", 70)
  2551.         akuru="GAME OVER!"
  2552.         text = font.render(akuru, 1, red)
  2553.         textpos = text.get_rect()
  2554.         screen.blit(text,(controls.Swidth/2-(textpos[2]/2),50))
  2555.         if realscore>20000 and controls.halloffame==False:
  2556.             ypos=hall_of_fame_message()
  2557.             # get the rank
  2558.             if controls.gamestate=="Level 2":
  2559.                 controls.rank="CADET"
  2560.             elif controls.gamestate=="Level 3":
  2561.                 controls.rank="SOLDIER"
  2562.             elif controls.gamestate=="Level 4":
  2563.                 controls.rank="WARRIOR"
  2564.             elif controls.gamestate=="player wins":
  2565.                 controls.rank="SPACE GLADIATOR"
  2566.  
  2567.             for event in pygame.event.get(): # get player name
  2568.                 if event.type==KEYDOWN:
  2569.                     if event.key==K_BACKSPACE and len(controls.name)>0:
  2570.                         controls.name=controls.name[:-1]
  2571.                     if event.key>=97 and event.key<=122 and \
  2572.                        len(controls.name)<10 or event.key>=48 and \
  2573.                        event.key<=57 and len(controls.name)<10:
  2574.                         text=str(pygame.key.name(event.key))
  2575.                         controls.name=controls.name+text
  2576.  
  2577.                     if event.key==K_RETURN:
  2578.                         newname=controls.name.upper()# convert to uppercase
  2579.                         controls.name=newname
  2580.                         controls.halloffame=True
  2581.                         if len(controls.name)>0:
  2582.                             addnew=[realscore,controls.name,controls.rank]
  2583.                             if len(highscores)<19:#max 20 items in list
  2584.                                 highscores.append(addnew)#add new data to file
  2585.                             #list is already full so add to last item if bigger
  2586.                             # than the number there
  2587.                             elif highscores[-1]<addnew:
  2588.                                 highscores[-1]=addnew
  2589.                             highscores.sort(reverse=True)
  2590.  
  2591.         font = pygame.font.Font("resources\data\prometheanb.ttf", 20)
  2592.         text=font.render((controls.name+'_'),1,[175,175,175])
  2593.         textpos = text.get_rect()
  2594.         screen.blit(text,(controls.Swidth/2-textpos[2]/2,ypos+10))
  2595.  
  2596.         font = pygame.font.Font("resources\data\prometheanb.ttf", 30)
  2597.         text = font.render("Hit ESC to Quit!", 1, red)
  2598.         textpos1 = text.get_rect()
  2599.         screen.blit(text,(controls.Swidth/2-(textpos1[2]/2),120))
  2600.         text = font.render("Enter to replay", 1, red)
  2601.         textpos2 = text.get_rect()
  2602.         screen.blit(text,(controls.Swidth/2-(textpos2[2]/2),150))
  2603.         pygame.event.pump()
  2604.         key=pygame.key.get_pressed()
  2605.         if key[pygame.K_RETURN]:
  2606.             Hero.life=450
  2607.             Hero.lives=6
  2608.             score=[0,0,0,0,0,0,0]#mil,hunthous, tenthous,thous,huns,tens,ones
  2609.             scorestr= "".join(str(i) for i in score)
  2610.             realscore=int(scorestr)
  2611.             controls=Game_controls()
  2612.             initialize_stars()
  2613.             asteroids=[]
  2614.             Hero =HeroObject(45,controls.Swidth/2, controls.Sheight/2,\
  2615.                              "N",6,["one",20,"S",0],[0,"D"],5,64)
  2616.             Hero.setHeading(90)
  2617.             bosx=-100
  2618.             bosy=random.randrange(50,controls.Sheight-100)
  2619.             # create boss1  and Mothership1 off screen but passive-"P"
  2620.             boss1=EnemyObject(bossA,bsxplode,80,bosx,bosy,100,30,8,400,"P")
  2621.             mx=random.randrange(50,100,5)
  2622.             Mothership1=EnemyObject(AMSHIP1,AMS1EXP,110,mx,0,75.0,0,10,250,"P")
  2623.             Mothership1.heading=90
  2624.             Mothership1.speed=2
  2625.             pygame.mixer.music.load(startmusic)
  2626.             pygame.mixer.music.set_volume(0.4)
  2627.             pygame.mixer.music.play(-1)
  2628.             controls.musicfader=pygame.mixer.music.get_volume()
  2629.         if key[pygame.K_ESCAPE]:
  2630.             add_and_close_file()
  2631.             game_over()
  2632.         update_all(text1,text2,text3)
  2633.     add_and_close_file()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement