tommyroyall

Blerg

Nov 29th, 2012
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.78 KB | None | 0 0
  1.  -- Example game.
  2.   -- By: Sledger721.
  3.   game={};
  4.   new={};
  5.   const={};
  6.   const.WIDTH,const.HEIGHT=50,18;
  7.  
  8. function clear()
  9.     term.clear()
  10.     term.setCursorPos(1,1)
  11. end
  12.  
  13. function new.world() -- World/level constructor.
  14.     self={}
  15.     self.objects={}
  16.     obj_counter=0
  17.    
  18.     function self:add_object(x,y) -- Add a new object.
  19.         obj_counter=obj_counter+1
  20.             self.objects[obj_counter].x,self.objects[obj_counter].y=x,y
  21.             return obj_counter
  22.     end
  23.     function self:move_object(ref,newx,newy) -- Move an object.
  24.         self.objects[ref].x=newx
  25.         self.objects[ref].y=newy
  26.     end
  27.     function self:check_collision(x,y) -- Check for collisions.
  28.         for i=0,#self.objects do
  29.             if self.objects[i].x==x or self.objects[i].y==y then -- If it senses a collision.
  30.                 return false
  31.             else
  32.                 return true -- Otherwise.
  33.             end
  34.         end
  35.     end
  36.    
  37.     return self;
  38. end
  39.  
  40. function new.player() -- The constructor for a new player.
  41.     self={} -- Establish the player's table.
  42.       self.body={}
  43.         self.body.x=1; -- And the player's stats.
  44.         self.body.y=1;
  45.         self.body.color=colors.red;
  46.    
  47.     function self:move_down(amount) -- Move the player up said amount. Amount must be an integer. No return.
  48.         check=world:check_collision(player.body.x,player.body.y+amount)
  49.         if check then
  50.             for i=1,tonumber(amount) do
  51.                 if player.body.y<const.HEIGHT then
  52.                     player.body.y=player.body.y+amount
  53.                     world:move_object(player.body,player.x,player.y)
  54.                 end
  55.             end
  56.         end
  57.     end
  58.     function self:move_up(amount) -- Move the player down said amount. Amount must be an integer. No return.
  59.         check=world:check_collision(player.x,player.y-amount)
  60.         if check then
  61.             for i=1,tonumber(amount) do
  62.                 if player.y>1 then
  63.                     player.y=player.y-amount
  64.                     world:move_object(player.body,player.x,player.y)
  65.                 end
  66.             end
  67.         end
  68.     end
  69.     function self:move_right(amount) -- Move the player right said amount. Amount must be an integer. No return.
  70.         check=world:check_collision(player.body.x+amount,player.body.y)
  71.         if check the
  72.             for i=1,tonumber(amount) do
  73.                 if player.x<const.WIDTH then
  74.                     player.body.x=player.body.x+amount
  75.                     world:move_object(player.body,player.x,player.y)
  76.                 end
  77.             end
  78.         end
  79.     end
  80.     function self:move_left(amount) -- Move the player left said amount. Amount must be an integer. No return.
  81.         check=world:check_collision(player.body.x,player.body.y+amount)
  82.         if check then
  83.             for i=1,tonumber(amount) do
  84.                 if player.body.x>1 then
  85.                     player.body.x=player.body.x-amount
  86.                     world:move_object(player.body,player.x,player.y)
  87.                 end
  88.             end
  89.         end
  90.     end
  91.     function self:change_color(newcolor) -- Change the color of the player. Amount must be an integer, but will most likely be a variable from the colors api. No return.
  92.         self.body.color=newcolor
  93.     end
  94.  
  95.     return self
  96. end
  97.  
  98. function create_objects() -- Create all of the objects for initiation and bind them to physics.
  99.     world=new.world()
  100.     player=new.player()
  101.     player=world:add_object(player.x,player.y)
  102. end
  103.  
  104. function game_init() -- The beginning function for the game.
  105.  term.setTextColor(colors.red)
  106.  term.setBackgroundColor(colors.black)
  107.   print('Welcome to Legion!!!')
  108.     sleep(2);
  109.   print('Beginning game.')
  110.     sleep(1);
  111.         game_state=1;
  112.         player=new.player()
  113.             game_loop() -- Call the main loop.
  114. end
  115.  
  116. function game_loop() -- The main loop for the game.
  117.     while game_state~=0 do
  118.  
  119.         -- Graphics loop.
  120.     GraphicsAPI.drawRect({1,1},{50,18},colors.black,true)
  121.     GraphicsAPI.drawPoint(player.body.x,player.body.y,player.body.color)
  122.     GraphicsAPI.update()
  123.  
  124.         -- Input loop.
  125.     ev,char=os.pullEvent('char')
  126.         if ev=='char' then
  127.             if char=='w' then
  128.                 player:move_up(1)
  129.             elseif char=='a' then
  130.                 player:move_left(1)
  131.             elseif char=='s' then
  132.                 player:move_down(1)
  133.             elseif char=='d' then
  134.                 player:move_right(1)
  135.             end
  136.         end
  137.  
  138.     end
  139. end
  140.  
  141. clear()
  142. game_init()
Advertisement
Add Comment
Please, Sign In to add comment