-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.lua
129 lines (102 loc) · 2.79 KB
/
game.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
require "subclass/AnAL.lua"
require "plugins/goomba.lua"
require "player/mario.lua"
require "subclass/shapeData.lua"
require "subclass/sfx.lua"
require "levels/1.lua"
require "subclass/class.lua"
game = class:new()
left, right = {}, {}
function game:init()
self:newWorlds()
screenX = 0 --screen x position relative to the left edge
destroyList = {} --place items in this list to be destroyed
--load level
level = lvl1:new()
--sprites array (basically, so multiplayer can be easily implemented in the future)
player = {}
player[1] = mario:new("up","down","left","right") --create player 1
--player[2] = mario:new("w", "s", "a", "d")
end
function game:mousepressed(x, y, button)
end
function game:reloadLevel()
screenX=0;
self:newWorlds()
level = lvl1:new()
end
function game:newWorlds()
--create some variables
world = love.physics.newWorld(0,0, 15360,640)
world:setGravity(0, 700)
world:setCallbacks(add, persist, remove)
world:setMeter(64)
world:setMeter(64)
dieWorld = love.physics.newWorld(0,0,15360,800)
dieWorld:setGravity(0, 700)
end
function game:update(dt)
world:update(dt)
dieWorld:update(dt)
for i=1, #player do
player[i]:update(dt)
end
level:update(dt)
--handle screen position
if player[1]:getBody():getX() > (sWidth * .8) + screenX then
screenX = player[1]:getBody():getX() - sWidth * .8
elseif player[1]:getBody():getX() < (sWidth * .2) + screenX then
screenX = player[1]:getBody():getX() - sWidth * .2
end
if screenX < 0 then
screenX = 0
end
--clean destroy list
for i=1, #destroyList do
if destroyList[i].time < love.timer.getTime() + 100 then
destroyList[i].object:destroy()
destroyList[i] = nil
end
end
if love.keyboard.isDown('q') then --quit the game
os.exit()
end
end
function game:draw()
--draw mario
for i=1, #player do
player[i]:draw()
end
level:draw()
love.graphics.setColor(255,255,255)
for i=1, #player do
love.graphics.print("Player: "..i, 20, 32*i)
love.graphics.print("Lives: "..player[i]:getLives(), 100, 32*i)
love.graphics.print("Coins: "..player[i]:getCoins(), 200, 32*i)
end
end
function game:keypressed(key, unicode)
--local marioX, marioY = player[1]:getBody():getLinearVelocity()
for i=1, #player do
player[i]:keyPressed(key)
end
--change width
if key == "1" then
sWidth = sWidth - 32
love.graphics.setMode(sWidth, sHeight, false, false, 0)
file = love.filesystem.newFile("res")
file:open('w')
file:write(sWidth)
file:close()
elseif key == "2" then
sWidth = sWidth + 32
love.graphics.setMode(sWidth, sHeight, false, false, 0)
file = love.filesystem.newFile("res")
file:open('w')
file:write(sWidth)
file:close()
end
end
function getabs(number)
return (number < 0 and -number or number)
end