Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- from pygame.locals import *
- from math import sqrt,fabs,cos,sin
- mapSize=24
- worldMap=[
- [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
- [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
- [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
- [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
- [1,0,0,0,0,0,2,2,2,2,2,0,0,0,0,3,0,3,0,3,0,0,0,1],
- [1,0,0,0,0,0,2,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,1],
- [1,0,0,0,0,0,2,0,0,0,2,0,0,0,0,3,0,0,0,3,0,0,0,1],
- [1,0,0,0,0,0,2,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,1],
- [1,0,0,0,0,0,2,2,0,2,2,0,0,0,0,3,0,3,0,3,0,0,0,1],
- [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
- [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
- [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
- [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
- [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
- [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
- [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
- [1,4,4,4,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
- [1,4,0,4,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
- [1,4,0,0,0,0,5,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
- [1,4,0,4,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
- [1,4,0,4,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
- [1,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
- [1,4,4,4,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
- [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
- ];
- posX = 22;posY = 12; #x and y start position
- dirX = -1;dirY = 0; #initial direction vector
- planeX = 0;planeY = 0.66; #the 2d raycaster version of camera plane
- time = 0; #time of current frame
- oldTime = 0; #time of previous frame
- DISP=pygame.display.set_mode((512,384))
- go = True
- w=512
- h=384
- rotSpeed=0.01
- moveSpeed=0.01
- while go:
- #Black Fill
- DISP.fill((0,0,0))
- #RAYCASTING
- for x in range(w):
- #calculate ray position and direction
- cameraX = 2 * x / w - 1; #x-coordinate in camera space
- rayPosX = posX;
- rayPosY = posY;
- rayDirX = dirX + planeX * cameraX;
- rayDirY = dirY + planeY * cameraX;
- #which box of the map we're in
- mapX = int(rayPosX);
- mapY = int(rayPosY);
- #length of ray from current position to next x or y-side
- sideDistX=0;
- sideDistY=0;
- #length of ray from one x or y-side to next x or y-side
- try:
- deltaDistX = sqrt(1 + (rayDirY * rayDirY) / (rayDirX * rayDirX));
- deltaDistY = sqrt(1 + (rayDirX * rayDirX) / (rayDirY * rayDirY));
- except ZeroDivisionError:
- pass
- perpWallDist=0;
- #what direction to step in x or y-direction (either +1 or -1)
- stepX=0;
- stepY=0;
- hit = 0; #was there a wall hit?
- side=0; #was a NS or a EW wall hit?
- #calculate step and initial sideDist
- if rayDirX < 0:
- stepX = -1;
- sideDistX = (rayPosX - mapX) * deltaDistX;
- else:
- stepX = 1;
- sideDistX = (mapX + 1.0 - rayPosX) * deltaDistX;
- if rayDirY < 0:
- stepY = -1;
- sideDistY = (rayPosY - mapY) * deltaDistY;
- else:
- stepY = 1;
- sideDistY = (mapY + 1.0 - rayPosY) * deltaDistY;
- #perform DDA
- while hit == 0:
- #jump to next map square, OR in x-direction, OR in y-direction
- if sideDistX < sideDistY:
- sideDistX += deltaDistX;
- mapX += stepX;
- side = 0;
- else:
- sideDistY += deltaDistY;
- mapY += stepY;
- side = 1;
- #Check if ray has hit a wall
- if worldMap[mapX][mapY] > 0:
- hit = 1;
- #Calculate distance projected on camera direction (oblique distance will give fisheye effect!)
- if side == 0:
- perpWallDist = fabs((mapX - rayPosX + (1 - stepX) / 2) / rayDirX);
- else:
- perpWallDist = fabs((mapY - rayPosY + (1 - stepY) / 2) / rayDirY);
- #Calculate height of line to draw on screen
- lineHeight = abs(int(h / perpWallDist));
- #calculate lowest and highest pixel to fill in current stripe
- drawStart = -lineHeight / 2 + h / 2;
- if drawStart < 0:
- drawStart = 0;
- drawEnd = lineHeight / 2 + h / 2;
- if drawEnd >= h:
- drawEnd = h - 1;
- #choose wall color
- if worldMap[mapX][mapY] == 1:
- color = [255,1,1]
- elif worldMap[mapX][mapY] == 2:
- color = [1,255,1]
- elif worldMap[mapX][mapY] == 3:
- color = [1,1,255]
- elif worldMap[mapX][mapY] == 4:
- color = [255,255,255]
- else:
- color = [1,255,255]
- #give x and y sides different brightness
- color = (color[0],color[1],color[2])
- if side == 1:
- color = (color[0]/2,color[1]/2,color[2]/2)
- #draw the pixels of the stripe as a vertical line
- pygame.draw.line(DISP, color, (x,drawStart), (x,drawEnd), 1)
- #Update Screen
- pygame.display.flip()
- #Event Loop
- keys = pygame.key.get_pressed()
- if keys[K_UP]:
- if worldMap[int(posX + dirX * moveSpeed)][int(posY)] == False:
- posX += dirX * moveSpeed;
- if worldMap[int(posX)][int(posY + dirY * moveSpeed)] == False:
- posY += dirY * moveSpeed;
- if keys[K_LEFT]:
- #both camera direction and camera plane must be rotated
- oldDirX = dirX;
- dirX = dirX * cos(rotSpeed) - dirY * sin(rotSpeed);
- dirY = oldDirX * sin(rotSpeed) + dirY * cos(rotSpeed);
- oldPlaneX = planeX;
- planeX = planeX * cos(rotSpeed) - planeY * sin(rotSpeed);
- planeY = oldPlaneX * sin(rotSpeed) + planeY * cos(rotSpeed);
- for e in pygame.event.get():
- if e.type==QUIT:
- pygame.display.quit()
- go=False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement