Advertisement
Rastus22

Python Raycaster

Mar 13th, 2018
550
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.93 KB | None | 0 0
  1. import pygame
  2. #import math
  3. from math import *
  4. from pygame.locals import *
  5.  
  6. #PyRay 20x20
  7. worldMap =  [
  8.             [1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2],
  9.             [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2],
  10.             [2, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  11.             [1, 0, 2, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 2, 3, 2, 3, 0, 0, 2],
  12.             [2, 0, 3, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  13.             [1, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2],
  14.             [2, 3, 1, 0, 0, 2, 0, 0, 0, 2, 3, 2, 0, 0, 0, 0, 0, 0, 0, 1],
  15.             [1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 2, 0, 0, 0, 2],
  16.             [2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, 2, 1, 0, 0, 0, 1],
  17.             [1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 0, 0, 0, 0, 0, 0, 0, 2],
  18.             [2, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  19.             [1, 0, 2, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2],
  20.             [2, 0, 3, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 3, 2, 1, 2, 0, 1],
  21.             [1, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 2],
  22.             [2, 3, 1, 0, 0, 2, 0, 0, 2, 1, 3, 2, 0, 2, 0, 0, 3, 0, 3, 1],
  23.             [1, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 1, 0, 0, 2, 0, 0, 2],
  24.             [2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 2, 3, 0, 1, 2, 0, 1],
  25.             [1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 3, 0, 2],
  26.             [2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 1],
  27.             [2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1]]
  28.  
  29.  
  30. mapSize = len(worldMap)
  31.  
  32. #Creates view window
  33. pygame.init()
  34. HEIGHT = 600
  35. WIDTH = 1000
  36. size = (WIDTH,HEIGHT)
  37. screen = pygame.display.set_mode(size)
  38. pygame.display.set_caption('Hmm')
  39.  
  40. keys = [False]*324 #List for keyboard values
  41. zde = 0.0000001 #Add to prvent division by zero
  42. res = 2
  43.  
  44.  
  45. #Staring Position
  46. xPos = 10.0
  47. yPos = 5.0
  48. xDir = -1
  49. yDir = 0
  50. xPlane = 0
  51. yPlane = 0.66
  52.  
  53. rotationSpeed = 0.01
  54. movementSpeed = 0.05
  55.  
  56.  
  57. #Exits pygame
  58. def close():
  59.     pygame.display.quit()
  60.     pygame.quit()
  61.  
  62. #Start game loop
  63. while True:
  64.     pygame.event.pump()
  65.  
  66.     #Draws background. Black and Grey
  67.     screen.fill((25,25,25))
  68.     pygame.draw.rect(screen, (50,50,50), (0, HEIGHT/2, WIDTH, HEIGHT/2)) #Draws grey rectangle in bottom half
  69.  
  70.     #Sets whether keys are held or not
  71.     for event in pygame.event.get():
  72.         if event.type == KEYDOWN:
  73.             keys[event.key] = True
  74.         elif event.type == KEYUP:
  75.             keys[event.key] = False
  76.  
  77.     #Keybindings
  78.     if keys[K_ESCAPE]:
  79.         close()
  80.  
  81.     if keys[K_UP]:
  82.         if not worldMap[int(xPos + xDir * movementSpeed)] [int(yPos)]:
  83.             xPos += xDir * movementSpeed
  84.         if not worldMap[int(xPos)][int(yPos + yDir * movementSpeed)]:
  85.             yPos += yDir * movementSpeed
  86.  
  87.     if keys[K_DOWN]:
  88.         if not worldMap[int(xPos - xDir * movementSpeed)] [int(yPos)]:
  89.             xPos -= xDir * movementSpeed
  90.         if not worldMap[int(xPos)][int(yPos - yDir * movementSpeed)]:
  91.             yPos -= yDir * movementSpeed
  92.  
  93.     if keys[K_RIGHT]:
  94.         xDirOld = xDir
  95.         xDir = xDir * cos(-rotationSpeed) - yDir * sin(-rotationSpeed)
  96.         yDir = xDirOld * sin(-rotationSpeed) + yDir * cos(-rotationSpeed)
  97.         xPlaneOld = xPlane
  98.         xPlane = xPlane * cos(-rotationSpeed) - yPlane * sin(-rotationSpeed)
  99.         yPlane = xPlaneOld * sin(-rotationSpeed) + yPlane * cos(-rotationSpeed)
  100.  
  101.     if keys[K_LEFT]:
  102.         xDirOld = xDir
  103.         xDir = xDir * cos(rotationSpeed) - yDir * sin(rotationSpeed)
  104.         yDir = xDirOld * sin(rotationSpeed) + yDir * cos(rotationSpeed)
  105.         xPlaneOld = xPlane
  106.         xPlane = xPlane * cos(rotationSpeed) - yPlane * sin(rotationSpeed)
  107.         yPlane = xPlaneOld * sin(rotationSpeed) + yPlane * cos(rotationSpeed)
  108.  
  109.  
  110.  
  111.  
  112.     for column in range(0,WIDTH,res):
  113.         xCamera = 2 * column / float(WIDTH) - 1.0
  114.         xRayDir = xDir + xPlane * xCamera
  115.         yRayDir = yDir + yPlane * xCamera
  116.  
  117.         xMap = int(xPos)
  118.         yMap = int(yPos)
  119.  
  120.         xSideDistance = 0.0
  121.         ySideDistance = 0.0
  122.         perpWallDistance = 0.0
  123.  
  124.         xDeltaDistance = abs(1/(xRayDir + zde))
  125.         yDeltaDistance = abs(1/(yRayDir + zde))
  126.  
  127.         xStep = 0
  128.         yStep = 0
  129.  
  130.         hit = False
  131.         side = 0
  132.  
  133.         if (xRayDir < 0):
  134.             xStep = -1
  135.             xSideDistance = (xPos - xMap) * xDeltaDistance
  136.         else:
  137.             xStep = -1
  138.             xSideDistance = (xMap + 1.0 - xPos) * xDeltaDistance
  139.  
  140.         if (xRayDir < 0):
  141.             yStep = -1
  142.             ySideDistance = (yPos - yMap) * yDeltaDistance
  143.         else:
  144.             yStep = 1;
  145.             ySideDistance = (yMap + 1.0 - yPos) * yDeltaDistance
  146.  
  147.         hit = False
  148.         while not hit:
  149.             if (xSideDistance < ySideDistance):
  150.               xSideDistance += xDeltaDistance
  151.               xMap += xStep
  152.               side = 'x'
  153.  
  154.             else:
  155.               ySideDistance += yDeltaDistance
  156.               yMap += yStep
  157.               side = 'y'
  158.  
  159.             if worldMap[xMap][yMap] != 0 :
  160.                 hit = True
  161.  
  162.         if (side == 0):
  163.             perpWallDist = (xMap - xPos + (1 - xStep) / 2) / (xRayDir + zde)
  164.         else:
  165.             perpWallDist = (yMap - yPos + (1 - yStep) / 2) / (yRayDir + zde)
  166.  
  167.         lineHeight = int(HEIGHT / (perpWallDist + zde))
  168.  
  169.         drawStart = -lineHeight / 2 + HEIGHT / 2
  170.  
  171.         if (drawStart < 0):
  172.             drawStart = 0
  173.  
  174.         drawEnd = lineHeight / 2 + HEIGHT / 2
  175.  
  176.         if (drawEnd >= HEIGHT):
  177.             drawEnd = HEIGHT - 1
  178.  
  179.         wallcolours = [ [255,255,255], [150,0,0], [0,150,0], [0,0,150] ]
  180.         colour = wallcolours[worldMap[xMap][yMap]]
  181.  
  182.         if side == 'y':
  183.             colour = [i/2 for i in colour]
  184.  
  185.         pygame.draw.line(screen, colour, (column,drawStart), (column, drawEnd), res)
  186.  
  187.  
  188.     pygame.display.update() #Updates screen
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement