-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
69 lines (60 loc) · 1.96 KB
/
main.lua
File metadata and controls
69 lines (60 loc) · 1.96 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
function love.load()
require('lib.class')
require('src.snake')
require('src.food')
require('src.keyboard')
require('src.colors')
require('src.utils')
gamestates = {PAUSE = 'PAUSE', RUNNING = 'RUNNING', GAME_OVER = 'GAME OVER'}
state = gamestates.RUNNING
width, height = love.graphics.getDimensions()
love.graphics.setBackgroundColor(GREY)
speed = Snake.body_width
dir_x, dir_y = 0, 0
delay = 0.2
timer = 0
score = 0
snake = Snake:new()
head = snake.body[1]
food = Food:new(Food.spawn())
end
function love.update(dt)
if state == gamestates.RUNNING then
timer = timer + dt
if timer >= delay then
local old_x = head.x
local old_y = head.y
snake:update_head()
snake:update_body(old_x, old_y)
if snake:is_colliding_with_self() then
state = gamestates.GAME_OVER
end
update_direction()
timer = 0
end
end
end
function love.draw()
love.graphics.setColor(WHITE)
snake:draw()
food:draw()
if grid_on then
draw_grid()
end
if state == gamestates.GAME_OVER then
love.graphics.setColor(BLACK)
local score_text_offset = -40
score_text = string.format('Score: %d', score)
text = love.graphics.newFont(40)
love.graphics.setFont(text)
score_x = (width / 2) - text:getWidth(score_text) / 2
score_y = (height / 2) - text:getHeight(score_text) / 2 + score_text_offset
love.graphics.print(score_text, score_x, score_y)
game_over_text = 'Press \'Space\' to Restart'
text = love.graphics.newFont(20)
love.graphics.setFont(text)
game_over_text_x = (width / 2) - text:getWidth(game_over_text) / 2
game_over_text_y = (height / 2) - text:getHeight(game_over_text) / 2
love.graphics.print(game_over_text, game_over_text_x, game_over_text_y)
end
end