Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Example game.
- -- By: Sledger721.
- game={};
- new={};
- const={};
- const.WIDTH,const.HEIGHT=50,18;
- function clear()
- term.clear()
- term.setCursorPos(1,1)
- end
- function new.world() -- World/level constructor.
- self={}
- self.objects={}
- obj_counter=0
- function self:add_object(x,y) -- Add a new object.
- obj_counter=obj_counter+1
- self.objects[obj_counter].x,self.objects[obj_counter].y=x,y
- return obj_counter
- end
- function self:move_object(ref,newx,newy) -- Move an object.
- self.objects[ref].x=newx
- self.objects[ref].y=newy
- end
- function self:check_collision(x,y) -- Check for collisions.
- for i=0,#self.objects do
- if self.objects[i].x==x or self.objects[i].y==y then -- If it senses a collision.
- return false
- else
- return true -- Otherwise.
- end
- end
- end
- return self;
- end
- function new.player() -- The constructor for a new player.
- self={} -- Establish the player's table.
- self.body={}
- self.body.x=1; -- And the player's stats.
- self.body.y=1;
- self.body.color=colors.red;
- function self:move_down(amount) -- Move the player up said amount. Amount must be an integer. No return.
- check=world:check_collision(player.body.x,player.body.y+amount)
- if check then
- for i=1,tonumber(amount) do
- if player.body.y<const.HEIGHT then
- player.body.y=player.body.y+amount
- world:move_object(player.body,player.x,player.y)
- end
- end
- end
- end
- function self:move_up(amount) -- Move the player down said amount. Amount must be an integer. No return.
- check=world:check_collision(player.x,player.y-amount)
- if check then
- for i=1,tonumber(amount) do
- if player.y>1 then
- player.y=player.y-amount
- world:move_object(player.body,player.x,player.y)
- end
- end
- end
- end
- function self:move_right(amount) -- Move the player right said amount. Amount must be an integer. No return.
- check=world:check_collision(player.body.x+amount,player.body.y)
- if check the
- for i=1,tonumber(amount) do
- if player.x<const.WIDTH then
- player.body.x=player.body.x+amount
- world:move_object(player.body,player.x,player.y)
- end
- end
- end
- end
- function self:move_left(amount) -- Move the player left said amount. Amount must be an integer. No return.
- check=world:check_collision(player.body.x,player.body.y+amount)
- if check then
- for i=1,tonumber(amount) do
- if player.body.x>1 then
- player.body.x=player.body.x-amount
- world:move_object(player.body,player.x,player.y)
- end
- end
- end
- end
- 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.
- self.body.color=newcolor
- end
- return self
- end
- function create_objects() -- Create all of the objects for initiation and bind them to physics.
- world=new.world()
- player=new.player()
- player=world:add_object(player.x,player.y)
- end
- function game_init() -- The beginning function for the game.
- term.setTextColor(colors.red)
- term.setBackgroundColor(colors.black)
- print('Welcome to Legion!!!')
- sleep(2);
- print('Beginning game.')
- sleep(1);
- game_state=1;
- player=new.player()
- game_loop() -- Call the main loop.
- end
- function game_loop() -- The main loop for the game.
- while game_state~=0 do
- -- Graphics loop.
- GraphicsAPI.drawRect({1,1},{50,18},colors.black,true)
- GraphicsAPI.drawPoint(player.body.x,player.body.y,player.body.color)
- GraphicsAPI.update()
- -- Input loop.
- ev,char=os.pullEvent('char')
- if ev=='char' then
- if char=='w' then
- player:move_up(1)
- elseif char=='a' then
- player:move_left(1)
- elseif char=='s' then
- player:move_down(1)
- elseif char=='d' then
- player:move_right(1)
- end
- end
- end
- end
- clear()
- game_init()
Advertisement
Add Comment
Please, Sign In to add comment