def attackEnemy(self,enemy,second = False):
     roll = d(20)
     attackRoll = self.attack + roll
     if attackRoll >= enemy.armorClass or roll == 20:
         self.hitEnemy(enemy,second)
         if roll >= self.critMin:
             self.hitEnemy(enemy,second)
 def attackTower(self,tower):
     roll = d(20)
     attackRoll = self.attackMod + roll
     if attackRoll >= tower.armorClass or roll == 20:
         self.hitTower(tower)
         if roll == 20:
             self.hitTower(tower)
 def upgrade(self):
     if player.money>=self.upgradeCost and self.xp>=self.upgradeXp:
         player.money-=self.upgradeCost
         self.level += 1
         self.bab = self.level
         self.maxhp += d(10,1) + 3
         self.hp = self.maxhp
         if self.level == 2:
             self.startTargetTimer /= 2
             self.attackMod -= 2
         if self.level == 4:
             self.damageMod += 3
         if self.level == 5:
             self.attackMod += 1
             self.damageMod += 1
         if self.level == 6:
             self.attackMod += 1
             self.damageMod += 1
             self.range += 1*squsize
         if self.level == 8:
             self.critMin -= 2
         if self.level == 9:
             self.attackMod += 1
             self.damageMod += 1
         self.calcUpgrade()
 def __init__(self,wave,letter):
     self.image = enemyimagearray[0]
     self.curnode = 0
     self.distance = 0
     if letter=='a':
         self.movelist = mapvar.pointmovelists[0][:]
     elif letter=='b':
         self.movelist = mapvar.pointmovelists[1][:]
     elif letter=='c':
         self.movelist = mapvar.pointmovelists[2][:]
     elif letter=='d':
         self.movelist = mapvar.pointmovelists[3][:]
     self.rect = self.image.get_rect(center=(self.movelist[self.curnode]))
     enemylist.append(self)
     self.CR = int(mapvar.mapdict['wave'+str(wave)+letter][1])
     self.cost = CRcost[self.CR]/mapvar.mapdict['wave'+str(wave)+letter][0]
     self.health = d(8,mapvar.mapdict['wave'+str(wave)+letter][2])
     self.speed = mapvar.mapdict['wave'+str(wave)+letter][4]
     self.starthealth = self.health
     self.startspeed = self.speed
     self.route = 1
     self.range = 2*squsize
     self.slowtimers = list()
     self.slowpercent = 1.0
     self.holdcentx = self.rect.centerx*1.0
     self.holdcenty = self.rect.centery*1.0
     self.poisontimer = None
     self.armorClass = 10 + mapvar.mapdict['wave'+str(wave)+letter][3]
     self.dr = 0
     self.xp = CRxp[self.CR]/mapvar.mapdict['wave'+str(wave)+letter][0]
     self.damageDie = mapvar.mapdict['wave'+str(wave)+letter][6]
     self.damageDieAmt = mapvar.mapdict['wave'+str(wave)+letter][5]
     self.damageMod = mapvar.mapdict['wave'+str(wave)+letter][7]
     self.attackMod = mapvar.mapdict['wave'+str(wave)+letter][8]
     self.targetTimer = self.startTargetTimer = 1.0
 def attack(self):
     player.health -= (d(self.damageDie,self.damageDieAmt) + self.damageMod)
     enemylist.remove(self)
     if self.poisontimer:
         self.poisontimer.kill = True
     if player.health<=0:
         print "You Lost!"
         sys.exit(1)
     return
 def attackEnemy(self,enemy):
     penalty = 0
     while(self.bab-penalty>=1):
         roll = d(20)
         attackRoll = self.bab-penalty + self.attackMod + roll
         if attackRoll >= enemy.armorClass or roll == 20:
             self.hitEnemy(enemy)
             if roll >= self.critMin:
                 self.hitEnemy(enemy)
         penalty += 5
