當前位置: 首頁>>代碼示例>>Python>>正文


Python Box.updateByPosShift方法代碼示例

本文整理匯總了Python中box.Box.updateByPosShift方法的典型用法代碼示例。如果您正苦於以下問題:Python Box.updateByPosShift方法的具體用法?Python Box.updateByPosShift怎麽用?Python Box.updateByPosShift使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在box.Box的用法示例。


在下文中一共展示了Box.updateByPosShift方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: __init__

# 需要導入模塊: from box import Box [as 別名]
# 或者: from box.Box import updateByPosShift [as 別名]
class CircularPlayer:
    def __init__(self, initialPosition):
        self.playerCircle = Circle(initialPosition, 13, RED)
        self.speed = 5
        self.perspective = Box(initialPosition, float(WINDOWHEIGHT), float(WINDOWWIDTH), 0.0, (0, 255, 0))
    
    def updatePerspectiveByPosShift(self, xShift, yShift):
        self.perspective.updateByPosShift(xShift, yShift)
    
    def move(self, directionShift, listOfObjects):
        direction = self.perspective.rotation + directionShift
        centerBeforeMove = self.playerCircle.center
        self.playerCircle.move(direction, self.speed, listOfObjects)
        
        xShift = self.playerCircle.center[0] - centerBeforeMove[0]
        yShift = self.playerCircle.center[1] - centerBeforeMove[1]
        
        self.updatePerspectiveByPosShift(xShift, yShift)
        
    def rotate(self, rotation, listOfObjects):
        self.perspective.rotation = self.perspective.rotation + rotation
        
    def draw(self, surface, color):
        self.playerCircle.draw(surface, color)
    
    def getWhatToDraw(self, listOfObjects):
        circlesToDraw, boxesToDraw = [] , []
        whatToDraw = [circlesToDraw, boxesToDraw]

        for circle in listOfObjects[circles]:
            if self.perspective.doesBoxContainCircle(circle) == True:
                whatToDraw[circles].append(circle)
                
        for box in listOfObjects[boxes]:
            if self.perspective.doesBoxContainBox(box) == True:
                whatToDraw[boxes].append(box)
        whatToDraw[circles].append(self.playerCircle)
        return whatToDraw
    
    def drawMyView(self, surface, playingField, listOfObjects):
        whatToDraw = self.getWhatToDraw(listOfObjects)
        
        
        
        whatToDraw[boxes].insert(0, playingField)
        visualBoxes = []
        for i in range (0, len(whatToDraw[boxes])):
            visualBoxes.append(Box(whatToDraw[boxes][i].center, whatToDraw[boxes][i].width, whatToDraw[boxes][i].height, whatToDraw[boxes][i].rotation, whatToDraw[boxes][i].color))
            
            visualBoxes[i].center = (WINDOWWIDTH/2 + (visualBoxes[i].center[0] - self.perspective.center[0]), WINDOWHEIGHT/2 + (visualBoxes[i].center[1] - self.perspective.center[1]))
            visualBoxes[i].rotateAroundPoint(CENTEROFWINDOW, -math.pi/2 - self.perspective.rotation)
            
            visualBoxes[i].draw(surface)
            
            
        visualCircles = []
        for i in range (0, len(whatToDraw[circles])):
            visualCircles.append(Circle(whatToDraw[circles][i].center, whatToDraw[circles][i].radius, whatToDraw[circles][i].color))
            
            visualCircles[i].center = (WINDOWWIDTH/2 + (visualCircles[i].center[0] - self.perspective.center[0]), WINDOWHEIGHT/2 + (visualCircles[i].center[1] - self.perspective.center[1]))
            visualCircles[i].center = function.rotateAroundPoint(visualCircles[i].center, CENTEROFWINDOW, -math.pi/2 - self.perspective.rotation)
            
            visualCircles[i].draw(surface)
開發者ID:KoenP,項目名稱:topdown,代碼行數:65,代碼來源:circularplayer.py

示例2: __init__

