-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
148 lines (119 loc) Β· 4.49 KB
/
main.lua
File metadata and controls
148 lines (119 loc) Β· 4.49 KB
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
-- virtual resolution handling library
-- https://github.com/Ulydev/push
push = require 'push'
-- classic OOP class library
-- https://github.com/vrld/hump/blob/master/class.lua
Class = require 'class'
require 'Bird'
require 'Pipe'
require 'PipePair'
require 'StateMachine'
require 'states/BaseState'
require 'states/TitleScreenState'
require 'states/PlayState'
require 'states/ScoreState'
require 'states/CountdownState'
-- physical screen dimensions
WINDOW_WIDTH = 1280
WINDOW_HEIGHT = 720
-- virtual resolution dimensions
VIRTUAL_WIDTH = 512
VIRTUAL_HEIGHT = 288
-- background image and starting scroll location (X axis)
local background1 = love.graphics.newImage('assets/background11.jpg')
local background2 = love.graphics.newImage('assets/background14.jpg')
local backgroundScroll1 = 0
local backgroundScroll2 = 0
-- point at which we should loop our background back to X 0
local BACKGROUND_LOOPING_POINT1 = 1
local BACKGROUND_LOOPING_POINT2 = 1000
-- ground image and starting scroll location (X axis)
local ground = love.graphics.newImage('assets/ground.png')
local groundScroll = 0
-- speed at which we should scroll our images
local BACKGROUND_SCROLL_SPEED = 50
local GROUND_SCROLL_SPEED = 100
function love.load()
love.graphics.setDefaultFilter('nearest', 'nearest')
love.window.setTitle('Fluffy Birds')
-- initilaize all the rquired fonts
smallFont = love.graphics.newFont('assets/font.ttf', 8)
mediumFont = love.graphics.newFont('assets/flappy.ttf', 14)
flappyFont = love.graphics.newFont('assets/flappy.ttf', 28)
hugeFont = love.graphics.newFont('assets/flappy.ttf', 56)
-- set current font to flappy font
love.graphics.setFont(flappyFont)
math.randomseed(os.time())
push:setupScreen(VIRTUAL_WIDTH, VIRTUAL_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT, {
vsync = true,
resizable = true,
fullscreen = false
})
-- initialize state machine with all state-returning functions
gStateMachine = StateMachine{
['title'] = function() return TitleScreenState() end,
['play'] = function() return PlayState() end,
['score'] = function() return ScoreState() end,
['countdown'] = function() return CountdownState() end,
}
gStateMachine:change('title')
sounds = {
['jump'] = love.audio.newSource('sounds/jump.wav', 'static'),
['explosion'] = love.audio.newSource('sounds/explosion.wav', 'static'),
['hurt'] = love.audio.newSource('sounds/hurt.wav', 'static'),
['score'] = love.audio.newSource('sounds/score.wav', 'static'),
-- https://www.youtube.com/watch?v=xw3C03Ba8Dk
['music'] = love.audio.newSource('sounds/music.mp3', 'static')
}
-- kick off music
sounds['music']:setLooping(true)
sounds['music']:play()
-- initialize input table
love.keyboard.keysPressed = {}
end
function love.resize(w, h)
push:resize(w, h)
end
function love.update(dt)
-- scroll background by preset speed * dt, looping back to 0 after the looping point
fps = love.timer.getFPS()
backgroundScroll1 = (backgroundScroll1 + BACKGROUND_SCROLL_SPEED * dt) % BACKGROUND_LOOPING_POINT1
backgroundScroll2 = -300 +((backgroundScroll2 + BACKGROUND_SCROLL_SPEED * dt) % BACKGROUND_LOOPING_POINT2)
-- scroll ground by preset speed * dt, looping back to 0 after the screen width passes
groundScroll = (groundScroll + GROUND_SCROLL_SPEED * dt) % VIRTUAL_WIDTH
gStateMachine:update(dt)
love.keyboard.keysPressed = {}
end
function love.draw()
push:start()
-- draw the background at the negative looping point
love.graphics.draw(background1, -backgroundScroll1, 0, 0, 0.3, 0.3)
love.graphics.setColor(255,255,255,100)
love.graphics.draw(background2, -backgroundScroll2, 0, 0, 0.3, 0.3)
love.graphics.setColor(255,255,255,255)
gStateMachine:render()
displayFPS()
-- draw the ground on top of the background, toward the bottom of the screen,
-- at its negative looping point
-- (height of ground = 16)
love.graphics.draw(ground, -groundScroll, VIRTUAL_HEIGHT - 16)
push:finish()
end
function love.keypressed(key)
love.keyboard.keysPressed[key] = true
if key == 'escape' then
love.event.quit()
end
end
function love.keyboard.wasPressed(key)
if love.keyboard.keysPressed[key] then
return true
else
return false
end
end
function displayFPS()
love.graphics.setFont(smallFont)
love.graphics.setColor(0,255,0,255)
love.graphics.print('FPS: ' .. tostring(love.timer.getFPS()), 10, 10)
end