-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
152 lines (114 loc) · 4.28 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
--[[
cookie clicker
author: abhinav lakhani
]]
local cookieTexture = love.graphics.newImage('graphics/cookie.png')
local cursorTexture = love.graphics.newImage('graphics/cursor.png')
local grandmaTexture = love.graphics.newImage('graphics/grandma.png')
local WINDOW_WIDTH = 1280
local WINDOW_HEIGHT = 720
local cookies = 0
local cps = 0
local cookiesLastSecond = 0
local cookiesThisSecond = 0
local passiveCookies = 0
local secondTimer = 0
local largefonts = love.graphics.newFont(32)
local smallfonts = love.graphics.newFont(16)
local left = WINDOW_WIDTH / 2 - cookieTexture:getWidth() / 2
local right = left + cookieTexture:getWidth()
local top = WINDOW_HEIGHT / 2 - cookieTexture:getHeight() / 2 - 64
local bottom = top + cookieTexture:getHeight()
local makeCookieBigger = false
-- cursors generate 1 cookie every 10 seconds
local cursors = 0
-- grandmas generate 1 cookie every 1 seconds
local grandmas = 0
function love.load()
love.window.setTitle('cookie clicker')
love.window.setMode(WINDOW_WIDTH, WINDOW_HEIGHT)
love.graphics.setFont(largefonts)
end
function love.update(dt)
local x, y = love.mouse.getPosition()
makeCookieBigger = x >= left and x <= right and y >= top and y <= bottom
updatePassiveCookies(dt)
secondTimer = secondTimer + dt
if secondTimer >= 1 then
secondTimer = secondTimer - 1
cps = (cookiesLastSecond + cookiesThisSecond) / 2
cookiesLastSecond = cookiesThisSecond
cookiesThisSecond = 0
end
end
function love.keypressed(key)
if key == 'escape'then
love.event.quit()
end
end
function love.mousepressed(x, y, button)
if button == 1 and makeCookieBigger then
cookies = cookies + 1
cookiesThisSecond = cookiesThisSecond + 1
end
if button == 1 and hoveringOverCursor() then
if cookies >= 10 then
passiveCookies = passiveCookies + 0.1
cookies = cookies - 10
cursors = cursors + 1
end
end
if button == 1 and hoveringOverGrandma() then
if cookies >= 100 then
passiveCookies = passiveCookies + 1
cookies = cookies - 100
grandmas = grandmas + 1
end
end
end
function love.draw()
love.graphics.printf('Cookies: ' .. tostring(math.floor(cookies)),
0, 16, WINDOW_WIDTH, 'center')
love.graphics.printf('CPS: ' .. tostring(cps),
0, 48, WINDOW_WIDTH, 'center')
love.graphics.draw(cookieTexture,
left + (makeCookieBigger and cookieTexture:getWidth() / 2 or 0),
top + (makeCookieBigger and cookieTexture:getHeight() / 2 or 0),
0,
makeCookieBigger and 1.2 or 1, makeCookieBigger and 1.2 or 1,
makeCookieBigger and cookieTexture:getWidth() / 2 or 0,
makeCookieBigger and cookieTexture:getHeight() / 2 or 0)
love.graphics.draw(cursorTexture, 64, WINDOW_HEIGHT - 120)
love.graphics.setFont(smallfonts)
love.graphics.print('Cursor', 50, WINDOW_HEIGHT - 90)
love.graphics.print('Cost: 10', 44, WINDOW_HEIGHT - 75)
love.graphics.setFont(largefonts)
love.graphics.print('Cursors: ' .. tostring(cursors))
love.graphics.draw(grandmaTexture, 148, WINDOW_HEIGHT - 132)
love.graphics.setFont(smallfonts)
love.graphics.print('Grandma', 132, WINDOW_HEIGHT - 90)
love.graphics.print('Cost: 100', 132, WINDOW_HEIGHT - 75)
love.graphics.setFont(largefonts)
love.graphics.print('Grandmas: ' .. tostring(grandmas), 0, 33)
end
function hoveringOverCursor()
local x, y = love.mouse.getPosition()
return x <= 64 + cursorTexture:getWidth() and x >= 64 and
y <= WINDOW_HEIGHT - 120 + cursorTexture:getHeight() and
y >= WINDOW_HEIGHT - 120
end
function hoveringOverGrandma()
local x, y = love.mouse.getPosition()
return x <= 148 + grandmaTexture:getWidth() and x >= 148 and
y <= WINDOW_HEIGHT - 132 + grandmaTexture:getHeight() and
y >= WINDOW_HEIGHT - 132
end
function updatePassiveCookies(dt)
local cookiesLastFrame = cookies
cookies = cookies + passiveCookies * dt
local cookiesAfterFrame = cookies
local newCookies = math.floor(cookiesAfterFrame) -
math.floor(cookiesLastFrame)
cookiesThisSecond = cookiesThisSecond + newCookies
end