添加贪吃蛇游戏草稿版

This commit is contained in:
Kirigaya Kazuto 2019-08-11 02:21:53 +08:00
parent a54b04dce9
commit ec0a6f9f6a
2 changed files with 149 additions and 1 deletions

View File

@ -96,7 +96,7 @@ function async(fn)
end
-- Load & run game
local gameMain = loadfile("code/game.lua")
local gameMain = loadfile("code/snake.lua")
xpcall(gameMain, function(err)
print("LuaMain Exception: ", err)
print(debug.traceback())

148
code/snake.lua Normal file
View File

@ -0,0 +1,148 @@
local wnd = Window("Snake", 1024, 768)
local rnd = Renderer(wnd)
local font = Font("asserts/msyh.ttf", 18)
local musicPlayer = MusicPlayer()
require("code/helper")
math.randomseed(os.time())
function GameBox()
local box = {}
box.w = 20
box.h = 20
box.size = 25
box.dots = Point(10, 10)
box.update = function()
end
box.draw = function()
local c = rnd:getColor()
rnd:setColor(RGBA(127, 127, 127, 0))
for i=0, box.w-1 do
for j=0, box.h-1 do
rnd:fillRect(Rect(i * box.size, j * box.size, box.size, box.size))
end
end
if box.dots then
rnd:setColor(RGBA(255, 0, 0, 0))
rnd:fillRect(Rect(box.dots.x * box.size, box.dots.y * box.size, box.size, box.size))
end
rnd:setColor(c)
end
return box
end
function Snake(box)
local s = {}
s.x = 5
s.y = 0
s.face = "right"
s.dead = false
s.body = {}
table.insert(s.body, Point(1,0))
table.insert(s.body, Point(2,0))
table.insert(s.body, Point(3,0))
table.insert(s.body, Point(4,0))
table.insert(s.body, Point(5,0))
s.queue = {}
s.box = box
s.update = function()
if not s.dead then
if s.queue[1] then
local key = table.remove(s.queue, 1)
if key == string.byte('w') and s.face ~= "down" then
s.face = "up"
elseif key == string.byte('s') and s.face ~= "up" then
s.face = "down"
elseif key == string.byte('a') and s.face ~= "right" then
s.face = "left"
elseif key == string.byte('d') and s.face ~= "left" then
s.face = "right"
end
end
local case = {
up = function()
s.y = s.y - 1
if s.y < 0 then
s.dead = true
end
end,
down = function()
s.y = s.y + 1
if s.y >= s.box.h then
s.dead = true
end
end,
left = function()
s.x = s.x - 1
if s.x < 0 then
s.dead = true
end
end,
right = function()
s.x = s.x + 1
if s.x >= s.box.w then
s.dead = true
end
end
}
case[s.face]()
if not s.dead then
if box.dots then
if box.dots.x == s.x and box.dots.y == s.y then
box.dots = Point(math.random(box.w)-1, math.random(box.h)-1)
else
table.remove(s.body, 1)
end
end
-- check if collapse
for _, p in ipairs(s.body) do
if p.x == s.x and p.y == s.y then
s.dead = true
break
end
end
if not s.dead then
table.insert(s.body, Point(s.x, s.y))
end
end
end
end
s.draw = function()
if not s.dead then
local c = rnd:getColor()
rnd:setColor(RGBA(0, 0, 255, 0))
for _, p in ipairs(s.body) do
rnd:fillRect(Rect(p.x * s.box.size, p.y * s.box.size, s.box.size, s.box.size))
end
rnd:setColor(RGBA(255, 255, 0, 0))
rnd:fillRect(Rect(s.x * s.box.size, s.y * s.box.size, s.box.size, s.box.size))
rnd:setColor(c)
end
end
return s
end
local world = GameBox()
local player = Snake(world)
wnd:on('keydown', function(key)
table.insert(player.queue, key)
end)
SetInterval(function()
world:update()
player:update()
rnd:clear()
world:draw()
player:draw()
rnd:update()
end, 200)
wnd:show()