def raycast(self): # looking right for rayAngleDegrees in frange(self.playerFacingDegrees, self.playerFacingDegrees + Raycast.FIELD_OF_VIEW_DEGREES, Raycast.ANGLE_INCREMENT_DEGREES): xIncrement = self.cosTable[int(rayAngleDegrees % 360)] yIncrement = self.sinTable[int(rayAngleDegrees % 360)] testX = self.playerX; testY = self.playerY; rayLength = 1 blockColor = 0 try: while Generator.isWall(blockColor) == False and Generator.isDoor(blockColor) == False: testX += xIncrement testY += yIncrement rayLength += 1 worldX = int((testX / Raycast.WORLD_BLOCK_SIZE)) worldY = int((testY / Raycast.WORLD_BLOCK_SIZE)) blockColor = self.world[worldY][worldX] except IndexError as msg: print(msg) # remove door when near if Generator.isDoor(blockColor) and rayLength < Raycast.WORLD_BLOCK_SIZE: self.world[worldY][worldX] = Generator.FLOOR # set start x for rectangles x = ((rayAngleDegrees - self.playerFacingDegrees) * Raycast.DRAW_LINE_WIDTH) / Raycast.ANGLE_INCREMENT_DEGREES # compensate for fisheye view as ray is cast from center beta = rayAngleDegrees - self.playerFacingDegrees - Raycast.HALF_FIELD_OF_VIEW_DEGREES rayLength = rayLength * math.cos(beta * Raycast.RADIANS_CONVERSION_FACTOR) # scale the wall according to distance, if the rayLength is shorter then the wall must be drawn bigger wallHeight = (1000 / rayLength) * 2 if wallHeight > Raycast.SCREEN_HEIGHT: wallHeight = Raycast.SCREEN_HEIGHT halfWallHeight = wallHeight / 2 self.drawCeiling(x, halfWallHeight) self.drawWall(x, halfWallHeight, blockColor) self.drawFloor(x, halfWallHeight)
def drawWall(self, x, halfWallHeight, itemColor): # x1,y1,x2,y2 fillColor = self.DOOR_COLOR if Generator.isDoor(itemColor) else COLORS[int(itemColor)] self.canvas.create_rectangle(x, Raycast.SCREEN_CENTER_Y - halfWallHeight, x + Raycast.DRAW_LINE_WIDTH, Raycast.SCREEN_CENTER_Y + halfWallHeight, width=0, fill=fillColor)