Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Here you go! #13

Merged
merged 7 commits into from Jul 9, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Expand Up @@ -2,4 +2,9 @@
*A snake clone built using the LOVE2d framework.*

## How to run
First install the love framework at [the love2d website](http://www.love2d.org/). Next, run love C:\path\to\snake_game_folder on Windows or love /path/to/snake_game on Linux OR download and run from [here.](http://dl.dropbox.com/u/2303550/lovely-snake.love)
First install the love framework at [the love2d website](http://www.love2d.org/). Next, run love C:\path\to\snake_game_folder on Windows or love /path/to/snake_game on Linux OR download and run from [here.](http://dl.dropbox.com/u/2303550/lovely-snake.love)

## How to play
* The goal is to eat as much food (red squares) as you can without dying.
* Use the arrow keys to control your snake's movement.
* If you hit the screen edge, a wall, or yourself, you will lose.
3 changes: 2 additions & 1 deletion classes/snake/snake.lua
Expand Up @@ -103,11 +103,12 @@ function Snake:check_collision(pos)
--table.insert(self.body, {x=self.body["head"]["x"], y=self.body["head"]["y"], dir=snake_direction})
--print("SNAKE BODY INSERTED AT: ", pos.x * 32, pos.y * 32)
table.insert(self.body, {x = pos.x * block_size, y = pos.y * block_size})
score = score + (100 * (1 - (current_difficulty*4)))-- score is dependent on difficulty level
end
end

function Snake:kill()
game_state = "main_menu"
game_state = "game_over"
self:init()
end

Expand Down
5 changes: 5 additions & 0 deletions engine.lua
Expand Up @@ -4,6 +4,11 @@ function update_options_file()
love.filesystem.write("options.cfg", options, all)
end

function update_high_score_file()
local file_string = "high_score = " .. score .. "\n"
love.filesystem.write("high_score.cfg", file_string, all)
end

function get_current_resolution()
for i = 1, #resolutions do
if resolutions[i].current == true then
Expand Down
40 changes: 36 additions & 4 deletions main.lua
Expand Up @@ -78,7 +78,12 @@ function love.load()
title = "LOVEly Snake",
"New Game",
"Options",
"Quit",
"Quit"
},
game_over_menu = {
title = "Game Over",
"Try Again",
"Main Menu"
},
options_menu = {
title = "Options",
Expand All @@ -104,6 +109,14 @@ function love.load()
update_options_file()
end

score = 0
if love.filesystem.exists("high_score.cfg") then
love.filesystem.load("high_score.cfg")()
else
high_score = 0
update_high_score_file()
end

hovering_over = nil
block_size = 32 -- Just realized this only works with 10,20,25,50,100 need to think more on collision and resolution
speed = difficulty.menu -- In milliseconds
Expand All @@ -127,6 +140,11 @@ function love.update(dt)
elseif game_state == "paused" then
return
elseif game_state == "game_over" then
if(score > high_score) then
update_high_score_file()
high_score = score
end
score = 0
return
end

Expand All @@ -139,6 +157,10 @@ function love.update(dt)
time_stamp = new_time_stamp
end

if score > high_score then
high_score = score
end

level:update_level(dt)
food:generate()
snake:move(dt)
Expand All @@ -153,6 +175,8 @@ function love.draw()

love.graphics.setColor({30, 40, 80, 255})
love.graphics.print("FPS: " .. love.timer.getFPS(), 10, 20)
love.graphics.print("HIGH SCORE: " .. high_score, 10, 50)
love.graphics.print("SCORE: " .. score, 10, 80)

love.graphics.setColor(colors.white)
-- Get the current x,y locations of the mouse cursor
Expand All @@ -170,8 +194,7 @@ function love.draw()
love.graphics.printf("PAUSED", 0, max_height / 2, max_width, 'center')

elseif game_state == "game_over" then
-- Game over
draw_main_menu(mouse_x, mouse_y)
draw_game_over_menu(mouse_x, mouse_y)

elseif game_state == "running" then
--[[ -- drawing in level for now
Expand Down Expand Up @@ -221,7 +244,16 @@ function love.mousereleased(x, y, button) -- needs updated to work with menus, t
print("clicked quit")
love.event.push('q')
end

elseif game_state == "game_over" then
if hovering_over == "Try Again" then
print("clicked Try Again")
game_state = "running"
snake:init() -- need to reset camera position...might need the idea of a spawn point on levels
end
if hovering_over == "Main Menu" then
print("clicked main menu")
game_state = "main_menu"
end
elseif game_state == "options_menu" then
for key, value in next, ui.options_menu.difficulty_buttons, nil do
if key ~= "width" and key ~= "height" then
Expand Down
4 changes: 4 additions & 0 deletions menus.lua
Expand Up @@ -7,6 +7,10 @@ function draw_options_menu(mouse_x, mouse_y)
display_menu_items(ui.options_menu, mouse_x, mouse_y)
end

function draw_game_over_menu(mouse_x, mouse_y)
display_menu_items(ui.game_over_menu, mouse_x, mouse_y)
end

function display_menu_items(t, mouse_x, mouse_y)
-- take in table and iterate through it displaying each item on a new line
for key, value in next, t, nil do
Expand Down
6 changes: 3 additions & 3 deletions resources/levels/snake_1.lua
Expand Up @@ -36,9 +36,9 @@ local tile_string = [[

local quad_data = {
{ '#', 0, 0 }, -- grass
{ ' ', 32, 0 },
{ '*', 0, 32 },
{ 'f', 32, 32 },
{ ' ', 32, 0 },-- trees
{ '*', 0, 32 },-- rocks
{ 'f', 32, 32 },-- flowers
}

level:new_level("/resources/images/tileset.png", tile_string, quad_data)