Exemplo n.º 1
0
    def event(self, e):
        # if e.type is KEYDOWN and e.key in (K_ESCAPE,):
        if e.type is USEREVENT and e.action == 'exit':
            next = menu.Transition(self.game, self.parent)
            return menu.Prompt(self.game, 'quit? y/n', next, self)

        elif e.type is USEREVENT and e.action == 'menu':
            next = menu.Transition(self.game, self.parent)
            return menu.Weapon(self.game, next, self)

        if self.player != None:
            self.player.event(self, self.player, e)
Exemplo n.º 2
0
 def event(self, e):
     # if e.type is KEYDOWN and e.key in (K_ESCAPE,):
     if e.type is USEREVENT and e.action == 'exit':
         next = menu.Transition(self.game, self.parent)
         return menu.Prompt(self.game, ['paused', '(' + EXIT_HELP + ')'],
                            next, self)
     elif self.player is not None:
         self.player.event(self, self.player, e)
Exemplo n.º 3
0
    def loop(self):
        #record the high scores
        #print('level loop')
        # curframe = inspect.currentframe()
        # calframe = inspect.getouterframes(curframe, 2)
        # print('caller name:', calframe[1][3])

        # stack = inspect.stack()
        # the_class = stack[1][0].f_locals["self"].__class__.__name__
        # the_method = stack[1][0].f_code.co_name
        #
        # print("I was called by {}.{}()".format(the_class, the_method))
        self.game.high = max(self.game.high, self.game.score)

        if self.status != '_first':
            # start up some new sprites ...
            r = self.get_border(INIT_BORDER)
            self.run_codes(
                pygame.Rect(r.x, r.y, r.w,
                            TH))  #top #MYNOTE this is where init normal enemy?
            self.run_codes(pygame.Rect(r.right - TW, r.y, TW, r.h))  #right
            self.run_codes(pygame.Rect(r.x, r.bottom - TH, r.w, TH))  #bottom
            self.run_codes(pygame.Rect(r.x, r.y, TW, r.h))  #left

            # grab the current existing sprites
            # doing this avoids a few odd situations
            sprites = self.sprites[:]  #MYNOTE first time self.sprites = []?? may be tile_to_sprite when print sprites it is living things
            #print(sprites)

            # mark off the previous rect
            for s in sprites:
                s.prev = pygame.Rect(s.rect)

            # let the sprites do their thing
            for s in sprites:
                if s.loop == None: continue
                s.loop(self, s)

            # re-calculate the groupings
            groups = {}
            for s in sprites:
                for g in s.groups:
                    if g not in groups: groups[g] = []
                    groups[g].append(s)

            # hit them sprites! w-tsh!
            for s1 in sprites:
                for g in s1.hit_groups:
                    if g not in groups: continue
                    for s2 in groups[g]:
                        if not s1.rect.colliderect(s2.rect): continue
                        s1.hit(self, s1, s2)

            # hit walls and junk like that
            for s in sprites:
                if not len(s.groups): continue
                r = s.rect
                hits = []
                for y in xrange(r.top - r.top % TH, r.bottom, TH):
                    for x in xrange(r.left - r.left % TW, r.right, TW):
                        t = self.layer[y / TH][x / TW]
                        if t == None: continue
                        if not t.hit_groups.intersection(s.groups): continue
                        dist = abs(t.rect.centerx -
                                   s.rect.centerx) + abs(t.rect.centery -
                                                         s.rect.centery)
                        hits.append([dist, t])

                hits.sort()
                for dist, t in hits:
                    if not t.rect.colliderect(s.rect): continue
                    t.hit(self, t, s)

            # remove inactive sprites
            border = self.get_border(DEINIT_BORDER)
            for s in sprites:
                if s.auto_gc and not border.colliderect(s.rect):
                    s.active = False
                if not s.active:
                    self.sprites.remove(
                        s)  # this removes 'em from the real list!
                    if hasattr(s, 'deinit'):
                        s.deinit(self, s)
                    if hasattr(s, '_code'):
                        if s._code not in self.codes:
                            print 'error in code GC', s._code
                            continue
                        del self.codes[s._code]

            #pan the screen
            if self.player != None:
                self.player.pan(self, self.player)

        # printing tiles position
        self.tilesData = []
        for l in self.layer:
            for t in l:
                if t is not None:
                    #if not hasattr(t,'standable'): break
                    #if t.standable != 1: break

                    tileData = [t.rect.centerx, t.rect.centery, t.hit_groups]
                    self.tilesData.append(tileData)

        # screenView = self.view

        # printing sprites rect position MYCODE
        self.bossSprite = None
        self.playerSprite = None
        self.playerPos = [0, 0]
        self.spritesData = []
        for s in self.sprites:
            spriteData = [s.rect.centerx, s.rect.centery]
            if hasattr(s, 'type'):
                spriteData.append(s.type)
                if s.type == 'player': self.playerSprite = s
                if s.type == 'boss': self.bossSprite = s
            self.spritesData.append(spriteData)

        # agentConnect.getScreen(tilesData, spritesData,screenView.left,screenView.top)

        if self.playerSprite != None:
            self.playerPos = [
                self.playerSprite.rect.centerx, self.playerSprite.rect.centery
            ]
        if self.bossSprite != None:
            self.bossStrength = self.bossSprite.strength
        else:
            self.bossStrength = 6
        self.currentFitness = agentConnect.fitnessF(self.playerPos, self.title,
                                                    self.bossStrength)
        if self.currentFitness > self.bestFitness:
            self.bestFitness = self.currentFitness
            self.notImproved = 0
        else:
            if self.title == "Boss" or self.title == "boss_1.tga":
                self.notImproved += 0.5
            else:
                self.notImproved += 1

        if self.currentFitness % 1 == 0:
            agentConnect.doAction('up')

        #print self.status

        #MYCODE random control
        # if self.frame % 180 < 120:
        #     agentConnect.doAction('right')
        # else:
        #     agentConnect.doAction('left')
        # agentConnect.doAction('jump')
        # agentConnect.doAction('bubble')

        # more frames
        self.frame += 1
        if (self.frame % FPS) == 0:
            pass
            #print self.player.rect.bottom
            #print ''
            #print 'frame:',self.frame
            #print 'sprites:',len(self.sprites)
            #print 'codes:',len(self.codes)

        #handle various game status'

        if self.status == '_first':
            if self.player.exploded:
                self.player.loop(self, self.player)
            else:
                self.status = 'first'
        # elif self.status == 'first':
        #     self.status = None
        #     return menu.Pause(self.game,'get ready',self)
        elif self.status == 'exit':
            self.game.lcur = (self.game.lcur + 1) % len(levels.LEVELS)
            if self.game.lcur == 0:
                # you really won!!!
                # self.game.music_play('finish')
                # next = menu.Transition(self.game,self.parent)
                # return menu.Pause(self.game,'CONGRATULATIONS!',next)
                next = menu.Transition(self.game, self.parent)
                return menu.Pause(self.game, 'GAME END', next)

            else:
                # self.game.music_play('lvlwin',1)
                # l2 = Level(self.game,self.fname,self.parent)
                # next = menu.Transition(self.game,l2)
                # return menu.Pause(self.game,'good job!',next)
                next = menu.Transition(self.game, self.parent)
                return menu.Pause(self.game, 'GAME END', next)

        elif self.status == 'dead':
            if self.agentCon is not None:
                self.agentCon.setGameEnd()
            # if self.game.lives:
            #     self.game.lives -= 1
            #     return menu.Transition(self.game,Level(self.game,self.fname,self.parent))
            # else:
            #     next = menu.Transition(self.game,self.parent)
            #     return menu.Pause(self.game,'game over',next)
            next = menu.Transition(self.game, self.parent)
            return menu.Pause(self.game, 'GAME END', next)
        elif self.status == 'transition':
            self.status = None
            return menu.Transition(self.game, self)
