SHARE
TWEET

Untitled

a guest Jun 19th, 2019 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pygame
  2. import pygame.locals
  3.  
  4. def load_tile_table(filename, width, height):
  5.     image = pygame.image.load(filename).convert()
  6.     image_width, image_height = image.get_size()
  7.     tile_table = []
  8.     for tile_x in range(0, int(image_width/width)):
  9.         line = []
  10.         tile_table.append(line)
  11.         for tile_y in range(0, int(image_height/height)):
  12.             rect = (tile_x*width, tile_y*height, width, height)
  13.             line.append(image.subsurface(rect))
  14.     return tile_table
  15.  
  16.  
  17. if __name__=='__main__':
  18.     pygame.init()
  19.     screen = pygame.display.set_mode((500, 500))
  20.     screen.fill((255, 255, 255))
  21.     table = load_tile_table("tiles.png", int(16), int(16))
  22.     print (table)
  23.     screen.blit(table[0][0], (int(0), int(0)))
  24.     screen.blit(table[1][0], (int(24), int(0)))
  25.     pygame.display.flip()
  26.     while pygame.event.wait().type != pygame.locals.QUIT:
  27.         pass
  28.  
  29.  
  30. OP = (0, 1)
  31. NED = (0, -1)
  32. VENSTRE = (-1, 0)
  33. HOJRE = (1, 0)
  34.  
  35. robot = {
  36.     OP: 'A',
  37.     NED: 'V',
  38.     VENSTRE: '<',
  39.     HOJRE: '>',
  40. }
  41.  
  42. MUR = 'm'
  43. TOM = '.'
  44. ROBOT = 'r'
  45. TARGET = 't'
  46.  
  47. def _v(retning):
  48.     dx, dy = retning
  49.     return -dy, dx
  50.  
  51. def _h(retning):
  52.     dx, dy = retning
  53.     return dy, -dx
  54.  
  55.  
  56. def plus(xy, dxy):
  57.     x, y = xy
  58.     dx, dy = dxy
  59.     return (x+dx, y+dy)
  60.  
  61. class ConsoleRenderer(object):
  62.     def __init__(self):
  63.         self.blocks = dict()
  64.         self.x_size = 10
  65.         self.y_size = 10
  66.  
  67.     def set_block(self, position, blok):
  68.         self.blocks[position] = blok
  69.  
  70.     def vis(self):
  71.         lines = list()
  72.         for y in range(self.y_size):
  73.             lines.append("".join(self.blocks.get((x, y), TOM) for x in range(self.x_size)))
  74.         print("\r".join(reversed(lines)))
  75.  
  76.     def vis_kig(self, position, til_position):
  77.         print("Kigger på {0}".format(til_position))
  78.  
  79.     def vis_flyt(self, position):
  80.         print("Flytter til {0}".format(position))
  81.  
  82.     def vis_drej(self, position, retning, ):
  83.         pass
  84.  
  85.  
  86. class Level(object):
  87.     def __init__(self):
  88.         self.x_size = 10
  89.         self.y_size = 10
  90.         self.blocks = dict()
  91.         self.spawn = (0, 0)
  92.         self.target = (9, 9)
  93.  
  94.     def __getitem__(self, position):
  95.         x, y = position
  96.         if x < 0 or y < 0 or x >= self.x_size or y >= self.y_size:
  97.             return MUR
  98.         return self.blocks.get(position, TOM)
  99.  
  100.     def __setitem__(self, position, blok):
  101.         self.blocks[position] = blok
  102.  
  103.  
  104. class Robot(object):
  105.     def __init__(self, level, renderer):
  106.         self.level = level
  107.         self.retning = OP
  108.         self.position = level.spawn
  109.         self.renderer = renderer
  110.         self.scan()
  111.  
  112.     def scan(self):
  113.         for y in range(self.level.y_size):
  114.             for x in range(self.level.x_size):
  115.                 self.renderer.set_block((x, y), self.kig((x,y)))
  116.         self.renderer.vis()
  117.  
  118.     def flyt(self, til_position=None):
  119.         til_position = til_position or plus(self.position, self.retning)
  120.         if self.kig(til_position, vis=False) == MUR:
  121.             self.renderer.vis_bump(self.position, til_position)
  122.             return False
  123.         else:
  124.             fra_position = self.position
  125.             self.position = til_position
  126.             self.renderer.vis_flyt(fra_position, til_position)
  127.             return True
  128.  
  129.     def kig_frem(self):
  130.         print("kig_frem")
  131.         return self.kig(plus(self.position, self.retning))
  132.  
  133.     def kig_venstre(self):
  134.         print("kig_venstre")
  135.         return self.kig(plus(self.position, _v(self.retning)))
  136.  
  137.     def kig_hojre(self):
  138.         print("kig_hojre")
  139.         return self.kig(plus(self.position, _h(self.retning)))
  140.  
  141.     def drej_venstre(self):
  142.         print("drej_venstre")
  143.         ny_retning = _v(self.retning)
  144.         self.renderer.vis_drej(self.position, self.retning, ny_retning)
  145.         self.retning = ny_retning
  146.  
  147.     def drej_hojre(self):
  148.         print("drej_hojre")
  149.         ny_retning = _h(self.retning)
  150.         self.renderer.vis_drej(self.position, self.retning, ny_retning)
  151.         self.retning = ny_retning
  152.  
  153.     def kig(self, position, retning=(0,0), vis=True):
  154.         position = plus(position, retning)
  155.         self.renderer.vis_kig(self.position, position)
  156.         if position == self.level.target:
  157.             return TARGET
  158.         elif position == self.position:
  159.             return robot[self.retning]
  160.         else:
  161.             return self.level[position] or '!'
  162.  
  163. def f():
  164.  
  165.     level = Level()
  166.     level[1, 0] = MUR
  167.  
  168.     robot = Robot(level, ConsoleRenderer())
  169.     turn = 0
  170.     done = False
  171.     while turn < 100 and not done:
  172.         if robot.kig_venstre() == MUR:
  173.             if robot.kig_frem() != MUR:
  174.                 robot.flyt()
  175.             elif robot.kig_frem() == MUR:
  176.                 robot.drej_hojre()
  177.         elif robot.kig_venstre() != MUR:
  178.             robot.drej_venstre()
  179.         turn += 1
  180.         done = robot.position == robot.level.target
  181.     print(done)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top