def __init__(self, posx, posy, width, height, movingdown, wave=False, group=False): Collidable.__init__(self,posx,posy,width,height,True) self.vel = 3 self.image.fill((255,255,255)) self.image.set_colorkey((255,255,255)) self.movingdown=movingdown self.destructable=True self.points = 20 self.strength = 1 self.frameindex = 0 self.deathcount = 0 self.group = group #For wave movement pattern self.wave = wave if movingdown: self.miny = posy self.maxy = posy + (height * 4) else: self.miny = posy - (height * 4) self.maxy = posy #load images self.frames = [pygame.image.load(get_file_path("i","boagpulse" + "/boagpulse" + str(n) + ".png")) for n in range(15)] self.deathSeq = [pygame.image.load(get_file_path("i","explosion" + "/explosion" + str(n) + ".png")) for n in range(18)] self.image.blit(self.frames[self.frameindex], (0,0))
def __init__(self, posx, posy, width, height, leftlimit, rightlimit): Collidable.__init__(self,posx,posy,width,height,True) self.vel = 3 self.width = width self.height = height self.shootcounter = 20 self.image.fill((255,255,255)) self.image.set_colorkey((255,255,255)) self.destructable=True self.points = 40 self.strength = 2 self.frameindex = 0 self.deathcount = 0 self.leftlimit = leftlimit self.rightlimit = rightlimit self.movingleft = True self.frameindex = 0 self.animatecount = 0 self.targetingtriangle = TargetTriangle(self.rect.x,self.rect.y,35,70,400) #For targeting the player self.targety = None #load images self.frames = [pygame.image.load(get_file_path("i","boagspider" + "/spider" + str(n) + ".png")) for n in range(9)] self.framesright = [pygame.image.load(get_file_path("i","boagspider-rght" + "/spider-r" + str(n) + ".png")) for n in range(9)] self.deathSeq = [pygame.image.load(get_file_path("i","explosion" + "/explosion" + str(n) + ".png")) for n in range(18)]
def __init__(self, posx, posy, width, height, enemy=None): Collidable.__init__(self,posx,posy,width,height,True) self.vel = 2 self.shootcounter = 0 self.image.fill((255,255,255)) self.image.set_colorkey((255,255,255)) self.destructable=True self.points = 30 self.strength = 2 self.frameindex = 0 self.deathcount = 0 #For targeting the player self.targety = None self.kamakazie = False self.movingright = False self.rotatingright = False self.rotatingleft = False self.rotatecount = 6 self.completedrotation = False #If the player evades the missile long enough it self destructs self.giveupcount = 0 #The enemy the missile is attached to self.enemy = enemy #load images self.frames = [pygame.image.load(get_file_path("i","homingmissile" + "/homingm" + str(n) + ".png")) for n in range(14)] self.deathSeq = [pygame.image.load(get_file_path("i","explosion" + "/explosion" + str(n) + ".png")) for n in range(18)]
def __init__(self, posx, posy, width, height): Projectile.__init__(self, posx, posy, width, height) self.enemy = True self.frames = [ pygame.image.load( get_file_path("i", "projd" + "/projd" + str(n) + ".png")) for n in range(17) ]
def __init__(self, posx, posy, width, height): Collidable.__init__(self,posx,posy,width,height,True) self.vel = 0 self.shootcounter = 0 self.image.fill((255,255,255)) self.image.set_colorkey((255,255,255)) self.destructable=True self.points = 30 self.strength = 2 self.frameindex = 0 self.deathcount = 0 #For targeting the player self.targety = None self.kamakazie = False #load images self.frames = [pygame.image.load(get_file_path("i","boagkama" + "/boagkama" + str(n) + ".png")) for n in range(14)] self.deathSeq = [pygame.image.load(get_file_path("i","explosion" + "/explosion" + str(n) + ".png")) for n in range(18)]
def __init__(self, posx, posy, width, height, movingdown=True, group=False): Collidable.__init__(self,posx,posy,width,height,True) self.vel = 3 self.image.fill((255,255,255)) self.image.set_colorkey((255,255,255)) self.movingdown=movingdown self.destructable=True self.points = 10 self.strength = 1 self.frameindex = 0 self.deathcount = 0 self.group = group #load images self.frames = [pygame.image.load(get_file_path("i","scobot" + "/scobot" + str(n) + ".png")) for n in range(4)] self.deathSeq = [pygame.image.load(get_file_path("i","explosion" + "/explosion" + str(n) + ".png")) for n in range(10)] self.image.blit(self.frames[self.frameindex], (0,0))
def __init__(self, posx, posy, width, height, angle, right=False): Projectile.__init__(self, posx, posy, width, height) self.enemy = True self.right = right self.frames = [ pygame.image.load( get_file_path("i", "projboag" + "/projboag" + str(n) + ".png")) for n in range(14) ] self.angle = angle
def __init__(self, screen, lives, font="Arial"): #Height of scoreboard is 8% of screen height self.height = screen["h"] / 100 * 8 self.livespos = int(screen["w"] / 100 * 75) self.lvpos = int(screen["w"] / 100 * 50) self.image = pygame.Surface((screen["w"], self.height)) self.font = pygame.font.SysFont(font, 30, True) self.score = 0 self.lives = lives self.lifeimage = pygame.image.load(get_file_path("i", "life.png")) self.level = 1
def __init__(self, posx, posy, width): Collidable.__init__(self, posx, posy, 30 * width, 30) self.vel = 3 self.launchcounter = 0 self.image.fill((255, 255, 255)) self.image.set_colorkey((255, 255, 255)) self.destructable = False self.picture = pygame.image.load(get_file_path("i", "block1.png")) for i in range(0, width): self.image.blit(self.picture, (i * 30, 0))
def __init__(self, posx, posy, width, height, map): Collidable.__init__(self,posx,posy,width,height,True) self.vel = 3 self.image.fill((255,255,255)) self.image.set_colorkey((255,255,255)) self.destructable=True self.points = 30 self.strength = 1 self.frameindex = 0 self.deathcount = 0 #Control missile self.missilexoffset = 10 self.missileyoffset = height + 5 self.launched = False #Add the homing missile map.addenemy(HomingMissile(posx+self.missilexoffset,posy+self.missileyoffset,40,10,self)) #load images self.frames = [pygame.image.load(get_file_path("i","boagfighter" + "/boagfight" + str(n) + ".png")) for n in range(9)] self.deathSeq = [pygame.image.load(get_file_path("i","explosion" + "/explosion" + str(n) + ".png")) for n in range(18)]
def __init__(self, posx, posy, width, height): Collidable.__init__(self,posx,posy,width,height,True) self.vel = 3 self.launchcounter = 0 self.image.fill((255,255,255)) self.image.set_colorkey((255,255,255)) self.destructable = True #load images self.frames = [pygame.image.load(get_file_path("i","rocket" + "/rocket" + str(n) + ".png")) for n in range(5)] self.image.blit(self.frames[0], (0,0))
def __init__(self, posx, posy, height): Collidable.__init__(self, posx, posy, 20, (30 * height)) self.vel = 3 self.launchcounter = 0 self.image.fill((255, 255, 255)) self.image.set_colorkey((255, 255, 255)) self.destructable = False #There is a minimum height of three if height < 3: height = 3 self.topend = pygame.image.load(get_file_path("i", "wall1/wall11.png")) self.botend = pygame.image.load(get_file_path("i", "wall1/wall12.png")) self.midsec = pygame.image.load(get_file_path("i", "wall1/wall10.png")) #Draw the wall for i in range(0, height): if i == 0: self.image.blit(self.topend, (0, 0)) elif i == height: self.image.blit(self.botend, (0, i * 30)) else: self.image.blit(self.midsec, (0, i * 30))
def __init__(self, posx, posy, width, height, down=True): Collidable.__init__(self,posx,posy,width,height,True) self.shootcounter = 0 self.image.fill((255,255,255)) self.image.set_colorkey((255,255,255)) self.destructable = False self.down = down #load images self.frame = pygame.image.load(get_file_path("i","gund0.png")) if down: self.image.blit(self.frame, (0,0)) else: self.image.blit(pygame.transform.flip(self.frame, False, True), (0,0))
def __init__(self, posx, posy, width, height): Collidable.__init__(self, posx, posy, width, height, True) self.vel = 10 self.shootcount = 0 self.image.fill((255, 255, 255)) self.image.set_colorkey((255, 255, 255)) self.destructable = False self.hitval = 1 self.enemy = False #Means projectile from the player self.projectile = True self.frames = [ pygame.image.load( get_file_path("i", "projbas" + "/projbas" + str(n) + ".png")) for n in range(9) ] self.image.blit(self.frames[0], (0, 0))
def __init__(self, posx, posy, width, height,type="N"): Collidable.__init__(self,posx,posy,width,height,True) self.vel = 0 self.image.fill((255,255,255)) self.image.set_colorkey((255,255,255)) self.type = type self.movingdown=False self.movingup = False self.movingright=False self.movingleft = False self.destructable=True self.points = 10 self.strength = 1 self.frameindex = 0 self.deathcount = 0 #Change count for deciding when to change behaviour self.changecount = 0 #For splitting self.reproduced = False #load images if type=="N": self.frames = [pygame.image.load(get_file_path("i","boagatom" + "/atom" + str(n) + ".png")) for n in range(19)] self.deathSeq = [pygame.image.load(get_file_path("i","atomsplit" + "/atomsplit" + str(n) + ".png")) for n in range(20)] self.deathlimit = 19 self.framelimit = 14 elif type=="R": self.frames = [pygame.image.load(get_file_path("i","boagatom2" + "/atom2" + str(n) + ".png")) for n in range(24)] self.deathSeq = [pygame.image.load(get_file_path("i","atomexplode" + "/atomexplode" + str(n) + ".png")) for n in range(11)] self.deathlimit = 10 self.framelimit = 23 elif type=="Y": self.frames = [pygame.image.load(get_file_path("i","boagatom3" + "/atom3" + str(n) + ".png")) for n in range(24)] self.deathSeq = [pygame.image.load(get_file_path("i","atomexplode" + "/atomexplode" + str(n) + ".png")) for n in range(11)] self.deathlimit = 10 self.framelimit = 23 elif type=="P": self.frames = [pygame.image.load(get_file_path("i","boagatom4" + "/atom4" + str(n) + ".png")) for n in range(24)] self.deathSeq = [pygame.image.load(get_file_path("i","atomexplode" + "/atomexplode" + str(n) + ".png")) for n in range(11)] self.deathlimit = 10 self.framelimit = 23
def loadlevel(self, levelnumber): if levelnumber == 1: self.levelno = 1 #Define level 1 self.gamearea = {"w": 10000, "h": 600} self.map.gamearea = self.gamearea self.player.rect.x = 100 self.player.rect.y = 300 #Background self.bgimage = pygame.image.load(get_file_path("i", "bg1.jpg")) self.bg.blit(self.bgimage, (0, 0)) #Add objects self.map.addenemy(Scobot(600, 100, 20, 30)) self.map.addenemy(Scobot(650, 200, 20, 30)) self.map.addenemy(Scobot(700, 250, 20, 30)) self.map.addenemy(Scobot(1000, 200, 20, 30)) self.map.addenemy(Scobot(1050, 250, 20, 30)) self.map.addenemy(Scobot(1100, 350, 20, 30)) self.map.addenemygroup(ScobotGroup(1400, 300, 5)) self.map.addenemygroup(ScobotGroup(2000, 100, 5)) self.map.addcollidable(Wall(1800, 250, 8)) self.map.addenemy(Scobot(2250, 500, 20, 30)) self.map.addenemy(Scobot(2550, 300, 20, 30)) self.map.addenemy(Scobot(2550, 400, 20, 30)) self.map.addenemy(BoagPulse(2900, 200, 30, 30, True, True)) self.map.addenemy(BoagPulse(3200, 300, 30, 30, True, True)) self.map.addenemy(BoagPulse(3400, 100, 30, 30, True, True)) self.map.addenemygroup(ScobotGroup(3500, 400, 5)) self.map.addenemygroup(ScobotGroup(3800, 200, 5)) self.map.addcollidable(Wall(4100, 200, 8)) self.map.addenemy(BoagPulse(4600, 50, 30, 30, True, True)) self.map.addenemy(BoagPulse(4600, 450, 30, 30, True, True)) self.map.addenemygroup(BoagPulseGroup(5200, 50, 2, False)) self.map.addenemygroup(BoagPulseGroup(5000, 250, 2, False)) self.map.addenemygroup(BoagPulseGroup(5400, 400, 2, False)) self.map.addenemy(Kamakazie(5700, 100, 55, 20)) self.map.addenemy(Kamakazie(5800, 100, 55, 20)) self.map.addenemy(Scobot(5600, 50, 20, 30)) self.map.addenemy(Scobot(5700, 500, 20, 30)) self.map.addenemy(BoagPulse(6000, 150, 30, 30, True, True)) self.map.addcollidable(Wall(6100, 0, 15)) self.map.addcollidable(Wall(6500, 200, 15)) self.map.addcollidable(Wall(7000, 0, 15)) self.map.addenemy(Scobot(6300, 500, 20, 30)) self.map.addenemy(Scobot(6700, 100, 20, 30)) self.map.addenemy(Scobot(7300, 500, 20, 30)) self.map.addenemygroup(ScobotWall(7500, 100, 15)) self.map.addenemygroup(ScobotWall(7800, 200, 10)) self.map.addenemygroup(ScobotWall(8000, 100, 15)) self.map.addcollidable(Wall(8200, 0, 6)) self.map.addcollidable(Wall(9380, 0, 6)) self.map.addcollidable(Floor1(8200, 170, 40)) self.map.addcollidable(Floor1(8200, 400, 40)) self.map.addcollidable(Wall(8200, 430, 6)) self.map.addcollidable(Wall(9380, 430, 6)) self.map.addenemy(GunUpDown(8400, 200, 20, 30)) self.map.addenemy(GunUpDown(8600, 370, 20, 30, False)) self.map.addenemygroup(ScobotGroup(9000, 250, 5)) self.map.addenemygroup(ScobotGroup(9300, 250, 5)) self.map.addenemy(GunUpDown(8800, 200, 20, 30)) self.map.addenemy(GunUpDown(9000, 370, 20, 30, False)) self.map.addcollidable(Wall(9980, 0, 20)) self.map.addenemy(BoagGunship(9800, 200, 60, 40)) self.map.addcollectable(WormHole(9870, 260, 30, 70, self)) #self.map.addcollidable(Wall(600,0,10)) #self.map.addcollidable(Wall(1200,100,10)) #self.map.addcollidable(Wall(2300,100,10)) elif levelnumber == 2: print("NEW LEVEL") self.levelno = 2 self.gamearea = {"w": 10000, "h": 600} #Reinitialise map self.map = GameMap(self.gamearea, self.screen, self.player) self.map.gamearea = self.gamearea #self.player.rect.x = 100 #self.player.rect.y = 300 self.player.rect.x = 100 self.player.rect.y = 200 self.player.newlevel = True self.map.addenemygroup(ScobotWall(400, 100, 10)) self.map.addenemy(Scobot(650, 200, 20, 30)) self.map.addenemygroup(ScobotGroup(900, 400, 5))
class Player(pygame.sprite.Sprite): moveForward = [ pygame.image.load( get_file_path("i", "shipf" + "/shipf" + str(n) + ".png")) for n in range(9) ] moveBack = [ pygame.image.load( get_file_path("i", "shipr" + "/shipr" + str(n) + ".png")) for n in range(9) ] deathSeq = [ pygame.image.load( get_file_path("i", "explosion" + "/explosion" + str(n) + ".png")) for n in range(20) ] # Constructor. Pass in the color of the block, # and its x and y position def __init__(self, x, y, width, height, lives=3): # Call the parent class (Sprite) constructor pygame.sprite.Sprite.__init__(self) # Create an image of the block, and fill it with a color. # This could also be an image loaded from the disk. self.image = pygame.Surface((width, height)) # Fetch the rectangle object that has the dimensions of the image # Update the position of this object by setting the values of rect.x and rect.y self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y self.width = width self.height = height self.vel = 5 self.isJump = False self.left = False self.right = False self.up = False self.down = False self.walkCount = 0 self.jumpCount = 0 self.standing = True self.grounded = False self.dead = False self.lives = lives self.deathCount = 0 self.death_drop_count = 5 self.deathpos = (0, 0) self.rateoffirecount = 0 #Initiation self.init = True self.initcount = 0 self.alpha = 255 self.newlevel = False def hit(self): print("Hit Goblin!") def draw(self, view): #self.image.fill((255, 255, 255, self.alpha), special_flags=pygame.BLEND_RGBA_MULT) self.image.fill((255, 255, 255)) self.image.set_colorkey((255, 255, 255)) self.image.set_alpha(self.alpha) #Set transparancy if self.walkCount + 1 > 9: self.walkCount = 7 if self.standing: self.walkCount = 0 #Control transparancy on initiation of player for invulnerability at start of level if self.init and self.initcount % 10 != 0: self.alpha -= 25.5 else: self.alpha = 255 if self.initcount >= 100: #Blank out for testing -make permanently invulnerable self.init = False self.alpha = 255 else: self.initcount += 1 if self.dead: #Animate death sequence #increase image size first self.image = pygame.transform.scale(self.image, (50, 50)) #self.image.fill((255,255,255)) #self.image.set_colorkey((255,255,255)) self.image.blit(self.moveForward[0], (0, 0)) self.image.blit(self.deathSeq[self.deathCount], (0, 0)) if self.deathCount < 19: #print(self.deathCount) self.deathCount += 1 else: #Normal movement if not (self.standing): if self.left: self.image.blit(self.moveBack[self.walkCount], (0, 0)) self.walkCount += 1 elif self.right: self.image.blit(self.moveForward[self.walkCount], (0, 0)) self.walkCount += 1 else: if self.right: self.image.blit(self.moveForward[0], (0, 0)) else: self.image.blit(self.moveBack[0], (0, 0)) view.blit(self.image, (self.rect.x, self.rect.y)) def moveleft(self): self.rect.x -= self.vel self.left = True self.right = False self.up = False self.down = False self.standing = False def moveright(self): self.rect.x += self.vel * 2 self.right = True self.left = False self.up = False self.down = False self.standing = False def moveup(self): self.rect.y -= self.vel self.right = True self.left = False self.up = False self.down = False self.standing = False def movedown(self): self.rect.y += self.vel self.right = True self.left = False self.up = False self.down = False self.standing = False def moveplayer(self, keys, screen, gamearea, map, xoffset): #Update the map with the player position #map.playerpos = (self.rect.x,self.rect.y) if self.dead: #Move player up and then off the screen if self.death_drop_count > 0: if self.rect.y > 0: self.rect.y -= 6 self.death_drop_count -= 1 else: self.rect.y += 6 else: if keys[pygame.K_LEFT] and self.rect.x > 0: self.moveleft() elif keys[pygame.K_RIGHT] and self.rect.x < gamearea[ "w"] - self.width and not self.rect.x > xoffset + ( screen["w"] - (self.width + 10)): self.moveright() #Need to stop player hitting bottom of scoreboard elif keys[pygame.K_UP] and self.rect.y > map.scoreboard.height: self.moveup() elif keys[pygame. K_DOWN] and self.rect.y < gamearea["h"] - self.height: self.movedown() elif keys[pygame.K_SPACE]: if self.rateoffirecount <= 0: map.addprojectile( Projectile(self.rect.x + self.width, self.rect.y + (self.height / 2), 20, 5)) self.rateoffirecount = 4 #print(map.gamearea) else: #Control rate of fire self.rateoffirecount -= 1 else: self.standing = True self.rect.x += 1 self.walkCount = 0 #Set jump in motion """ if keys[pygame.K_UP] and self.jumpCount <= 0 and self.grounded: self.jumpCount = 10 self.grounded = False #in the air if self.jumpCount > 0 and self.jumpCount <= 10: print("Jump",self.rect.y) if self.rect.y > 0: self.rect.y -= 12 self.jumpCount -= 1 else: self.jumCount = 0 """ #Get location to scroll the screen along x axis when the man moves def calculatexoffset(self, screen, gamearea): if self.rect.x - screen["w"] // 2 >= 0 and self.rect.x < gamearea[ "w"] - screen["w"] // 2: xoffset = screen["w"] // 2 - self.rect.x elif self.rect.x - screen["w"] // 2 < 0: xoffset = 0 else: xoffset = screen["w"] - gamearea["w"] return xoffset #Get location to scroll the screen along y axis when the man moves def calculateyoffset(self, screen, gamearea): if self.rect.y > screen["h"] // 2 and gamearea["h"] - self.rect.y > ( screen["h"] // 2) - 10: yoffset = 5 + (screen["h"] // 2) - self.rect.y elif gamearea["h"] - self.rect.y < (screen["h"] // 2): yoffset = (screen["h"]) - gamearea["h"] else: yoffset = 0 return yoffset #Detect screen catching player def catchscreen(self, xoffset): xoffset = xoffset * -1 #print(xoffset, ",",self.rect.x) if self.rect.x < xoffset: return True else: return False #Routine for player death def die(self, map, xoffset): if not self.dead and not self.init: self.dead = True self.lives -= 1 map.scoreboard.updatelives(self.lives) #If statement below is for getting the player to jump forward if catching the screen #To stop the insta death after pressing space if self.rect.x < xoffset * -1: self.rect.x += 10 + self.width self.deathpos = (self.rect.x, self.rect.y)
def __init__(self,posx,posy,width,height,level): Collectable.__init__(self, posx, posy, width, height) self.frames = [pygame.image.load(get_file_path("i","wormhole" + "/w-hole" + str(n) + ".png")) for n in range(15)] self.frameindex = 0 self.level=level