Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- from pygame.locals import *
- from loads import load_image
- import math, sys, os
- import numpy as N
- from numpy import int32
- SCREEN_WIDTH = 640
- SCREEN_HEIGHT = 480
- MAP_WIDTH = 24
- MAP_HEIGHT = 24
- TEXT_WIDTH = 64
- TEXT_HEIGHT = 64
- def load_map():
- map = {}
- for x in range(0, MAP_WIDTH):
- for y in range(0, MAP_HEIGHT):
- if x == 0 or x == MAP_WIDTH - 1 \
- or y == 0 or y == MAP_HEIGHT - 1:
- map[(x, y)] = 1
- else:
- map[(x, y)] = 0
- return map
- """
- def load_textures():
- textures = []
- files = ['brick.png']
- for f in files:
- #get the pixel buffer of the textures
- textures.append(pygame.surfarray.array2d(
- load_image(os.path.join('data', 'textures', f))[0]))
- return textures
- """
- def draw_verline(asf, x, start, end, line_height):
- for y in xrange(start, end):
- """
- d = y * 256 - SCREEN_HEIGHT * 128 + line_height * 128
- text_y = ((d * TEXT_HEIGHT) / line_height) / 256
- color = (255, 0, 0)
- """
- asf[x][y] = 255
- #s.set_at((x, y), color)
- #pygame.draw.line(s, (255, 0, 0), (x, start), (x, end))
- #pygame.draw.line(s, (255, 0, 0), (x, start), (x, end))
- return asf
- def main():
- clock = pygame.time.Clock()
- loop = True
- pos_x, pos_y = 12.0, 12.0
- dir_x, dir_y = -1.0, 1.0
- plane_x, plane_y = 0.0, 0.66
- move_speed = 0.2
- rot_speed = 0.05
- world_map = load_map()
- pygame.surfarray.use_arraytype('numpy')
- pygame.init()
- screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), HWSURFACE|DOUBLEBUF, 16)
- #textures = load_textures()
- while (loop):
- clock.tick(60)
- asf = N.zeros((SCREEN_WIDTH, SCREEN_HEIGHT), int32)
- for x in range(0, SCREEN_WIDTH):
- camera_x = 2.0 * x / float(SCREEN_WIDTH) - 1.0
- ray_pos_x = pos_x
- ray_pos_y = pos_y
- ray_dir_x = dir_x + plane_x * camera_x
- ray_dir_y = dir_y + plane_y * camera_x
- map_x = int(ray_pos_x)
- map_y = int(ray_pos_y)
- delta_dist_x = math.sqrt(1 + (ray_dir_y * ray_dir_y) /
- (ray_dir_x * ray_dir_x))
- delta_dist_y = math.sqrt(1 + (ray_dir_x * ray_dir_x) /
- (ray_dir_y * ray_dir_y))
- if ray_dir_x < 0:
- step_x = -1
- side_dist_x = (ray_pos_x - map_x) * delta_dist_x
- else:
- step_x = 1
- side_dist_x = (map_x + 1.0 - ray_pos_x) * delta_dist_x
- if ray_dir_y < 0:
- step_y = -1
- side_dist_y = (ray_pos_y - map_y) * delta_dist_y
- else:
- step_y = 1
- side_dist_y = (map_y + 1.0 - ray_pos_y) * delta_dist_y
- hit = 0
- while hit == 0:
- if side_dist_x < side_dist_y:
- side_dist_x += delta_dist_x
- map_x += step_x
- side = 0
- else:
- side_dist_y += delta_dist_y
- map_y += step_y
- side = 1
- if world_map[(map_x, map_y)] > 0: hit = 1
- if side == 0:
- perp_wall_dist = math.fabs(
- (map_x - ray_pos_x + (1 - step_x) / 2) / ray_dir_x)
- else:
- perp_wall_dist = math.fabs(
- (map_y - ray_pos_y + (1 - step_y) / 2) / ray_dir_y);
- line_height = math.fabs(int(SCREEN_HEIGHT / perp_wall_dist))
- draw_start = -line_height / 2 + SCREEN_HEIGHT / 2
- if draw_start < 0: draw_start = 0
- draw_end = line_height / 2 + SCREEN_HEIGHT / 2
- if draw_end >= SCREEN_HEIGHT: draw_end = SCREEN_HEIGHT - 1
- text_num = world_map[(map_x, map_y)] - 1
- if side == 1:
- wall_x = ray_pos_x + ((map_y - ray_pos_y + (1 - step_y) / 2) /
- ray_dir_y) * ray_dir_x
- else:
- wall_x = ray_pos_y + ((map_x - ray_pos_x + (1 - step_x) / 2) /
- ray_dir_x) * ray_dir_y
- wall_x -= math.floor((wall_x))
- text_x = int(wall_x * float(TEXT_WIDTH))
- if side == 0 and ray_dir_x > 0: text_x = TEXT_WIDTH - text_x - 1
- if side == 1 and ray_dir_y < 0: text_x = TEXT_WIDTH - text_x - 1
- asf = draw_verline(asf, x, int(draw_start), int(draw_end), line_height)
- for e in pygame.event.get():
- if e.type == QUIT:
- loop = False
- if e.type == KEYDOWN:
- if e.key == K_ESCAPE:
- loop = False
- key = pygame.key.get_pressed()
- if key[K_UP]:
- if world_map[(int(pos_x + dir_x * move_speed), int(pos_y))] == 0: pos_x += dir_x * move_speed
- if world_map[(int(pos_x), int(pos_y + dir_y * move_speed))] == 0: pos_y += dir_y * move_speed
- if key[K_DOWN]:
- if world_map[(int(pos_x - dir_x * move_speed), int(pos_y))] == 0: pos_x -= dir_x * move_speed
- if world_map[(int(pos_x), int(pos_y - dir_y * move_speed))] == 0: pos_y -= dir_y * move_speed
- if key[K_RIGHT]:
- old_dir_x = dir_x
- dir_x = dir_x * math.cos(-rot_speed) - dir_y * \
- math.sin(-rot_speed)
- dir_y = old_dir_x * math.sin(-rot_speed) + dir_y * \
- math.cos(-rot_speed)
- old_plane_x = plane_x
- plane_x = plane_x * math.cos(-rot_speed) - plane_y * \
- math.sin(-rot_speed)
- plane_y = old_plane_x * math.sin(-rot_speed) + \
- plane_y * math.cos(-rot_speed)
- if key[K_LEFT]:
- old_dir_x = dir_x
- dir_x = dir_x * math.cos(rot_speed) - dir_y * \
- math.sin(rot_speed)
- dir_y = old_dir_x * math.sin(rot_speed) + dir_y * \
- math.cos(rot_speed)
- old_plane_x = plane_x
- plane_x = plane_x * math.cos(rot_speed) - plane_y * \
- math.sin(rot_speed)
- plane_y = old_plane_x * math.sin(rot_speed) + \
- plane_y * math.cos(rot_speed)
- pygame.surfarray.blit_array(screen, asf)
- pygame.display.flip()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement