forked from games50/assignment1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bird.lua
60 lines (48 loc) · 1.7 KB
/
Bird.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
--[[
Bird Class
Author: Colton Ogden
The Bird is what we control in the game via clicking or the space bar; whenever we press either,
the bird will flap and go up a little bit, where it will then be affected by gravity. If the bird hits
the ground or a pipe, the game is over.
]]
Bird = Class{}
-- I am so bad at the game I needed to make it easier to properly test
local GRAVITY = 10 --20
function Bird:init()
self.image = love.graphics.newImage('bird.png')
self.x = VIRTUAL_WIDTH / 2 - 8
self.y = VIRTUAL_HEIGHT / 2 - 8
self.width = self.image:getWidth()
self.height = self.image:getHeight()
self.dy = 0
end
--[[
AABB collision that expects a pipe, which will have an X and Y and reference
global pipe width and height values.
]]
function Bird:collides(pipe)
-- the 2's are left and top offsets
-- the 4's are right and bottom offsets
-- both offsets are used to shrink the bounding box to give the player
-- a little bit of leeway with the collision
if (self.x + 2) + (self.width - 4) >= pipe.x and self.x + 2 <= pipe.x + PIPE_WIDTH then
if (self.y + 2) + (self.height - 4) >= pipe.y and self.y + 2 <= pipe.y + PIPE_HEIGHT then
return true
end
end
return false
end
function Bird:update(dt)
self.dy = self.dy + GRAVITY * dt
-- burst of anti-gravity when space or left mouse are pressed
if love.keyboard.wasPressed('space') or love.mouse.wasPressed(1) then
-- making easier so I could properly test
self.dy = -2 --5
sounds['jump']:play()
end
self.y = self.y + self.dy
end
function Bird:render()
love.graphics.draw(self.image, self.x, self.y)
end