-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
136 lines (99 loc) · 3.42 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
push = require 'push'
Class = require 'class'
require 'StateMachine'
require 'states/BaseState'
require 'states/TitleScreenState'
require 'states/PlayState'
require 'states/ScoreState'
require 'states/PauseState'
require 'states/ResumeState'
require 'Bird'
require 'Pipe'
require 'PipePair'
require 'Medals'
WINDOW_WITDH = 1280
WINDOW_HEIGHT = 720
VIRTUAL_WIDTH = 512
VIRTUAL_HEIGHT = 288
local background = love.graphics.newImage('background.png')
local background_scroll = 0
local ground = love.graphics.newImage('ground.png')
local ground_scroll = 0
local BACKGROUND_SCROLL_SPEED = 30
local GROUND_SCROLL_SPEED = 60
local BACKGROUND_LOOPING_PONIT = 413
-- let s us pause the game when the bird collides with a pipe
local scrolling = true
function love.load()
math.randomseed(os.time())
love.graphics.setDefaultFilter('nearest', 'nearest')
love.window.setTitle('FutileFlappybird')
-- initialize retro fonts
smallFont = love.graphics.newFont('font.ttf', 8)
mediumFont = love.graphics.newFont('font.ttf', 14)
flappyFont = love.graphics.newFont('flappy.ttf', 28)
hugeFont = love.graphics.newFont('font.ttf', 56)
love.graphics.setFont(flappyFont)
-- initialize sound effects
sounds = {
['explosion'] = love.audio.newSource('explosion.wav', 'static'),
['hurt'] = love.audio.newSource('hurt.wav', 'static'),
['jump'] = love.audio.newSource('jump.wav', 'static'),
['score'] = love.audio.newSource('score.wav', 'static'),
['music'] = love.audio.newSource('marios_way.mp3', 'static')
}
-- set loop for background music
sounds['music']:setLooping(true)
sounds['music']:play()
push:setupScreen(VIRTUAL_WIDTH, VIRTUAL_HEIGHT, WINDOW_WITDH, WINDOW_HEIGHT, {
vsync = true,
fullscreen = false,
resizable = true
})
gStateMachine = StateMachine {
['title'] = function() return TitleScreenState() end,
['score'] = function() return ScoreState() end,
['play'] = function() return PlayState() end,
['pause'] = function() return PauseState() end,
['resume'] = function() return ResumeState() end
}
gStateMachine:change('title')
love.keyboard.keysPressed = {}
end
function love.resize(w, h)
push:resize(w, h)
end
function love.keypressed(key)
-- add the key the user pressed to the keysPressed table
love.keyboard.keysPressed[key] = true
if key == 'escape' then
love.event.quit()
end
end
-- check to see if a key was pressed (this function can be accessed from any file in the project)
function love.keyboard.wasPressed(key)
if love.keyboard.keysPressed[key] then
return true
else
return false
end
end
function love.update(dt)
-- make the background move forver
background_scroll = (background_scroll + BACKGROUND_SCROLL_SPEED * dt) % BACKGROUND_LOOPING_PONIT
ground_scroll = (ground_scroll + GROUND_SCROLL_SPEED * dt) % VIRTUAL_WIDTH
-- update game play according to the current state
gStateMachine:update(dt)
-- reset the keys pressed table every frame
love.keyboard.keysPressed = {}
end
function love.draw()
push:start()
-- draw backdround
love.graphics.draw(background, -background_scroll, 0)
-- draw ground
love.graphics.draw(ground, -ground_scroll, VIRTUAL_HEIGHT - 16)
-- draw text, bird and pipes depending on the state
gStateMachine:render()
push:finish()
end