Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import pygame
- #from mapGen import *
- from math import *
- from pygame.locals import *
- def mapGenBlank(size):
- #Making a blank map.
- xP = int(size/2)
- yP = int(size/2)
- worldMap = [[random.randint(0,0) for i in range(0,size)] for n in range(0,size)]
- for yIndex, y in enumerate(worldMap):
- for xIndex, x in enumerate(y):
- if yIndex == 0 or yIndex == size-1:
- worldMap[yIndex][xIndex] = 2
- if xIndex == 0 or xIndex == size-1:
- worldMap[yIndex][xIndex] = 3
- return worldMap
- def mapGenTiled(size):
- xPTile = int(size/2)
- yPTile = int(size/2)
- tileMap = [[random.randint(1,3) for i in range(0,size)] for n in range(0,size)]
- worldMap = mapGenBlank(size*7+2)
- tileMap[yPTile][xPTile] = 1
- xP = 7*xPTile+3
- yP = 7*yPTile+3
- for yTileIndex, yTile in enumerate(tileMap):
- for xTileIndex, xTile in enumerate(yTile):
- current = tileDict[xTile]
- yMultiplier = yTileIndex*7+1
- xMultiplier = xTileIndex*7+1
- for yIndex, y in enumerate(current):
- for xIndex, x in enumerate(y):
- worldMap[yIndex+yMultiplier][xIndex+xMultiplier] = current[yIndex][xIndex]
- return worldMap, xP, yP
- #Tiled shapes
- tileDict = {
- 1:[
- [1, 1, 0, 0, 0, 1, 1],
- [1, 0, 0, 0, 0, 0, 1],
- [1, 0, 0, 0, 0, 0, 1],
- [0, 0, 0, 0, 0, 0, 0],
- [1, 0, 0, 0, 0, 0, 1],
- [1, 0, 0, 0, 0, 0, 1],
- [1, 1, 0, 0, 0, 1, 1]
- ],
- 2:[
- [2, 2, 0, 0, 0, 2, 2],
- [2, 2, 0, 0, 0, 2, 2],
- [2, 0, 2, 0, 2, 0, 2],
- [0, 0, 0, 0, 0, 0, 0],
- [2, 0, 2, 0, 2, 0, 2],
- [2, 2, 0, 0, 0, 2, 2],
- [2, 2, 0, 0, 0, 2, 2]
- ],
- 3:[
- [3, 3, 0, 0, 0, 3, 3],
- [3, 0, 0, 0, 0,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 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
- screenSize = (WIDTH,HEIGHT)
- screen = pygame.display.set_mode(screenSize)
- 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 = -0.7
- yDir = 0
- xPlane = 0
- yPlane = 0.66
- #worldMap = mapGenRandom(20)
- worldMap, xPos, yPos = mapGenTiled(5)
- for i in worldMap:
- print(''.join(str(i)))
- #worldMap = mapGenBlank(20)
- rotationSpeed = 0.01
- movementSpeed = 0.05
- #Exits pygame
- def close():
- pygame.display.quit()
- pygame.quit()
- quit()
- pygame.mouse.set_visible(False)
- pygame.event.set_grab(True)
- clock = pygame.time.Clock()
- #Start game loop
- while True:
- pygame.event.pump()
- #print(xPos, yPos, xPlane, yPlane)
- #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_w]:
- 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_s]:
- 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_d]:
- if not worldMap[int(xPos + xPlane * movementSpeed)] [int(yPos)]:
- xPos += xPlane * movementSpeed
- if not worldMap[int(xPos)][int(yPos + yPlane * movementSpeed)]:
- yPos += yPlane * movementSpeed
- if keys[K_a]:
- if not worldMap[int(xPos - xPlane * movementSpeed)] [int(yPos)]:
- xPos -= xPlane * movementSpeed
- if not worldMap[int(xPos)][int(yPos - yPlane * movementSpeed)]:
- yPos -= yPlane * 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)
- mouse = pygame.mouse.get_rel()
- xMouse = mouse[0]/100
- if xMouse < 0:
- xDirOld = xDir
- xDir = xDir * cos(-xMouse) - yDir * sin(-xMouse)
- yDir = xDirOld * sin(-xMouse) + yDir * cos(-xMouse)
- xPlaneOld = xPlane
- xPlane = xPlane * cos(-xMouse) - yPlane * sin(-xMouse)
- yPlane = xPlaneOld * sin(-xMouse) + yPlane * cos(-xMouse)
- if xMouse > 0:
- xDirOld = xDir
- xDir = xDir * cos(-xMouse) - yDir * sin(-xMouse)
- yDir = xDirOld * sin(-xMouse) + yDir * cos(-xMouse)
- xPlaneOld = xPlane
- xPlane = xPlane * cos(-xMouse) - yPlane * sin(-xMouse)
- yPlane = xPlaneOld * sin(-xMouse) + yPlane * cos(-xMouse)
- for column in range(0,WIDTH,2):
- 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 (yRayDir < 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 == 'x'):
- 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)
- clock.tick(60)
- print(clock.get_fps())
- pygame.display.update() #Updates screen
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement