Advertisement
Guest User

raycasterino

a guest
Jan 27th, 2016
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.86 KB | None | 0 0
  1. import pygame
  2. from pygame.locals import *
  3.  
  4. from math import sqrt,fabs,cos,sin
  5.  
  6. mapSize=24
  7.  
  8. worldMap=[
  9.   [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
  10.   [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  11.   [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  12.   [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  13.   [1,0,0,0,0,0,2,2,2,2,2,0,0,0,0,3,0,3,0,3,0,0,0,1],
  14.   [1,0,0,0,0,0,2,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,1],
  15.   [1,0,0,0,0,0,2,0,0,0,2,0,0,0,0,3,0,0,0,3,0,0,0,1],
  16.   [1,0,0,0,0,0,2,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,1],
  17.   [1,0,0,0,0,0,2,2,0,2,2,0,0,0,0,3,0,3,0,3,0,0,0,1],
  18.   [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  19.   [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  20.   [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  21.   [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  22.   [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  23.   [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  24.   [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  25.   [1,4,4,4,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  26.   [1,4,0,4,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  27.   [1,4,0,0,0,0,5,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  28.   [1,4,0,4,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  29.   [1,4,0,4,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  30.   [1,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  31.   [1,4,4,4,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
  32.   [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
  33. ];
  34.  
  35. posX = 22;posY = 12; #x and y start position
  36. dirX = -1;dirY = 0; #initial direction vector
  37. planeX = 0;planeY = 0.66; #the 2d raycaster version of camera plane
  38.  
  39. time = 0; #time of current frame
  40. oldTime = 0; #time of previous frame
  41.  
  42. DISP=pygame.display.set_mode((512,384))
  43.  
  44. go = True
  45. w=512
  46. h=384
  47. rotSpeed=0.01
  48. moveSpeed=0.01
  49. while go:
  50.     #Black Fill
  51.     DISP.fill((0,0,0))
  52.  
  53.     #RAYCASTING
  54.     for x in range(w):
  55.         #calculate ray position and direction
  56.         cameraX = 2 * x / w - 1; #x-coordinate in camera space
  57.         rayPosX = posX;
  58.         rayPosY = posY;
  59.         rayDirX = dirX + planeX * cameraX;
  60.         rayDirY = dirY + planeY * cameraX;
  61.  
  62.  
  63.  
  64.         #which box of the map we're in
  65.         mapX = int(rayPosX);
  66.         mapY = int(rayPosY);
  67.  
  68.         #length of ray from current position to next x or y-side
  69.         sideDistX=0;
  70.         sideDistY=0;
  71.  
  72.         #length of ray from one x or y-side to next x or y-side
  73.         try:
  74.             deltaDistX = sqrt(1 + (rayDirY * rayDirY) / (rayDirX * rayDirX));
  75.             deltaDistY = sqrt(1 + (rayDirX * rayDirX) / (rayDirY * rayDirY));
  76.         except ZeroDivisionError:
  77.             pass
  78.         perpWallDist=0;
  79.  
  80.         #what direction to step in x or y-direction (either +1 or -1)
  81.         stepX=0;
  82.         stepY=0;
  83.  
  84.         hit = 0; #was there a wall hit?
  85.         side=0; #was a NS or a EW wall hit?
  86.  
  87.         #calculate step and initial sideDist
  88.         if rayDirX < 0:
  89.             stepX = -1;
  90.             sideDistX = (rayPosX - mapX) * deltaDistX;
  91.         else:
  92.             stepX = 1;
  93.             sideDistX = (mapX + 1.0 - rayPosX) * deltaDistX;
  94.         if rayDirY < 0:
  95.             stepY = -1;
  96.             sideDistY = (rayPosY - mapY) * deltaDistY;
  97.         else:
  98.             stepY = 1;
  99.             sideDistY = (mapY + 1.0 - rayPosY) * deltaDistY;
  100.  
  101.         #perform DDA
  102.         while hit == 0:
  103.             #jump to next map square, OR in x-direction, OR in y-direction
  104.             if sideDistX < sideDistY:
  105.               sideDistX += deltaDistX;
  106.               mapX += stepX;
  107.               side = 0;
  108.             else:
  109.               sideDistY += deltaDistY;
  110.               mapY += stepY;
  111.               side = 1;
  112.             #Check if ray has hit a wall
  113.             if worldMap[mapX][mapY] > 0:
  114.                 hit = 1;
  115.  
  116.  
  117.  
  118.         #Calculate distance projected on camera direction (oblique distance will give fisheye effect!)
  119.         if side == 0:
  120.             perpWallDist = fabs((mapX - rayPosX + (1 - stepX) / 2) / rayDirX);
  121.         else:
  122.             perpWallDist = fabs((mapY - rayPosY + (1 - stepY) / 2) / rayDirY);
  123.  
  124.  
  125.  
  126.         #Calculate height of line to draw on screen
  127.         lineHeight = abs(int(h / perpWallDist));
  128.  
  129.         #calculate lowest and highest pixel to fill in current stripe
  130.         drawStart = -lineHeight / 2 + h / 2;
  131.         if drawStart < 0:
  132.             drawStart = 0;
  133.         drawEnd = lineHeight / 2 + h / 2;
  134.         if drawEnd >= h:
  135.             drawEnd = h - 1;
  136.  
  137.  
  138.         #choose wall color
  139.         if worldMap[mapX][mapY] == 1:
  140.           color = [255,1,1]
  141.         elif worldMap[mapX][mapY] == 2:
  142.           color = [1,255,1]
  143.         elif worldMap[mapX][mapY] == 3:
  144.           color = [1,1,255]
  145.         elif worldMap[mapX][mapY] == 4:
  146.           color = [255,255,255]
  147.         else:
  148.           color = [1,255,255]
  149.  
  150.         #give x and y sides different brightness
  151.         color = (color[0],color[1],color[2])
  152.         if side == 1:
  153.           color = (color[0]/2,color[1]/2,color[2]/2)
  154.  
  155.         #draw the pixels of the stripe as a vertical line
  156.         pygame.draw.line(DISP, color, (x,drawStart), (x,drawEnd), 1)
  157.  
  158.  
  159.     #Update Screen
  160.     pygame.display.flip()
  161.  
  162.     #Event Loop
  163.  
  164.     keys = pygame.key.get_pressed()
  165.     if keys[K_UP]:
  166.         if worldMap[int(posX + dirX * moveSpeed)][int(posY)] == False:
  167.             posX += dirX * moveSpeed;
  168.         if worldMap[int(posX)][int(posY + dirY * moveSpeed)] == False:
  169.             posY += dirY * moveSpeed;
  170.     if keys[K_LEFT]:
  171.         #both camera direction and camera plane must be rotated
  172.         oldDirX = dirX;
  173.         dirX = dirX * cos(rotSpeed) - dirY * sin(rotSpeed);
  174.         dirY = oldDirX * sin(rotSpeed) + dirY * cos(rotSpeed);
  175.         oldPlaneX = planeX;
  176.         planeX = planeX * cos(rotSpeed) - planeY * sin(rotSpeed);
  177.         planeY = oldPlaneX * sin(rotSpeed) + planeY * cos(rotSpeed);
  178.  
  179.     for e in pygame.event.get():
  180.         if e.type==QUIT:
  181.             pygame.display.quit()
  182.             go=False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement