diff --git a/pyray.py b/pyray.py index 9cd9374..9efc7ef 100644 --- a/pyray.py +++ b/pyray.py @@ -20,11 +20,13 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -""" +""" try: import math import time + import cv2 + import numpy as np import pygame from pygame.locals import * @@ -32,10 +34,11 @@ print "PyRay could not import necessary modules" raise ImportError -keys=[False]*324 +keys = [False] * 500 +constantMinus = 1073741603 # A map over the world -worldMap = [ +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], @@ -57,8 +60,19 @@ [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]] -# Closes the program -def close(): +# Alternative: Use black / white image as map data +#worldMap = cv2.imread("map.png", 0) +#worldMap = cv2.bitwise_not(worldMap) +#worldMap = (worldMap / 255) +#worldMap = np.where(worldMap > 0.5, 1, 0).tolist() +#worldMap.insert(0,[2] * len(worldMap[0])) +#worldMap.append([2] * len(worldMap[0])) +#for i in range(0, len(worldMap)): +# worldMap[i].append(2) +# worldMap[i].insert(0, 2) + +# Closes the program +def close(): pygame.display.quit() pygame.quit() @@ -69,15 +83,15 @@ def main(): font = pygame.font.SysFont("Verdana",20) HUD = font.render("F1 / F2 - Screenshot JPEG/BMP F5/F6 - Shadows on/off F7/F8 - HUD Show/Hide", True, (0,0,0)) - # Creates window + # Creates window WIDTH = 1000 HEIGHT = 800 screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("PyRay - Python Raycasting Engine (v0.03)") showShadow = True - showHUD = True - + showHUD = True + # Defines starting position and direction positionX = 3.0 positionY = 7.0 @@ -88,7 +102,7 @@ def main(): planeX = 0.0 planeY = 0.5 - # Movement constants + # Movement constants ROTATIONSPEED = 0.02 MOVESPEED = 0.03 @@ -96,25 +110,25 @@ def main(): TGM = (math.cos(ROTATIONSPEED), math.sin(ROTATIONSPEED)) ITGM = (math.cos(-ROTATIONSPEED), math.sin(-ROTATIONSPEED)) COS, SIN = (0,1) - + while True: # Catches user input # Sets keys[key] to True or False for event in pygame.event.get(): - if event.type == KEYDOWN: + if event.type == KEYDOWN and event.key - constantMinus >= 0: if event.key == K_ESCAPE: close() return - keys[event.key] = True - elif event.type == KEYUP: - keys[event.key] = False + keys[event.key - constantMinus] = True + elif event.type == KEYUP and event.key - constantMinus >= 0: + keys[event.key - constantMinus] = False # Checks with keys are pressed by the user - # Uses if so that more than one button at a time can be pressed. + # Uses if so that more than one button at a time can be pressed. if keys[K_ESCAPE]: close() - if keys[K_LEFT]: + if keys[K_LEFT - constantMinus]: oldDirectionX = directionX directionX = directionX * ITGM[COS] - directionY * ITGM[SIN] directionY = oldDirectionX * ITGM[SIN] + directionY * ITGM[COS] @@ -122,62 +136,62 @@ def main(): planeX = planeX * ITGM[COS] - planeY * ITGM[SIN] planeY = oldPlaneX * ITGM[SIN] + planeY * ITGM[COS] - if keys[K_RIGHT]: + if keys[K_RIGHT - constantMinus]: oldDirectionX = directionX directionX = directionX * TGM[COS] - directionY * TGM[SIN] directionY = oldDirectionX * TGM[SIN] + directionY * TGM[COS] oldPlaneX = planeX planeX = planeX * TGM[COS] - planeY * TGM[SIN] - planeY = oldPlaneX * TGM[SIN] + planeY * TGM[COS] + planeY = oldPlaneX * TGM[SIN] + planeY * TGM[COS] - if keys[K_UP]: + if keys[K_UP - constantMinus]: if not worldMap[int(positionX + directionX * MOVESPEED)][int(positionY)]: positionX += directionX * MOVESPEED if not worldMap[int(positionX)][int(positionY + directionY * MOVESPEED)]: positionY += directionY * MOVESPEED - - if keys[K_DOWN]: + + if keys[K_DOWN - constantMinus]: if not worldMap[int(positionX - directionX * MOVESPEED)][int(positionY)]: positionX -= directionX * MOVESPEED if not worldMap[int(positionX)][int(positionY - directionY * MOVESPEED)]: positionY -= directionY * MOVESPEED - if keys[K_F1]: + if keys[K_F1 - constantMinus]: try: pygame.image.save(screen,('PyRay' + time.strftime('%Y%m%d%H%M%S')+ '.jpeg')) except: - print "Couldn't save jpeg screenshot" - - if keys[K_F2]: + print("Couldn't save jpeg screenshot") + + if keys[K_F2 - constantMinus]: try: pygame.image.save(screen,('PyRay' + time.strftime('%Y%m%d%H%M%S')+ '.bmp')) except: - print "Couldn't save bmp screenshot" + print("Couldn't save bmp screenshot") # showShadows - On / Off - if keys[K_F5]: + if keys[K_F5 - constantMinus]: showShadow = True - if keys[K_F6]: + if keys[K_F6 - constantMinus]: showShadow = False # showHUD - Show / Hide - if keys[K_F7]: + if keys[K_F7 - constantMinus]: showHUD = True - if keys[K_F8]: + if keys[K_F8 - constantMinus]: showHUD = False - + # Draws roof and floor screen.fill((25,25,25)) - pygame.draw.rect(screen, (50,50,50), (0, HEIGHT/2, WIDTH, HEIGHT/2)) - - # Starts drawing level from 0 to < WIDTH - column = 0 + pygame.draw.rect(screen, (50,50,50), (0, HEIGHT/2, WIDTH, HEIGHT/2)) + + # Starts drawing level from 0 to < WIDTH + column = 0 while column < WIDTH: cameraX = 2.0 * column / WIDTH - 1.0 rayPositionX = positionX rayPositionY = positionY rayDirectionX = directionX + planeX * cameraX - rayDirectionY = directionY + planeY * cameraX + .000000000000001 # avoiding ZDE + rayDirectionY = directionY + planeY * cameraX + .000000000000001 # avoiding ZDE # In what square is the ray? mapX = int(rayPositionX) @@ -212,12 +226,12 @@ def main(): sideDistanceX += deltaDistanceX mapX += stepX side = 0 - + else: sideDistanceY += deltaDistanceY mapY += stepY side = 1 - + if (worldMap[mapX][mapY] > 0): hit = 1 @@ -242,17 +256,17 @@ def main(): # Wall colors 0 to 3 wallcolors = [ [], [150,0,0], [0,150,0], [0,0,150] ] - color = wallcolors[ worldMap[mapX][mapY] ] + color = wallcolors[ worldMap[mapX][mapY] ] # If side == 1 then ton the color down. Gives a "showShadow" an the wall. # Draws showShadow if showShadow is True if showShadow: if side == 1: for i,v in enumerate(color): - color[i] = int(v / 1.2) + color[i] = int(v / 1.2) - # Drawing the graphics - pygame.draw.line(screen, color, (column,drawStart), (column, drawEnd), 2) + # Drawing the graphics + pygame.draw.line(screen, color, (column,drawStart), (column,drawEnd), 2) column += 2 # Drawing HUD if showHUD is True @@ -262,6 +276,6 @@ def main(): # Updating display pygame.event.pump() - pygame.display.flip() - -main() \ No newline at end of file + pygame.display.flip() + +main()