Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- #import math
- from math import *
- from pygame.locals import *
- #PyRay 20x20
- worldMap = [
- [1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2],
- [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2],
- [2, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
- [1, 0, 2, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 2, 3, 2, 3, 0, 0, 2],
- [2, 0, 3, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
- [1, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2],
- [2, 3, 1, 0, 0, 2, 0, 0, 0, 2, 3, 2, 0, 0, 0, 0, 0, 0, 0, 1],
- [1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 2, 0, 0, 0, 2],
- [2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, 2, 1, 0, 0, 0, 1],
- [1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 0, 0, 0, 0, 0, 0, 0, 2],
- [2, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
- [1, 0, 2, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2],
- [2, 0, 3, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 3, 2, 1, 2, 0, 1],
- [1, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 2],
- [2, 3, 1, 0, 0, 2, 0, 0, 2, 1, 3, 2, 0, 2, 0, 0, 3, 0, 3, 1],
- [1, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 1, 0, 0, 2, 0, 0, 2],
- [2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 2, 3, 0, 1, 2, 0, 1],
- [1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 3, 0, 2],
- [2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 1],
- [2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1]]
- mapSize = len(worldMap)
- #Creates view window
- pygame.init()
- HEIGHT = 600
- WIDTH = 1000
- size = (WIDTH,HEIGHT)
- screen = pygame.display.set_mode(size)
- pygame.display.set_caption('Hmm')
- keys = [False]*324 #List for keyboard values
- zde = 0.0000001 #Add to prvent division by zero
- res = 2
- #Staring Position
- xPos = 10.0
- yPos = 5.0
- xDir = -1
- yDir = 0
- xPlane = 0
- yPlane = 0.66
- rotationSpeed = 0.01
- movementSpeed = 0.05
- #Exits pygame
- def close():
- pygame.display.quit()
- pygame.quit()
- #Start game loop
- while True:
- pygame.event.pump()
- #Draws background. Black and Grey
- screen.fill((25,25,25))
- pygame.draw.rect(screen, (50,50,50), (0, HEIGHT/2, WIDTH, HEIGHT/2)) #Draws grey rectangle in bottom half
- #Sets whether keys are held or not
- for event in pygame.event.get():
- if event.type == KEYDOWN:
- keys[event.key] = True
- elif event.type == KEYUP:
- keys[event.key] = False
- #Keybindings
- if keys[K_ESCAPE]:
- close()
- if keys[K_UP]:
- if not worldMap[int(xPos + xDir * movementSpeed)] [int(yPos)]:
- xPos += xDir * movementSpeed
- if not worldMap[int(xPos)][int(yPos + yDir * movementSpeed)]:
- yPos += yDir * movementSpeed
- if keys[K_DOWN]:
- if not worldMap[int(xPos - xDir * movementSpeed)] [int(yPos)]:
- xPos -= xDir * movementSpeed
- if not worldMap[int(xPos)][int(yPos - yDir * movementSpeed)]:
- yPos -= yDir * movementSpeed
- if keys[K_RIGHT]:
- xDirOld = xDir
- xDir = xDir * cos(-rotationSpeed) - yDir * sin(-rotationSpeed)
- yDir = xDirOld * sin(-rotationSpeed) + yDir * cos(-rotationSpeed)
- xPlaneOld = xPlane
- xPlane = xPlane * cos(-rotationSpeed) - yPlane * sin(-rotationSpeed)
- yPlane = xPlaneOld * sin(-rotationSpeed) + yPlane * cos(-rotationSpeed)
- if keys[K_LEFT]:
- xDirOld = xDir
- xDir = xDir * cos(rotationSpeed) - yDir * sin(rotationSpeed)
- yDir = xDirOld * sin(rotationSpeed) + yDir * cos(rotationSpeed)
- xPlaneOld = xPlane
- xPlane = xPlane * cos(rotationSpeed) - yPlane * sin(rotationSpeed)
- yPlane = xPlaneOld * sin(rotationSpeed) + yPlane * cos(rotationSpeed)
- for column in range(0,WIDTH,res):
- xCamera = 2 * column / float(WIDTH) - 1.0
- xRayDir = xDir + xPlane * xCamera
- yRayDir = yDir + yPlane * xCamera
- xMap = int(xPos)
- yMap = int(yPos)
- xSideDistance = 0.0
- ySideDistance = 0.0
- perpWallDistance = 0.0
- xDeltaDistance = abs(1/(xRayDir + zde))
- yDeltaDistance = abs(1/(yRayDir + zde))
- xStep = 0
- yStep = 0
- hit = False
- side = 0
- if (xRayDir < 0):
- xStep = -1
- xSideDistance = (xPos - xMap) * xDeltaDistance
- else:
- xStep = -1
- xSideDistance = (xMap + 1.0 - xPos) * xDeltaDistance
- if (xRayDir < 0):
- yStep = -1
- ySideDistance = (yPos - yMap) * yDeltaDistance
- else:
- yStep = 1;
- ySideDistance = (yMap + 1.0 - yPos) * yDeltaDistance
- hit = False
- while not hit:
- if (xSideDistance < ySideDistance):
- xSideDistance += xDeltaDistance
- xMap += xStep
- side = 'x'
- else:
- ySideDistance += yDeltaDistance
- yMap += yStep
- side = 'y'
- if worldMap[xMap][yMap] != 0 :
- hit = True
- if (side == 0):
- perpWallDist = (xMap - xPos + (1 - xStep) / 2) / (xRayDir + zde)
- else:
- perpWallDist = (yMap - yPos + (1 - yStep) / 2) / (yRayDir + zde)
- lineHeight = int(HEIGHT / (perpWallDist + zde))
- drawStart = -lineHeight / 2 + HEIGHT / 2
- if (drawStart < 0):
- drawStart = 0
- drawEnd = lineHeight / 2 + HEIGHT / 2
- if (drawEnd >= HEIGHT):
- drawEnd = HEIGHT - 1
- wallcolours = [ [255,255,255], [150,0,0], [0,150,0], [0,0,150] ]
- colour = wallcolours[worldMap[xMap][yMap]]
- if side == 'y':
- colour = [i/2 for i in colour]
- pygame.draw.line(screen, colour, (column,drawStart), (column, drawEnd), res)
- pygame.display.update() #Updates screen
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement