Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # THEMAGE RENDERING-ENGINE V1
- import os
- import pygame
- from pygame.locals import *
- pygame.init()
- length = 50*5
- width = 50*5
- size = length, width
- screen = pygame.display.set_mode(size)
- # ((x, y), (12, 12))
- # (screen, (255, 0, 255), rect)
- def rgb(de, rg):
- new = []
- for i in rg:
- if i < de:
- new.append(0)
- else:
- new.append((i-de))
- return tuple(new)
- class Screen:
- def __init__(self, screen):
- self.map = Map()
- self.screen = screen
- self.mapping = [i for i in range(self.map.mr)]
- def render(self):
- r = self.map.get_mapping()
- for y in self.mapping:
- for x in self.mapping:
- if r[y][x] == 0:
- pygame.draw.rect(self.screen, rgb(self.map.shading[y][x], (255, 255, 255)), ((x*50, y*50), (50, 50)))
- elif r[y][x] == 1:
- pygame.draw.rect(self.screen, rgb(self.map.shading[y][x], (255, 0, 255)), ((x*50, y*50), (50, 50)))
- elif r[y][x] == 2:
- pygame.draw.rect(self.screen, rgb(self.map.shading[y][x], (255, 255, 0)), ((x*50, y*50), (50, 50)))
- elif r[y][x] == 3:
- pygame.draw.rect(self.screen, rgb(self.map.shading[y][x], (0, 0, 0)), ((x*50, y*50), (50, 50)))
- class Map:
- def __init__(self):
- self.matrix = [
- [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
- [1, 2, 1, 0, 0, 0, 0, 0, 0, 1],
- [1, 0, 1, 0, 1, 1, 1, 1, 0, 1],
- [1, 0, 1, 0, 0, 0, 0, 1, 0, 1],
- [1, 0, 1, 1, 1, 1, 0, 1, 0, 1],
- [1, 0, 1, 0, 0, 0, 0, 1, 0, 1],
- [1, 0, 1, 0, 1, 0, 0, 1, 0, 1],
- [1, 0, 1, 1, 1, 1, 1, 1, 0, 1],
- [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
- [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
- ]
- self.shading = [
- [100, 70, 60, 70, 100],
- [70, 50, 35, 50, 70],
- [60, 35, 00, 35, 60],
- [70, 50, 35, 50, 70],
- [100, 70, 60, 70, 100]
- ]
- self.px = 1
- self.py = 1
- self.mx = 10
- self.my = 10
- self.mr = 5
- self.curx = 1
- self.cury = 1
- self.mapping = [i for i in range(self.mr)]
- def get_range(self):
- xn = [i for i in range(0+(self.px-2), (self.mr-1)+(self.px-1))]
- yn = [i for i in range(0+(self.py-2), (self.mr-1)+(self.py-1))]
- return (yn, xn)
- def get_mapping(self):
- mapping = [[0 for i in range(self.mr)] for k in range(self.mr)]
- m = self.mapping
- r = self.get_range()
- ry = r[0]
- rx = r[1]
- for y in m:
- for x in m:
- if ry[y] > (self.my - 1) or ry[y] < 0:
- mapping[y][x] = 3
- elif rx[x] > (self.mx - 1) or rx[x] < 0:
- mapping[y][x] = 3
- else:
- mapping[y][x] = self.matrix[ry[y]][rx[x]]
- return mapping
- def update_p(self):
- self.matrix[self.cury][self.curx] = 0
- self.matrix[self.py][self.px] = 2
- self.curx = self.px
- self.cury = self.py
- s = Screen(screen)
- clock = pygame.time.Clock()
- mz = 3
- m = 0
- while True:
- clock.tick(60)
- s.screen.fill((255, 255, 255))
- s.render()
- for event in pygame.event.get():
- if event.type == pygame.KEYDOWN:
- if m <= 0:
- if event.key == pygame.K_UP:
- if s.map.py > 0:
- if s.map.matrix[s.map.py-1][s.map.px] != 1:
- s.map.py -= 1
- elif event.key == pygame.K_LEFT:
- if s.map.px > 0:
- if s.map.matrix[s.map.py][s.map.px-1] != 1:
- s.map.px -= 1
- elif event.key == pygame.K_RIGHT:
- if s.map.px < (s.map.mx-1):
- if s.map.matrix[s.map.py][s.map.px+1] != 1:
- s.map.px += 1
- elif event.key == pygame.K_DOWN:
- if s.map.py < (s.map.my-1):
- if s.map.matrix[s.map.py+1][s.map.px] != 1:
- s.map.py += 1
- s.map.update_p()
- m = 10
- elif event.type == pygame.QUIT:
- pygame.quit()
- os._exit(0)
- else:
- s.render()
- m -= 1
- for y in range(9):
- for x in range(9):
- if s.map.matrix[y][x] == 0:
- pygame.draw.rect(s.screen, (0, 0, 255), ((x*5, y*5), (5, 5)))
- elif s.map.matrix[y][x] == 2:
- pygame.draw.rect(s.screen, (255, 0, 0), ((x*5, y*5), (5, 5)))
- pygame.display.flip()
Add Comment
Please, Sign In to add comment