Certainly! Let's create a bullet hell roguelike game using LÖVE (Love2D) with Lua. We'll implement the features you've described and add additional elements to enhance the gameplay experience. Below is a step-by-step guide along with the code snippets for each part.
Table of Contents
- Project Setup
- Player Implementation
- Enemy Implementation
- Bullet Implementation
- Level System and Cards
- Power-ups and Upgrades
- Additional Features
- Full Source Code
Project Setup
First, ensure you have LÖVE installed. You can download it from the official website.
Create a new directory for your game. Inside this directory, create a main.lua file, which is the entry point for any LÖVE game.
Player Implementation
We'll start by creating a player that can move with WASD and shoot bullets towards the mouse pointer.
-- main.lua
function love.load()
love.window.setTitle("Bullet Hell Roguelike")
love.window.setMode(800, 600)
player = {
x = 400,
y = 300,
speed = 200,
size = 20,
health = 100,
maxHealth = 100,
bullets = {},
fireRate = 0.2,
timeSinceLastShot = 0,
bulletSpeed = 400,
bulletSize = 5,
bulletDamage = 10,
shotsPerFire = 1,
bulletBounce = false,
}
enemies = {}
enemySpawnTimer = 0
enemySpawnRate = 2
cards = {}
gameState = "playing" -- other states: "card_selection", "game_over"
end
function love.update(dt)
if gameState == "playing" then
updatePlayer(dt)
updateBullets(dt)
updateEnemies(dt)
checkCollisions()
elseif gameState == "card_selection" then
-- Handle card selection logic
end
end
function love.draw()
if gameState == "playing" then
drawPlayer()
drawBullets()
drawEnemies()
drawUI()
elseif gameState == "card_selection" then
drawCardSelection()
elseif gameState == "game_over" then
drawGameOver()
end
end
Player Movement
function updatePlayer(dt)
local dx, dy = 0, 0
if love.keyboard.isDown('w') then dy = dy - player.speed * dt end
if love.keyboard.isDown('s') then dy = dy + player.speed * dt end
if love.keyboard.isDown('a') then dx = dx - player.speed * dt end
if love.keyboard.isDown('d') then dx = dx + player.speed * dt end
player.x = player.x + dx
player.y = player.y + dy
-- Keep the player within window bounds
player.x = math.max(0, math.min(love.graphics.getWidth(), player.x))
player.y = math.max(0, math.min(love.graphics.getHeight(), player.y))
-- Shooting
player.timeSinceLastShot = player.timeSinceLastShot + dt
if love.mouse.isDown(1) and player.timeSinceLastShot >= player.fireRate then
shootBullet()
player.timeSinceLastShot = 0
end
end
Drawing the Player
function drawPlayer()
love.graphics.setColor(0, 1, 0)
love.graphics.circle('fill', player.x, player.y, player.size)
end
Bullet Implementation
We will create bullets that move towards the mouse pointer when the player clicks.
function shootBullet()
local mouseX, mouseY = love.mouse.getPosition()
local angle = math.atan2(mouseY - player.y, mouseX - player.x)
for i = 1, player.shotsPerFire do
local bullet = {
x = player.x,
y = player.y,
dx = math.cos(angle) * player.bulletSpeed,
dy = math.sin(angle) * player.bulletSpeed,
size = player.bulletSize,
damage = player.bulletDamage,
bounce = player.bulletBounce,
}
table.insert(player.bullets, bullet)
end
end
function updateBullets(dt)
for i = #player.bullets, 1, -1 do
local bullet = player.bullets[i]
bullet.x = bullet.x + bullet.dx * dt
bullet.y = bullet.y + bullet.dy * dt
-- Bullet bouncing logic
if bullet.bounce then
if bullet.x <= 0 or bullet.x >= love.graphics.getWidth() then
bullet.dx = -bullet.dx
end
if bullet.y <= 0 or bullet.y >= love.graphics.getHeight() then
bullet.dy = -bullet.dy
end
else
-- Remove bullet if off-screen
if bullet.x < 0 or bullet.x > love.graphics.getWidth() or bullet.y < 0 or bullet.y > love.graphics.getHeight() then
table.remove(player.bullets, i)
end
end
end
end
function drawBullets()
love.graphics.setColor(1, 1, 0)
for _, bullet in ipairs(player.bullets) do
love.graphics.circle('fill', bullet.x, bullet.y, bullet.size)
end
end
Enemy Implementation
Enemies will spawn at intervals and move towards the player.
function updateEnemies(dt)
enemySpawnTimer = enemySpawnTimer + dt
if enemySpawnTimer >= enemySpawnRate then
spawnEnemy()
enemySpawnTimer = 0
end
for i = #enemies, 1, -1 do
local enemy = enemies[i]
local angle = math.atan2(player.y - enemy.y, player.x - enemy.x)
enemy.x = enemy.x + math.cos(angle) * enemy.speed * dt
enemy.y = enemy.y + math.sin(angle) * enemy.speed * dt
-- Remove enemy if dead
if enemy.health <= 0 then
table.remove(enemies, i)
-- TODO: Increase score or trigger events
end
end
end
function spawnEnemy()
local enemy = {
x = math.random(0, love.graphics.getWidth()),
y = -20,
speed = math.random(50, 100),
size = 15,
health = 30,
maxHealth = 30,
}
table.insert(enemies, enemy)
end
function drawEnemies()
love.graphics.setColor(1, 0, 0)
for _, enemy in ipairs(enemies) do
love.graphics.circle('fill', enemy.x, enemy.y, enemy.size)
end
end
Collision Detection
function checkCollisions()
-- Bullet and Enemy collision
for i = #player.bullets, 1, -1 do
local bullet = player.bullets[i]
for j = #enemies, 1, -1 do
local enemy = enemies[j]
if distance(bullet.x, bullet.y, enemy.x, enemy.y) < bullet.size + enemy.size then
enemy.health = enemy.health - bullet.damage
table.remove(player.bullets, i)
break
end
end
end
-- Enemy and Player collision
for _, enemy in ipairs(enemies) do
if distance(enemy.x, enemy.y, player.x, player.y) < enemy.size + player.size then
player.health = player.health - 20
enemy.health = 0
-- Check for game over
if player.health <= 0 then
gameState = "game_over"
end
end
end
end
function distance(x1, y1, x2, y2)
return math.sqrt((x2 - x1)^2 + (y2 - y1)^2)
end
Level System and Cards
After clearing a stage, we present the player with card choices for upgrades.
function updateGameState()
if #enemies == 0 and gameState == "playing" then
gameState = "card_selection"
generateCards()
end
end
function generateCards()
cards = {}
table.insert(cards, {
name = "Double Health",
description = "Doubles your maximum health.",
apply = function()
player.maxHealth = player.maxHealth * 2
player.health = player.maxHealth
end
})
table.insert(cards, {
name = "Triple Shot",
description = "Shoots 2 extra bullets.",
apply = function()
player.shotsPerFire = player.shotsPerFire + 2
end
})
table.insert(cards, {
name = "Bouncing Bullets",
description = "Your bullets bounce off walls.",
apply = function()
player.bulletBounce = true
end
})
end
function love.mousepressed(x, y, button)
if gameState == "card_selection" and button == 1 then
-- Determine which card was selected
for i, card in ipairs(cards) do
local cardX = 100 + (i - 1) * 250
if x > cardX and x < cardX + 200 and y > 200 and y < 400 then
card.apply()
gameState = "playing"
break
end
end
end
end
function drawCardSelection()
love.graphics.setColor(1, 1, 1)
love.graphics.print("Choose an upgrade:", 300, 150)
for i, card in ipairs(cards) do
local cardX = 100 + (i - 1) * 250
love.graphics.rectangle("line", cardX, 200, 200, 200)
love.graphics.printf(card.name, cardX, 220, 200, "center")
love.graphics.printf(card.description, cardX + 10, 260, 180, "left")
end
end
Power-ups and Upgrades
We can add more upgrades for variety.
function generateCards()
cards = {}
local possibleUpgrades = {
{
name = "Double Health",
description = "Doubles your maximum health.",
apply = function()
player.maxHealth = player.maxHealth * 2
player.health = player.maxHealth
end
},
{
name = "Triple Shot",
description = "Shoots 2 extra bullets.",
apply = function()
player.shotsPerFire = player.shotsPerFire + 2
end
},
{
name = "Bouncing Bullets",
description = "Your bullets bounce off walls.",
apply = function()
player.bulletBounce = true
end
},
{
name = "Increased Damage",
description = "Bullets deal more damage.",
apply = function()
player.bulletDamage = player.bulletDamage + 5
end
},
{
name = "Faster Shooting",
description = "Decrease the time between shots.",
apply = function()
player.fireRate = player.fireRate * 0.8
end
},
{
name = "Speed Boost",
description = "Increase your movement speed.",
apply = function()
player.speed = player.speed + 50
end
},
}
-- Randomly select 3 upgrades
for i = 1, 3 do
local index = math.random(#possibleUpgrades)
table.insert(cards, possibleUpgrades[index])
table.remove(possibleUpgrades, index)
end
end
Additional Features
Scoring System
Add a scoring system to keep track of the player's progress.
score = 0
function checkCollisions()
-- Existing collision code...
-- Update score when enemies are defeated
if enemy.health <= 0 then
score = score + 100
end
end
function drawUI()
love.graphics.setColor(1, 1, 1)
love.graphics.print("Health: " .. player.health .. "/" .. player.maxHealth, 10, 10)
love.graphics.print("Score: " .. score, 10, 30)
end
Game Over Screen
Display a game over screen when the player runs out of health.
function drawGameOver()
love.graphics.setColor(1, 0, 0)
love.graphics.printf("Game Over", 0, love.graphics.getHeight()/2 - 30, love.graphics.getWidth(), "center")
love.graphics.setColor(1, 1, 1)
love.graphics.printf("Score: " .. score, 0, love.graphics.getHeight()/2, love.graphics.getWidth(), "center")
love.graphics.printf("Press R to Restart", 0, love.graphics.getHeight()/2 + 30, love.graphics.getWidth(), "center")
end
function love.keypressed(key)
if gameState == "game_over" and key == "r" then
love.event.quit("restart")
end
end
Different Enemy Types
Introduce varied enemies to enhance difficulty.
function spawnEnemy()
local enemyTypes = {
{
speed = 50,
size = 15,
health = 30,
color = {1, 0, 0},
},
{
speed = 80,
size = 10,
health = 20,
color = {1, 0.5, 0},
},
{
speed = 30,
size = 25,
health = 50,
color = {0.5, 0, 0.5},
},
}
local enemyType = enemyTypes[math.random(#enemyTypes)]
local enemy = {
x = math.random(0, love.graphics.getWidth()),
y = -20,
speed = enemyType.speed,
size = enemyType.size,
health = enemyType.health,
maxHealth = enemyType.health,
color = enemyType.color,
}
table.insert(enemies, enemy)
end
function drawEnemies()
for _, enemy in ipairs(enemies) do
love.graphics.setColor(enemy.color)
love.graphics.circle('fill', enemy.x, enemy.y, enemy.size)
end
end
Background Music and Sound Effects
You can add background music and sound effects using love.audio.
function love.load()
-- Existing code...
shootSound = love.audio.newSource("shoot.wav", "static")
hitSound = love.audio.newSource("hit.wav", "static")
gameOverSound = love.audio.newSource("gameover.wav", "static")
backgroundMusic = love.audio.newSource("background.mp3", "stream")
backgroundMusic:setLooping(true)
love.audio.play(backgroundMusic)
end
function shootBullet()
-- Existing code...
love.audio.play(shootSound:clone())
end
function checkCollisions()
-- Existing code...
if enemy.health <= 0 then
love.audio.play(hitSound:clone())
end
if player.health <= 0 then
love.audio.play(gameOverSound)
end
end
Note: You'll need to provide your own shoot.wav, hit.wav, gameover.wav, and background.mp3 files.
Pause Functionality
Allow the player to pause the game.
isPaused = false
function love.keypressed(key)
if key == "escape" then
isPaused = not isPaused
end
end
function love.update(dt)
if not isPaused then
-- Existing update code...
end
end
function drawUI()
-- Existing UI code...
if isPaused then
love.graphics.setColor(1, 1, 1, 0.7)
love.graphics.rectangle("fill", 0, 0, love.graphics.getWidth(), love.graphics.getHeight())
love.graphics.setColor(1, 0, 0)
love.graphics.printf("Paused", 0, love.graphics.getHeight()/2 - 30, love.graphics.getWidth(), "center")
end
end
Full Source Code
Below is the cumulative code that incorporates all the above features.
View Full Source Code on GitHub (Provide a link to the full source code if possible)
Next Steps
- Art and Graphics: Replace placeholder shapes with actual sprites.
- More Upgrades: Add additional upgrades and power-ups to increase variety.
- Enemy Behaviors: Implement different enemy movement patterns and attacks.
- Boss Fights: Introduce boss enemies at certain stages.
- Save Progress: Allow players to save and load their game.
- Difficulty Scaling: Adjust the game difficulty as the player progresses.
Feel free to modify and expand upon this code to suit your vision for the game. Happy coding!