Posted 04 June 2013 - 12:52 AM
So I gave up on Tetris (too dificult) and I just decided to make my own game, it's not complete yet but the basic stuff is there. The one thing I do want is that the "Enemy" will sometimes be more than 1. Say if you get 200 points and then a second enemy comes along. How would I do that? Here is the code….
l,h = term.getSize()
running = true
refreshRate = .5
gtID = os.startTimer(refreshRate)
local score = 00
local health = 100
player = {
xpos = math.floor(l/2);
ypos = h -2;
length = 1;
update = function(self, key)
if key == keys.left and self.xpos > 1 then
self.xpos = self.xpos - 1
elseif key == keys.right and self.xpos < l then
self.xpos = self.xpos + 1
elseif key == keys.up and self.ypos > 1 then
self.ypos = self.ypos - 1
elseif key == keys.down and self.ypos < h then
self.ypos = self.ypos + 1
end
end;
draw = function(self)
term.setCursorPos(self.xpos, self.ypos)
term.setBackgroundColour(colours.white)
term.write(string.rep(" ", self.length))
end;
}
food = {
xpos = math.random(1, l);
ypos = math.random(1, h);
amount = 1;
updateFood = function(self)
if self.xpos == player.xpos and self.ypos == player.ypos then
score = score + 20
self.amount = 0
self.xpos = math.random(1, l)
self.ypos = math.random(1, h)
end
end;
drawFood = function(self)
if self.amount == 0 then
self.amount = 1
elseif self.amount == 1 then
term.setCursorPos(self.xpos, self.ypos)
term.setBackgroundColor(colours.green)
term.write(" ")
end
end;
}
enemy = {
xpos = math.random(l);
ypos = math.random(h);
updateEnemy = function(self)
if self.xpos < player.xpos then
self.xpos = self.xpos + 1
elseif self.xpos > player.xpos then
self.xpos = self.xpos - 1
end
if self.ypos < player.ypos then
self.ypos = self.ypos + 1
elseif self.ypos > player.ypos then
self.ypos = self.ypos - 1
end
if self.xpos == player.xpos and self.ypos == player.ypos then
health = health - 20
sleep(.01)
end
end;
drawEnemy = function(self)
term.setCursorPos(self.xpos, self.ypos)
term.setBackgroundColour(colours.magenta)
term.write(" ")
end;
}
function gameDraw()
term.setBackgroundColour(colours.black)
term.clear()
term.setCursorPos(1, 1)
term.write("Health is "..health)
term.setCursorPos(1, 2)
term.write("Score is " ..score)
player:draw()
food:drawFood()
enemy:drawEnemy()
end
function gameUpdate()
local id, p1 = os.pullEvent()
food:updateFood()
if id == "key" then
if p1 == keys.q then running = false end
player:update(p1)
end
if id == "timer" and p1 == gtID then
enemy:updateEnemy()
gtID = os.startTimer(refreshRate)
end
end
function gameLoop()
while running do
gameUpdate()
gameDraw()
if health == 0 then
running = false
end
end
end
gameLoop()
term.setBackgroundColour(colours.black)
term.setCursorPos(math.floor(l/2) - #"Game Over"/2 + 1, math.floor(h/2))
term.write("Game Over")
sleep(3)
shell.run("clear")
sleep(0.01)