def __init__(self, level, windowSize, totalRays, fovDegrees, targetFps): self.level = level self.windowSize = windowSize self.fovDegrees = fovDegrees self.targetFps = targetFps self.moveSpeed = NORMALIZED_MOVE * (60 / targetFps) self.turnSpeed = NORMALIZED_TURN * (60 / targetFps) * (totalRays / 600) self.cataclysmSpeed = NORMALIZED_CATACLYSM * 0.00001 * (60 / targetFps) self.raycasting = None self.screen = None self.font = None self.minimap = None self.fovRays = None self.pixelsPerRay = None self.distanceToProjection = None self.player = None self.winScreen = None # Initialize raycasting logic self.raycasting = Raycasting(totalRays, BLOCK_SIZE, self.level) # Initialize pygame pygame.init() pygame.display.set_caption("Raycasting labyrint") self.screen = pygame.display.set_mode(self.windowSize) # Prepare font rendering pygame.font.init() self.font = pygame.font.SysFont("Sans Serif", 30) # Prepare minimap minimapSize = self.level.get_size() * BLOCK_SIZE // MINIMAP_SIZE_DIV self.minimap = pygame.Surface((minimapSize.x, minimapSize.y)) # Compute fov related stuff self.fovRays = self.raycasting.degrees_to_ray_number(self.fovDegrees) self.pixelsPerRay = windowSize[0] // (totalRays // (360 // self.fovDegrees)) # Compute distance between player and the projection plane (also fov stuff) self.distanceToProjection = int( (PROJECTION_WIDTH) / (tan(radians(self.fovDegrees / 2)))) # Prepare fisheye correction self.fisheyeCoefficients = self.raycasting.fisheye_coefficients( self.fovDegrees, self.fovRays) # Create player object startPos = self.level.get_start_block() * BLOCK_SIZE startPos[0] = startPos.x + (BLOCK_SIZE // 2) # Center vertically startPos[1] = startPos.y + (BLOCK_SIZE // 2) # Center horizontally self.player = Player(startPos, 0, self.raycasting, self.fovRays) # Create win screen self.winScreen = pygame.Surface(windowSize, flags=pygame.SRCALPHA) self.winScreen.fill( (CEIL_COLOR[0], CEIL_COLOR[1], CEIL_COLOR[2], WIN_SCREEN_OPACITY)) youWon = self.font.render("You won!", False, TEXT_COLOR) pressQ = self.font.render("Press 'Q' to quit.", False, TEXT_COLOR) self.winScreen.blit(youWon, (8, 8)) self.winScreen.blit(pressQ, (8, 40))
clock = pygame.time.Clock() player = Player() font = pygame.font.SysFont("Arial", 18) def update_fps(): fps = str(int(clock.get_fps())) fps_text = font.render(fps, 1, pygame.Color("coral")) return fps_text # game loop while True: for event in pygame.event.get(): if event.type == pygame.QUIT: exit() player.move() screen.fill(BLACK) pygame.draw.rect(screen,SKY,(0,0,WIDTH,HEIGHT//2)) pygame.draw.rect(screen, GROUND, (0, HEIGHT//2, WIDTH, HEIGHT // 2)) Raycasting(screen,player) drawMinimap(screen, player, world_map,3) screen.blit(update_fps(), (10, 0)) pygame.display.flip() clock.tick(FPS)