-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbird.lua
More file actions
55 lines (43 loc) · 1.41 KB
/
Copy pathbird.lua
File metadata and controls
55 lines (43 loc) · 1.41 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
--[[
Bird Class
Author: Colton Ogden
cogden@cs50.harvard.edu
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{}
local GRAVITY = 20
function Bird:init()
-- load bird image from disk and assign its width and height
self.image = love.graphics.newImage('bird.png')
self.width = self.image:getWidth()
self.height = self.image:getHeight()
-- position bird in the middle of the screen
self.x = VIRTUAL_WIDTH / 2 - (self.width / 2)
self.y = VIRTUAL_HEIGHT / 2 - (self.height / 2)
-- Y velocity; gravity
self.dy = 0
end
--AABB collision detection system
function Bird:collides(pipe)
if (self.x + 2) + (self.width - 4) >= 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)
-- apply gravity to velocity
self.dy = self.dy + GRAVITY * dt
if love.keyboard.wasPressed('space') then
self.dy = -5
sounds['jump']:play()
end
-- apply current velocity to Y position
self.y = self.y + self.dy
end
function Bird:render()
love.graphics.draw(self.image, self.x, self.y)
end