|
| 1 | +#! /usr/bin/env python |
| 2 | +import pygame |
| 3 | +#from pygame.locals import * |
| 4 | +import sys |
| 5 | +import obj |
| 6 | +import random |
| 7 | + |
| 8 | +pygame.init() |
| 9 | + |
| 10 | +#set up window |
| 11 | +screen = pygame.display.set_mode((obj.SCREENW, obj.SCREENH), pygame.DOUBLEBUF) |
| 12 | +pygame.display.set_caption("KILL THE ALIENS") |
| 13 | + |
| 14 | +#set text font |
| 15 | +myfont = pygame.font.SysFont("monospace", 15) |
| 16 | + |
| 17 | +#create singleton images for efficiency |
| 18 | +shipimg = pygame.image.load("spaceshipa.png") |
| 19 | +saucerimg = pygame.image.load("saucera.png") |
| 20 | +bulletimg = pygame.image.load("bullet.png").convert() |
| 21 | +bossimg = pygame.image.load("invader.png") |
| 22 | + |
| 23 | +#actually transparent square |
| 24 | +blacksquare = pygame.Surface((obj.explosion[0].get_width()-15, obj.explosion[0].get_height()-15), pygame.SRCALPHA, 32) |
| 25 | + |
| 26 | +#set up game objects |
| 27 | +ship = obj.Player(shipimg) |
| 28 | +#sprite groups |
| 29 | +saucers = [] |
| 30 | +bullets = [] |
| 31 | +#gone = False |
| 32 | +#killed = pygame.sprite.Group() |
| 33 | + |
| 34 | +#create enemies |
| 35 | +for x in range(0,3): |
| 36 | + saucers.append(obj.Enemy(random.randrange(0, obj.SCREENW), random.randrange(0, 100), saucerimg)) |
| 37 | + |
| 38 | +#create boss |
| 39 | +boss = obj.Boss(100, -1200, bossimg,0) |
| 40 | + |
| 41 | +#create boss explosions |
| 42 | +boom = [] |
| 43 | +boom.append(obj.MoveableObject(0, 0, pygame.Surface((1, 1)))) |
| 44 | + |
| 45 | +clock = pygame.time.Clock() |
| 46 | +bg = pygame.image.load("map.png").convert() |
| 47 | +bgoffset = 0 |
| 48 | +FPS = 30 |
| 49 | + |
| 50 | +#flags |
| 51 | +#0 means play, 1 means user exit, 2 means death, 3 means victory |
| 52 | +endgame = 0 |
| 53 | +#0 means no boss, 1 means clear out shop for boss, 2 means boss entering, |
| 54 | +#3 means boss is out, 4 means dying, 5 means dead |
| 55 | +BEASTMODE = 0 |
| 56 | + |
| 57 | +BLACK = (0,0,0) |
| 58 | +blacksquare.fill(BLACK) |
| 59 | + |
| 60 | +score = 0 |
| 61 | +time = 0 #total play time |
| 62 | +endtime = 0 |
| 63 | + |
| 64 | +changeover = 0 #for scroll change over |
| 65 | +ychng = 0 |
| 66 | + |
| 67 | +goright = False |
| 68 | +goleft = False |
| 69 | + |
| 70 | +#print saucers |
| 71 | +deadindex = -10 |
| 72 | + |
| 73 | +intro = 1 |
| 74 | +introscreen = pygame.image.load("intro.png") |
| 75 | + |
| 76 | +#opening screen |
| 77 | +while(intro == 1): |
| 78 | + screen.fill(BLACK) |
| 79 | + screen.blit(introscreen, (0,0)) |
| 80 | + pygame.display.flip() |
| 81 | + for event in pygame.event.get(): |
| 82 | + if event.type == pygame.QUIT: |
| 83 | + sys.exit() |
| 84 | + if not hasattr(event, 'key'): continue |
| 85 | + if event.type == pygame.KEYDOWN: |
| 86 | + if (event.key == pygame.K_RETURN): intro = 0 |
| 87 | + |
| 88 | + |
| 89 | +#begin main game loop |
| 90 | +#this should have all been put in a function T_T |
| 91 | +while(endgame == 0): |
| 92 | + ticktime = clock.tick(FPS) #update time in milliseconds |
| 93 | + time += ticktime |
| 94 | + |
| 95 | + #add more saucers to increase difficulty as time goes on |
| 96 | + #maybe we could combine this making number of saucers a function of time |
| 97 | + #boom, it is done |
| 98 | + if(len(saucers)-3 < time/12000 and BEASTMODE == 0): |
| 99 | + #print len(saucers) |
| 100 | + #print time/6000 |
| 101 | + saucers.append(obj.Enemy(random.randrange(0, obj.SCREENW), random.randrange(-200, -50), saucerimg)) |
| 102 | + #if(time >= 8000 and len(saucers) < 5): |
| 103 | + #saucers.append(obj.Enemy(random.randrange(0, obj.SCREENW), random.randrange(-200, -50), saucerimg)) |
| 104 | + |
| 105 | + #ENTER THE BOSS |
| 106 | + if(len(saucers) > 10): #change that number for max saucers on screen |
| 107 | + BEASTMODE = 1 |
| 108 | + #del saucers[:] #this removes the whole list |
| 109 | + |
| 110 | + #user input |
| 111 | + for event in pygame.event.get(): |
| 112 | + if event.type == pygame.QUIT: endgame = 1 |
| 113 | + if not hasattr(event, 'key'): continue |
| 114 | + if event.key == pygame.K_ESCAPE: endgame = 1 |
| 115 | + if(event.type == pygame.KEYDOWN): |
| 116 | + if (event.key == pygame.K_LEFT and ship.x >= 0): |
| 117 | + goleft = True |
| 118 | + elif (event.key == pygame.K_RIGHT and ship.x+ship.width <= obj.SCREENW): |
| 119 | + goright = True |
| 120 | + if(event.type == pygame.KEYUP): |
| 121 | + if (event.key == pygame.K_LEFT): |
| 122 | + goleft = False |
| 123 | + elif (event.key == pygame.K_RIGHT): |
| 124 | + goright = False |
| 125 | + if (event.key == pygame.K_SPACE and ship.active): |
| 126 | + bullets.append(ship.fire(bulletimg)) |
| 127 | + |
| 128 | + if(goright == True and ship.x+ship.width <= obj.SCREENW): ship.x += 5 |
| 129 | + elif(goleft == True and ship.x >= 0): ship.x -= 5 |
| 130 | + |
| 131 | + ship.updatepos() |
| 132 | + |
| 133 | + if(BEASTMODE == 3): |
| 134 | + if(boss.infirerange(ship) > 0): |
| 135 | + if(random.randrange(0,10) == 1): |
| 136 | + bullets.append(boss.fire(bulletimg, obj.LEFT)) |
| 137 | + if(random.randrange(0,10) == 1): |
| 138 | + bullets.append(boss.fire(bulletimg, obj.RIGHT)) |
| 139 | + #elif(random.randrange(0,20) == 1): |
| 140 | + # bullets.append(boss.fire(bulletimg, obj.LEFT)) |
| 141 | + # bullets.append(boss.fire(bulletimg, obj.RIGHT)) |
| 142 | + |
| 143 | + #potentially better collision detection |
| 144 | + #for bullet in bullets: |
| 145 | + #bullet.move() |
| 146 | + #pygame.sprite.spritecollide(bullet, saucers, 1) |
| 147 | + #if killed: |
| 148 | + #print killed |
| 149 | + #print saucers |
| 150 | + |
| 151 | + for saucer in saucers: |
| 152 | + dietest = saucer.move(BEASTMODE) |
| 153 | + if dietest == 1: |
| 154 | + deadindex = saucers.index(saucer) |
| 155 | + #print "DEAD " + str(deadindex) |
| 156 | + |
| 157 | + #print "DEAD " + str(deadindex) |
| 158 | + #print "LEN " + str(len(saucers)) |
| 159 | + |
| 160 | + #move bullets, check for collisions with player or boss or off screen |
| 161 | + #explosions as well |
| 162 | + #just an iteration through all bullets |
| 163 | + for bullet in bullets: |
| 164 | + bullet.move() |
| 165 | + #-60 to go a little off screen, for high up explosions |
| 166 | + if(bullet.y < -60 or bullet.y > obj.SCREENH): bullet.active = False |
| 167 | + if(obj.collide(ship, bullet)): |
| 168 | + ship.health -= 1 |
| 169 | + bullet.active = False |
| 170 | + elif(BEASTMODE == 3 and obj.collide(boss, bullet)): |
| 171 | + boss.health -= 5 |
| 172 | + if(boss.health <= 0): |
| 173 | + boss.die() |
| 174 | + BEASTMODE += 1 |
| 175 | + bullet.active = False |
| 176 | + if(-1 < bullet.exploding < 4): bullet.explode(time) |
| 177 | + if(bullet.active == False): bullets.remove(bullet) |
| 178 | + |
| 179 | + #print "Boss Health: " + str(boss.health) |
| 180 | + #just for kicks |
| 181 | + #inefficient collision detection |
| 182 | + #but it works for now |
| 183 | + for saucer in saucers: |
| 184 | + if(obj.collide(saucer, ship)): |
| 185 | + ship.health -= 1 |
| 186 | + saucer.respawn() |
| 187 | + #if ship.health <= 0: endgame = 0 #change to 2 for kill |
| 188 | + for bullet in bullets: |
| 189 | + if(obj.collide(saucer, bullet)): |
| 190 | + bullet.x = saucer.x |
| 191 | + bullet.y = saucer.y |
| 192 | + bullet.updatepos() |
| 193 | + #respawn saucer off screen and increment score |
| 194 | + if(BEASTMODE != 1): saucer.respawn() |
| 195 | + else: |
| 196 | + deadindex = saucers.index(saucer) |
| 197 | + dietest = 1 |
| 198 | + bullet.explode(time) |
| 199 | + #saucers.remove(saucer) #this removes te actual object from the list |
| 200 | + score += 5 |
| 201 | + |
| 202 | + #this is so that we don't mess up the previous for iteration |
| 203 | + if(dietest == 1): |
| 204 | + saucers.pop(deadindex) |
| 205 | + dietest = 0 |
| 206 | + #print "LEN " + str(len(saucers)) |
| 207 | + if(len(saucers) == 0): |
| 208 | + BEASTMODE = 2 |
| 209 | + boss.inittime = time |
| 210 | + |
| 211 | + #for player death |
| 212 | + if(ship.health <= 0): |
| 213 | + ship.die() |
| 214 | + if(ship.active == False): |
| 215 | + ship.explode(time) |
| 216 | + if(endtime == 0): |
| 217 | + endtime = time |
| 218 | + #for time delay after death |
| 219 | + if(time >= endtime + 4000 and endtime != 0): |
| 220 | + #print "game over" |
| 221 | + endgame = 2 |
| 222 | + |
| 223 | + #if boss got killed make sonic style boss death explosion |
| 224 | + #explode is called multiple times over several main loops to advance the explosion frame |
| 225 | + if(BEASTMODE == 4): |
| 226 | + for splat in boom: |
| 227 | + #if first pass, initialize explosion sequence |
| 228 | + if(len(boom) == 1 and splat.exploding == -1): |
| 229 | + splat.x = random.randrange(boss.x, boss.x+boss.width-obj.explosion[0].get_width()) #subtract explosion width |
| 230 | + splat.y = random.randrange(boss.y, boss.y+boss.height-obj.explosion[0].get_height()) |
| 231 | + obj.explosion.append(blacksquare) #to take chunks out of boss |
| 232 | + splat.explode(time) |
| 233 | + if(boom[-1].exploding == 2 and len(boom) < 8): |
| 234 | + boom.append(obj.MoveableObject(random.randrange(boss.x, boss.x+boss.width-obj.explosion[0].get_width()), random.randrange(boss.y, boss.y+boss.height-obj.explosion[0].get_height()), pygame.Surface((1,1)))) |
| 235 | + #if boss is done exploding |
| 236 | + if(len(boom) == 8 and boom[-1].exploding == len(obj.explosion)-1): |
| 237 | + BEASTMODE = 5 |
| 238 | + score += 1000 |
| 239 | + endtime = time |
| 240 | + print "BEASTMODE 5" |
| 241 | + |
| 242 | + #if(boom[7].exploding == 4): |
| 243 | + # endtime = time |
| 244 | + # BEASTMODE = 5 |
| 245 | + #meh inefficient |
| 246 | + #this may have to be reexamined, testing beastmode < 3 |
| 247 | + if(boss.y > 0 and BEASTMODE < 3): BEASTMODE = 3 |
| 248 | + |
| 249 | + #if boss is displayed |
| 250 | + if(4 > BEASTMODE >= 2): |
| 251 | + boss.move(ship, time) |
| 252 | + |
| 253 | + #text rendering |
| 254 | + healthlbl = myfont.render("Health: " + str(ship.health), 1, (255,255,0)) |
| 255 | + scorelbl = myfont.render("Score: " + str(score), 1, (255,255,0)) |
| 256 | + bosslbl = myfont.render("Boss Health: " + str(boss.health), 1, (255,255,0)) |
| 257 | + #render images |
| 258 | + screen.fill(BLACK) |
| 259 | + |
| 260 | + if(changeover == 0): |
| 261 | + screen.blit(bg, (0,0), (0, 2000-obj.SCREENH-bgoffset, obj.SCREENW, 2000-bgoffset)) |
| 262 | + elif(changeover == 1): |
| 263 | + #print "ychng: " + str(ychng) |
| 264 | + #print "bgoffset: " + str(bgoffset) |
| 265 | + screen.blit(bg, (0,0), (0, bg.get_height()-ychng, obj.SCREENW, 2000)) |
| 266 | + screen.blit(bg, (0, ychng), (0, 0, obj.SCREENW, obj.SCREENH - ychng)) |
| 267 | + |
| 268 | + if(BEASTMODE >= 2): screen.blit(boss.image, (boss.x, boss.y)) |
| 269 | + screen.blit(ship.image, (ship.x, ship.y)) |
| 270 | + for saucer in saucers: |
| 271 | + screen.blit(saucer.image, (saucer.x, saucer.y)) |
| 272 | + for bullet in bullets: |
| 273 | + screen.blit(bullet.image, (bullet.x, bullet.y)) |
| 274 | + if(BEASTMODE >= 4): |
| 275 | + #if(boom[-1].exploding == 2): |
| 276 | + #print "blacksquare" |
| 277 | + #boss.image.blit(blacksquare, (boom[-1].x, boom[-1].y)) |
| 278 | + for splat in boom: |
| 279 | + screen.blit(splat.image, (splat.x, splat.y)) |
| 280 | + #screen.blit(explosion[explframe], (obj.SCREENW/2, obj.SCREENH/2)) |
| 281 | + screen.blit(healthlbl, (obj.SCREENW - 100, 20)) |
| 282 | + screen.blit(scorelbl, (obj.SCREENW - 100, 35)) |
| 283 | + if(BEASTMODE >= 3): screen.blit(bosslbl, (obj.SCREENW/2, 20)) |
| 284 | + |
| 285 | + #for i in range(0,5): |
| 286 | + #screen.blit(obj.explosion[i], (i*120, 300)) |
| 287 | + |
| 288 | + pygame.display.flip() #apply double buffer |
| 289 | + |
| 290 | + #if(explframe > 3): explframe=0 |
| 291 | + #else: explframe += 1 |
| 292 | + |
| 293 | + #change offset for vertical scroll |
| 294 | + if(bgoffset > 2000): |
| 295 | + bgoffset = 0 |
| 296 | + changeover = 0 |
| 297 | + ychng = 0 |
| 298 | + else: bgoffset+=2 |
| 299 | + |
| 300 | + if(2000 >= bgoffset > 2000-obj.SCREENH): |
| 301 | + ychng += 2 |
| 302 | + changeover = 1 |
| 303 | + #print "time: " + str(time) |
| 304 | + #print "health: " + str(ship.health) |
| 305 | + #print "score: " + str(score) |
| 306 | +#end game loop |
| 307 | + |
| 308 | +#display end screens |
| 309 | +if(BEASTMODE == 5): |
| 310 | + disp = pygame.image.load("victory.png") |
| 311 | +else: |
| 312 | + disp = pygame.image.load("dead.png") |
| 313 | + |
| 314 | +#add loop to get input, continue to high scores, etc |
| 315 | +cont = 0 |
| 316 | +while(cont == 0): |
| 317 | + for event in pygame.event.get(): |
| 318 | + if event.type == pygame.QUIT: cont = 1 |
| 319 | + if not hasattr(event, 'key'): continue |
| 320 | + if event.key == pygame.K_ESCAPE: cont = 1 |
| 321 | + screen.blit(disp, (0,0)) |
| 322 | + pygame.display.flip() |
| 323 | +#elif(endgame == 1): continue |
| 324 | + |
| 325 | +#update and view high scores |
| 326 | +#open file |
| 327 | +#scores = [] |
| 328 | +#f = open('scores', 'rw') |
| 329 | +#for line in f: |
| 330 | +# scores.append(line) |
| 331 | +#print scores |
| 332 | +#read scores into list |
| 333 | +#compare score to list |
| 334 | +#display high scores |
| 335 | + |
| 336 | + |
| 337 | +pygame.quit() |
| 338 | +sys.exit() |
0 commit comments