Skip to menu

Robotics with Object Pascal

Rover

2D Lidar enabled Rover Tutorial Project

2023.07.17 18:59

me Views:210

2d_plane.png

 

 


# packages
import pygame
import sys
import math

# global constants
SCREEN_HEIGHT = 500
SCREEN_WIDTH = SCREEN_HEIGHT * 2
MAP_SIZE = 50
TILE_SIZE = int((SCREEN_WIDTH / 2) / MAP_SIZE)

# global variables
player_x = (SCREEN_WIDTH / 2) / 2
player_y = (SCREEN_WIDTH / 2) / 2

# map
MAP = (
    '##################################################'
    '#                                   #            #'
    '#                                   #            #'
    '#                                   #            #'
    '#       #################           #            #'        
    '#             #                     #            #'
    '#             #                     #            #'
    '#             #                     #            #'
    '#       #################           #            #'
    '#       #               #           #            #'
    '#       #               #           #####   ######'
    '#       #               #                        #'
    '#       #                                        #'        
    '#       #                                        #'
    '#       #               #                        #'
    '#       #               #                        #'
    '#       #################                        #'
    '#                                                #'
    '#                                                #'
    '#                                                #'
    '#                                                #'        
    '#                                                #'
    '#                                                #'
    '######################################           #'
    '#                                                #'
    '#                                                #'
    '#                                                #'
    '#                                                #'
    '#                                                #'        
    '#                                                #'
    '######################                           #'
    '#                    #                           #'
    '#                    #                           #'    
    '#                    #                           #'        
    '#                    #                           #'
    '#                    #                           #'
    '#                    #                           #'
    '#         ############         ######     ########'
    '#                              #                 #'
    '#                              #                 #'
    '#                              #                 #'
    '#                              #                 #'        
    '#                    #############################'
    '#                    #                           #'
    '#                    #                           #'
    '#                                                #'     
    '#                                                #'
    '#                    #                           #'
    '#                    #                           #'            
    '##################################################'
)

# init pygame
pygame.init()

# create game window
win = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

# set window title
pygame.display.set_caption('Raycasting')

# init timer
clock = pygame.time.Clock()

# draw map
def draw_map():
    # loop over map rows
    for row in range(MAP_SIZE):
        # loop over map columns
        for col in range(MAP_SIZE):
            # calculate square index
            square = row * MAP_SIZE + col
            
            # draw map in the game window # window, color,
            pygame.draw.rect(
                win,
                (50, 50, 100) if MAP[square] == '#' else (250, 250, 250),
                (col * TILE_SIZE, row * TILE_SIZE, TILE_SIZE - 1, TILE_SIZE - 1)
            )

    # draw player on 2D board
    pygame.draw.circle(win, (50, 100, 255), (int(player_x), int(player_y)), 6)

# game loop
while True:
    # escape condition
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit(0)
    
    # draw 2D map
    draw_map()
    

    # update display
    pygame.display.flip()
    
    # set FPS
    clock.tick(30)