# 需要導入模塊: from box import Box [as 別名]
# 或者: from box.Box import updateByPosShift [as 別名]
class Player:
    def __init__(self, initialPosition, speed):
        self.playerBox = Box(initialPosition, 25.0, 17.0, 0.0, RED) #stores the player's coordinates and provides functionality for interacting with the game world.
        self.speed = speed
        self.perspective = Box(initialPosition, float(WINDOWHEIGHT), float(WINDOWWIDTH), 0.0, (0, 255, 0)) #perspective is a box object which should be constantly updated so that its center and rotation are equal to the center and rotation of the playerBox object. It is used to check which other Box objects are 'visible' (within the bounds of the game window) by checking on all box objects on the map whether any of their points lie within the perspective box' bounds, or whether any of its sides intersect with any of the sides of these box objects.
    
    #After every move of the playerBox object, the perspective should be updated so that the center and rotation stay up-to-date.
    def updatePerspective(self):
        self.perspective.center = self.playerBox.center
        self.perspective.rotation = self.playerBox.rotation
        self.perspective.update()
    
    def updatePerspectiveByPosShift(self, xShift, yShift):
        self.perspective.updateByPosShift(xShift, yShift)

    #directionShift represents the difference in angle between the direction the player is facing and the direction he's moving in.
    def move(self, directionShift, listOfObjects):
        direction = self.playerBox.rotation + directionShift
        centerBeforeMove = self.playerBox.center
        self.playerBox.move(direction, self.speed, listOfObjects)
        
        xShift = self.playerBox.center[0] - centerBeforeMove[0]
        yShift = self.playerBox.center[1] - centerBeforeMove[1]
        
        self.updatePerspectiveByPosShift(xShift, yShift)

    def rotate(self, rotation, listOfObjects):
        self.playerBox.rotate(rotation, listOfObjects)
        self.updatePerspective()

    def draw(self, surface, color):
        self.playerBox.draw(surface, color)


    #Used to indicate the player's front.
    def drawArrow(self, surface, color):
        point = ((self.playerBox.points[A][0] + self.playerBox.points[D][0]) / 2,
                 (self.playerBox.points[A][1] + self.playerBox.points[D][1]) / 2)
        pygame.draw.polygon(surface, color, (self.playerBox.points[B], self.playerBox.points[C], point))
    
    #This function uses the perspective box object to check which other Box objects are 'visible' (within the bounds of the game window) by checking on all box objects on the map whether any of their points lie within the perspective box' bounds, or whether any of its sides intersect with any of the sides of these box objects.
    def getWhatToDraw(self, listOfObjects):
        circlesToDraw, boxesToDraw = [] , []
        whatToDraw = [circlesToDraw, boxesToDraw]
#        whatToDraw.append(self.perspective.listOfBoxes[len(self.perspective.listOfBoxes) - 1])
        for circle in listOfObjects[circles]:
            if self.perspective.doesBoxContainCircle(circle) == True:
                whatToDraw[circles].append(circle)
        for box in listOfObjects[boxes]:
            if self.perspective.doesBoxContainBox(box) == True:
                whatToDraw[boxes].append(box)
        whatToDraw[boxes].append(self.playerBox)
        return whatToDraw
    
    #Uses the data collected in getWhatToDraw() to create a list of temporary 'visual' boxes, which can be modified at will without interfering with the rest of the game's data. The new coordinates are calculated so that the player is always in the center of the screen and the surrounding world moves relative to him.
    def drawMyView(self, surface, playingField, listOfObjects):
        whatToDraw = self.getWhatToDraw(listOfObjects)
        
        
        
        whatToDraw[boxes].insert(0, playingField)
        visualBoxes = []
        for i in range (0, len(whatToDraw[boxes])):
            visualBoxes.append(Box(whatToDraw[boxes][i].center, whatToDraw[boxes][i].width, whatToDraw[boxes][i].height, whatToDraw[boxes][i].rotation, whatToDraw[boxes][i].color))
            
            visualBoxes[i].center = (WINDOWWIDTH/2 + (visualBoxes[i].center[0] - self.perspective.center[0]), WINDOWHEIGHT/2 + (visualBoxes[i].center[1] - self.perspective.center[1]))
            visualBoxes[i].rotateAroundPoint(CENTEROFWINDOW, -math.pi/2 - self.perspective.rotation)
            
            visualBoxes[i].draw(surface)
            
            
        visualCircles = []
        for i in range (0, len(whatToDraw[circles])):
            visualCircles.append(Circle(whatToDraw[circles][i].center, whatToDraw[circles][i].radius, whatToDraw[circles][i].color))
            
            visualCircles[i].center = (WINDOWWIDTH/2 + (visualCircles[i].center[0] - self.perspective.center[0]), WINDOWHEIGHT/2 + (visualCircles[i].center[1] - self.perspective.center[1]))
            visualCircles[i].center = function.rotateAroundPoint(visualCircles[i].center, CENTEROFWINDOW, -math.pi/2 - self.perspective.rotation)
            
            visualCircles[i].draw(surface)
開發者ID:KoenP,項目名稱:topdown,代碼行數:81,代碼來源:player.py


注:本文中的box.Box.updateByPosShift方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。