forked from UAB-ACM-Hackathon-2/someGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
180 lines (147 loc) · 4.43 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
function love.load()
initializeWindow()
initializeObjects()
state = 'notstarted'
end
function love.update(dt)
if state ~= 'play' then
return
end
if player.gridY < 0 then
player.gridY = 0
elseif (player.gridY + 32) > screenHeight then
player.gridY = screenHeight - 32
end
if player.gridX < 0 then
player.gridX = 0
elseif (player.gridX + 32) > screenWidth then
player.gridX = screenWidth - 32
end
if enemy.gridY < 0 then
enemy.gridY = 0
elseif (enemy.gridY + 32) > screenHeight then
enemy.gridY = screenHeight - 32
end
if enemy.gridX < 0 then
enemy.gridX = 0
elseif (enemy.gridX + 32) > screenWidth then
enemy.gridX = screenWidth - 32
end
if (player.gridX + player.width > enemy.gridX) and (player.gridX < enemy.gridX + enemy.width) and
(player.gridY + player.height > enemy.gridY) and (player.gridY < enemy.gridY + enemy.height) then
state = 'lose'
initializeObjects()
end
if (player.gridX + player.width > goal.gridX) and (player.gridX < goal.gridX + goal.width) and
(player.gridY + player.height > goal.gridY) and (player.gridY < goal.gridY + goal.height) then
state = 'win'
initializeObjects()
end
distX = player.gridX - enemy.gridX
distY = player.gridY - enemy.gridY
distance = math.sqrt(distX * distX + distY * distY)
velocityX = distX/distance * enemy.speed
velocityY = distY/distance * enemy.speed
enemy.gridX = enemy.gridX + velocityX * dt
enemy.gridY = enemy.gridY + velocityY * dt
enemy.speed = enemy.speed + (enemy.speed * dt)/2
end
function love.draw()
love.graphics.setColor(0, 0, 255)
love.graphics.rectangle("fill", player.gridX, player.gridY, 32, 32)
love.graphics.setColor(255, 0, 0)
love.graphics.rectangle("fill", enemy.gridX, enemy.gridY, 32, 32)
love.graphics.setColor(0, 255, 0)
love.graphics.rectangle("fill", goal.gridX, goal.gridY, 32, 32)
if state == 'notstarted' then
stopScreen()
width = love.graphics.getWidth()
love.graphics.printf("Press ENTER to start playing!", 0, (screenHeight / 2) - 50, width, "center")
love.graphics.printf("Use the arrow keys for movement and avoid the other entity!", 0, (screenHeight / 2) - 30, width, "center")
love.graphics.printf("To pause the game, press P. To quit, press Q or ESC.", 0, (screenHeight / 2) - 10, width, "center")
end
if state == 'pause' then
stopScreen()
width = love.graphics.getWidth()
love.graphics.printf("PAUSED", 0, (screenHeight / 2) - 50, width, "center")
end
if state == 'win' then
stopScreen()
width = love.graphics.getWidth()
love.graphics.printf("YOU WIN", 0, (screenHeight / 2) - 50, width, "center")
love.graphics.printf("Press r to play again!", 0, (screenHeight / 2) - 30, width, "center")
end
if state == 'lose' then
stopScreen()
width = love.graphics.getWidth()
love.graphics.printf("GAME OVER", 0, (screenHeight / 2) - 50, width, "center")
love.graphics.printf("Press r to play again!", 0, (screenHeight / 2) - 30, width, "center")
end
end
function love.keypressed(key)
if key == 'up' then
player.gridY = player.gridY - 32
end
if key == 'down' then
player.gridY = player.gridY + 32
end
if key == 'left' then
player.gridX = player.gridX - 32
end
if key == 'right' then
player.gridX = player.gridX + 32
end
if key == 'return' then
if state == 'notstarted' then
state = 'play'
end
end
if key == 'q' or key == 'escape' then
love.event.quit()
end
if key == 'p' then
if state == 'play' then
state = 'pause'
elseif state == 'pause' then
state = 'play'
end
end
if key == 'r' then
if state == 'win' or state == 'lose' then
state = 'play'
end
end
end
function initializeWindow()
screenWidth = 800
screenHeight = 600
love.window.setTitle('The Bullied Box')
love.window.setMode(screenWidth, screenHeight)
end
function initializeObjects()
player = {
width = 32,
height = 32,
gridX = love.math.random(0, 800),
gridY = love.math.random(0, 600)
}
enemy = {
width = 32,
height = 32,
gridX = love.math.random(0, 800),
gridY = love.math.random(0, 600),
speed = 8
}
goal = {
width = 32,
height = 32,
gridX = love.math.random(0, 800),
gridY = love.math.random(0, 600)
}
end
function stopScreen()
love.graphics.setColor(0, 0, 0, 100)
love.graphics.rectangle('fill', 0, 0, screenWidth, screenHeight)
love.graphics.setColor(255,255,255)
love.graphics.setFont(love.graphics.newFont(18))
end