Facebook
From Mikołaj, 2 Months ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 26
  1. class Vector2:
  2.     def __init__(self, X, Y):
  3.         self.x = X
  4.         self.y = Y
  5.  
  6.  
  7. class Overlay(Sprite):
  8.     def __init__(self, vector, KeepPiority=False):
  9.         self.keepPiority = KeepPiority
  10.         self.RelVector = vector
  11.         self.AbsVector = Vector2(self.RelVector.x, self.RelVector.y)
  12.  
  13.     def OnRemove(self):
  14.         if self.keepPiority:
  15.             if self.size == 0:
  16.                 return
  17.             self.CarrySize = self.size
  18.             self.size = 0
  19.         else:
  20.             self.IsVisible = False
  21.  
  22.     def OnAdd(self):
  23.         if self.keepPiority and self.IsVisible:
  24.             self.size = self.CarrySize
  25.         else:
  26.             game.add(self)
  27.  
  28.     def SetPosition(self, X, Y):
  29.         self.RelVector = Vector2(X, Y)
  30.         if X <= 100 and X >= -100 and Y <= 80 and Y >= -100:
  31.             if not self.IsVisible or self.size == 0:
  32.                 self.OnAdd()
  33.             self.position = Vector(X, Y)
  34.         else:
  35.             self.OnRemove()
  36.  
  37.     # internal
  38.     def CamMove(self, dist, rot):
  39.         multiply = 0.017453292519944
  40.         num = 90.0
  41.         num2 = dist * math.sin(multiply * (-rot + num))
  42.         num3 = dist * math.cos(multiply * (-rot + num))
  43.         self.SetPosition(self.RelVector.x + num2, self.RelVector.y + num3)
  44.  
  45.     def Move(self, dist, rot):
  46.         self.CamMove(dist, rot)
  47.         self.AbsVector = Vector2(self.RelVector.x, self.RelVector.y)
  48.  
  49.  
  50. class Engine:
  51.     def __init__(self):
  52.         self.Sprites = []
  53.         self.camera = Camera(self)
  54.  
  55.     def append(self, sprite):
  56.         if self.Sprites.count(sprite) == 0:
  57.             self.Sprites.append(sprite)
  58.  
  59.     def remove(self, sprite):
  60.         self.Sprites.remove(sprite)
  61.         game.remove(sprite)
  62.  
  63.     def InitSprites(self):
  64.         self.camera.Move(0, 0)
  65.  
  66.  
  67. class Camera:
  68.     def __init__(self, Engine):
  69.         self.engine = Engine
  70.         self.vector = Vector2(0, 0)
  71.  
  72.     def Move(self, dist, rot):
  73.         for obj in self.engine.Sprites:
  74.             obj.CamMove(dist, rot)
  75.         self.__move(dist, rot)
  76.  
  77.     def __move(self, dist, rot):
  78.         num = 90.0
  79.         multiply = 0.017453292519944
  80.         num2 = dist * math.sin(multiply * (-rot + num))
  81.         num3 = dist * math.cos(multiply * (-rot + num))
  82.         self.vector.x += num2
  83.         self.vector.y += num3
  84.  
  85.  
  86. ##################################################
  87. class HeartBar:
  88.         def __init__(self):
  89.                 self.hearts = []
  90.                 for i in range(0,10):
  91.                         self.hearts.append(Heart(i));
  92.                         game.add(self.hearts[i]);
  93.                 self.SetHealth(10);
  94.         def SetHealth(self,health):
  95.                 self.Health = health;
  96.                 TempHealth = health;
  97.                 for i in range(0,10):
  98.                         if(TempHealth>0):
  99.                                 self.hearts[i].image = 69;
  100.                                 TempHealth -=1;
  101.                         else:
  102.                                 self.hearts[i].image = 70;
  103.         def DealFallDamage(self,distance):
  104.                 damage = distance/50;
  105.                 print(distance);
  106.                 if damage >= 1:
  107.                         self.SetHealth(self.Health-int(damage));
  108. class Heart(Sprite):
  109.         def __init__(self,i):
  110.                 self.image = 70;
  111.                 self.position = Vector(-90+(i*10),95);
  112.                 self.size = 10;
  113.                 self.color = Color(255,2,0);
  114. ##################################################
  115. size = 20
  116.  
  117.  
  118. class Player(Overlay):
  119.     def __init__(self, pointer,heartBar):
  120.         self.heartBar = heartBar;
  121.         self.pointer = pointer
  122.         Overlay.__init__(self, Vector2(0, 0))
  123.         self.position = Vector(0, 0)
  124.         self.size = 10
  125.         self.speed = 0
  126.         self.FallDistance = 0
  127.         self.image = 0
  128.         self.Grounded = False
  129.  
  130.     def update(self):
  131.         if game.key('a'):
  132.             self.flip = True
  133.             manager.camera.Move(5, 0)
  134.             for block in blocksManager.ActiveBlocks:
  135.                 if self.IsCollisionBlock(block) and block.RelVector.y >= -6:
  136.                     manager.camera.Move(5, -180)
  137.                     break
  138.         elif game.key('d'):
  139.             self.flip = False
  140.             manager.camera.Move(5, -180)
  141.             for block in blocksManager.ActiveBlocks:
  142.                 if self.IsCollisionBlock(block) and block.RelVector.y >= -6:
  143.                     manager.camera.Move(5, 0)
  144.                     break
  145.         if game.key('space') and self.Grounded:
  146.             self.Grounded = False
  147.             self.speed = 6
  148.         ###
  149.         for block in blocksManager.ActiveBlocks:
  150.             if self.IsCollisionBlock(block):
  151.                 if block.RelVector.y <= -8:
  152.                    self.ground_player();
  153.                 elif block.RelVector.y > 0:
  154.                     if self.speed > 0:
  155.                         self.speed = -self.speed
  156.  
  157.         if self.speed != 0:
  158.             manager.camera.Move(self.speed, -90)
  159.             if self.speed<0:
  160.                 self.FallDistance -=self.speed
  161.         if self.speed > -6:
  162.             self.speed = self.speed - 1
  163.  
  164.     def IsVectorInRange(self, vector, range):
  165.         res = vector.y > range or vector.y < -range or vector.x > range or vector.x < -range
  166.         return not res
  167.    
  168.     def IsCollisionBlock(self, block):
  169.         num = False
  170.         if block.size != 0:
  171.             num = self.IsVectorInRange(block.RelVector, 14)
  172.         return num
  173.     def ground_player(self):
  174.         self.Grounded = True
  175.         self.heartBar.DealFallDamage(self.FallDistance)
  176.         self.FallDistance = 0
  177.         if self.speed < 0:
  178.                 self.speed = 0
  179.  
  180.  
  181.  
  182. ##########################################
  183. class Pointer(Overlay):
  184.     def __init__(self, manager):
  185.         Overlay.__init__(self, Vector2(0, 0))
  186.         self.manager = manager
  187.         self.size = 5
  188.         self.image = 56
  189.         self.LastFoliage = None
  190.  
  191.     def update(self):
  192.         if game.key("c"):
  193.             self.point.X = self.LastFoliage.Block.X
  194.             self.point.Y = self.LastFoliage.Block.Y + 19
  195.         elif game.key("left"):
  196.             self.Move(20, -180, )
  197.         elif game.key("right"):
  198.             self.Move(20, 0)
  199.         elif game.key("up"):
  200.             self.Move(20, 90)
  201.         elif game.key("down"):
  202.             self.Move(20, -90)
  203.         elif game.key("l"):
  204.             game.Log = True
  205.         elif game.key("m"):
  206.             for b in blocksManager.ActiveBlocks:
  207.                 if self.collide(b):
  208.                     blocksManager.RemoveTile(b)
  209.                     break
  210.         elif game.key("n"):
  211.             for b in blocksManager.ActiveBlocks:
  212.                 if self.collide(b):
  213.                     return
  214.             blocksManager.AddBlockTile(Block(self.RelVector.x/20, self.RelVector.y/20, 1), True)
  215.  
  216.  
  217. #########################
  218. class BlocksManager:
  219.     def __init__(self, manager):
  220.         self.manager = manager
  221.         self.ActiveBlocks = []
  222.  
  223.     def AddBlockTile(self, block, draw):
  224.         manager.append(block)
  225.         self.ActiveBlocks.append(block)
  226.         if draw:
  227.             block.Move(0, 0)
  228.  
  229.     def RemoveTile(self, block):
  230.         manager.remove(block)
  231.         self.ActiveBlocks.remove(block)
  232.  
  233.  
  234. #################
  235. class Block(Overlay):
  236.     def __init__(self, X, Y, ID):
  237.         Overlay.__init__(self, Vector2(X*size, Y*size), True)
  238.         self.image = 63
  239.         self.size = size
  240.         self.ID = ID
  241.         IDProcessor().ProcessID(self.ID, self)
  242.  
  243. #################
  244. class Noise:
  245.         def __init__(self,minHeight,maxHeight):
  246.                 self.minHeight = minHeight;
  247.                 self.maxHeight = maxHeight;
  248.                 self.current = 3;
  249.         def generateNext(self):
  250.                 num = random.randint(-1,1);
  251.                 self.current+=num;
  252.                 if self.current<self.minHeight:
  253.                         self.current = self.minHeight
  254.                 elif self.current>self.maxHeight:
  255.                         self.current = self.maxHeight
  256.                 return self.current
  257. #################
  258. class Generator:
  259.     def __init__(self, engine,noise,noise2):
  260.         self.engine = engine
  261.         self.noise = noise;
  262.         self.dirt_noise = noise2;
  263.         self.CanGenerateTree = 0
  264.  
  265.     def Generate(self, blocks):
  266.         self.GenerateTerrian(blocks)
  267.        
  268.  
  269.     def GenerateTerrian(self, blocks):
  270.         for i in range(-blocks, blocks):
  271.             num = self.noise.generateNext();
  272.             dirtSize = self.dirt_noise.generateNext();
  273.             for j in range(num,num-dirtSize,-1):
  274.               self.GenerateFillarOfDirt(j, i, num)
  275.             self.GenerateStoneCollumn(i,num-dirtSize);
  276.  
  277.     def GenerateFillarOfDirt(self, BlockY, BlockX, num):
  278.         if num  == BlockY:
  279.             self.engine.AddBlockTile(Block(BlockX,BlockY, 1), False)
  280.             if random.randint(0, 4) == 3 and num >= 3 and self.CanGenerateTree == 3:
  281.                 self.GenerateTree(BlockX, BlockY)
  282.             elif self.CanGenerateTree != 3:
  283.                 self.CanGenerateTree += 1
  284.         else:
  285.             self.engine.AddBlockTile(Block(BlockX, BlockY, 2), False)
  286.  
  287.     def GenerateTree(self, X, Y):
  288.         for i in range(Y + 1, Y + 4):
  289.             self.engine.AddBlockTile(Block(X,  i, 4), False)
  290.         self.engine.AddBlockTile(Block(X, (Y + 4), 5), False)
  291.         self.engine.AddBlockTile(Block((X - 1),(Y + 3), 5), False)
  292.         self.engine.AddBlockTile(Block((X + 1),  (Y + 3), 5), False)
  293.         self.CanGenerateTree = 0
  294.  
  295.     def GenerateStoneCollumn(self, x,startI):
  296.         for y in range(startI, -15, -1):
  297.             self.engine.AddBlockTile(Block(x,y, 3), False)
  298.  
  299.  
  300. #################
  301. class IDProcessor:
  302.     def Grass(self, block):
  303.         block.color = Color(100, 200, 50)
  304.  
  305.     def Dirt(self, block):
  306.         block.color = Color(200, 100, 50)
  307.  
  308.     def Wood(self, block):
  309.         block.color = Color(153, 51, 0)
  310.  
  311.     def Leaves(self, block):
  312.         block.color = Color(102, 153, 51)
  313.  
  314.     def Stone(self, block):
  315.         block.color = Color(156, 159, 161)
  316.  
  317.     def ProcessID(self, ID, block):
  318.         if ID == 1:
  319.             self.Grass(block)
  320.         elif ID == 2:
  321.             self.Dirt(block)
  322.         elif ID == 3:
  323.             self.Stone(block)
  324.         elif ID == 4:
  325.             self.Wood(block)
  326.         elif ID == 5:
  327.             self.Leaves(block)
  328.  
  329.  
  330. #################
  331. manager = Engine()
  332. blocksManager = BlocksManager(manager)
  333. pointer = Pointer(manager)
  334. grass_noise = Noise(1,5)
  335. dirt_noise = Noise(2,5)
  336. heartBar = HeartBar();
  337. player = Player(pointer,heartBar)
  338. generator = Generator(blocksManager,grass_noise,dirt_noise)
  339. generator.Generate(50)
  340. manager.camera.Move(100, -90)
  341. manager.append(pointer)
  342. game.add(player)
  343. manager.InitSprites()
  344. game.start()
  345.