How do you make 2 controllable players and solid objects?
Posted: Mon Apr 06, 2015 3:37 am
Hello everyone! My name is Michael and I am beginning my journey into making games . I am very new to the whole coding process and execution so I'm a "newbie/simpleton" as of right now. Ok so enough with the intro here's why I am posting this to forums: I am trying to make a game, but i am having issues. So I am a person to make my life easier so i "stole" another persons character/player code, but i understand it for the most part so win win. I want to make a game where there's 2 controllable players. One by arrow keys as shown in the code below and another character controlled by WASD keys. This code is currently all i have by the way and it's in my "main.lua" file . So would i make a separate file for each character or would everything be in the main.lua folder? Go ahead and c&p the code and test it out and see if you can add a border to it because that's my next problem. I have made a game, well, i upgraded a game where theres 2 circles (one controllable by wasd and other is by arrow keys) and i made a border and everything and the circles can collide, well i used the same method where i made that border and tried to put it in this new project and it made my character blue and the rectangle i used as the border was not solid meaning my character was able to go through it....or is my character not solid? That's the kind of problem i'm having with being a newbie and not 100% understanding everything. Ill have the codes below;both my new project and my old one i made with the circles just to look off of something. Now thats all i need help with as of right now so thank you guys for reading and if you need me to describe my problems more let me know and i would gladly explain them more.....and for those who answer please try your best to provide an in depth answer <3 here are the codes:
New project that i'm currently making and need help on:
and thats that now on to the next one>>>>>>
Old project to look off of
and that's that now finally just so theres no extra confusion here is my configure file>>>
Configure file (conf.lua)
So again to make it clearly stated as possible: I am trying to make a 2 controllable players that are SOLID meaning they can't go through other solids and can't go through each other......also I am trying to make a border using the same method as shown in my second code i posted which is my old project........I tried using that method, but like i said it placed the shape with the exact coordinates and color, but my character was well above it and for some reason turned a blue highlighted color and was able to go THROUGH the object I created ;(.
Do I have to implement a physics system? If so how? I just want to make 2 controllable objects that collide with each other and can't go outside the window and then I'll get more complicated from there. Thank you for reading and I look forward to your responses!
New project that i'm currently making and need help on:
Code: Select all
function love.load()
sprites = love.graphics.newImage("Textures/sprites.png")
sprites:setFilter("nearest", "linear")
screenWidth = love.graphics.getWidth()
screenHeight = love.graphics.getHeight()
speed = 100
x = 50
y = 50
link = love.graphics.newQuad(36, 96, 23, 31, sprites:getWidth(), sprites:getHeight())
linkBack1 = love.graphics.newQuad(35, 96, 23, 31, sprites:getWidth(), sprites:getHeight())
linkBack0 = love.graphics.newQuad(3, 96, 23, 32, sprites:getWidth(), sprites:getHeight())
linkBack2 = love.graphics.newQuad(67, 97, 23, 31, sprites:getWidth(), sprites:getHeight())
linkRSide0 = love.graphics.newQuad(2, 65, 25, 31, sprites:getWidth(), sprites:getHeight())
linkRSide1 = love.graphics.newQuad(36, 64, 23, 33, sprites:getWidth(), sprites:getHeight())
linkRSide2 = love.graphics.newQuad(68, 65, 23, 31, sprites:getWidth(), sprites:getHeight())
linkLSide0 = love.graphics.newQuad(1, 33, 23, 31, sprites:getWidth(), sprites:getHeight())
linkLSide1 = love.graphics.newQuad(33, 32, 24, 32, sprites:getWidth(), sprites:getHeight())
linkLSide2 = love.graphics.newQuad(65, 33, 25, 31, sprites:getWidth(), sprites:getHeight())
linkFront0 = love.graphics.newQuad(1, 1, 27, 32, sprites:getWidth(), sprites:getHeight())
linkFront1 = love.graphics.newQuad(33, 0, 27, 32, sprites:getWidth(), sprites:getHeight())
linkFront2 = love.graphics.newQuad(66, 2, 70, 27, sprites:getWidth(), sprites:getHeight())
end
function love.update(dt)
if love.keyboard.isDown("right") then
x = x + (speed * dt)
link = linkRSide1
elseif love.keyboard.isDown("left") then
x = x - (speed * dt)
link = linkLSide1
end
if love.keyboard.isDown("down") then
y = y + (speed * dt)
link = linkFront1
elseif love.keyboard.isDown("up") then
link = linkBack1
y = y - (speed * dt)
end
end
function love.draw()
love.graphics.draw(sprites, link, x, y, 0, 1, 1, 0, 0)
-- love.graphics.draw(sprites, linkBack0, x, y, 0, 1, 1, 0, 0)
-- love.graphics.draw(sprites, linkBack1, x+50, y, 0, 1, 1, 0, 0)
-- love.graphics.draw(sprites, linkBack2, x+100, y, 0, 1, 1, 0, 0)
-- love.graphics.draw(sprites, linkRSide0, x+150, y, 0, 1, 1, 0, 0)
-- love.graphics.draw(sprites, linkRSide1, x+200, y, 0, 1, 1, 0, 0)
-- love.graphics.draw(sprites, linkRSide2, x+250, y, 0, 1, 1, 0, 0)
end
Old project to look off of
Code: Select all
function love.load()
love.physics.setMeter(64) --the height of a meter our worlds will be 64px
world = love.physics.newWorld(0, 0 * 64, true) --gravity is being set to 0 in the x direction and 200 in the y direction <3
objects = {} -- table to hold all our physical objects
-- Ground/Borders
objects.roof = {}
objects.roof.body = love.physics.newBody(world, 0, 0)
objects.roof.shape = love.physics.newRectangleShape(1600, 25) --make a rectangle with a width of 1600 and a height of 25
objects.roof.fixture = love.physics.newFixture(objects.roof.body, objects.roof.shape); --attach shape to body
objects.wallL = {}
objects.wallL.body = love.physics.newBody(world, 1, 0)
objects.wallL.shape = love.physics.newRectangleShape(2, 1200) --make a rectangle with a width of 2 and a height of 1200
objects.wallL.fixture = love.physics.newFixture(objects.wallL.body, objects.wallL.shape); --attach shape to body
objects.wallR = {}
objects.wallR.body = love.physics.newBody(world, 800, 0)
objects.wallR.shape = love.physics.newRectangleShape(2, 1200) --make a rectangle with a width of 1000 and a height of 25
objects.wallR.fixture = love.physics.newFixture(objects.wallR.body, objects.wallR.shape); --attach shape to body
objects.floor = {}
objects.floor.body = love.physics.newBody(world, 0, 600)
objects.floor.shape = love.physics.newRectangleShape(1600, 25) --make a rectangle with a width of 1000 and a height of 25
objects.floor.fixture = love.physics.newFixture(objects.floor.body, objects.floor.shape); --attach shape to body
--Ball
objects.ball = {}
objects.ball.body = love.physics.newBody(world, 950/2, 600/2, "dynamic") --place the body in the center of the world and make it dynamic, so it can move around
objects.ball.shape = love.physics.newCircleShape(20) --the ball's shape has a radius of 40
objects.ball.fixture = love.physics.newFixture(objects.ball.body, objects.ball.shape, 1) -- Attach fixture to body and give it a density of 1.
objects.ball.fixture:setRestitution(0.6) --let the ball bounce
--circle
objects.circle = {}
objects.circle.body = love.physics.newBody(world, 550/2, 600/2, "dynamic") --place the body in the center of the world and make it dynamic, so it can move around
objects.circle.shape = love.physics.newCircleShape(20) --the ball's shape has a radius of 40
objects.circle.fixture = love.physics.newFixture(objects.circle.body, objects.circle.shape, 1) -- Attach fixture to body and give it a density of 1.
objects.circle.fixture:setRestitution(0.6) --let the ball bounce
love.graphics.setBackgroundColor(62, 62, 62) --set the background color to grey
love.window.setMode(800, 600) --set the window dimensions to 800 (Width) by 650(Height)
end
function love.update(dt)
world:update(dt) --this puts the world into motion
--here we are going to create some keyboard events
if love.keyboard.isDown("right") then --press the right arrow key to push the ball to the right
objects.ball.body:applyForce(600, 0)
elseif love.keyboard.isDown("left") then --press the left arrow key to push the ball to the left
objects.ball.body:applyForce(-600, 0)
elseif love.keyboard.isDown("up") then --press the up arrow key to push the ball up
objects.ball.body:applyForce(0, -1000)
elseif love.keyboard.isDown("down") then --press the down arrow key to push the ball down
objects.ball.body:applyForce(0, 900)
elseif love.keyboard.isDown("r") then --press the "r" key to set the ball in the air
objects.ball.body:setPosition(950/2, 750/2)
objects.ball.body:setLinearVelocity(0, 0) --we must set the velocity to zero to prevent a potentially large velocity generated by the change in position
end
if love.keyboard.isDown("d") then --press the d key to push the ball to the right
objects.circle.body:applyForce(600, 0)
elseif love.keyboard.isDown("a") then --press the a key to push the ball to the left
objects.circle.body:applyForce(-600, 0)
elseif love.keyboard.isDown("w") then --press the w arrow key to push the ball up
objects.circle.body:applyForce(0, -1000)
elseif love.keyboard.isDown("s") then --press the s key to push the ball down
objects.circle.body:applyForce(0, 900)
elseif love.keyboard.isDown("r") then --press the "r" key to set the ball in the air
objects.circle.body:setPosition(950/2, 750/2)
objects.circle.body:setLinearVelocity(0, 0) --we must set the velocity to zero to prevent a potentially large velocity generated by the change in position
end
score = 0
end
function love.draw()
love.graphics.setColor(35, 35, 35) -- set the drawing color to dark-grey for the roof
love.graphics.polygon("fill", objects.roof.body:getWorldPoints(objects.roof.shape:getPoints())) -- draw a "filled in" polygon using the ground's coordinates
love.graphics.setColor(210, 0, 0) --set the drawing color to Red for the ball
love.graphics.circle("fill", objects.ball.body:getX(), objects.ball.body:getY(), objects.ball.shape:getRadius())
love.graphics.setColor(35, 35, 35) -- set the drawing color to dark-grey for the wall
love.graphics.polygon("fill", objects.wallR.body:getWorldPoints(objects.wallR.shape:getPoints()))
love.graphics.setColor(35, 35, 35) -- set the drawing color to dark-grey for the wall
love.graphics.polygon("fill", objects.wallL.body:getWorldPoints(objects.wallL.shape:getPoints()))
love.graphics.setColor(35, 35, 35) -- set the drawing color to dark-grey for the floor
love.graphics.polygon("fill", objects.floor.body:getWorldPoints(objects.floor.shape:getPoints()))
love.graphics.setColor(0, 0, 160) --set the drawing color to blue for the circle
love.graphics.circle("fill", objects.circle.body:getX(), objects.circle.body:getY(), objects.circle.shape:getRadius())
love.graphics.setColor( 25, 25, 25, 255 )
love.graphics.print("Score: " .. score, 16, 16, 0, 1, 1)
end
function addScore(n)
score = score + tonumber(n)
end
Configure file (conf.lua)
Code: Select all
function love.conf(t)
t.modules.joystick = true -- Enable the joystick module (boolean)
t.modules.audio = true -- Enable the audio module (boolean)
t.modules.keyboard = true -- Enable the keyboard module (boolean)
t.modules.event = true -- Enable the event module (boolean)
t.modules.image = true -- Enable the image module (boolean)
t.modules.graphics = true -- Enable the graphics module (boolean)
t.modules.timer = true -- Enable the timer module (boolean)
t.modules.mouse = true -- Enable the mouse module (boolean)
t.modules.sound = true -- Enable the sound module (boolean)
t.modules.thread = true
t.modules.physics = true -- Enable the physics module (boolean)
t.console = true -- Attach a console (boolean, Windows only)
t.title = "Project" -- The title of the window the game is in (string)
t.author = "Michael" -- The author of the game (string)
t.window.fullscreen = false -- Enable fullscreen (boolean)
t.window.vsync = false -- Enable vertical sync (boolean)
t.window.fsaa = 0 -- The number of FSAA-buffers (number)
t.window.height = 600 -- The window height (number)
t.window.width = 800 -- The window width (number)
end
So again to make it clearly stated as possible: I am trying to make a 2 controllable players that are SOLID meaning they can't go through other solids and can't go through each other......also I am trying to make a border using the same method as shown in my second code i posted which is my old project........I tried using that method, but like i said it placed the shape with the exact coordinates and color, but my character was well above it and for some reason turned a blue highlighted color and was able to go THROUGH the object I created ;(.
Do I have to implement a physics system? If so how? I just want to make 2 controllable objects that collide with each other and can't go outside the window and then I'll get more complicated from there. Thank you for reading and I look forward to your responses!