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)
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)