They said I should apply the local variable to my code. And I see, when using local variables, accessing it elsewhere becomes extremely difficult. So today, I want to ask you some questions:
- * Firstly, in my particular case. They say listOfFruits and listOfBlock must be local. When I fixed it to local, I got an error with the Apple.lua file, saying that I cannot access listOfFruits. Take a look at my specific code, you will understand my case. I want to program with local variables the right way, using it as much as possible.
Please take a look and adjust my code so listOfFruits and Apple.lua still work well, help me.
* Secondly, I need a correct game structure. Some helpers, they also told me I had to restructure my game to make it easier to use local variables and manage objects. But I don't know what I should do. Please help me, read through my code and refactor so I can use local properly,
But don't get too far from the current structure. If you give me specific examples, I would be very happy. For example, now I need a way to manage the bottles in the game, add it in main.lua, manage itself in bottle.lua, some functions of the bottle are to destroy it from the world. , update, ... In the example,
You try to come up with a few cases which require access to variables from elsewhere. You just need to write a sample for me 2 files, then I will read and understand that code, later I can program the game properly. For me to imitate is the fastest way of learning (of course, I have to understand it too). I know I have a lot of demands,
but please forgive me for this, I'm still incompetent and in need of your guidance and advice. Thank you for your interest
Code: Select all
--- require libs
Object = require "codes/libs/classic"
bump = require "codes/libs/bump"
anim8 = require "codes/libs/anim8"
-- create new world
world = bump.newWorld()
---------game variables
listOfBlocks = {}
listOfFruits = {}
isRequired = false
currentLevel = 1
local background = love.graphics.newImage("sources/arts/tiles/tiles.png")
--- require
if isRequired == false then
require "codes/entities/characters/knight"
require "codes/entities/blocks/block"
require "codes/entities/items/fruits/apple"
isRequired = true
end
function love.load()
initGame()
loadMapLevel1()
end
function love.update(dt)
--- update player object
player:update(dt)
end
function love.draw()
--- draw background
drawBackGround()
--- draw blocks
for i,v in ipairs(listOfBlocks) do
v:draw()
end
--- draw fruits
for i,v in ipairs(listOfFruits) do
v:draw()
end
--- draw player object
player:draw()
end
---- keyboard event
function love.keypressed(key)
if key == "w" and player.onGround then
player.yVelocity = player.jumpVelocity
end
end
--- load map level 1
function loadMapLevel1()
--- blocks
map = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,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, 0, 0, 0},
{0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 2, 2, 2, 1, 0, 0, 0, 0, 1, 2, 2, 2, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}}
buildMap()
--- fruits
table.insert(listOfFruits, Apple(256, 64, 1))
table.insert(listOfFruits, Apple(128, 16, 2))
table.insert(listOfFruits, Apple(512, 400, 3))
table.insert(listOfFruits, Apple(256, 128, 4))
end
--- build map for all level with the data in function loadMapLevel'i'
function buildMap()
--id : 1.grass 2.dirt
for y = 1, 15 do
for x = 1, 30 do
if map[y][x] == 1 then
table.insert(listOfBlocks, Block((x - 1) * 32, (y - 1) * 32, 1))
elseif map[y][x] == 2 then
table.insert(listOfBlocks, Block((x - 1) * 32, (y - 1) * 32, 2))
end
end
end
end
--- init the game
function initGame()
-- create player
addPlayer("knight", 400, 300)
end
--- addPlayer function
function addPlayer(name, x, y)
local who = name
local xPos, yPos = x, y
if who == "knight" then
player = Knight(xPos, yPos)
end
end
--- draw background
function drawBackGround()
if currentLevel == 1 then
local quads = {}
---winter quad----
for i = 10, 16 do
local quad = love.graphics.newQuad(0, (i-1) * 32, 32, 32, background:getDimensions())
table.insert(quads, quad)
end
--- draw background---
for i = 1, 30 do
love.graphics.draw(background, quads[1], (i-1)*32, 0)
love.graphics.draw(background, quads[1], (i-1)*32, 32)
love.graphics.draw(background, quads[2], (i-1)*32, 32*2)
love.graphics.draw(background, quads[3], (i-1)*32, 32*3)
love.graphics.draw(background, quads[3], (i-1)*32, 32*4)
love.graphics.draw(background, quads[3], (i-1)*32, 32*5)
love.graphics.draw(background, quads[3], (i-1)*32, 32*6)
love.graphics.draw(background, quads[3], (i-1)*32, 32*7)
love.graphics.draw(background, quads[4], (i-1)*32, 32*8)
love.graphics.draw(background, quads[5], (i-1)*32, 32*9)
love.graphics.draw(background, quads[5], (i-1)*32, 32*10)
love.graphics.draw(background, quads[6], (i-1)*32, 32*11)
love.graphics.draw(background, quads[7], (i-1)*32, 32*12)
love.graphics.draw(background, quads[7], (i-1)*32, 32*13)
love.graphics.draw(background, quads[7], (i-1)*32, 32*14)
end
end
end
Code: Select all
Knight = Object:extend()
function Knight:new(x, y)
self.x = x
self.y = y
self.width = 32
self.height = 48
self.scaleX = 1
--- art
self.sprite = love.graphics.newImage("sources/arts/characters/knight/knight.png")
self.grid = anim8.newGrid(self.width, self.height, self.sprite:getWidth(), self.sprite:getHeight())
self.animations = {}
self.animations.idle = anim8.newAnimation(self.grid(1, 1, 2, 1, 3, 1, 2, 1), 0.1)
self.animations.walk = anim8.newAnimation(self.grid(4, 1, 5, 1, 6, 1, 7, 1, 8, 1, 4, 1), 0.05)
self.anim = self.animations.idle
---- collision properties
self.colWidth = 16
self.colHeight = 24
--- physics properties
self.xVelocity = 0
self.yVelocity = 0
self.gravity = 1000
self.terminalVelocity = 800
self.onGround = false
self.jumpVelocity = -400
----add to world
world:add(self, self.x, self.y, self.colWidth, self.colHeight)
end
--- knight collision filter
local function knightFilter(item, other)
if other.isBlock then return "slide"
elseif other.isApple then return "cross" end
end
function Knight:update(dt)
--- moveleft and right
if love.keyboard.isDown("a") then
self.xVelocity = -100
self.scaleX = -1
self.anim = self.animations.walk
elseif love.keyboard.isDown("d") then
self.xVelocity = 100
self.scaleX = 1
self.anim = self.animations.walk
else
self.xVelocity = 0
self.anim = self.animations.idle
end
--- apply gravity
if self.yVelocity < self.terminalVelocity then
self.yVelocity = self.yVelocity + self.gravity * dt
else
self.yVelocity = self.terminalVelocity
end
--- apply to move with bump
local goalX, goalY = self.x + self.xVelocity * dt, self.y + self.yVelocity * dt
local actualX, actualY, cols, len = world:move(self, goalX, goalY, knightFilter)
--- check for collision
self.onGround = false
for i = 1, len do
local col = cols[i]
local other = cols[i].other
if (col.normal.y == -1 or col.normal.y == 1) and col.other.isBlock then
self.yVelocity = 0
end
if col.normal.y == -1 and col.other.isBlock then
self.onGround = true
end
-- check collision with fruit
if other.isApple then
other:boom()
end
end
--- store real x, y
self.x, self.y = actualX, actualY
--- update animations
self.anim:update(dt)
end
function Knight:draw()
love.graphics.setColor(1, 1, 1)
self.anim:draw(self.sprite, self.x + 8, self.y + 4, nil, self.scaleX, 1, 16, 24)
love.graphics.rectangle("line", self.x, self.y, self.colWidth, self.colHeight)
end
Code: Select all
Block = Object:extend()
function Block:new(x, y, id)
self.x = x
self.y = y
self.width = 32
self.height = 32
self.sprite = love.graphics.newImage("sources/arts/tiles/tiles.png")
self.id = id
if self.id == 1 then
self.quad = love.graphics.newQuad(0, 0, 32, 32, self.sprite:getDimensions())
elseif self.id == 2 then
self.quad = love.graphics.newQuad(0, 32, 32, 32, self.sprite:getDimensions())
end
self.isBlock = true
world:add(self, self.x, self.y, self.width, self.height)
end
function Block:update(dt)
end
function Block:draw()
love.graphics.setColor(1, 1, 1)
love.graphics.draw(self.sprite, self.quad, self.x, self.y)
love.graphics.rectangle("line", self.x, self.y, self.width, self.height)
end
Code: Select all
Apple = Object:extend()
function Apple:new(x, y, ripelevel)
self.x = x
self.y = y
self.width = 16
self.height = 16
self.ripelevel = ripelevel
self.sprite = love.graphics.newImage("sources/arts/fruits/fruit.png")
self.quads = {}
for i = 1, 4 do
local quad = love.graphics.newQuad(0, (i-1)*32, 32, 32, self.sprite:getDimensions())
table.insert(self.quads, quad)
end
self.isApple = true
world:add(self, self.x, self.y, self.width, self.height)
end
function Apple:update(dt)
end
function Apple:boom()
for i = #listOfFruits, 1, -1 do
if listOfFruits[i] == self then
world:remove(listOfFruits[i])
table.remove(listOfFruits, i)
end
end
end
function Apple:draw()
love.graphics.setColor(1, 1, 1)
love.graphics.draw(self.sprite, self.quads[self.ripelevel], self.x - 10, self.y - 12)
love.graphics.rectangle("line", self.x, self.y, self.width, self.height)
end
Edit : The link to my current problem on Github: https://github.com/bananaback/Help-me-in-this
If I provide incomplete information then you just ask, I will provide as soon as possible