Étiqueté : tilemap personnage carre noir affichage
- Ce sujet contient 2 réponses, 2 participants et a été mis à jour pour la dernière fois par
Pouranat, le il y a 1 année et 11 mois.
-
AuteurMessages
-
-
01/09/2020 à 10:29 #66309
bonjour,
désolé pour le double post maisil me semble qu il y a un beug sur le forum que je n arrive pas du tout apres de multiples essais a répondre a mon premier message …j ai un souci sur l atelier « Déplacements et collisions sur une Tilemap »
le code fonctionne bien mais j ai un probleme sur l affichage du personnage
en effet au lieu d avoir simplement le personnage, j ai un personnage qui fonctionne normalement(animation, déplacement etc) mais il est entouré d un carré noir d une dizaine de fois sa taille environ et se retrouve au milieu de ce carré. il semblerait que cela vienne plutot des images que du code. j ai pourtant plusieurs fois essayé de changer les images du personnage sans succes. je précise que je suis sous mac, je ne sais pas si cela peut avoir une incidence.image:
https://www.noelshack.com/2020-36-1-1598903030-imggamecodeur.jpegcode:
main.lua
— Cette ligne permet d’afficher des traces dans la console pendant l’éxécution
io.stdout:setvbuf(‘no’)— Empèche Love de filtrer les contours des images quand elles sont redimentionnées
— Indispensable pour du pixel art
love.graphics.setDefaultFilter(« nearest »)— Cette ligne permet de déboguer pas à pas dans ZeroBraneStudio
if arg[#arg] == « -debug » then require(« mobdebug »).start() endlocal myGame = require(« game »)
function love.load()
love.window.setMode(1024,768)
love.graphics.setFont(love.graphics.newFont(24))largeur = love.graphics.getWidth()
hauteur = love.graphics.getHeight()myGame.Load()
end
function love.update(dt)
myGame.Update(dt)
end
function love.draw()
myGame.Draw()
end
function love.keypressed(key)
print(key)
end
hero.lua
local hero = {}
hero.images = {}
hero.images [1] = love.graphics.newImage(« images/player_1.png »)
hero.images [2] = love.graphics.newImage(« images/player_2.png »)
hero.images [3] = love.graphics.newImage(« images/player_3.png »)
hero.images [4] = love.graphics.newImage(« images/player_4.png »)
hero.imgCurrent = 1
hero.line = 1
hero.column = 1
hero.keyPressed =falsefunction hero.Update (pMap, dt)
hero.imgCurrent = hero.imgCurrent + (5 * dt)
if math.floor(hero.imgCurrent) > #hero.images then
hero.imgCurrent = 1
endif love.keyboard.isDown(« up », »right », »down », »left ») then
if hero.keyPressed == false then
if love.keyboard.isDown(« up ») and hero.line > 1 then
hero.line = hero.line -1
end
if love.keyboard.isDown(« right ») and hero.column < pMap.MAP_WIDTH then
hero.column =hero.column +1end
if love.keyboard.isDown(« down ») and hero.line < pMap.MAP_HEIGHT then
hero.line = hero.line +1
end
if love.keyboard.isDown(« left ») and hero.column > 1 thenhero.column =hero.column -1
end
hero.keyPressed = true
end
else hero.keyPressed = falseend
end
function hero.Draw (pMap)
local x = (hero.column -1) * pMap.TILE_WIDTH
local y = (hero.line -1) * pMap.TILE_HEIGHT
love.graphics.draw (hero.images[math.floor(hero.imgCurrent)], x, y, 0 , 2 , 2 )
endreturn hero
game.lua:
local Game = {}
Game.Map = {}
Game.Map.Grid = {
{10, 10, 10, 10, 10, 10, 10, 10, 10, 61, 10, 13, 10, 10, 10, 10, 10, 10, 13, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15},
{10, 10, 10, 10, 10, 11, 11, 11, 10, 10, 10, 13, 10, 10, 10, 10, 10, 10, 10, 14, 15, 15, 129, 15, 15, 15, 15, 15, 15, 68, 15, 15},
{10, 10, 61, 10, 11, 19, 19, 19, 11, 10, 10, 13, 10, 10, 169, 10, 10, 10, 10, 13, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15},
{10, 10, 10, 11, 19, 19, 19, 19, 19, 11, 10, 13, 10, 10, 10, 10, 10, 10, 10, 10, 13, 14, 15, 15, 15, 68, 15, 15, 15, 15, 15, 15},
{10, 10, 10, 11, 19, 19, 19, 19, 19, 11, 10, 13, 10, 10, 10, 10, 10, 10, 61, 10, 10, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15},
{10, 10, 61, 10, 11, 19, 19, 19, 11, 10, 10, 13, 10, 10, 10, 10, 10, 10, 10, 10, 10, 14, 15, 15, 129, 15, 15, 15, 68, 15, 129, 15},
{10, 10, 10, 10, 10, 11, 11, 11, 10, 10, 61, 13, 10, 10, 10, 10, 10, 10, 10, 10, 10, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15},
{10, 10, 10, 10, 10, 13, 13, 13, 13, 13, 13, 13, 10, 10, 10, 10, 10, 169, 10, 10, 10, 13, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15},
{10, 10, 10, 10, 10, 10, 10, 10, 13, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 61, 10, 13, 14, 14, 14, 14, 14, 14, 14, 15, 129},
{10, 10, 10, 10, 10, 10, 10, 10, 13, 55, 10, 58, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 13, 14, 14},
{10, 10, 10, 10, 10, 10, 10, 10, 13, 10, 10, 10, 55, 10, 58, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
{10, 10, 10, 10, 10, 10, 10, 10, 13, 10, 58, 10, 10, 10, 10, 10, 10, 169, 10, 10, 10, 10, 10, 10, 61, 10, 10, 10, 10, 10, 1, 1},
{10, 10, 10, 10, 10, 10, 10, 10, 13, 10, 10, 10, 58, 10, 10, 10, 10, 10, 10, 10, 10, 61, 10, 10, 10, 10, 10, 10, 10, 1, 37, 37},
{13, 13, 13, 13, 13, 13, 13, 13, 13, 10, 55, 10, 10, 10, 55, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 1, 1, 37, 37, 37},
{10, 10, 10, 10, 13, 10, 10, 10, 10, 10, 10, 10, 55, 10, 10, 10, 10, 169, 10, 10, 10, 10, 10, 10, 10, 10, 1, 37, 37, 37, 37, 37},
{10, 10, 10, 10, 13, 10, 10, 10, 10, 142, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 1, 37, 37, 37, 37, 37, 37},
{10, 10, 10, 10, 13, 10, 10, 10, 10, 10, 10, 10, 10, 142, 10, 10, 10, 10, 10, 10, 10, 169, 10, 10, 1, 37, 37, 37, 37, 37, 37, 37},
{14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 1, 37, 37, 37, 37, 37, 37, 37},
{14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 1, 37, 37, 37, 37, 37, 37, 37},
{19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 1, 37, 37, 37, 37, 37, 37, 37},
{20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 1, 37, 37, 37, 37, 37, 37},
{21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 1, 37, 37, 37, 37},
{21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 1, 37, 37, 37}
}Game.Map.MAP_WIDTH = 32
Game.Map.MAP_HEIGHT = 23
Game.Map.TILE_WIDTH = 32
Game.Map.TILE_HEIGHT = 32Game.TileSheet = {}
Game.TileTextures = {}
Game.TileTypes = {}Game.Hero = require(« hero »)
function Game.Map.isSolid(pID)
local tileType = Game.TileTypes[pID]if tileType == « Sea » or
tileType == « Tree » or
tileType == « Rock » then
return true
endreturn false
endfunction Game.Load()
print(« Game:Chargement des textures… »)Game.TileSheet = love.graphics.newImage(« images/tilesheet.png »)
local nbColumns = Game.TileSheet:getWidth() / Game.Map.TILE_WIDTH
local nbLines = Game.TileSheet:getHeight() / Game.Map.TILE_HEIGHT
local id = 1Game.TileTextures[0] = nil
for l=1,nbLines do
for c=1,nbColumns do
Game.TileTextures[id] = love.graphics.newQuad(
(c-1)*Game.Map.TILE_WIDTH, (l-1)*Game.Map.TILE_HEIGHT,
Game.Map.TILE_WIDTH, Game.Map.TILE_HEIGHT,
Game.TileSheet:getWidth(), Game.TileSheet:getHeight()
)
id = id + 1
end
endGame.TileTypes[10] = « Grass »
Game.TileTypes[11] = « Grass »
Game.TileTypes[13] = « Sand »
Game.TileTypes[14] = « Sand »
Game.TileTypes[15] = « Sand »
Game.TileTypes[19] = « Water »
Game.TileTypes[20] = « Water »
Game.TileTypes[21] = « Sea »
Game.TileTypes[37] = « Lava »
Game.TileTypes[55] = « Tree »
Game.TileTypes[58] = « Tree »
Game.TileTypes[61] = « Tree »
Game.TileTypes[68] = « Tree »
Game.TileTypes[169] = « Rock »
Game.TileTypes[129] = « Rock »print(« Game:Chargement des textures terminées… »)
endfunction Game.Update(dt)
Game.Hero.Update(Game.Map, dt)
endfunction Game.Draw()
local c,lfor l=1,Game.Map.MAP_HEIGHT do
for c=1,Game.Map.MAP_WIDTH do
local id = Game.Map.Grid[l][c]
local texQuad = Game.TileTextures[id]
if texQuad ~= nil then
love.graphics.draw(Game.TileSheet, texQuad, (c-1)*Game.Map.TILE_WIDTH, (l-1)*Game.Map.TILE_HEIGHT)
end
end
endGame.Hero.Draw(Game.Map)
local x = love.mouse.getX()
local y = love.mouse.getY()
local c = math.floor(x / Game.Map.TILE_WIDTH) + 1
local l = math.floor(y / Game.Map.TILE_HEIGHT) + 1
if l>0 and c>0 and l <= Game.Map.MAP_HEIGHT and c <= Game.Map.MAP_WIDTH then
local id = Game.Map.Grid[l][c]
–[[
love.graphics.print(
« Ligne: « ..tostring(l)..
» Colonne: « ..tostring(c)..
» ID: « ..tostring(id)..
» (« ..tostring(Game.TileTypes[id]).. ») »
,1,1)
]]–
end
endreturn Game
je vous remercie de votre aide.
tom
-
01/09/2020 à 11:46 #66310
desolé j’ai tester ton code est je n’ai pas eu le probleme que tu decrit! je n’avais pas de carré noir et tout fonctionnai.
-
03/09/2020 à 9:21 #66337
merci d avoir essayé. ca ne vient donc pas du code mais possiblement de l image et peut etre du fait d etre sur mac… a voir
-
-
-
AuteurMessages
- Vous devez être connecté pour répondre à ce sujet.