当前位置: 首页>>代码示例>>Python>>正文


Python Enemy.drawTo方法代码示例

本文整理汇总了Python中enemy.Enemy.drawTo方法的典型用法代码示例。如果您正苦于以下问题:Python Enemy.drawTo方法的具体用法?Python Enemy.drawTo怎么用?Python Enemy.drawTo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在enemy.Enemy的用法示例。


在下文中一共展示了Enemy.drawTo方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: GameSession

# 需要导入模块: from enemy import Enemy [as 别名]
# 或者: from enemy.Enemy import drawTo [as 别名]
class GameSession(object):
    """A session of playing the game."""

    def __init__(self, screen):
        self.screen = screen
        w, h = self.screen.get_size()
        pygame.draw.rect(self.screen, (0,0,0), pygame.Rect(0,0,w,h))
        self.world = World(10, 10, self.screen)

    def newGame(self):
        pygame.mixer.stop()
        self.world.resetWorld()
        self.world.genWorld(5, 5)
        self.player = Player(*self.world.getStartPoint())
        # get enemy's random position
        pntLs = self.world.getPntList()
        # put enemy somewhere > 5 hallways away from player to start with
        xPlayer, yPlayer = self.world.getClosestIntersectPoint(self.player)
        enemyStartPositions = [p for p in pntLs if abs(p[0]-xPlayer)>4 or abs(p[1]-yPlayer)>4]
        pnt = random.choice(pntLs if len(enemyStartPositions)==0 else enemyStartPositions)
        xl, yu, xr, yd = self.world.getIntersectBoundingBox(pnt)
        px, py = ((xl+xr)/2, (yu+yd)/2)
        self.enemy = Enemy(px, py, self.world)
        self.flashlight = Flashlight(self.screen, 1)  # flashlight w/ range of 1 radian
        self.keys = None
        self.xCam = 0
        self.yCam = 0
        self.messages = ["iloveyou","yourhairsmellsnice","stop","imscared","canyouhearme?","youcantleave"]
        self.startTime = time.clock()  # time since last message started / removed
        self.currentMessage = None

    def startGame(self):
        """Start session of playing game."""
        beatLevel = True
        currentLevel = 0
        while beatLevel:
            beatLevel = self.startLevel()
            if beatLevel: currentLevel += 1
        return currentLevel

    def startLevel(self):
        """Start play of a level. Returns True if player got to end, False if killed by enemy or player quit."""
        self.newGame()
        # make transition effect to new level
        prevScreen = pygame.Surface(self.screen.get_size())
        prevScreen.blit(self.screen, (0,0))
        self.keys = pygame.key.get_pressed()
        self.update()
        self.render()
        self.renderTransition(prevScreen, self.screen)
        wasESCPressed = False  # was ESC pressed last turn
        paused = False
        while not self.keys[pygame.K_q]:
            self.keys = pygame.key.get_pressed()
            if not self.keys[pygame.K_ESCAPE] and wasESCPressed:  # invert whether paused or not if escape pressed + released
                paused = not paused
                if paused: pygame.mixer.pause()
                else:      pygame.mixer.unpause()
            if paused:
                self.renderPause()
            else:
                self.update()
                self.render()
            wasESCPressed = self.keys[pygame.K_ESCAPE]
            if (-10 < self.enemy.xPos-self.player.xPos < 10) and (-10 < self.enemy.yPos-self.player.yPos < 10):
                # enemy got to player, player = killed
                return False
            elif self.world.hasReachedExit(self.player):
                # player reached exit
                return True
            pygame.display.flip()
            pygame.event.pump()
        return False  # player hit 'q' to quit game

    def update(self):
        self.player.update(self.keys, self.world)
        self.enemy.update(self.world, self.player, self.flashlight, (self.xCam, self.yCam))
        x, y = self.screen.get_size()
        self.xCam = self.player.xPos - x/2
        self.yCam = self.player.yPos - y/2

    def render(self):
        self.world.drawWorld(self.xCam, self.yCam, self.player)
        self.player.drawTo(self.screen)
        self.enemy.drawTo(self.screen, self.flashlight, self.player, (self.xCam, self.yCam))
        self.flashlight.drawLight(self.world, self.player, (self.xCam, self.yCam))
        # draw random message from enemy
        if time.clock() - self.startTime >= random.choice(range(90, 120)) \
                and random.random() < 0.1 and self.currentMessage==None:
            self.currentMessage = random.choice(self.messages)
            self.startTime = time.clock()
        if self.currentMessage != None:
            textFont = pygame.font.SysFont("comicsans", 50)
            line = textFont.render(self.currentMessage, 1, (127, 0, 0))
            self.screen.blit(line, (400, 600))
            if time.clock() - self.startTime > 3:
                self.currentMessage = None
                self.startTime = time.clock()

    def updateFake(self):
#.........这里部分代码省略.........
开发者ID:jackromo,项目名称:iseeyou_game,代码行数:103,代码来源:gamesession.py


注:本文中的enemy.Enemy.drawTo方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。