Using Hump to pause the gamestate.
Posted: Sun Apr 13, 2014 5:16 pm
im trying to pause the game state when the user presses "p".
i have tried putting this in a new file:
and my game code is:
i want it top bring up a screen staying the game has paused while pausing the previous gamestate
i have tried putting this in a new file:
Code: Select all
Pause = Gamestate.new()
function Pause:enter(from)
self.from = from -- record previous state
end
function Pause:draw()
local W, H = love.graphics.getWidth(), love.graphics.getHeight()
-- draw previous screen
self.from:draw()
-- overlay with pause message
love.graphics.setColor(0,0,0, 100)
love.graphics.rectangle('fill', 0,0, W,H)
love.graphics.setColor(255,255,255)
love.graphics.printf('PAUSE', 0, H/2, W, 'center')
end
Code: Select all
Gamestate.level1 = Gamestate.new( )
local state = Gamestate.level1
local AdvTiledLoader = require("AdvTiledLoader.Loader") -- imports TileLoader module
require("camera") -- imports camera.lua
function state:init()
love.graphics.setBackgroundColor( 255, 255, 255,255 )
char1 = love.graphics.newImage('/images/Character1.png')
char2 = love.graphics.newImage('/images/Character2.png')
char3 = love.graphics.newImage('/images/Character3.png')
AdvTiledLoader.path = "maps/"
map = AdvTiledLoader.load("level1.tmx") -- loads map in folder
map:setDrawRange(0, 0, map.width * map.tileWidth, map.height * map.tileHeight) -- draws map at co-ordinates 0,0
camera:setBounds(0, 0, map.width * map.tileWidth - love.graphics.getWidth(), map.height * map.tileHeight - love.graphics.getHeight() )
light = love.graphics.newFont(20) -- defines text font
score = 0
world = {
gravity = 1536,
}
player = { -- defines our player characteristics
x = 256, -- x co-ordinate the player spawns in
y = 256, -- y co-ordinate player spawns in
x_vel = 0, -- character x velocity speed
y_vel = 0, -- character y velocity speed
jump_vel = -900,
speed = 200,
flySpeed = 800,
h = 50, --pixel height of character
w = 50, --pixel width of character
standing = false,
}
-- Character movement functions.
function player:jump() -- player jump function
if self.standing then
self.y_vel = self.jump_vel -- changes player y velocity value to jump velocity value
self.standing = false
end
end
function player:collide(event) -- player collide function
if event == "floor" then -- defines event as floor ( if character is in contact with ground)
self.y_vel = 0
self.standing = true
end
if event == "death" then -- death collision detection
self.y_vel = 0
player.x = 256 -- changes character co-ordinate to 256
player.y = 256
score = 0
end
end
function player:update(dt) -- update player position per tic
local halfX = self.w / 2
local halfY = self.h / 2
self.y_vel = self.y_vel + (world.gravity * dt) -- adds gravity to the player
self.y_vel = math.clamp(self.y_vel, -self.flySpeed, self.flySpeed) -- adds y speed of the player depending on velocity value, by allowing the player not to go above the set speed limit
local nextY = self.y + (self.y_vel*dt) -- calculate player position after frame has rendered ( Y position)
if self.y_vel < -100 then
if not (self:isColliding(map, self.x - halfX, nextY - halfY))
and not (self:isColliding(map, self.x + halfX - 1, nextY - halfY)) then
self.y = nextY -- allows player to move to next position
self.standing = false
else
self.y = nextY + map.tileHeight - ((nextY - halfY) % map.tileHeight)
self:collide("ceiling")
end
end
if self.y_vel > 0 then
if not (self:isColliding(map, self.x-halfX, nextY + halfY))
and not(self:isColliding(map, self.x + halfX - 1, nextY + halfY)) then
self.y = nextY
self.standing = false
else
self.y = nextY - ((nextY + halfY) % map.tileHeight)
self:collide("floor")
end
end
if player.y > 600 then
self.y = nextY - ((nextY + halfY) % map.tileHeight)
self:collide("death")
end
end
function player:isColliding(map, x, y) -- checks whether character is colliding with the tile map.
local tileX, tileY = math.floor(x / map.tileWidth), math.floor(y / map.tileHeight)
local layer = map.tl["ground"] -- colides with tile map layer solid only
local tile = layer.tileData(tileX, tileY) -- gets the tile data from the tmx file
print(tile)
return (tile~=nil)
end
end
function state:update(dt)
if dt > 0.1 then
dt = 0.1
end
player.x = player.x + (player.speed * dt)
score = score + (player.speed * 1)/1000
if love.keyboard.isDown("d") then -- when user presses keyboard button d , the player:right function will initialise
player.speed = 300
end
if love.keyboard.isDown("a") then --when user presses keyboard button d , the player:left function will initialise
player.speed = 100
end
if love.keyboard.isDown("w") and not(hasJumped) then
player:jump()
end
player:update(dt) -- runs the player update function per tic
camera:setPosition( player.x - (love.graphics.getWidth()/2), player.y - (love.graphics.getHeight()/2)) -- makes the camera follow the player position using the x and y co-ordinates
function love.keypressed(key)
if Gamestate.current() ~= menu and key == 'p' then
Gamestate.push(pause)
end
end
end
function love.keyreleased(key)
if (key == "a") or (key == "d") then
player.speed = 200 -- stops player from moving immediately when user has released the key.
end
end
function state:draw()
camera:set() -- initialises the camera
love.graphics.setColor(255, 255, 255, 255)
if love.keyboard.isDown("w") and not(hasJumped) then
character = love.graphics.draw(char3, player.x - player.w/2, player.y - player.h/2)
love.graphics.draw(char1, player.x - player.w/2, player.y - player.h/2, (player.w - 25), (player.h + 25)) -- player block
elseif love.keyboard.isDown("s") then
character = love.graphics.draw(char2, player.x - player.w/2, (player.y + 25) - player.h/2)
love.graphics.draw(char1, player.x - player.w/2, player.y - player.h/2, (player.w + 25), (player.h - 25)) -- player block
else
character = love.graphics.draw(char1, player.x - player.w/2, player.y - player.h/2)
love.graphics.draw(char1, player.x - player.w/2, player.y - player.h/2, player.w, player.h) -- player block
end
love.graphics.setColor( 255, 255, 255,255 )
map:draw() -- draws map onto the screen
camera:unset() -- run's camera function
love.graphics.setColor(0, 0, 0, 255)
love.graphics.printf("Score: "..tostring(string.format("%.f", score)), 780, 10,5, "right")
end