mixster

Untitled

Apr 22nd, 2010
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.88 KB | None | 0 0
  1. #!/user/bin/env python
  2. import sys, pygame, os, math
  3. from pygame.locals import *
  4.  
  5. #--- Init pygame
  6. os.environ["SDL_VIDEO_CENTERED"] = "1"
  7. pygame.init
  8. clock = pygame.time.Clock()
  9. pygame.display.set_caption("3D")
  10. screen = pygame.display.set_mode((640, 480))
  11.  
  12.  
  13.  
  14. def World(object):
  15.     def __init__(self, list):
  16.         self.SectorList = list
  17.  
  18.  
  19. def Sector(object):
  20.     def __init__(self, polys, objects):
  21.         self.PolygonList = polys
  22.         self.ObjectList = objects
  23.  
  24.  
  25. def Object(object):
  26.     def __init__(self, polys, objects, matrix):
  27.         self.PolygonList = polys
  28.         self.Children = objects
  29.         self.matrix = matrix
  30.  
  31.  
  32. def Polygon(object):
  33.     def __init__(self, vertex, texture, sector):
  34.         self.VertexChildren = vertex
  35.         self.Texture = texture
  36.         sefl.Sector = sector
  37.    
  38.  
  39. def Vertex(object):
  40.     def __init__(self, position, (u, v)):
  41.         self.Coordinate = position
  42.         self.U, self.V = u, v
  43.  
  44.  
  45. def Coordinate(object):
  46.     def __init__(self, original, rotated, processed):
  47.         self.original = original#vertex
  48.         self.rotated = rotated#vertex
  49.         self.processed = processed
  50.  
  51.  
  52. angle = 0.0
  53. x = [0] * 8
  54. y = [0] * 8
  55. z = [0] * 8
  56. rx = [0] * 8
  57. ry = [0] * 8
  58. rz = [0] * 8
  59. scrx = [0] * 8
  60. scry = [0] * 8
  61. scry = [0] * 8
  62.  
  63. def line(buf, x1, y1, x2, y2):
  64.     pygame.draw.line(screen, (0,0,0), (x1, y1), (x2, y2))
  65.     hl = abs(x2 - x1)
  66.     vl = abs(y2 - y1)
  67.     if hl > vl:
  68.         length = hl
  69.     else:
  70.         length = vl
  71.     deltax = (x2 - x1) / float(length)
  72.     deltay = (y2 - y1) / float(length)
  73.     for i in range(int(math.ceil(length)) - 1):
  74.         x1 = x1 + deltax
  75.         y1 = y1 + deltay
  76.         x = int((x1))
  77.         y = int((y1))
  78.         if ((x < 640) and (y < 480)):
  79.             buf.set_at((x, y), (255,255, 255))
  80.  
  81. def render (buf, xa, ya, za):
  82.     mat = [[0 for i in range(4)] for j in range(4)]  # Determine rotation matrix
  83.     xdeg = float(xa * 3.1416 / 180)
  84.     ydeg = float(ya * 3.1416 / 180)
  85.     zdeg = float(za * 3.1416 / 180)
  86.     sx = float(math.sin(xdeg))
  87.     sy = float(math.sin(ydeg))
  88.     sz = float(math.sin(zdeg))
  89.     cx = float(math.cos(xdeg))
  90.     cy = float(math.cos(ydeg))
  91.     cz = float(math.cos(zdeg))
  92.     mat[0][0] = cx * cz + sx * sy * sz
  93.     mat[1][0] = -cx * sz + cz * sx * sy
  94.     mat[2][0] = cy * sx
  95.     mat[0][1] = cy * sz
  96.     mat[1][1] = cy * cz
  97.     mat[2][1] = -sy
  98.     mat[0][2] = -cz * sx + cx * sy * sz
  99.     mat[1][2] = sx * sz + cx * cz * sy
  100.     mat[2][2] = cx * cy
  101.     for i in range(8):     # Rotate and apply perspective
  102.         rx[i] = x[i] * mat[0][0] + y[i] * mat[1][0] + z[i] * mat[2][0]
  103.         ry[i] = x[i] * mat[0][1] + y[i] * mat[1][1] + z[i] * mat[2][1]
  104.         rz[i] = x[i] * mat[0][2] + y[i] * mat[1][2] + z[i] * mat[2][2] + 300
  105.         scrx[i] = (rx[i] * 500) / rz[i] + 320
  106.         scry[i] = (ry[i] * 500) / rz[i] + 240
  107.     for i in range(4):        # Actual drawing
  108.         line(buf, scrx[i], scry[i], scrx[i + 4], scry[i + 4])
  109.         line(buf, scrx[i], scry[i], scrx[(i + 1) % 4], scry[(i + 1) % 4])
  110.         line(buf, scrx[i + 4], scry[i + 4], scrx[((i + 1) % 4) + 4], scry[((i + 1) % 4) + 4])
  111.        
  112. def getEvents():
  113.     for event in pygame.event.get():
  114.         if event.type == pygame.QUIT:
  115.             gameRunning = False
  116.             sys.exit()
  117.  
  118. '''for i in range(0,7):     # Define the cube
  119.    x[i] = float((50 - 100 * (((i + 1) / 2) % 2)))
  120.    y[i] = float((50 - 100 * ((i / 2) % 2)))
  121.    z[i] = float((50 - 100 * ((i / 4) % 2)))'''
  122. x = [-50, 50, 50, -50, -50, 50, 50, -50]
  123. y = [-50, -50, 50, 50, -50, -50, 50, 50]
  124. z = [-50, -50, -50, -50, 50, 50, 50, 50]
  125.  
  126. gameRunning = True
  127. while gameRunning:
  128.     getEvents()
  129.    
  130.     screen.fill((0, 0, 0))
  131.     screen.lock
  132.     render(screen, angle, 360 - angle, 0)
  133.     angle = angle + 0.2
  134.     if (angle == 360):
  135.         angle = 0
  136.  
  137.     screen.unlock
  138.     clock.tick(60)
  139.     pygame.display.flip()
Add Comment
Please, Sign In to add comment