def main():
    pygame.init()
    print "Pygame Initialized"
    pygame.display.set_caption("PyGame Tower Defence Game")
    pygame.mouse.set_visible(1)
    #localdefs: mapvar = Map()
    background = mapvar.loadMap((scrwid,scrhei),pickMap())
    print "Map Object Generated"
    screen = pygame.display.set_mode((scrwid,scrhei))
    print "Display Initialized"
    clock = pygame.time.Clock()
    genEnemyImageArray() #localdefs
    print "Enemy Image Array Generated"
    run=True #Run loop
    wavenum = 0
    selected = None #Nothing is selected

    MainFunctions.makeIcons()
    
    #Openbutton is the white surface on which the Icon sits
    openbutton = pygame.Surface((iconlist[len(iconlist)-1].rect.right+5,25))
    openbuttonrect = pygame.Rect((0,scrhei-25),(iconlist[len(iconlist)-1].rect.right+5,25))
    openbutton.fill((255,255,255))
    print "Begin Play Loop!!!"
    speed = 3
    frametime = speed/30.0
    while run:
        starttime = time.time()

        MainFunctions.tickAndClear(screen, clock, background)

        MainFunctions.workSenders(frametime)

        MainFunctions.workTowers(screen,frametime)

        MainFunctions.dispExplosions(screen)

        MainFunctions.dispText(screen,wavenum)

        MainFunctions.workEnemies(screen,frametime)

        for event in pygame.event.get():
            if event.type == MOUSEBUTTONUP:
                #If the user left-clicks...
                if event.dict['button']==1:
                    if nexttextpos.collidepoint(event.dict['pos']):
                        wavenum+=1
                        player.health += d(8) + 2
                        for tower in towerlist:
                            if tower.hp < tower.maxhp:
                                tower.hp = min(tower.hp+tower.level,tower.maxhp)
                        if ('wave'+str(wavenum)+'a') in mapvar.mapdict:
                            Sender(wavenum,'a')
                        if ('wave'+str(wavenum)+'b') in mapvar.mapdict:
                            Sender(wavenum,'b')
                        if ('wave'+str(wavenum)+'c') in mapvar.mapdict:
                            Sender(wavenum,'c')
                        if ('wave'+str(wavenum)+'d') in mapvar.mapdict:
                            Sender(wavenum,'d')
                        if all([('wave'+str(wavenum)+letter) not in mapvar.mapdict for letter in ['a','b','c','d']]):
                            if len(enemylist) == 0:
                                background=mapvar.loadMap((scrwid,scrhei),pickMap())
                                screen = pygame.display.set_mode((scrwid,scrhei))
                                wavenum=1
                            else:
                                print "There are still enemies on the screen!"
                                wavenum-=1
                    elif selected:
                        if selected.__class__ == Icon:
                            if event.dict['pos'][1]<scrhei-squsize:
                                if player.money>=eval(selected.type+"Tower").basecost:
                                    if (not any([ttower.rect.collidepoint(event.dict['pos']) for ttower in towerlist])
                                        and not any([p.inflate(25,25).collidepoint(event.dict['pos']) for pathrectlist in mapvar.pathrectlists for p in pathrectlist])):
                                        eval(selected.type+selected.base)(event.dict['pos'])
                                        selected = None
                            else:
                                selected = None
                        elif Tower in selected.__class__.__bases__:
                            EventFunctions.leftSelectedTower(event, selected)
                    else:
                        for object in (towerlist+iconlist):
                            if object.rect.collidepoint(event.dict['pos']):
                                selected = object
                else:
                    selected = None
            keyinput = pygame.key.get_pressed()
            if event.type == QUIT:
                sys.exit()
            if keyinput[K_ESCAPE]:
                sys.exit()
            if keyinput[K_f]:
                screen = pygame.display.set_mode((scrwid,scrhei),FULLSCREEN)
            if keyinput[K_w]:
                screen = pygame.display.set_mode((scrwid,scrhei))

        font = pygame.font.Font(None,20)

        MainFunctions.dispStructures(screen)

        nexttextpos = MainFunctions.dispNextWave(screen)

        if selected and selected.__class__ == Icon:
            MainFunctions.selectedIcon(screen, selected)

        if selected and Tower in selected.__class__.__bases__:
            MainFunctions.selectedTower(screen, selected)

        screen.blit(openbutton,openbuttonrect)
        for icon in iconlist:
            screen.blit(icon.img,icon.rect)
            if icon.rect.collidepoint(pygame.mouse.get_pos()):
                text = font.render("%s Tower (%d)" % (icon.type,eval(icon.type+"Tower").basecost),1,(0,0,0))
                textpos = text.get_rect(left=icon.rect.left,bottom=icon.rect.top-2)
                screen.blit(text,textpos)

        screen.blit(mapvar.baseimg,mapvar.baserect)

        pygame.display.flip()

        frametime = (time.time() - starttime) * speed
 def damage(self):
     return d(self.damageDie,self.damageDieAmt) + self.damageMod
 def damage(self):
     return d(6,2) + self.damageMod