Advertisement
Guest User

Untitled

a guest
Apr 26th, 2012
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.37 KB | None | 0 0
  1. import pygame
  2. from pygame.locals import *
  3. from loads import load_image
  4. import math, sys, os
  5. import numpy as N
  6. from numpy import int32
  7.  
  8. SCREEN_WIDTH = 640
  9. SCREEN_HEIGHT = 480
  10. MAP_WIDTH = 24
  11. MAP_HEIGHT = 24
  12. TEXT_WIDTH = 64
  13. TEXT_HEIGHT = 64
  14.  
  15. def load_map():
  16.     map = {}
  17.     for x in range(0, MAP_WIDTH):
  18.         for y in range(0, MAP_HEIGHT):
  19.             if x == 0 or x == MAP_WIDTH - 1 \
  20.                or y == 0 or y == MAP_HEIGHT - 1:
  21.                 map[(x, y)] = 1
  22.             else:
  23.                 map[(x, y)] = 0
  24.     return map
  25.  
  26. """
  27. def load_textures():
  28.    textures = []
  29.    files = ['brick.png']
  30.    for f in files:
  31.        #get the pixel buffer of the textures
  32.        textures.append(pygame.surfarray.array2d(
  33.            load_image(os.path.join('data', 'textures', f))[0]))
  34.    return textures
  35. """
  36.  
  37. def draw_verline(asf, x, start, end, line_height):
  38.     for y in xrange(start, end):
  39.         """
  40.        d = y * 256 - SCREEN_HEIGHT * 128 + line_height * 128
  41.        text_y = ((d * TEXT_HEIGHT) / line_height) / 256
  42.        color = (255, 0, 0)
  43.        """
  44.         asf[x][y] = 255
  45.         #s.set_at((x, y), color)
  46.         #pygame.draw.line(s, (255, 0, 0), (x, start), (x, end))
  47.     #pygame.draw.line(s, (255, 0, 0), (x, start), (x, end))
  48.     return asf
  49.  
  50. def main():
  51.     clock = pygame.time.Clock()
  52.     loop = True
  53.     pos_x, pos_y = 12.0, 12.0
  54.     dir_x, dir_y = -1.0, 1.0
  55.     plane_x, plane_y = 0.0, 0.66
  56.     move_speed = 0.2
  57.     rot_speed = 0.05
  58.     world_map = load_map()
  59.     pygame.surfarray.use_arraytype('numpy')
  60.     pygame.init()
  61.     screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), HWSURFACE|DOUBLEBUF, 16)
  62.     #textures = load_textures()
  63.     while (loop):
  64.         clock.tick(60)
  65.         asf = N.zeros((SCREEN_WIDTH, SCREEN_HEIGHT), int32)
  66.         for x in range(0, SCREEN_WIDTH):
  67.             camera_x = 2.0 * x / float(SCREEN_WIDTH) - 1.0
  68.             ray_pos_x = pos_x
  69.             ray_pos_y = pos_y
  70.             ray_dir_x = dir_x + plane_x * camera_x
  71.             ray_dir_y = dir_y + plane_y * camera_x
  72.             map_x = int(ray_pos_x)
  73.             map_y = int(ray_pos_y)
  74.             delta_dist_x = math.sqrt(1 + (ray_dir_y * ray_dir_y) /
  75.                                      (ray_dir_x * ray_dir_x))
  76.             delta_dist_y = math.sqrt(1 + (ray_dir_x * ray_dir_x) /
  77.                                      (ray_dir_y * ray_dir_y))
  78.             if ray_dir_x < 0:
  79.                 step_x = -1
  80.                 side_dist_x = (ray_pos_x - map_x) * delta_dist_x
  81.             else:
  82.                 step_x = 1
  83.                 side_dist_x = (map_x + 1.0 - ray_pos_x) * delta_dist_x
  84.             if ray_dir_y < 0:
  85.                 step_y = -1
  86.                 side_dist_y = (ray_pos_y - map_y) * delta_dist_y
  87.             else:
  88.                 step_y = 1
  89.                 side_dist_y = (map_y + 1.0 - ray_pos_y) * delta_dist_y
  90.             hit = 0
  91.             while hit == 0:
  92.                 if side_dist_x < side_dist_y:
  93.                     side_dist_x += delta_dist_x
  94.                     map_x += step_x
  95.                     side = 0
  96.                 else:
  97.                     side_dist_y += delta_dist_y
  98.                     map_y += step_y
  99.                     side = 1
  100.                 if world_map[(map_x, map_y)] > 0: hit = 1
  101.             if side == 0:
  102.                 perp_wall_dist = math.fabs(
  103.                     (map_x - ray_pos_x + (1 - step_x) / 2) / ray_dir_x)
  104.             else:
  105.                 perp_wall_dist = math.fabs(
  106.                     (map_y - ray_pos_y + (1 - step_y) / 2) / ray_dir_y);
  107.             line_height = math.fabs(int(SCREEN_HEIGHT / perp_wall_dist))
  108.             draw_start = -line_height / 2 + SCREEN_HEIGHT / 2
  109.             if draw_start < 0: draw_start = 0
  110.             draw_end = line_height / 2 + SCREEN_HEIGHT / 2
  111.             if draw_end >= SCREEN_HEIGHT: draw_end = SCREEN_HEIGHT - 1
  112.             text_num = world_map[(map_x, map_y)] - 1
  113.             if side == 1:
  114.                 wall_x = ray_pos_x + ((map_y - ray_pos_y + (1 - step_y) / 2) /
  115.                                       ray_dir_y) * ray_dir_x
  116.             else:
  117.                 wall_x = ray_pos_y + ((map_x - ray_pos_x + (1 - step_x) / 2) /
  118.                                       ray_dir_x) * ray_dir_y
  119.             wall_x -= math.floor((wall_x))
  120.             text_x = int(wall_x * float(TEXT_WIDTH))
  121.             if side == 0 and ray_dir_x > 0: text_x = TEXT_WIDTH - text_x - 1
  122.             if side == 1 and ray_dir_y < 0: text_x = TEXT_WIDTH - text_x - 1
  123.             asf = draw_verline(asf, x, int(draw_start), int(draw_end), line_height)
  124.         for e in pygame.event.get():
  125.             if e.type == QUIT:
  126.                 loop = False
  127.             if e.type == KEYDOWN:
  128.                 if e.key == K_ESCAPE:
  129.                     loop = False
  130.         key = pygame.key.get_pressed()
  131.         if key[K_UP]:
  132.             if world_map[(int(pos_x + dir_x * move_speed), int(pos_y))] == 0: pos_x += dir_x * move_speed
  133.             if world_map[(int(pos_x), int(pos_y + dir_y * move_speed))] == 0: pos_y += dir_y * move_speed
  134.         if key[K_DOWN]:
  135.             if world_map[(int(pos_x - dir_x * move_speed), int(pos_y))] == 0: pos_x -= dir_x * move_speed
  136.             if world_map[(int(pos_x), int(pos_y - dir_y * move_speed))] == 0: pos_y -= dir_y * move_speed
  137.         if key[K_RIGHT]:
  138.             old_dir_x = dir_x
  139.             dir_x = dir_x * math.cos(-rot_speed) - dir_y * \
  140.                 math.sin(-rot_speed)
  141.             dir_y = old_dir_x * math.sin(-rot_speed) + dir_y * \
  142.                 math.cos(-rot_speed)
  143.             old_plane_x = plane_x
  144.             plane_x = plane_x * math.cos(-rot_speed) - plane_y * \
  145.                 math.sin(-rot_speed)
  146.             plane_y = old_plane_x * math.sin(-rot_speed) + \
  147.                 plane_y * math.cos(-rot_speed)
  148.         if key[K_LEFT]:
  149.             old_dir_x = dir_x
  150.             dir_x = dir_x * math.cos(rot_speed) - dir_y * \
  151.                 math.sin(rot_speed)
  152.             dir_y = old_dir_x * math.sin(rot_speed) + dir_y * \
  153.                 math.cos(rot_speed)
  154.             old_plane_x = plane_x
  155.             plane_x = plane_x * math.cos(rot_speed) - plane_y * \
  156.                 math.sin(rot_speed)
  157.             plane_y = old_plane_x * math.sin(rot_speed) + \
  158.                 plane_y * math.cos(rot_speed)
  159.         pygame.surfarray.blit_array(screen, asf)
  160.         pygame.display.flip()
  161.  
  162. if __name__ == '__main__':
  163.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement