Last active
July 1, 2025 10:24
-
-
Save tangorri/c5b1b1ef4e87c313cf4a1a65a3ec78a2 to your computer and use it in GitHub Desktop.
bug des refences value
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
io.stdout:setvbuf("no") | |
TANK_SPEED = 240 | |
local offsetX, offsetY | |
local player = { | |
angle = 0 | |
} | |
player.sprite = {} | |
player.position = {} | |
player.velocity = 0 | |
local bullets = {} | |
function love.load() | |
player.sprite = love.graphics.newImage("assets/sprites/tank_sand.png") | |
player.position = { | |
x = 100, y = 100 | |
} | |
end | |
function love.update(dt) | |
local orientation | |
local isFiring = false | |
if love.keyboard.isDown("down") then | |
orientation = "down" | |
elseif love.keyboard.isDown("up") then | |
orientation = "up" | |
end | |
if love.keyboard.isDown("right") then | |
orientation = "right" | |
elseif love.keyboard.isDown("left") then | |
orientation = "left" | |
end | |
if love.keyboard.isDown("space") then | |
isFiring = true | |
else | |
isFiring = false | |
end | |
-- engine handler | |
if orientation == "down" then | |
player.velocity = -TANK_SPEED * dt | |
elseif orientation == "up" then | |
player.velocity = TANK_SPEED * dt | |
end | |
if orientation == "left" then | |
player.angle = player.angle - 90 * dt | |
elseif orientation == "right" then | |
player.angle = player.angle + 90 * dt | |
end | |
offsetX = math.cos(math.rad(player.angle)) * player.velocity | |
offsetY = math.sin(math.rad(player.angle)) * player.velocity | |
player.position.x = player.position.x + offsetX | |
player.position.y = player.position.y + offsetY | |
if isFiring then | |
spawnBullet(player.position, player.angle) | |
end | |
els | |
for i = 1, #bullets, 1 do | |
local b = bullets[i] | |
b.position.x = b.position.x + math.cos(math.rad(b.angle)) * dt * b.speed | |
b.position.y = b.position.y + math.sin(math.rad(b.angle)) * dt * b.speed | |
end | |
end | |
function spawnBullet(position, angle) | |
-- angle = 0 | |
print("spawn bullet") | |
local bullet = { | |
position = { | |
-- et non pas position= position | |
-- sinon on passerrai une référence de player.position | |
--- et modifier bullet.position.x par exemple modifirai également plauer.position.y | |
--- en passant par les propriétés x et y manuellement on passer des copie de ces valeuer | |
--- dite "primitive" => pas de reference mais bien un copie | |
x= position.x, | |
y= position.y | |
}, | |
angle = angle, | |
speed = 100 | |
} | |
table.insert(bullets, bullet) | |
end | |
function love.draw() | |
love.graphics.draw(player.sprite, player.position.x, player.position.y, math.rad(player.angle - 90)) | |
love.graphics.print(player.angle) | |
love.graphics.print(#bullets) | |
for i = 1, #bullets, 1 do | |
love.graphics.circle("fill", bullets[i].position.x, bullets[i].position.y, 10) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment