forked from maxabb/LOVE2D-shooter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
148 lines (124 loc) · 4.32 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
142
143
144
145
146
147
148
function love.load()
love.window.setTitle("Shooter")
love.window.setMode(600, 900., {vsync=false})
love.graphics.setDefaultFilter("nearest", "nearest")
-- controller
joysticks = love.joystick.getJoysticks()
joystick = joysticks[1]
playing = true
score = 0
player = {sprite = love.graphics.newImage("images/tank.png"), x=275, y=840, w=7, h=7, s=300, d=0.1}
bullets = {}
enemies = {}
velocity = 1000;
enemySpeed = 100
font = love.graphics.newFont("fonts/font.ttf", 30)
math.randomseed(os.time())
end
function read_input_left()
if joystick then
return love.keyboard.isDown('d') or joystick:getGamepadAxis("leftx") > 0.1 or joystick:isGamepadDown("dpright")
end
return love.keyboard.isDown('d')
end
function read_input_right()
if joystick then
return love.keyboard.isDown('a') or joystick:getGamepadAxis("leftx") < -0.1 or joystick:isGamepadDown("dpleft")
end
return love.keyboard.isDown('a')
end
function read_input_fire()
if joystick then
return love.keyboard.isDown('space') or joystick:getGamepadAxis("triggerright") > 0.1
end
return love.keyboard.isDown('space')
end
function love.keypressed(key)
if (key == 'escape') then
love.event.quit()
end
end
enemyDelay = 2
count = enemyDelay
function love.update(dt)
if playing then
if read_input_left() and player.x < 550 then
player.x = player.x + player.s * dt
end
if read_input_right() and player.x > 0 then
player.x = player.x - player.s * dt
end
if read_input_fire() and player.d < 0 then
table.insert(bullets, create_bullet())
player.d = 0.1
score = score - 1
else
player.d = player.d - (1 * dt)
end
for index, bullet in pairs(bullets) do
bullet.y = bullet.y - velocity * dt;
if (bullet.y < 0) then
bullets[index] = nil;
end
end
for index, enemy in pairs(enemies) do
enemy.y = enemy.y + (enemySpeed * dt);
if enemy.y > 900 then
enemies[index] = nil;
playing = false
end
if (enemy.health < 1) then
enemies[index] = nil;
score = score + 2
if (enemyDelay > 5) then
enemyDelay = enemyDelay - 0.1
end
end
for index, bullet in pairs(bullets) do
if bullet.x > enemy.x and bullet.x < enemy.x + 70 and bullet.y < enemy.y + 100 and bullet.y > enemy.y - 56 then
enemy.health = enemy.health - 1
bullets[index] = nil;
end
end
end
if count < 1 then
table.insert(enemies, create_enemy())
count = enemyDelay
else
count = count - (1 * dt)
end
end
end
function love.draw()
if playing then
-- player
love.graphics.setColor(1, 1, 1)
love.graphics.draw(player.sprite, math.floor(player.x), math.floor(player.y), 0, player.w, player.h)
-- score
love.graphics.printf(score, font, 0, 20, 600, 'center')
--[[ fps (uncomment to show fps in left corner)
love.graphics.setColor(0,1,0)
love.graphics.printf("fps: " .. love.timer.getFPS(), 10, 10, 600, 'left')
-- ]]
-- bullets
love.graphics.setColor(1, 0, 0)
for index, bullet in pairs(bullets) do
love.graphics.circle('fill', math.floor(bullet.x), math.floor(bullet.y), bullet.r)
end
-- enemies
for index, enemy in pairs(enemies) do
love.graphics.draw(enemy.sprite, math.floor(enemy.x), math.floor(enemy.y), 0, enemy.w, enemy.h)
end
else
love.graphics.setColor(1, 1, 1)
love.graphics.printf("Final Score: " .. score, font, 0, 450, 600, 'center')
end
end
function create_bullet()
bullet = {x=player.x+25, y=player.y+15, r=3}
return bullet;
end
function create_enemy()
enemy = {sprite=love.graphics.newImage("images/enemy.png"), x=math.random(80, 520), y=10, w=7, h=7, health=1}
return enemy
end