Advertisement
Rastus22

Raycaster

Mar 21st, 2018
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.43 KB | None | 0 0
  1. import random
  2. import pygame
  3. #from mapGen import *
  4. from math import *
  5. from pygame.locals import *
  6.  
  7. def mapGenBlank(size):
  8.     #Making a blank map.
  9.     xP = int(size/2)
  10.     yP = int(size/2)
  11.     worldMap = [[random.randint(0,0) for i in range(0,size)] for n in range(0,size)]
  12.     for yIndex, y in enumerate(worldMap):
  13.         for xIndex, x in enumerate(y):
  14.             if yIndex == 0 or yIndex == size-1:
  15.                 worldMap[yIndex][xIndex] = 2
  16.             if xIndex == 0 or xIndex == size-1:
  17.                 worldMap[yIndex][xIndex] = 3
  18.     return worldMap
  19.  
  20. def mapGenTiled(size):
  21.     xPTile = int(size/2)
  22.     yPTile = int(size/2)
  23.     tileMap = [[random.randint(1,3) for i in range(0,size)] for n in range(0,size)]
  24.     worldMap = mapGenBlank(size*7+2)
  25.     tileMap[yPTile][xPTile] = 1
  26.     xP = 7*xPTile+3
  27.     yP = 7*yPTile+3
  28.     for yTileIndex, yTile in enumerate(tileMap):
  29.         for xTileIndex, xTile in enumerate(yTile):
  30.             current = tileDict[xTile]
  31.             yMultiplier = yTileIndex*7+1
  32.             xMultiplier = xTileIndex*7+1
  33.             for yIndex, y in enumerate(current):
  34.                 for xIndex, x in enumerate(y):
  35.                     worldMap[yIndex+yMultiplier][xIndex+xMultiplier] = current[yIndex][xIndex]
  36.  
  37.     return worldMap, xP, yP
  38.  
  39. #Tiled shapes
  40. tileDict = {
  41.     1:[
  42.         [1, 1, 0, 0, 0, 1, 1],
  43.         [1, 0, 0, 0, 0, 0, 1],
  44.         [1, 0, 0, 0, 0, 0, 1],
  45.         [0, 0, 0, 0, 0, 0, 0],
  46.         [1, 0, 0, 0, 0, 0, 1],
  47.         [1, 0, 0, 0, 0, 0, 1],
  48.         [1, 1, 0, 0, 0, 1, 1]
  49.     ],
  50.     2:[
  51.         [2, 2, 0, 0, 0, 2, 2],
  52.         [2, 2, 0, 0, 0, 2, 2],
  53.         [2, 0, 2, 0, 2, 0, 2],
  54.         [0, 0, 0, 0, 0, 0, 0],
  55.         [2, 0, 2, 0, 2, 0, 2],
  56.         [2, 2, 0, 0, 0, 2, 2],
  57.         [2, 2, 0, 0, 0, 2, 2]
  58.     ],
  59.     3:[
  60.         [3, 3, 0, 0, 0, 3, 3],
  61.         [3, 0, 0, 0, 0, 0, 3],
  62.         [3, 0, 3, 0, 3, 0, 3],
  63.         [0, 0, 3, 0, 3, 0, 0],
  64.         [3, 0, 3, 0, 3, 0, 3],
  65.         [3, 0, 0, 0, 0, 0, 3],
  66.         [3, 3, 0, 0, 0, 3, 3]
  67.     ]
  68. }
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75. '''
  76. #PyRay 20x20
  77. worldMap =  [
  78.            [1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2],
  79.            [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2],
  80.            [2, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  81.            [1, 0, 2, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 2, 3, 2, 3, 0, 0, 2],
  82.            [2, 0, 3, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  83.            [1, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2],
  84.            [2, 3, 1, 0, 0, 2, 0, 0, 0, 2, 3, 2, 0, 0, 0, 0, 0, 0, 0, 1],
  85.            [1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 2, 0, 0, 0, 2],
  86.            [2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, 2, 1, 0, 0, 0, 1],
  87.            [1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 0, 0, 0, 0, 0, 0, 0, 2],
  88.            [2, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  89.            [1, 0, 2, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2],
  90.            [2, 0, 3, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 3, 2, 1, 2, 0, 1],
  91.            [1, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 2],
  92.            [2, 3, 1, 0, 0, 2, 0, 0, 2, 1, 3, 2, 0, 2, 0, 0, 3, 0, 3, 1],
  93.            [1, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 1, 0, 0, 2, 0, 0, 2],
  94.            [2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 2, 3, 0, 1, 2, 0, 1],
  95.            [1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 3, 0, 2],
  96.            [2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 1],
  97.            [2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1]]
  98. mapSize = len(worldMap)'''
  99.  
  100. #Creates view window
  101. pygame.init()
  102. HEIGHT = 600
  103. WIDTH = 1000
  104. screenSize = (WIDTH,HEIGHT)
  105. screen = pygame.display.set_mode(screenSize)
  106. pygame.display.set_caption('Hmm')
  107.  
  108. keys = [False]*324 #List for keyboard values
  109. zde = 0.0000001 #Add to prvent division by zero
  110. res = 2
  111.  
  112.  
  113.  
  114. #Staring Position
  115. xPos = 10.0
  116. yPos = 5.0
  117. xDir = -0.7
  118. yDir = 0
  119. xPlane = 0
  120. yPlane = 0.66
  121.  
  122. #worldMap = mapGenRandom(20)
  123. worldMap, xPos, yPos = mapGenTiled(5)
  124. for i in worldMap:
  125.     print(''.join(str(i)))
  126. #worldMap = mapGenBlank(20)
  127.  
  128. rotationSpeed = 0.01
  129. movementSpeed = 0.05
  130.  
  131.  
  132. #Exits pygame
  133. def close():
  134.     pygame.display.quit()
  135.     pygame.quit()
  136.     quit()
  137.  
  138. pygame.mouse.set_visible(False)
  139. pygame.event.set_grab(True)
  140.  
  141. clock = pygame.time.Clock()
  142.  
  143. #Start game loop
  144. while True:
  145.     pygame.event.pump()
  146.     #print(xPos, yPos, xPlane, yPlane)
  147.     #Draws background. Black and Grey
  148.     screen.fill((25,25,25))
  149.     pygame.draw.rect(screen, (50,50,50), (0, HEIGHT/2, WIDTH, HEIGHT/2)) #Draws grey rectangle in bottom half
  150.  
  151.     #Sets whether keys are held or not
  152.     for event in pygame.event.get():
  153.         if event.type == KEYDOWN:
  154.             keys[event.key] = True
  155.         elif event.type == KEYUP:
  156.             keys[event.key] = False
  157.  
  158.     #Keybindings
  159.     if keys[K_ESCAPE]:
  160.         close()
  161.  
  162.     if keys[K_w]:
  163.         if not worldMap[int(xPos + xDir * movementSpeed)] [int(yPos)]:
  164.             xPos += xDir * movementSpeed
  165.         if not worldMap[int(xPos)][int(yPos + yDir * movementSpeed)]:
  166.             yPos += yDir * movementSpeed
  167.  
  168.     if keys[K_s]:
  169.         if not worldMap[int(xPos - xDir * movementSpeed)] [int(yPos)]:
  170.             xPos -= xDir * movementSpeed
  171.         if not worldMap[int(xPos)][int(yPos - yDir * movementSpeed)]:
  172.             yPos -= yDir * movementSpeed
  173.  
  174.     if keys[K_d]:
  175.         if not worldMap[int(xPos + xPlane * movementSpeed)] [int(yPos)]:
  176.             xPos += xPlane * movementSpeed
  177.         if not worldMap[int(xPos)][int(yPos + yPlane * movementSpeed)]:
  178.             yPos += yPlane * movementSpeed
  179.  
  180.  
  181.     if keys[K_a]:
  182.         if not worldMap[int(xPos - xPlane * movementSpeed)] [int(yPos)]:
  183.             xPos -= xPlane * movementSpeed
  184.         if not worldMap[int(xPos)][int(yPos - yPlane * movementSpeed)]:
  185.             yPos -= yPlane * movementSpeed
  186.  
  187.     if keys[K_RIGHT]:
  188.         xDirOld = xDir
  189.         xDir = xDir * cos(-rotationSpeed) - yDir * sin(-rotationSpeed)
  190.         yDir = xDirOld * sin(-rotationSpeed) + yDir * cos(-rotationSpeed)
  191.         xPlaneOld = xPlane
  192.         xPlane = xPlane * cos(-rotationSpeed) - yPlane * sin(-rotationSpeed)
  193.         yPlane = xPlaneOld * sin(-rotationSpeed) + yPlane * cos(-rotationSpeed)
  194.  
  195.     if keys[K_LEFT]:
  196.         xDirOld = xDir
  197.         xDir = xDir * cos(rotationSpeed) - yDir * sin(rotationSpeed)
  198.         yDir = xDirOld * sin(rotationSpeed) + yDir * cos(rotationSpeed)
  199.         xPlaneOld = xPlane
  200.         xPlane = xPlane * cos(rotationSpeed) - yPlane * sin(rotationSpeed)
  201.         yPlane = xPlaneOld * sin(rotationSpeed) + yPlane * cos(rotationSpeed)
  202.  
  203.     mouse = pygame.mouse.get_rel()
  204.     xMouse = mouse[0]/100
  205.  
  206.  
  207.  
  208.     if xMouse < 0:
  209.         xDirOld = xDir
  210.         xDir = xDir * cos(-xMouse) - yDir * sin(-xMouse)
  211.         yDir = xDirOld * sin(-xMouse) + yDir * cos(-xMouse)
  212.         xPlaneOld = xPlane
  213.         xPlane = xPlane * cos(-xMouse) - yPlane * sin(-xMouse)
  214.         yPlane = xPlaneOld * sin(-xMouse) + yPlane * cos(-xMouse)
  215.  
  216.  
  217.  
  218.  
  219.     if xMouse > 0:
  220.         xDirOld = xDir
  221.         xDir = xDir * cos(-xMouse) - yDir * sin(-xMouse)
  222.         yDir = xDirOld * sin(-xMouse) + yDir * cos(-xMouse)
  223.         xPlaneOld = xPlane
  224.         xPlane = xPlane * cos(-xMouse) - yPlane * sin(-xMouse)
  225.         yPlane = xPlaneOld * sin(-xMouse) + yPlane * cos(-xMouse)
  226.  
  227.  
  228.     for column in range(0,WIDTH,2):
  229.         xCamera = 2 * column / float(WIDTH) - 1.0
  230.         xRayDir = xDir + xPlane * xCamera
  231.         yRayDir = yDir + yPlane * xCamera
  232.  
  233.         xMap = int(xPos)
  234.         yMap = int(yPos)
  235.  
  236.         xSideDistance = 0.0
  237.         ySideDistance = 0.0
  238.         perpWallDistance = 0.0
  239.  
  240.         xDeltaDistance = abs(1/(xRayDir + zde))
  241.         yDeltaDistance = abs(1/(yRayDir + zde))
  242.  
  243.         xStep = 0
  244.         yStep = 0
  245.  
  246.         hit = False
  247.         side = 0
  248.  
  249.         if (xRayDir < 0):
  250.             xStep = -1
  251.             xSideDistance = (xPos - xMap) * xDeltaDistance
  252.         else:
  253.             xStep = 1
  254.             xSideDistance = (xMap + 1.0 - xPos) * xDeltaDistance
  255.  
  256.         if (yRayDir < 0):
  257.             yStep = -1
  258.             ySideDistance = (yPos - yMap) * yDeltaDistance
  259.         else:
  260.             yStep = 1
  261.             ySideDistance = (yMap + 1.0 - yPos) * yDeltaDistance
  262.  
  263.         hit = False
  264.  
  265.         while not hit:
  266.             if (xSideDistance < ySideDistance):
  267.               xSideDistance += xDeltaDistance
  268.               xMap += xStep
  269.               side = 'x'
  270.  
  271.             else:
  272.               ySideDistance += yDeltaDistance
  273.               yMap += yStep
  274.               side = 'y'
  275.  
  276.  
  277.  
  278.             if worldMap[xMap][yMap] != 0 :
  279.                 hit = True
  280.  
  281.         if (side == 'x'):
  282.             perpWallDist = (xMap - xPos + (1 - xStep) / 2) / (xRayDir + zde)
  283.         else:
  284.             perpWallDist = (yMap - yPos + (1 - yStep) / 2) / (yRayDir + zde)
  285.  
  286.         lineHeight = int(HEIGHT / (perpWallDist + zde))
  287.  
  288.         drawStart = -lineHeight / 2 + HEIGHT / 2
  289.  
  290.         if (drawStart < 0):
  291.             drawStart = 0
  292.  
  293.         drawEnd = lineHeight / 2 + HEIGHT / 2
  294.  
  295.         if (drawEnd >= HEIGHT):
  296.             drawEnd = HEIGHT - 1
  297.  
  298.         wallcolours = [ [255,255,255], [150,0,0], [0,150,0], [0,0,150] ]
  299.         colour = wallcolours[worldMap[xMap][yMap]]
  300.  
  301.         if side == 'y':
  302.             colour = [i/2 for i in colour]
  303.  
  304.         pygame.draw.line(screen, colour, (column,drawStart), (column, drawEnd), res)
  305.     clock.tick(60)
  306.     print(clock.get_fps())
  307.  
  308.  
  309.     pygame.display.update() #Updates screen
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement