Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local image
- function love.load()
- anim8 = require('anim8-master/anim8')
- cameraFile = require("hump-master/camera")
- cam = cameraFile()
- image = love.graphics.newImage('sprites/portal2.png')
- local portalgrid = anim8.newGrid(48, 48, 432, 48)
- animation = anim8.newAnimation(portalgrid('1-8', 1), .09)
- sprites = {}
- sprites.player = love.graphics.newImage('sprites/testingplayer.png')
- sprites.bullet = love.graphics.newImage('sprites/testingbullet.png')
- sprites.zombie = love.graphics.newImage('sprites/testingzombie.png')
- sprites.background = love.graphics.newImage('sprites/background.png')
- player = {}
- player.x = love.graphics.getWidth()/2
- player.y = love.graphics.getHeight()/2
- player.walk_speed = 100
- player.sprint_speed = player.walk_speed * 1.5
- player.speed = 180
- zombies = {}
- bullets = {}
- gameState = 1
- maxTime = 2
- timer = maxTime
- score = 0
- myFont = love.graphics.newFont(40)
- end
- function love.update(dt)
- animation:update(dt)
- -- cam:lookAt(player.x, player.y) -- Optional Camera Attach to the player
- if gameState == 2 then
- if love.keyboard.isDown("lshift")then
- player.speed = player.sprint_speed
- else
- player.speed = player.walk_speed
- end
- if love.keyboard.isDown("s") and player.y < love.graphics.getHeight() then
- player.y = player.y + player.speed * dt
- end
- if love.keyboard.isDown("w") and player.y > 0 then
- player.y = player.y - player.speed * dt
- end
- if love.keyboard.isDown("a") and player.x > 0 then
- player.x = player.x - player.speed * dt
- end
- if love.keyboard.isDown("d") and player.x < love.graphics.getWidth() then
- player.x = player.x + player.speed * dt
- end
- end
- for i,z in ipairs(zombies) do
- z.x = z.x + math.cos(zombie_player_angle(z)) * z.speed * dt
- z.y = z.y + math.sin(zombie_player_angle(z)) * z.speed * dt
- z.animation:update(dt)
- if distanceBetween(z.x, z.y, player.x, player.y) < 30 then
- for i,z in ipairs(zombies) do
- zombies[i] = nil
- gameState = 1
- player.x = love.graphics.getWidth()/2
- player.y = love.graphics.getHeight()/2
- end
- end
- end
- for i,b in ipairs(bullets) do
- b.x = b.x + math.cos(b.direction) * b.speed * dt
- b.y = b.y + math.sin(b.direction) * b.speed * dt
- end
- for i=#bullets, 1, -1 do
- local b = bullets[i]
- if b.x < 0 or b.y < 0 or b.x > love.graphics.getWidth() or b.y > love.graphics.getHeight() then
- table.remove(bullets, i)
- end
- end
- for i,z in ipairs(zombies) do
- for j, b in ipairs(bullets) do
- if distanceBetween(z.x, z.y, b.x, b.y) < 20 then
- z.dead = true
- b.dead = true
- score = score + 1
- end
- end
- end
- for i=#zombies,1,-1 do
- local z = zombies[i]
- if z.dead == true then
- table.remove(zombies, i)
- end
- end
- for i=#bullets, 1, -1 do
- local b = bullets[i]
- if b.dead == true then
- table.remove(bullets, i)
- end
- end
- if gameState == 2 then
- timer = timer - dt
- if timer <= 0 then
- spawnZombie()
- maxTime = maxTime * 0.95
- timer = maxTime
- end
- end
- end
- function love.draw()
- cam:attach()
- love.graphics.draw(sprites.background, 0, 0)
- if gameState == 1 then
- love.graphics.setFont(myFont)
- love.graphics.printf("Click anywhere to begin", 0, 50, love.graphics.getWidth(), "center")
- end
- love.graphics.printf("Score: " .. score, 0, love.graphics.getHeight() - 100, love.graphics.getWidth(), "center")
- love.graphics.draw(sprites.player, player.x, player.y, nil, nil, nil, sprites.player:getWidth()/2, sprites.player:getHeight()/2)
- for i,z in ipairs(zombies) do
- love.graphics.draw(sprites.zombie, z.x, z.y, nil, nil, nil, sprites.zombie:getWidth()/2, sprites.zombie:getHeight()/2)
- z.animation:draw(image,z.x-25, z.y-24)
- end
- for i,b in ipairs(bullets) do
- love.graphics.draw(sprites.bullet, b.x, b.y, nil, nil, nil, sprites.bullet:getWidth()/2, sprites.bullet:getHeight()/2)
- end
- cam:detach()
- end
- function player_mouse_angle()
- return math.atan2(player.y - love.mouse.getY(), player.x - love.mouse.getX()) + math.pi
- end
- function zombie_player_angle(enemy)
- return math.atan2(player.y - enemy.y,player.x - enemy.x)
- end
- function spawnZombie()
- zombie = {}
- zombie.x = 0
- zombie.y = 0
- zombie.speed = 140
- zombie.dead = false
- zombie.portalgrid = anim8.newGrid(48, 48, 432, 48)
- zombie.animation = anim8.newAnimation(zombie.portalgrid('1-8', 1), .09)
- local side = math.random(1, 4)
- if side == 1 then
- zombie.x = 200
- zombie.y = math.random(300, love.graphics.getHeight())
- elseif side == 2 then
- zombie.x = math.random(30, love.graphics.getWidth())
- zombie.y = 200
- elseif side == 3 then
- zombie.x = love.graphics.getWidth() - 200
- zombie.y = math.random(-30, love.graphics.getHeight())
- else
- zombie.x = math.random(-30, love.graphics.getWidth())
- zombie.y = love.graphics.getHeight() - 200
- end
- table.insert(zombies, zombie)
- end
- function spawnBullet()
- bullet = {}
- bullet.x = player.x
- bullet.y = player.y
- bullet.speed = 500
- bullet.direction = player_mouse_angle()
- bullet.dead = false
- table.insert(bullets, bullet)
- end
- function love.mousepressed(x, y, b, istouch)
- if b == 1 and gameState == 2 then
- spawnBullet()
- end
- if gameState == 1 then
- gameState = 2
- maxTime = 2
- time = maxTime
- score = 0
- end
- end
- function distanceBetween(x1, y1, x2, y2)
- return math.sqrt((y2 - y1)^2 + (x2 - x1)^2)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement