Using Hump to pause the gamestate.

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
goosingout
Prole
Posts: 7
Joined: Thu Feb 27, 2014 10:42 pm

Using Hump to pause the gamestate.

Post by goosingout »

im trying to pause the game state when the user presses "p".

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
and my game code is:

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
i want it top bring up a screen staying the game has paused while pausing the previous gamestate
Attachments
pause.lua
(475 Bytes) Downloaded 104 times
level1.lua
(6.05 KiB) Downloaded 93 times
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

Re: Using Hump to pause the gamestate.

Post by HugoBDesigner »

The easiest way I can think of it is by giving a variable to your pause mode. On the game loading:

Code: Select all

function love.load()
    --your loading stuff
    gamepaused = false
end
On the game keypress:

Code: Select all

function love.keypressed(key, unicode) --unicode only if you're using LÖVE 0.8.0
    --your keypress stuff
    gamepaused = not gamepaused
end
On game drawing:

Code: Select all

function love.draw()
    if gamepaused then
        --your game paused draw stuff
    end
    -- your game draw stuff, if you still want. Otherwise, add "return" before "end" above.
end
And on updates:

Code: Select all

function love.update(dt)
    if gamepaused then
        --your paused update stuff, if any...
        return
    end
    --your game update stuff
end
By adding "return" on the end of "if gamepaused", you make your game stop updating. The game will be normal again as soon as you unpause it (by pressing "q" again). If your game isn't directly loaded in "love" (perhaps in "game_load", "game_update", etc.), just make the same thing. It doesn't change :)
@HugoBDesigner - Twitter
HugoBDesigner - Blog
goosingout
Prole
Posts: 7
Joined: Thu Feb 27, 2014 10:42 pm

Re: Using Hump to pause the gamestate.

Post by goosingout »

It worked thank you :)
Post Reply

Who is online

Users browsing this forum: No registered users and 12 guests