Guest User

Untitled

a guest
Mar 19th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. # THEMAGE RENDERING-ENGINE V1
  2. import os
  3. import pygame
  4. from pygame.locals import *
  5.  
  6. pygame.init()
  7.  
  8.  
  9. length = 50*5
  10. width = 50*5
  11.  
  12. size = length, width
  13.  
  14. screen = pygame.display.set_mode(size)
  15.  
  16. # ((x, y), (12, 12))
  17. # (screen, (255, 0, 255), rect)
  18.  
  19. def rgb(de, rg):
  20. new = []
  21. for i in rg:
  22. if i < de:
  23. new.append(0)
  24. else:
  25. new.append((i-de))
  26. return tuple(new)
  27.  
  28. class Screen:
  29.  
  30. def __init__(self, screen):
  31. self.map = Map()
  32. self.screen = screen
  33. self.mapping = [i for i in range(self.map.mr)]
  34.  
  35. def render(self):
  36. r = self.map.get_mapping()
  37. for y in self.mapping:
  38. for x in self.mapping:
  39. if r[y][x] == 0:
  40. pygame.draw.rect(self.screen, rgb(self.map.shading[y][x], (255, 255, 255)), ((x*50, y*50), (50, 50)))
  41. elif r[y][x] == 1:
  42. pygame.draw.rect(self.screen, rgb(self.map.shading[y][x], (255, 0, 255)), ((x*50, y*50), (50, 50)))
  43. elif r[y][x] == 2:
  44. pygame.draw.rect(self.screen, rgb(self.map.shading[y][x], (255, 255, 0)), ((x*50, y*50), (50, 50)))
  45. elif r[y][x] == 3:
  46. pygame.draw.rect(self.screen, rgb(self.map.shading[y][x], (0, 0, 0)), ((x*50, y*50), (50, 50)))
  47.  
  48. class Map:
  49.  
  50. def __init__(self):
  51.  
  52. self.matrix = [
  53. [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
  54. [1, 2, 1, 0, 0, 0, 0, 0, 0, 1],
  55. [1, 0, 1, 0, 1, 1, 1, 1, 0, 1],
  56. [1, 0, 1, 0, 0, 0, 0, 1, 0, 1],
  57. [1, 0, 1, 1, 1, 1, 0, 1, 0, 1],
  58. [1, 0, 1, 0, 0, 0, 0, 1, 0, 1],
  59. [1, 0, 1, 0, 1, 0, 0, 1, 0, 1],
  60. [1, 0, 1, 1, 1, 1, 1, 1, 0, 1],
  61. [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  62. [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
  63. ]
  64. self.shading = [
  65. [100, 70, 60, 70, 100],
  66. [70, 50, 35, 50, 70],
  67. [60, 35, 00, 35, 60],
  68. [70, 50, 35, 50, 70],
  69. [100, 70, 60, 70, 100]
  70. ]
  71. self.px = 1
  72. self.py = 1
  73. self.mx = 10
  74. self.my = 10
  75. self.mr = 5
  76. self.curx = 1
  77. self.cury = 1
  78. self.mapping = [i for i in range(self.mr)]
  79.  
  80. def get_range(self):
  81. xn = [i for i in range(0+(self.px-2), (self.mr-1)+(self.px-1))]
  82. yn = [i for i in range(0+(self.py-2), (self.mr-1)+(self.py-1))]
  83. return (yn, xn)
  84.  
  85. def get_mapping(self):
  86. mapping = [[0 for i in range(self.mr)] for k in range(self.mr)]
  87. m = self.mapping
  88. r = self.get_range()
  89. ry = r[0]
  90. rx = r[1]
  91. for y in m:
  92. for x in m:
  93. if ry[y] > (self.my - 1) or ry[y] < 0:
  94. mapping[y][x] = 3
  95. elif rx[x] > (self.mx - 1) or rx[x] < 0:
  96. mapping[y][x] = 3
  97. else:
  98. mapping[y][x] = self.matrix[ry[y]][rx[x]]
  99. return mapping
  100.  
  101. def update_p(self):
  102. self.matrix[self.cury][self.curx] = 0
  103. self.matrix[self.py][self.px] = 2
  104. self.curx = self.px
  105. self.cury = self.py
  106.  
  107. s = Screen(screen)
  108. clock = pygame.time.Clock()
  109. mz = 3
  110. m = 0
  111. while True:
  112. clock.tick(60)
  113. s.screen.fill((255, 255, 255))
  114. s.render()
  115. for event in pygame.event.get():
  116. if event.type == pygame.KEYDOWN:
  117. if m <= 0:
  118. if event.key == pygame.K_UP:
  119. if s.map.py > 0:
  120. if s.map.matrix[s.map.py-1][s.map.px] != 1:
  121. s.map.py -= 1
  122. elif event.key == pygame.K_LEFT:
  123. if s.map.px > 0:
  124. if s.map.matrix[s.map.py][s.map.px-1] != 1:
  125. s.map.px -= 1
  126. elif event.key == pygame.K_RIGHT:
  127. if s.map.px < (s.map.mx-1):
  128. if s.map.matrix[s.map.py][s.map.px+1] != 1:
  129. s.map.px += 1
  130. elif event.key == pygame.K_DOWN:
  131. if s.map.py < (s.map.my-1):
  132. if s.map.matrix[s.map.py+1][s.map.px] != 1:
  133. s.map.py += 1
  134. s.map.update_p()
  135. m = 10
  136. elif event.type == pygame.QUIT:
  137. pygame.quit()
  138. os._exit(0)
  139. else:
  140. s.render()
  141. m -= 1
  142. for y in range(9):
  143. for x in range(9):
  144. if s.map.matrix[y][x] == 0:
  145. pygame.draw.rect(s.screen, (0, 0, 255), ((x*5, y*5), (5, 5)))
  146. elif s.map.matrix[y][x] == 2:
  147. pygame.draw.rect(s.screen, (255, 0, 0), ((x*5, y*5), (5, 5)))
  148. pygame.display.flip()
Add Comment
Please, Sign In to add comment