Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/user/bin/env python
- import sys, pygame, os, math
- from pygame.locals import *
- #--- Init pygame
- os.environ["SDL_VIDEO_CENTERED"] = "1"
- pygame.init
- clock = pygame.time.Clock()
- pygame.display.set_caption("3D")
- screen = pygame.display.set_mode((640, 480))
- def World(object):
- def __init__(self, list):
- self.SectorList = list
- def Sector(object):
- def __init__(self, polys, objects):
- self.PolygonList = polys
- self.ObjectList = objects
- def Object(object):
- def __init__(self, polys, objects, matrix):
- self.PolygonList = polys
- self.Children = objects
- self.matrix = matrix
- def Polygon(object):
- def __init__(self, vertex, texture, sector):
- self.VertexChildren = vertex
- self.Texture = texture
- sefl.Sector = sector
- def Vertex(object):
- def __init__(self, position, (u, v)):
- self.Coordinate = position
- self.U, self.V = u, v
- def Coordinate(object):
- def __init__(self, original, rotated, processed):
- self.original = original#vertex
- self.rotated = rotated#vertex
- self.processed = processed
- angle = 0.0
- x = [0] * 8
- y = [0] * 8
- z = [0] * 8
- rx = [0] * 8
- ry = [0] * 8
- rz = [0] * 8
- scrx = [0] * 8
- scry = [0] * 8
- scry = [0] * 8
- def line(buf, x1, y1, x2, y2):
- pygame.draw.line(screen, (0,0,0), (x1, y1), (x2, y2))
- hl = abs(x2 - x1)
- vl = abs(y2 - y1)
- if hl > vl:
- length = hl
- else:
- length = vl
- deltax = (x2 - x1) / float(length)
- deltay = (y2 - y1) / float(length)
- for i in range(int(math.ceil(length)) - 1):
- x1 = x1 + deltax
- y1 = y1 + deltay
- x = int((x1))
- y = int((y1))
- if ((x < 640) and (y < 480)):
- buf.set_at((x, y), (255,255, 255))
- def render (buf, xa, ya, za):
- mat = [[0 for i in range(4)] for j in range(4)] # Determine rotation matrix
- xdeg = float(xa * 3.1416 / 180)
- ydeg = float(ya * 3.1416 / 180)
- zdeg = float(za * 3.1416 / 180)
- sx = float(math.sin(xdeg))
- sy = float(math.sin(ydeg))
- sz = float(math.sin(zdeg))
- cx = float(math.cos(xdeg))
- cy = float(math.cos(ydeg))
- cz = float(math.cos(zdeg))
- mat[0][0] = cx * cz + sx * sy * sz
- mat[1][0] = -cx * sz + cz * sx * sy
- mat[2][0] = cy * sx
- mat[0][1] = cy * sz
- mat[1][1] = cy * cz
- mat[2][1] = -sy
- mat[0][2] = -cz * sx + cx * sy * sz
- mat[1][2] = sx * sz + cx * cz * sy
- mat[2][2] = cx * cy
- for i in range(8): # Rotate and apply perspective
- rx[i] = x[i] * mat[0][0] + y[i] * mat[1][0] + z[i] * mat[2][0]
- ry[i] = x[i] * mat[0][1] + y[i] * mat[1][1] + z[i] * mat[2][1]
- rz[i] = x[i] * mat[0][2] + y[i] * mat[1][2] + z[i] * mat[2][2] + 300
- scrx[i] = (rx[i] * 500) / rz[i] + 320
- scry[i] = (ry[i] * 500) / rz[i] + 240
- for i in range(4): # Actual drawing
- line(buf, scrx[i], scry[i], scrx[i + 4], scry[i + 4])
- line(buf, scrx[i], scry[i], scrx[(i + 1) % 4], scry[(i + 1) % 4])
- line(buf, scrx[i + 4], scry[i + 4], scrx[((i + 1) % 4) + 4], scry[((i + 1) % 4) + 4])
- def getEvents():
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- gameRunning = False
- sys.exit()
- '''for i in range(0,7): # Define the cube
- x[i] = float((50 - 100 * (((i + 1) / 2) % 2)))
- y[i] = float((50 - 100 * ((i / 2) % 2)))
- z[i] = float((50 - 100 * ((i / 4) % 2)))'''
- x = [-50, 50, 50, -50, -50, 50, 50, -50]
- y = [-50, -50, 50, 50, -50, -50, 50, 50]
- z = [-50, -50, -50, -50, 50, 50, 50, 50]
- gameRunning = True
- while gameRunning:
- getEvents()
- screen.fill((0, 0, 0))
- screen.lock
- render(screen, angle, 360 - angle, 0)
- angle = angle + 0.2
- if (angle == 360):
- angle = 0
- screen.unlock
- clock.tick(60)
- pygame.display.flip()
Add Comment
Please, Sign In to add comment