Exemplo n.º 4
0
    def loop(self):
        #record the high scores
        self.game.high = max(self.game.high, self.game.score)

        if self.status != '_first':
            # start up some new sprites ...
            r = self.get_border(INIT_BORDER)
            self.run_codes(pygame.Rect(r.x, r.y, r.w, TH))  #top
            self.run_codes(pygame.Rect(r.right - TW, r.y, TW, r.h))  #right
            self.run_codes(pygame.Rect(r.x, r.bottom - TH, r.w, TH))  #bottom
            self.run_codes(pygame.Rect(r.x, r.y, TW, r.h))  #left

            # grab the current existing sprites
            # doing this avoids a few odd situations
            sprites = self.sprites[:]

            # mark off the previous rect
            for s in sprites:
                s.prev = pygame.Rect(s.rect)

            # let the sprites do their thing
            for s in sprites:
                if s.loop == None: continue
                s.loop(self, s)

            # re-calculate the groupings
            groups = {}
            for s in sprites:
                for g in s.groups:
                    if g not in groups: groups[g] = []
                    groups[g].append(s)

            # hit them sprites! w-tsh!
            for s1 in sprites:
                for g in s1.hit_groups:
                    if g not in groups: continue
                    for s2 in groups[g]:
                        if not s1.rect.colliderect(s2.rect): continue
                        s1.hit(self, s1, s2)

            # hit walls and junk like that
            for s in sprites:
                if not len(s.groups): continue
                r = s.rect
                hits = []
                for y in xrange(r.top - r.top % TH, r.bottom, TH):
                    for x in xrange(r.left - r.left % TW, r.right, TW):
                        t = self.layer[y / TH][x / TW]
                        if t == None: continue
                        if not t.hit_groups.intersection(s.groups): continue
                        dist = abs(t.rect.centerx -
                                   s.rect.centerx) + abs(t.rect.centery -
                                                         s.rect.centery)
                        hits.append([dist, t])

                hits.sort()
                for dist, t in hits:
                    if not t.rect.colliderect(s.rect): continue
                    t.hit(self, t, s)

            # remove inactive sprites
            border = self.get_border(DEINIT_BORDER)
            for s in sprites:
                if s.auto_gc and not border.colliderect(s.rect):
                    s.active = False
                if not s.active:
                    self.sprites.remove(
                        s)  # this removes 'em from the real list!
                    if hasattr(s, 'deinit'):
                        s.deinit(self, s)
                    if hasattr(s, '_code'):
                        if s._code not in self.codes:
                            print 'error in code GC', s._code
                            continue
                        del self.codes[s._code]

            #pan the screen
            if self.player != None:
                self.player.pan(self, self.player)

        # more frames
        self.frame += 1
        if (self.frame % FPS) == 0:
            pass
            #print self.player.rect.bottom
            #print ''
            #print 'frame:',self.frame
            #print 'sprites:',len(self.sprites)
            #print 'codes:',len(self.codes)

        #handle various game status'
        if self.status == '_first':
            if self.player.exploded:
                self.player.loop(self, self.player)
            else:
                self.status = 'first'
        elif self.status == 'first':
            self.status = None
            return menu.Pause(self.game, 'get ready', self)
        elif self.status == 'exit':
            self.game.lcur = (self.game.lcur + 1) % len(levels.LEVELS)
            if self.game.lcur == 0:
                # you really won!!!
                self.game.music_play('finish')
                next = menu.Transition(self.game, self.parent)
                return menu.Pause(self.game, 'CONGRATULATIONS!', next)

            else:
                self.game.music_play('lvlwin', 1)
                l2 = Level(self.game, self.fname, self.parent)
                next = menu.Transition(self.game, l2)
                return menu.Pause(self.game, 'good job!', next)

        elif self.status == 'dead':
            if self.game.lives:
                self.game.lives -= 1
                return menu.Transition(
                    self.game, Level(self.game, self.fname, self.parent))
            else:
                next = menu.Transition(self.game, self.parent)
                return menu.Pause(self.game, 'game over', next)
        elif self.status == 'transition':
            self.status = None
            return menu.Transition(self.game, self)