-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.lua
More file actions
136 lines (112 loc) · 3.14 KB
/
main.lua
File metadata and controls
136 lines (112 loc) · 3.14 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
speed = 150
bottomY=420
playerFloorY=370
jumpAccel = 16
initGravity = 200
counter = 0
obstacles = {}
lastTimeObstacle = 1000
gravity = 200
jumpSND = nil
hitSND = nil
pointSND = nil
player = { x = 100, y = playerFloorY,jumping = false, accel = jumpAccel, img = nil }
font = nil
isGameOver = false
function love.load()
player.img = love.graphics.newImage("dude.png")
love.graphics.setBackgroundColor(122,195,255)
jumpSND = love.audio.newSource("Jump2.wav")
hitSND = love.audio.newSource("hit.wav")
pointSND = love.audio.newSource("point.wav")
font = love.graphics.newFont(35)
end
function love.update(dt)
-- exit game
if love.keyboard.isDown('escape') then
love.event.push('quit')
end
-- main loop
if isGameOver == false then
-- input
if love.keyboard.isDown('a') and player.jumping == false then
love.audio.play(jumpSND)
player.jumping = true
end
--player is jumping
if player.jumping and player.accel > 0 then
player.y = player.y - player.accel
player.accel = player.accel - 1
end
--Gravity
if player.y < playerFloorY then
player.y = player.y + gravity*dt;
gravity = gravity + 10;
end
if player.y > playerFloorY then
player.y = playerFloorY
end
if player.y == playerFloorY then
player.jumping = false
player.accel = jumpAccel
gravity = initGravity
end
--generates obstacles
lastTimeObstacle = lastTimeObstacle - 10
if lastTimeObstacle <= 0 then
lastTimeObstacle = love.math.random(200,700)
newObstacle= { x = 640, y = 370, width = 25, height = 50, counted = false}
table.insert(obstacles, newObstacle)
end
--moves obstacles
for i, obstacle in ipairs(obstacles) do
obstacle.x = obstacle.x - 10
--obstacle off screen gets removed
if obstacle.x < 0 then
table.remove(obstacle, i)
end
--obstacle past player counts 1 point
if obstacle.counted == false and obstacle.x < player.x then
obstacle.counted = true
counter = counter + 1
love.audio.play(pointSND)
end
end
--checks collisions with player
for i, obstacle in ipairs(obstacles) do
if CheckCollision(player.x,player.y,player.img:getWidth(),player.img:getHeight(),obstacle.x,obstacle.y,obstacle.width,obstacle.height) then
isGameOver = true
love.audio.play(hitSND)
end
end
end
end
function love.draw()
--draws floor
love.graphics.setColor(64,199,84)
love.graphics.rectangle('fill', 0, 420, 640, 60)
--draws sun
love.graphics.setColor(255,255,51)
love.graphics.circle("fill", 550, 70, 40, 100)
--draws player
love.graphics.draw(player.img, player.x, player.y)
--draws jump counter
love.graphics.setColor(94,117,113)
love.graphics.setFont(font)
love.graphics.print(counter, 300, 200)
if (isGameOver) then
love.graphics.setColor(94,117,113)
love.graphics.print("GAME OVER", 220, 100)
end
-- draws obstacles
for i, obstacle in ipairs(obstacles) do
love.graphics.setColor(255,0,0)
love.graphics.rectangle('fill', obstacle.x, obstacle.y, obstacle.width, obstacle.height);
end
end
function CheckCollision(x1,y1,w1,h1, x2,y2,w2,h2)
return x1 < x2+w2 and
x2 < x1+w1 and
y1 < y2+h2 and
y2 < y1+h1
end