knorke wrote: ↑Sun Dec 25, 2022 6:19 pm
Do you already have the rest of your game working?
If you have the planets and asteroids working then you could do background stars in the same way - just without collision.
I would not use a dozen-thousand pixels image as background. Space is mostly empty anyway.
Instead draw individual stars.
You did not post what code you have written so far, so it is difficult to give advice.
Nothing works yet :-). I really just started but I am struggeling with a lot of problems :-).
This is why I didn't post any code.
Actually I was working on 2 things:
1. I wanted to add a background - it just looks boring, if you fly through dark space and nothing is happening.
2. I tried to apply kind of realistic space-physics to the spaceship.
So you always keep moving in the same direction until you change something using thrust.
So there is forward thrust, backward thrust, and sidwards to rotate the ship.
But when you rotate the ship, you keep flying in the same direction.
Here is what I have so far:
Code: Select all
math.randomseed(os.time())
basic_settings = require "basic-settings"
local game = {
difficulty = 1,
state = {
menu = true,
ingame = false,
paused = false,
ended = false,
},
score = 0,
score_spawn_new_enemie = 5,
}
local fonts = {
medium = {
font_size = 16,
font = love.graphics.newFont(16),
},
large = {
font_size = 20,
font = love.graphics.newFont(20),
},
massive = {
font_size = 60,
font = love.graphics.newFont(60),
}
}
--local enemies = {}
local function changeGameState(state)
game.state.menu = state == "menu"
game.state.ingame = state == "ingame"
game.state.paused = state == "paused"
game.state.ended = state == "ended"
print(game.state.menu, state)
end
local function startNewGame()
changeGameState("ingame")
game.score = 0
end
function calculateFlightDirection(angle)
player.orientation = math.rad(angle)
end
function love.keypressed(key)
if key == "w" then -- forward thrust
player.speed = player.speed + 1
player.draw_thrust = 1
player.reset.thrust = 1
end
if key == "s" then -- backward thrust
player.speed = player.speed - 1
player.draw_thrust = 0
player.reset.thrust = 1
end
if key == "a" then
player.angle = player.angle - 3
if player.angle <= 0 then player.angle = 360 end
player.draw_thrust = 0
player.reset.thrust = 1
end
-- right side thrust
if key == "d" then
player.angle = player.angle + 3
if player.angle >= 360 then player.angle = 0 end
player.draw_thrust = 0
player.reset.thrust = 1
end
end
function love.load()
love.mouse.setVisible(false)
player_image = love.graphics.newImage("images/spaceship-sprite.png")
player = {
radius = 20,
x = 200,
y = 200,
ox = player_image:getWidth(), -- offset x (caluclate the width of the image)
oy = player_image:getHeight(), -- offset y (caluclate the height of the image)
speed = 0,
angle = 0, -- 360 degree to fly around in space
orientation = 0,
image = player_image,
sprite = {
show = 0,
width = 135,
height = 80,
count_sprites = 2,
},
draw_thrust = 0,
reset = {
thrust = 0, -- 0 if no need to reset, 1 = check if reset is needed (using thrust_count)
thrust_count = 0,
}
}
spaceship_array = {}
for i=0, 1, 1 do
spaceship_array[i] = love.graphics.newQuad(player.sprite.width * i, 0, player.sprite.width, player.sprite.height, player.sprite.width*player.sprite.count_sprites, player.sprite.height)
end
--background_image = love.graphics.newImage("images/universe-background.jpg")
startNewGame()
end
function love.update(dt)
-- adapt the speed to the actual direction of the player
if player.speed ~= 0 then
player.x = player.x + (.1 * player.speed) -- for both directions, because player.speed could be negative
end
if player.reset.thrust > 0 and player.reset.thrust_count < .5 then
print("reset.thrust != 0", player.reset.thrust)
player.reset.thrust_count = player.reset.thrust_count+dt
else
print("no need to reset, ", player.reset.thrust)
player.reset.thrust = 0
player.reset.thrust_count = 0
player.draw_thrust = 0
end
calculateFlightDirection(player.angle)
end
function love.draw()
love.graphics.setFont(fonts.medium.font)
love.graphics.printf("FPS: " .. love.timer.getFPS(), fonts.medium.font, 10, love.graphics.getHeight()-30, love.graphics.getWidth())
if game.state.ingame then
print("draw thrust: ", player.draw_thrust, "reset thrust: ", player.reset.thrust, "reset thrust count: ", player.reset.thrust_count)
love.graphics.draw(player.image, spaceship_array[player.draw_thrust], player.x, player.y,
player.orientation, 1, 1, player.ox/4, player.oy/4)
end
end
Rotation works (kind of).
Forward and backwards thrust too.
But actually the thrust is only applied to the x-axis ... Not sure how to solve that.