Advertisement
diliupg

Earthwar a Space shooter written in Python

Sep 19th, 2013
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 28.27 KB | None | 0 0
  1. # -------------------------EARTHWAR-------------------------#
  2. # A game by DiliupG 2013                                    #
  3. # Concept, design and coding by Diliupg                     #
  4. # Maths/Physics consultant : Shanika Amarasoma              #
  5. # first game written in python.
  6. # --------------------------------------------------------- #
  7.  
  8. import pygame,sys,math,time,random
  9. from pygame.locals import *
  10. clock=pygame.time.Clock(); seconds=0
  11. pygame.mixer.pre_init(44100, 16, 2, 3072) # setup mixer to avoid sound lag
  12. pygame.init()
  13. w = 1200   #screen width
  14. h = 600   #screen height
  15. screen = pygame.display.set_mode((w, h),FULLSCREEN,32) #display screen
  16. pygame.display.set_caption('EARTHWARS by DiliupG 2013')
  17. hero = pygame.image.load("earthwars_resources\heroshipl.png").convert_alpha()
  18. blt= pygame.image.load("earthwars_resources\dong.png").convert_alpha()
  19. background=pygame.image.load("earthwars_resources\deep_space2.jpg").convert()
  20. alien001=pygame.image.load("earthwars_resources\zaragon.png").convert_alpha()
  21. explosiongreen=pygame.image.load("earthwars_resources\explosiondg003.png").convert_alpha()
  22. heroexplode=pygame.image.load("earthwars_resources\explosiondg001.png").convert_alpha()
  23. alien002=pygame.image.load("earthwars_resources\kabaraya.png").convert_alpha()
  24. albomb=pygame.image.load("earthwars_resources\karakena.png").convert_alpha()
  25. albexp=pygame.image.load("earthwars_resources\podibombex.png").convert_alpha()
  26. fbombe=pygame.image.load("earthwars_resources\eshbomb.png").convert_alpha()
  27. fbombe2=pygame.image.load("earthwars_resources\dabomb.png").convert_alpha()
  28. fbexp=pygame.image.load("earthwars_resources\FR_explosion_10.png").convert_alpha()
  29. bosa1=pygame.image.load("earthwars_resources\elaz3.png").convert_alpha()
  30. bsbomb=pygame.image.load("earthwars_resources\lokubom.png").convert_alpha()
  31. thanks=pygame.image.load("earthwars_resources\mythanks.jpg").convert()
  32.  
  33. mainhero = pygame.transform.scale(hero, (35,35))
  34. bullet=pygame.transform.scale(blt,(5,5))
  35. albombe=pygame.transform.scale(albomb,(15,15))
  36. alien1=pygame.transform.scale(alien001,(45,45))
  37. fighter=pygame.transform.scale(alien002,(25,25))
  38. fbomb1=pygame.transform.scale(fbombe,(8,8))
  39. fbomb2=pygame.transform.scale(fbombe2,(10,10))
  40. fbexplode=pygame.transform.scale(fbexp,(270,270))
  41. bossbomb=pygame.transform.scale(bsbomb,(12,12))
  42. imageturned=hero; myscreen=screen
  43.  
  44. laserfire=pygame.mixer.Sound("earthwars_resources\LASER1.WAV")
  45. heroexplodesound=pygame.mixer.Sound("earthwars_resources\explosion2.WAV")
  46. alien1explode=pygame.mixer.Sound("earthwars_resources\saucerexplode.WAV")
  47. healthloss=pygame.mixer.Sound("earthwars_resources\sparks.WAV")
  48. fighterexplode=pygame.mixer.Sound("earthwars_resources\smallexp.wav")
  49. fbexs=pygame.mixer.Sound("earthwars_resources\podipipireema.wav")
  50. laserfire.set_volume(0.08); alien1explode.set_volume(0.5)
  51. healthloss.set_volume(0.02); heroexplodesound.set_volume(0.1)
  52. fighterexplode.set_volume(0.4); fbexs.set_volume(0.3)
  53.  
  54. rotate=90; angle_rad=math.radians(rotate);
  55. score=[0,0,0,0,0,0]#hunthous, tenthous,thous,huns,tens,ones
  56. scorestr=str(score[0])+str(score[1])+str(score[2])+\
  57.            str(score[3])+str(score[4])+str(score[5])
  58. realscore=int(scorestr)
  59. gamespeed=100; gamecounter=0; game_paused=False
  60. kalaya=0
  61. red=[255,0,0]
  62. bossrotate=bosa1
  63. # All Hero info in Hinfo list
  64. # 0-heroexpldsound, 1-x, 2-y, 3-expldx, 4-expldy, 5-resethero, 6-hvel, 7-maxhvel
  65. # 8-hero size, 9-if moving forward True flag, 10-rotatevalue befor w was pressed
  66. #11-newvelocity
  67. Hinfo=[False,(w/2),(h-mainhero.get_width()),0,0,False,0,3,35,False,0,0,4]
  68. vitals=[150,6,225] #health, lives, fade
  69. bullets=[]
  70. aliens1=[]; aliens1limit=25; aliens1delay=0; aliens1delaylimit=35
  71. albombs=[]; albmx=albmy=0
  72. bsbombs=[]
  73. #engineex=[0,0]
  74. fablts=[];fbblts=[]; bombalaunch=bombblaunch=0
  75. smallboss1=[False,0,0,0,0,0,100,0]
  76. smallboss2=[False,0,0,0,0,0,100,0]
  77. bossinfo=[False,0,0,0,0,0,1000]
  78. # event list flags for all event activation
  79. # 0-alien1 1-albom 2-smallboss1 3- smallboss2 4-boss1
  80. eventlist=[False, False, False, False, False]
  81. text="It was in 3145 that Earth was taken fromus by the Kroggs from a distant \
  82. unheard of Galaxy. Now we live in exile on one  of the moons of Jupiter. Over \
  83. the past  few centuries we started rebuilding our lost civilisation and now the \
  84. final     battle is about to begin. The Kroggs arecoming with a new plan to \
  85. annihilate oursun so all life forms on all planets in the solar system will \
  86. be wiped out. It  is your task to defend the Solar System.YOU ARE OUR LAST \
  87. AND ONLY HOPE.........."
  88.  
  89. smallfiles=[]
  90. sentl=40 # length of a sentence in characters
  91. color=(200,25,200)
  92. intro=True
  93. def makefiles(bigfile,smallfiles):
  94.     a=0;y=h+25
  95.     for i in xrange (len(text)//sentl):
  96.         appendfile=bigfile[a:a+sentl]
  97.         smallfiles.append([appendfile,y+a])
  98.         a+=40                              
  99.     if len(text)%sentl>0:
  100.         x=smallfiles[-1][1]
  101.         x+=40
  102.         smallfiles.append([bigfile[-(len(text)%sentl):-1],x])
  103.     return smallfiles
  104.    
  105. def story_to_screen(textfile):
  106.     font = pygame.font.Font(None,28)
  107.     textpic = font.render(textfile[0], 0, color)
  108.     a=textpic.get_rect()
  109.     screen.blit(textpic,((w-a[2])/2,textfile[1]))# bottom center
  110.     textfile[1]-=2
  111.  
  112. def game_over():
  113.     for x in xrange(4000):
  114.         screen.blit(thanks,(w/2-(324),50))
  115.         pygame.display.update()
  116.     pygame.quit()
  117.     sys.exit()
  118.     pygame.mouse.set_visible(True)
  119. def update_all(Hinfo):
  120.     update_score(score,scorestr,realscore)
  121.     update_game_text()
  122.     if vitals[0]>0:
  123.         screen.blit(imageturned, (Hinfo[1], Hinfo[2]))
  124.     fighter_bombs(fablts,fbomb1,8,2,5)
  125.     fighter_bombs(fbblts,fbomb2,10,5,0)
  126.     bombs(albombs, albombe,albexp,1.8,15,15,25)
  127.     bombs(bsbombs, bossbomb,albexp,1.1,40,40,50)
  128.     pygame.display.update()
  129.     screen.blit(background,(0,0))   # copy background to screen
  130.  
  131. def update_score(score,scorestr,realscore):
  132.     if score[5]>=10: score[5]=0; score[4]+=1
  133.     if score[4]>=10: score[4]=0; score[3]+=1
  134.     if score[3]>=10: score[3]=0; score[2]+=1
  135.     if score[2]>=10: score[2]=0; score[1]+=1
  136.     if score[1]>=10: score[1]=0; score[0]+=1
  137.     scorestr=str(score[0])+str(score[1])+\
  138.            str(score[2])+str(score[3])+str(score[4])+str(score[5])
  139.     realscore=int(scorestr)
  140. def update_game_text():
  141.     font = pygame.font.Font(None, 36)
  142.     text = font.render("Score:", 2, red)
  143.     screen.blit(text, [2,0])# print text->score, vitals[1]
  144.     text = font.render(scorestr, 2, red)
  145.     screen.blit(text, [80,1])# print score
  146.     life=pygame.transform.scale(hero,(25,25))
  147.     for i in xrange (vitals[1]):
  148.         screen.blit(life,(w-(25*(i+1)),2))
  149.     pygame.draw.rect(screen, (128,0,0), [2, h-(300), 25,300],4)# vitals[0] bar border
  150.     if vitals[2]<0: vitals[2]=1
  151.     pygame.draw.rect(screen, (255,vitals[2],0), [6, h-vitals[0]*2+4, 19,295],0)# vitals[0] bar
  152.  
  153. def getAngle(x1,y1,x2,y2):
  154.     # Return value is 0 for right, 90 for up, 180 for left\
  155.     #, and 270 for down (and all values between 0 and 360)
  156.     ydiff = y1 - y2
  157.     xdiff = x1 - x2
  158.     angleinrad = math.atan2(xdiff,ydiff) # get the angle in radians
  159.     angleindeg = math.degrees(angleinrad) # convert to degrees
  160.     return angleindeg, angleinrad
  161.  
  162. def rot_center(image, angle): # use a square image
  163.     """rotate an image while keeping its center and size"""
  164.     orig_rect = image.get_rect()
  165.     rot_image = pygame.transform.rotate(image, angle)
  166.     rot_rect = orig_rect.copy()
  167.     rot_rect.center = rot_image.get_rect().center
  168.     rot_image = rot_image.subsurface(rot_rect).copy()
  169.     return rot_image
  170.  
  171. def hero_explode():
  172.     if vitals[0]<=0:
  173.         screen.blit(heroexplode,(Hinfo[1]-32,Hinfo[2]-32)\
  174.                     ,Rect((Hinfo[3], Hinfo[4]),(100,100)))
  175.         Hinfo[3]+=100
  176.         if Hinfo[3]>=600:
  177.             Hinfo[3]=0
  178.             Hinfo[4]+=100
  179.         if Hinfo[4]>=600:
  180.             Hinfo[3], Hinfo[4]=0,0; Hinfo[5]=True; vitals[0]=1; vitals[2]=1
  181.             vitals[1]-=1; Hinfo[6]=0; rotate=90; Hinfo[1],Hinfo[2]=w/2, h-50
  182.             angle_rad=math.radians(rotate)
  183.             imageturned=rot_center(mainhero,rotate)
  184.             Hinfo[0]=True
  185.  
  186. def explode(i,whoexplds):# i=saucers_list pos, x y=saucer Hinfo[1] poy
  187.     screen.blit(explosiongreen,(whoexplds[i][3]-27,whoexplds[i][4]-27),\
  188.                 Rect((whoexplds[i][6],whoexplds[i][7] ),(100,100)))
  189.     whoexplds[i][6]+=100
  190.     if whoexplds[i][6]>=600:
  191.         whoexplds[i][6]=0
  192.         whoexplds[i][7]+=100
  193.     if whoexplds[i][7]>=600:
  194.         whoexplds[i][4]=h+100
  195.         whoexplds[i][0]=True
  196.         whoexplds[i][5]=0
  197.         whoexplds[i][2]=h+100
  198.  
  199. # USE THE FUNCTION BELOW FOR ALL BOMBS
  200. #hl-vitals[0] loss
  201. def bombs(amolist,image1,image2,firerate,w,h,hl):
  202.     if len(amolist)>0:
  203.         for i in xrange (len(amolist)):
  204.             if amolist[i][0]>0 and amolist[i][6]==False:
  205.                 screen.blit(image1,(amolist[i][1],amolist[i][2]))
  206.                 amolist[i][1]+=math.cos(amolist[i][3])*constant*firerate
  207.                 amolist[i][2]-=math.sin(amolist[i][3])*constant*firerate
  208.                 bomb_rect=pygame.Rect(amolist[i][1],amolist[i][2],w,h)
  209.                 hero_rect=pygame.Rect(Hinfo[1],Hinfo[2],Hinfo[8], Hinfo[8])
  210.                 amolist[i][0]-=1
  211.                 if bomb_rect.colliderect(hero_rect):
  212.                     fbexs.play()
  213.                     vitals[0]-=hl
  214.                     vitals[2]-=hl*1.5
  215.                     amolist[i][0]=0
  216.             if amolist[i][0]<=0 and amolist[i][6]==False:
  217. # bomb life over so call explosion routine
  218.                 screen.blit(image2,(amolist[i][1]-10,amolist[i][2]-10),\
  219.                             Rect((amolist[i][3],amolist[i][4]),(40,40)))
  220.                 amolist[i][4]+=40
  221.                 if amolist[i][4]>=280:
  222.                     amolist[i][4]=0
  223.                     amolist[i][5]+=40
  224.                 if amolist[i][5]>=80:
  225.                     amolist[i][6]=True
  226.                     amolist[i][1]=w=100 # away from screen area
  227.         if amolist[-1][0]<=0 and amolist[-1][6]==True:
  228.                  amolist.pop()
  229.  
  230. def shoot(amolist,image,firerate):
  231.     # bullets[i]=[life, x, y, rotation angle] rotation angle inserted at time of
  232.     #               0   1  2      3           bullet creation and adding to list
  233.     if len(amolist)>0:
  234.         for i in xrange (len(amolist)):
  235.             if amolist[i][0]>0:
  236.                 amolist[i][1]+=math.cos(amolist[i][3])*constant*firerate
  237.                 amolist[i][2]-=math.sin(amolist[i][3])*constant*firerate
  238.                 screen.blit(image,(amolist[i][1],amolist[i][2]))
  239.                 amolist[i][0]-=1
  240.    
  241. def fighters(whichship,fbulletlist,score):
  242.  
  243.     screen.blit(fighter, (whichship[1], whichship[2]))
  244.     whichship[1]+=math.cos(math.radians(whichship[2])/math.pi)*(constant)
  245.     whichship[2]-=math.sin(math.radians(-whichship[1])/math.pi*5)*(constant)
  246.     if whichship[1]>=w-fighter.get_width():
  247.         whichship[1]=0
  248.     if whichship<0:
  249.         whichship[1]=w
  250.     hero_rect=pygame.Rect(Hinfo[1],Hinfo[2],Hinfo[8],Hinfo[8])
  251.     fighter_rect=pygame.Rect(whichship[1],whichship[2],25,25)
  252.     if fighter_rect.colliderect(hero_rect):
  253.         vitals[0]-=10
  254.         vitals[2]-=8*1.5
  255.         healthloss.play()
  256. #check if we shot the fighter and reduce vitals[0]
  257.     if len(bullets)>0:
  258.         for i in xrange (len(bullets)):
  259.             bullet_rect=pygame.Rect(bullets[i][1], bullets[i][2],6,6)
  260.             if bullet_rect.colliderect(fighter_rect):
  261.                 bullets[i][0]=0
  262.                 whichship[6]-=1
  263.                 score[4]+=1 #tens
  264. #make the fighter bombs ready for launch
  265.     if whichship[6]>=0:
  266.         if len(fbulletlist)<=100:
  267.             if whichship[5]>=random.randrange(8,15):
  268.                 fbulletlist.insert(0,[500,whichship[1],whichship[2],0,0,0,0,False])
  269.                 whichship[5]=0
  270.     if whichship[6]<=0:
  271.         whichship[0]=False
  272.         whichship[6]=100
  273.         whichship[7]=0
  274.     whichship[5]+=1
  275.  
  276. def fighter_bombs(fblist,spritelist, size,score1,score2):
  277.     global w
  278. #launch the small bombs which home in on the hero
  279.     if len(fblist)>0:
  280.         for i in xrange(len(fblist)):
  281.             if fblist[i][0]>0:
  282.                 x=Hinfo[1]-fblist[i][1]; y=Hinfo[2]-fblist[i][2]
  283.                 fblist[i][1]+=math.cos(math.atan2 (y,x))*(constant/1.6)*2
  284.                 fblist[i][2]+=math.sin(math.atan2 (y,x))*(constant/1.6)*2
  285.                 screen.blit(spritelist,(fblist[i][1], fblist[i][2]))
  286.                 bomb_rect=pygame.Rect(fblist[i][1], fblist[i][2],size, size)
  287.                 hero_rect=pygame.Rect(Hinfo[1],Hinfo[2],Hinfo[8],Hinfo[8])
  288.                 fblist[i][0]-=1
  289.                 if bomb_rect.colliderect(hero_rect):
  290.                     vitals[0]-=25
  291.                     vitals[2]-=25*1.5
  292.                     fblist[i][0]=0
  293.                     fbexs.play()
  294.                     fblist[i][3]=fblist[i][1]# put current x here for explosion
  295.                     fblist[i][4]=fblist[i][2]# put current y here for explosion
  296.                     fblist[i][1]=w+100 # move bomb away from screen
  297.  
  298.             fb_rect=pygame.Rect(fblist[i][1], fblist[i][2], 10,10)
  299.             for j in xrange (len(bullets)):
  300.                 bullet_rect=pygame.Rect(bullets[j][1], bullets[j][2],6,6)
  301.                 if bullet_rect.colliderect(fb_rect):
  302.                     fbexs.play()
  303.                     fblist[i][3]=fblist[i][1]# put current x here for explosion
  304.                     fblist[i][4]=fblist[i][2]# put current y here for explosion
  305.                     fblist[i][1]=w+100 # move bomb away from screen
  306.                     score[4]+=1
  307.                     fblist[i][0]=0
  308.                     bullets[j][0]=0
  309.                     fblist[i][7]=True
  310.                    
  311.             if fblist[i][0]<=0:
  312.                 screen.blit(fbexplode,(fblist[i][3]-5,fblist[i][4]-5),\
  313.                         Rect((fblist[i][5],fblist[i][6]),(45,45)))
  314.                 fblist[i][5]+=45
  315.                 if fblist[i][5]>=270:
  316.                     fblist[i][5]=0
  317.                     fblist[i][6]+=45
  318.                 if fblist[i][6]>=270:
  319.                     fblist[i][7]=True
  320.                    
  321. #remove the used bomb from the list
  322.         if fblist[-1][7]==True:
  323.             fblist.pop()
  324.  
  325. imageturned=rot_center(mainhero,rotate)
  326. m=makefiles(text,smallfiles)# this is the text file broken into segments
  327. num=len(smallfiles) # this is the number of lines we to print to the screen
  328. pygame.mouse.set_visible(False)
  329. while True:#------------------------ Main Game Loop----------------------#
  330.     while intro==True:
  331.         pygame.event.pump()
  332.         keys=pygame.key.get_pressed()       #get key pressed
  333.         if keys[pygame.K_q]: #PAUSE GAME
  334.             intro=False
  335.         for i in xrange(len(smallfiles)):
  336.             story_to_screen(smallfiles[i])
  337.             if smallfiles[len(smallfiles)-1][1]<-50:
  338.                 intro=False      
  339.         pygame.display.update()
  340.         screen.blit(background,(0,0))
  341.    
  342.     timegone=clock.tick(40)
  343.     timegone_sec=timegone/1000.0
  344.     constant=gamespeed*timegone_sec
  345.     scorestr=str(score[0])+str(score[1])+str(score[2])\
  346.                +str(score[3])+str(score[4])+str(score[5])
  347.     realscore=int(scorestr)
  348.     smallboss1[7]+=.5
  349.     smallboss2[7]+=.5
  350.  
  351.     kalaya+=timegone_sec
  352.     seconds=int(kalaya)
  353.     gamecounter+=1
  354.     if realscore>2000 and score <4000:
  355.         aliens1delaylimit=40
  356.     vitals[0]+=0.05 # hero auto healing function
  357.     vitals[2]+=0.05*1.5
  358.     if vitals[0]==150:
  359.         vitals[0]=150
  360.         vitals[2]=225
  361. # set the eventlist flags to trigger events happening
  362.     if gamecounter==50:
  363.         eventlist[0]=True # bring on the aliens##    if realscore> 2000:
  364.  
  365.     if realscore>= 2000:
  366.         eventlist[1]=True # bring on the albombs
  367.         aliens1limit=60
  368.         aliens1delaylimit=25
  369.        
  370.     if realscore>= 4000 and realscore <=10000:
  371.         eventlist[2]=True # bring on the smallboss1
  372.         eventlist[1]=False# stop the albombs
  373.         eventlist[0]=False# stop the aliens
  374.         eventlist[3]=False
  375.         eventlist[4]=False
  376.         bossinfo[0]=False
  377.        
  378.     if realscore>= 6000 and realscore <=10000:
  379.         eventlist[3]=True # bring on the smallboss2
  380.         eventlist[0]=False# stop the aliens1
  381.         eventlist[1]=False# stop the albombs
  382.         eventlist[4]=False
  383.         bossinfo[0]=False
  384.          
  385.     if realscore>= 8000 and realscore <=10000: # bring the alien ships and albombs
  386.         eventlist[0]=True# bring the aliens1
  387.         eventlist[1]=True# bring the albombs
  388.         eventlist[4]=False
  389.         bossinfo[0]=False
  390.          
  391.     if realscore>= 10000:
  392.         eventlist[4]=True# bring on the BAD BOSS1
  393.         eventlist[3]=False# stop the smallboss2
  394.         eventlist[2]=False# stop the smallboss1
  395.         eventlist[1]=False# stop the aliens1
  396.         eventlist[0]=False# stop the albombs
  397.         smallboss1[0]=False
  398.         smallboss2[0]=False
  399.        
  400.     if eventlist[2]==True and smallboss1[0]==False:
  401.         smallboss1[0]=True
  402.         smallboss1[1]=random.randint(w/2, w-w/3)
  403.         smallboss1[2]=random.randint(h/3, h-h/3)
  404.     if smallboss1[0] is True and smallboss1[7]>50:
  405.         fighters(smallboss1,fablts,score)
  406.  
  407.     if eventlist[3]==True and smallboss2[0]==False:
  408.         smallboss2[0]=True
  409.         smallboss2[1]=random.randint(w/3, w/2)
  410.         smallboss2[2]=random.randint(h/3, h-h/3)
  411.     if smallboss2[0] is True and smallboss2[7]>90:
  412.         fighters(smallboss2,fbblts,score)
  413.  
  414.     pygame.event.pump()
  415.     keys=pygame.key.get_pressed()       #get key pressed
  416.     if (keys[pygame.K_ESCAPE]): # exit game
  417.         game_over()
  418.     for event in pygame.event.get():
  419.         if event.type==QUIT:
  420.             game_over()
  421.  
  422. # move hero
  423.     if vitals[0]<=0:
  424.         if Hinfo[0]==True:
  425.             heroexplodesound.play()
  426.             Hinfo[0]=False
  427.         hero_explode()
  428.     if Hinfo[5]==True:
  429.         vitals[0]+=5
  430.         vitals[2]+=5*1.5
  431.     if vitals[0]>=150: # max health
  432.         vitals[0]=150
  433.         vitals[2]=225 # colour fade amount
  434.         Hinfo[5]=False
  435.     if vitals[0]>0:
  436.         if (keys[pygame.K_a])or (keys[pygame.K_LEFT]):#rotate left
  437.             rotate+=4
  438.         if (keys[pygame.K_d])or (keys[pygame.K_RIGHT]):#rotate right
  439.             rotate-=4
  440.         if (keys[pygame.K_w])or (keys[pygame.K_UP]):#accellerate (to max velocity) and move forward
  441.             Hinfo[9]=True
  442.             Hinfo[6]+=.01
  443.             if Hinfo[6]>=Hinfo[7]:
  444.                 Hinfo[6]=Hinfo[7]
  445.             Hinfo[11]+=Hinfo[6]
  446.             if Hinfo[11]>=Hinfo[12]:
  447.                 Hinfo[11]=Hinfo[12]
  448.         else:
  449.             Hinfo[9]=False
  450.             Hinfo[6]-=.01
  451.             Hinfo[6]<=0
  452.             Hinfo[6]=0
  453.             Hinfo[11]-=.04
  454.             if Hinfo[11]<0:
  455.                 Hinfo[11]=0
  456.         if (keys[pygame.K_s])or (keys[pygame.K_DOWN]):#reverse
  457.             Hinfo[11]-=.01
  458.             if Hinfo[11]<=0:
  459.                 Hinfo[11]=0
  460.         imageturned=rot_center(mainhero,rotate)
  461.         if Hinfo[9]==True:
  462.             Hinfo[10]=rotate
  463.  
  464.         angle_rad=math.radians(Hinfo[10])
  465.         Hinfo[1]+=Hinfo[11]*math.cos(angle_rad)*(constant)
  466.         Hinfo[2]-=Hinfo[11]*math.sin(angle_rad)*(constant)
  467.         angle=math.radians(rotate)
  468.         if Hinfo[2]>h:
  469.             Hinfo[2]=0-hero.get_height()
  470.         if Hinfo[2]<0-hero.get_height():
  471.             Hinfo[2]=h
  472.         if Hinfo[1]<0-hero.get_width():
  473.             Hinfo[1]=w
  474.         if Hinfo[1]>w:
  475.             Hinfo[1]=0-hero.get_width()
  476. # put a bullet in the list if spacebar pressed
  477.     if gamecounter%3==0:# slows down adding bullets so slows down firing
  478.         if (keys[pygame.K_SPACE]) and vitals[0]>0:
  479.             bltx=(Hinfo[1]-3)+mainhero.get_width()/2
  480.             blty=(Hinfo[2]-3)+mainhero.get_height()/2
  481.             bltangle=math.radians(rotate)
  482.             bltlife=25
  483.             bullets.insert(0,[bltlife,bltx, blty,bltangle, False])
  484.             laserfire.play()
  485. # if bullet list not empty then shoot!
  486.     if len(bullets)>0:
  487.         shoot(bullets, bullet,4)
  488.         for i in xrange (len(bullets)):
  489.             if bullets[i][0]>0:
  490.                 bullet_rect=pygame.Rect(bullets[i][1],bullets[i][2],6,6)
  491. # check for collision with boss1
  492.                 if bossinfo[0]==True:
  493.                     boss1_rect=pygame.Rect(bossinfo[1], bossinfo[2],90,90)
  494.                     if bullet_rect.colliderect(boss1_rect):
  495.                         score[5]+=1 # ones
  496.                         score[4]+=1 # tens
  497.                         bossinfo[6]-=.5
  498. # check if any bullet hits any alien
  499.                 if len (aliens1)>0:
  500.                     for j in xrange (len(aliens1)):
  501.                         if aliens1[j][5]>0:
  502.                             alien1_rect=pygame.Rect(aliens1[j][1]\
  503.                                                     , aliens1[j][2],45,45)
  504.                             if bullet_rect.colliderect(alien1_rect):
  505.                                 bullets[i][0]=0
  506.                                 aliens1[j][5]-=8
  507.                                 score[5]+=1
  508. # if alien life 0 take out of screen and prepare for explosion
  509.                         if aliens1[j][5]<=0 and aliens1[j][0]==True:
  510.                             alien1explode.play()
  511.                             score[3]+=1
  512.                             aliens1[j][0]=False
  513.                             aliens1[j][3]=aliens1[j][1] # get the last Hinfo[1] and
  514.                             aliens1[j][4]=aliens1[j][2] # Hinfo[2] of expld alien and
  515.                             aliens1[j][1]=w+100 # move alien out of screen x
  516.                             aliens1[j][5]=0
  517.  
  518.     if len(bullets)>0 and bullets[-1][0]<=0:#bullet life over so remove from list
  519.         bullets.pop()
  520.  
  521. # if conditions correct add alien to list
  522.     if eventlist[0]==True:
  523.         aliens1delay+=1
  524.  
  525.     if gamecounter>75 and len(aliens1)<=aliens1limit \
  526.        and aliens1delay==aliens1delaylimit:
  527.         aliens1.insert(0,[True,random.randint(0,(w-60)),-60,0, 0,100,0,0,0])
  528.         #                  0    1                         2 3  4  5  6 7 8
  529.     if len(aliens1)>0:
  530.         for i in xrange(len(aliens1)):
  531.             if eventlist[1]==True: # shoot bombs if score if higher than this
  532.                 bombcorridor=pygame.Rect(aliens1[i][1],aliens1[i][2]+ 60,60,300)
  533.                 hero_rect=pygame.Rect(Hinfo[1],Hinfo[2]\
  534.                                       ,Hinfo[8],Hinfo[8])
  535.                 if bombcorridor.colliderect(hero_rect) and aliens1[i][6]<30:
  536.                     aliens1[i][6]+=1
  537.                     if aliens1[i][6]==3:# albomb shoot delay flag
  538.                         aliens1[i][6]=-30
  539.                         rotinfo=getAngle(aliens1[i][1],aliens1[i][2]\
  540.                                          ,aliens1[i][1],Hinfo[2])
  541.                         abx=aliens1[i][1]+18
  542.                         aby=aliens1[i][2]+60
  543.                         angle=math.radians(rotinfo[0]-90); life=40
  544.                         albombs.insert(0,[life,abx,aby,-angle,0,0,False])
  545.  
  546.             if aliens1[i][2]<h and aliens1[i][5]>=0:
  547.                 screen.blit(alien1,(aliens1[i][1], aliens1[i][2]))
  548.                 aliens1[i][2]+=(constant*.5)
  549.  
  550. # if hero collides with alien1 reduce vitals[0]
  551.                 alien1_rect=pygame.Rect(aliens1[i][1], aliens1[i][2],60,60)
  552.                 hero_rect=pygame.Rect(Hinfo[1],Hinfo[2]\
  553.                                       ,Hinfo[8],Hinfo[8])
  554.                 if alien1_rect.colliderect(hero_rect):
  555.                     vitals[0]-=2
  556.                     vitals[2]-=2*1.5
  557.                     healthloss.play()
  558.  
  559.             if aliens1[i][2]<h and aliens1[i][5]<=0 and aliens1[i][0]==False:
  560.                 explode(i, aliens1)
  561.  
  562.             if aliens1[i][2]>=h and aliens1[i][0]==True:
  563.                 aliens1[i][0]=False
  564.                 aliens1[i][5]=0
  565.  
  566.     if len (aliens1)>0 and aliens1[-1][2]>=h and aliens1[-1][5]==0:
  567.         aliens1.pop()
  568.     if aliens1delay>aliens1delaylimit:
  569.         aliens1delay=0
  570.  
  571. #-------------------------------------------------------#
  572. #               THE FIRST ALIEN BAD BOSS                #
  573. #-------------------------------------------------------#
  574.     if eventlist[4]==True and bossinfo[0]==False:
  575.         bossinfo[0]=True; bossinfo[1]=0-50-bosa1.get_width()/2; bossinfo[2]=h/2
  576.     if bossinfo[0]==True:
  577.         rotinfo=getAngle(bossinfo[1], bossinfo[2],Hinfo[1],Hinfo[2])
  578.         bossangle=rotinfo[0]
  579.         bossrad=rotinfo[1]
  580.         bossrotate=rot_center(bosa1,bossangle)
  581.         bossinfo[1]+=math.cos(bossangle)*constant*.5
  582.         bossinfo[2]-=math.sin(bossangle)*constant*.5
  583.         bossinfo[5]+=1
  584.         if bossinfo[5]==25:# bossbomb shoot delay flag
  585.             bossinfo[5]=-60
  586.             abx=bossinfo[1]
  587.             aby=bossinfo[2]
  588.             bsbangle=math.radians(bossangle+90)
  589.             bsblife=80
  590.             bsbombs.insert(0,[bsblife,abx,aby,bsbangle,0,0,False])
  591.         screen.blit(bossrotate,(bossinfo[1]-35,bossinfo[2]-35))
  592.         # draw the boss energy level bar above boss and reduce if hit
  593.         pygame.draw.rect(screen,(255,255,255),[bossinfo[1]-38\
  594.                                            ,bossinfo[2]-38,103,10],2)
  595.         pygame.draw.rect(screen,(255,0,0),[bossinfo[1]-35\
  596.                                            ,bossinfo[2]-35,bossinfo[6]/10,6],0)
  597.  
  598.     update_all(Hinfo)
  599.  
  600. ################################################################################
  601.  
  602.     if keys[pygame.K_p]: #PAUSE GAME
  603.         pygame.image.save(screen,"earthwars_resources\myscreen.jpeg")
  604.         myscreen=pygame.image.load("earthwars_resources\myscreen.jpeg").convert()
  605.         game_paused=True
  606.     if keys[pygame.K_h]: # cheat to increase vitals[0] to max anytime
  607.         vitals[0]=150
  608.         vitals[2]=225
  609.  
  610.  
  611.  
  612. #################### G A  M  E    P A U S E ####################
  613.     while game_paused==True:
  614.         font = pygame.font.Font(None, 100)
  615.         a=random.randrange(0, 255)
  616.         b=random.randrange(0,1)
  617.         c=random.randrange(0,1)
  618.         text = font.render("Paused.", 1, [a,b,c])
  619.         textpos = text.get_rect()
  620.         screen.blit(text,(w/2-(textpos[2]/2),h/2-textpos[3]/2))
  621.         font = pygame.font.Font(None, 40)
  622.         text = font.render("Hit SPACE to continue", 1, red)
  623.         textpos = text.get_rect()
  624.         screen.blit(text,(w/2-(textpos[2]/2),h/2+textpos[3]))
  625.         update_all(Hinfo)
  626.         screen.blit(myscreen,(0,0))
  627.         pygame.event.pump()
  628.         key=pygame.key.get_pressed()
  629.         if key[pygame.K_SPACE]:
  630.                 game_paused=False
  631.  
  632. #################### G A  M  E    O  V  E  R ####################
  633. #                         replay or quit
  634.     while vitals[1]<=0 or bossinfo[6]<=0:
  635.         font = pygame.font.Font(None, 50)
  636.         if bossinfo[6]<=0:
  637.             akuru="YOU WON THE WAR! YOU ARE A HERO!'"
  638.         else:
  639.             akuru="THE WAR WAS LOST!"
  640.         text = font.render(akuru, 1, red)
  641.         textpos = text.get_rect()
  642.         screen.blit(text,(w/2-(textpos[2]/2),h/2-textpos[3]/2))
  643.         font = pygame.font.Font(None, 30)
  644.         text = font.render("Hit ESC to Quit!", 1, red)
  645.         textpos = text.get_rect()
  646.         screen.blit(text,(w/2-(textpos[2]/2),h/2+textpos[3]))
  647.         text = font.render("Hit r to try again", 1, red)
  648.         textpos = text.get_rect()
  649.         screen.blit(text,(w/2-(textpos[2]/2),360))
  650.         update_all(Hinfo)
  651.         screen.blit(myscreen,(0,0))
  652.         pygame.event.pump()
  653.         key=pygame.key.get_pressed()
  654.         if key[pygame.K_r]:
  655.             rotate=90; angle_rad=math.radians(rotate);
  656.             score=[0,0,0,0,0,0]
  657.             scorestr=str(score[0])+str(score[1])+str(score[2])+\
  658.                        str(score[3])+str(score[4])+str(score[5])
  659.             realscore=int(scorestr)
  660.             gamespeed=150; gamecounter=0; game_paused=False
  661.             red=[255,0,0]; kalaya=0
  662.             bossrotate=bosa1
  663.             Hinfo=[False,(w/2),(h-mainhero.get_width()),0,0,False,0,3,35,False,0,0,4,False]
  664.             vitals=[150,5,225]
  665.             bullets=[]
  666.             aliens1=[]; aliens1limit=25; aliens1delay=0; aliens1delaylimit=35
  667.             albombs=[]; albmx=albmy=0
  668.             bsbombs=[]
  669.             fablts=[];fbblts=[]; bombalaunch=bombblaunch=0
  670.             smallboss1=[False,0,0,0,0,0,100,0]
  671.             smallboss2=[False,0,0,0,0,0,100,0]
  672.             bossinfo=[False,0,0,0,0,0,1000]
  673.             eventlist=[False, False, False, False, False]
  674.             angle_rad=math.radians(rotate)
  675.             imageturned=rot_center(mainhero,rotate)
  676.         if key[pygame.K_ESCAPE]:
  677.             game_over()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement