-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
141 lines (104 loc) · 2.76 KB
/
main.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
130
131
132
133
134
135
136
137
138
139
140
141
-- Dear programmer:
-- When I wrote this code, only god and
-- I knew how it worked.
-- Now, only god knows it!
-- Therefore, if you are trying to optimize
-- this and you fail (most surely),
-- please increase this counter as a
-- warning for the next person:
-- total time wasted here = 1 hour
push = require "push"
Class = require 'Class'
-- imports
require 'alien' -- alien.lua
require 'player' -- player.lua
require 'boss' -- boss.lua
require 'wave' -- wave.lua
require 'alienattack' --alienattack.lua
-- store click pos
clickpos = {
x = 0,
y = 0
}
points = 0 -- score
-- just the window info
WINDOW_WIDTH = 1280
WINDOW_HEIGHT = 720
VIRTUAL_WIDTH = WINDOW_WIDTH
VIRTUAL_HEIGHT = WINDOW_HEIGHT
-- load bg
Background = love.graphics.newImage("sprites/Background.png")
-- init user class
user = player(700,WINDOW_HEIGHT-50,0)
-- init wave
wave1 = wave(1)
function love.load()
love.graphics.setDefaultFilter('nearest', 'nearest')
love.window.setTitle('Space Invaders') -- set window title
--set resolution
push:setupScreen(VIRTUAL_WIDTH, VIRTUAL_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT, {
fullscreen = false,
resizable = false,
vsync = false -- ew vsync never
})
end
function love.mousepressed(x, y, button, istouch)
if button == 1 then -- Versions prior to 0.10.0 use the MouseConstant 'l'
clickpos["x"] = x
clickpos["y"] = y
end
end
function love.keypressed(key)
if key == 'escape' then
love.event.quit()
elseif key == 'left' then
if user.x < 120 then
user.dx = 0
else
user.dx = user.dx -5
end
elseif key == 'right' then
if user.x > 1230 then
user.dx = 0
else
user.dx = user.dx + 5
end
elseif key == 'space' then
user:fire()
end
end
-- redef draw?
function love.draw()
push:apply('start')
love.graphics.setFont(love.graphics.newFont('Press-Start-2P.ttf', 30))
love.graphics.clear(0.1, 0.11, 0.14, 1)
love.graphics.draw(Background, 0, 0)
user:render()
wave1:render()
wave1:check()
displayFPS()
displayPoints()
push:apply('end')
end
-- FPS renderer
function displayFPS()
love.graphics.setFont(love.graphics.newFont('Press-Start-2P.ttf', 20)) -- set font
love.graphics.setDefaultFilter('linear', 'linear') -- the heck
-- decide color
if love.timer.getFPS() < 30 then
love.graphics.setColor(240, 252, 3, 1)
elseif love.timer.getFPS() < 60 and love.timer.getFPS() > 30 then
love.graphics.setColor(0, 1, 0, 1)
else
love.graphics.setColor(255, 0, 0, 1)
end
-- print it
love.graphics.print('FPS: ' .. tostring(love.timer.getFPS()), 20, WINDOW_HEIGHT - 50)
end
-- point renderer
function displayPoints()
love.graphics.setFont(love.graphics.newFont('Press-Start-2P.ttf', 15))
love.graphics.setDefaultFilter('linear', 'linear')
love.graphics.setColor(1,1,1, 1)
love.graphics.print(tostring(points), 20, 20)
end