make a bubble bobble game in love2d
- User
make a bubble bobble game in love2d
- Assistant
- User
put it all in one code
- Assistant
- User
fix the code there are runtime errors
- Assistant
- User
remake the game for pico8
- Assistant
make a bubble bobble game in love2d
put it all in one code
fix the code there are runtime errors
remake the game for pico8
Creating a Bubble Bobble-like game in LÖVE (often referred to as LÖVE2D) involves a combination of Lua programming for game logic, asset creation for visuals, and using LÖVE's framework for handling game mechanics. Below, I'll guide you through setting up a basic version of such a game. You will need the LÖVE framework installed on your computer to run the game.
Create a new directory for your game project and add the following files:
main.lua: The main entry point of the game.player.lua: Script for handling player behavior.bubble.lua: Script for handling bubble behavior.enemy.lua: Script for handling enemy behavior.main.lua-- main.lua
love.graphics.setDefaultFilter('nearest', 'nearest')
local player = require 'player'
local bubble = require 'bubble'
local enemy = require 'enemy'
function love.load()
player.load()
bubble.load()
enemy.load()
end
function love.update(dt)
player.update(dt)
bubble.update(dt)
enemy.update(dt)
end
function love.draw()
player.draw()
bubble.draw()
enemy.draw()
end
player.lua-- player.lua
local player = {}
local playerImage
local moveSpeed = 100
local bubbles = require 'bubble'
function player.load()
playerImage = love.graphics.newImage('assets/player.png')
player.x = 100
player.y = 100
player.width = playerImage:getWidth()
player.height = playerImage:getHeight()
end
function player.update(dt)
if love.keyboard.isDown('left') then
player.x = player.x - moveSpeed * dt
elseif love.keyboard.isDown('right') then
player.x = player.x + moveSpeed * dt
end
if love.keyboard.isDown('space') then
bubbles.shoot(player.x + player.width / 2, player.y)
end
end
function player.draw()
love.graphics.draw(playerImage, player.x, player.y)
end
return player
bubble.lua-- bubble.lua
local bubble = {}
local bubbleImage
local activeBubbles = {}
local bubbleSpeed = 200
function bubble.load()
bubbleImage = love.graphics.newImage('assets/bubble.png')
end
function bubble.shoot(x, y)
table.insert(activeBubbles, {x = x, y = y})
end
function bubble.update(dt)
for i, b in ipairs(activeBubbles) do
b.y = b.y - bubbleSpeed * dt
if b.y < -bubbleImage:getHeight() then
table.remove(activeBubbles, i)
end
end
end
function bubble.draw()
for i, b in ipairs(activeBubbles) do
love.graphics.draw(bubbleImage, b.x, b.y)
end
end
return bubble
enemy.lua-- enemy.lua
local enemy = {}
local enemyImage
local enemies = {}
local spawnTimer = 0
function enemy.load()
enemyImage = love.graphics.newImage('assets/enemy.png')
end
function enemy.update(dt)
spawnTimer = spawnTimer + dt
if spawnTimer > 2 then -- spawn every 2 seconds
table.insert(enemies, {x = math.random(800), y = 0})
spawnTimer = 0
end
for i, e in ipairs(enemies) do
e.y = e.y + 50 * dt -- move down at fixed speed
if e.y > 600 then -- remove enemies below screen
table.remove(enemies, i)
end
end
end
function enemy.draw()
for i, e in ipairs(enemies) do
love.graphics.draw(enemyImage, e.x, e.y)
end
end
return enemy
Place your player, bubble, and enemy images in an assets folder. For simplicity, use placeholder images with dimensions around 32x32 pixels.
To run the game, use LÖVE's command line tool:
love /path/to/your/project
This basic setup provides a framework for the game's core mechanics. Here are additional features you can implement:
Experiment with LÖVE's tutorials and documentation to enhance your game with more features and polish.
Certainly! Below is a combined version of the Bubble Bobble-like game in LÖVE2D, all in one file (main.lua). This includes basic functionality for a player that can move and shoot bubbles, and simple enemy handling. The code assumes you have placeholder images for the player, bubbles, and enemies.
-- Declare game objects and load assets
local player = {}
local bubbles = {}
local enemies = {}
local playerImage
local bubbleImage
local enemyImage
local moveSpeed = 100
local bubbleSpeed = 200
local enemySpeed = 50
local spawnTimer = 0
function love.load()
love.graphics.setDefaultFilter('nearest', 'nearest')
-- Load Images
playerImage = love.graphics.newImage('assets/player.png')
bubbleImage = love.graphics.newImage('assets/bubble.png')
enemyImage = love.graphics.newImage('assets/enemy.png')
-- Initialize Player
player.x = 100
player.y = 100
player.width = playerImage:getWidth()
player.height = playerImage:getHeight()
end
function love.update(dt)
-- Player Movement
if love.keyboard.isDown('left') then
player.x = player.x - moveSpeed * dt
elseif love.keyboard.isDown('right') then
player.x = player.x + moveSpeed * dt
end
-- Shooting Bubbles
if love.keyboard.isDown('space') then
shootBubble(player.x + player.width / 2, player.y)
end
-- Update Bubbles
for i, b in ipairs(bubbles) do
b.y = b.y - bubbleSpeed * dt
if b.y < -bubbleImage:getHeight() then
table.remove(bubbles, i)
end
end
-- Update Enemies
spawnTimer = spawnTimer + dt
if spawnTimer > 2 then -- spawn every 2 seconds
spawnEnemy()
spawnTimer = 0
end
for i, e in ipairs(enemies) do
e.y = e.y + enemySpeed * dt
if e.y > love.graphics.getHeight() then
table.remove(enemies, i)
end
end
-- Check for collisions between bubbles and enemies
checkCollisions()
end
function love.draw()
-- Draw Player
love.graphics.draw(playerImage, player.x, player.y)
-- Draw Bubbles
for i, b in ipairs(bubbles) do
love.graphics.draw(bubbleImage, b.x, b.y)
end
-- Draw Enemies
for i, e in ipairs(enemies) do
love.graphics.draw(enemyImage, e.x, e.y)
end
end
function shootBubble(x, y)
table.insert(bubbles, {x = x, y = y})
end
function spawnEnemy()
table.insert(enemies, {x = math.random(love.graphics.getWidth()), y = 0})
end
function checkCollisions()
for i = #bubbles, 1, -1 do
local b = bubbles[i]
for j = #enemies, 1, -1 do
local e = enemies[j]
if e.x < b.x + bubbleImage:getWidth() and
b.x < e.x + enemyImage:getWidth() and
e.y < b.y + bubbleImage:getHeight() and
b.y < e.y + enemyImage:getHeight() then
table.remove(bubbles, i)
table.remove(enemies, j)
break
end
end
end
end
assets directory.player.png, bubble.png, and enemy.png, respectively.main.lua file in a project folder.love /path/to/your/project.Make sure LÖVE2D is installed and your assets folder contains those required images for successful execution.
Certainly! Let's refine the code to resolve potential runtime errors and ensure it runs smoothly. I'll address common issues like checking for empty arrays during iteration and clarifying the asset paths.
Here's the revised version of the game code:
local player = {}
local bubbles = {}
local enemies = {}
local playerImage
local bubbleImage
local enemyImage
local moveSpeed = 100
local bubbleSpeed = 200
local enemySpeed = 50
local spawnTimer = 0
function love.load()
love.graphics.setDefaultFilter('nearest', 'nearest')
-- Load Images
playerImage = love.graphics.newImage('assets/player.png')
bubbleImage = love.graphics.newImage('assets/bubble.png')
enemyImage = love.graphics.newImage('assets/enemy.png')
-- Initialize Player
player.x = 100
player.y = 100
player.width = playerImage:getWidth()
player.height = playerImage:getHeight()
end
function love.update(dt)
-- Player Movement
if love.keyboard.isDown('left') then
player.x = player.x - moveSpeed * dt
elseif love.keyboard.isDown('right') then
player.x = player.x + moveSpeed * dt
end
-- Shooting Bubbles (to prevent holding the space bar continuously, you may want to add a shooting cooldown)
if love.keyboard.isDown('space') then
if not player.shooting then
shootBubble(player.x + player.width / 2 - bubbleImage:getWidth() / 2, player.y)
player.shooting = true
end
else
player.shooting = false
end
-- Update Bubbles
for i = #bubbles, 1, -1 do
local b = bubbles[i]
b.y = b.y - bubbleSpeed * dt
if b.y < -bubbleImage:getHeight() then
table.remove(bubbles, i)
end
end
-- Update Enemies
spawnTimer = spawnTimer + dt
if spawnTimer > 2 then -- spawn every 2 seconds
spawnEnemy()
spawnTimer = 0
end
for i = #enemies, 1, -1 do
local e = enemies[i]
e.y = e.y + enemySpeed * dt
if e.y > love.graphics.getHeight() then
table.remove(enemies, i)
end
end
-- Check for collisions
checkCollisions()
end
function love.draw()
-- Draw Player
love.graphics.draw(playerImage, player.x, player.y)
-- Draw Bubbles
for _, b in ipairs(bubbles) do
love.graphics.draw(bubbleImage, b.x, b.y)
end
-- Draw Enemies
for _, e in ipairs(enemies) do
love.graphics.draw(enemyImage, e.x, e.y)
end
end
function shootBubble(x, y)
table.insert(bubbles, {x = x, y = y})
end
function spawnEnemy()
local enemyX = math.random(0, love.graphics.getWidth() - enemyImage:getWidth())
table.insert(enemies, {x = enemyX, y = 0})
end
function checkCollisions()
for i = #bubbles, 1, -1 do
local b = bubbles[i]
for j = #enemies, 1, -1 do
local e = enemies[j]
if checkCollision(b.x, b.y, bubbleImage:getWidth(), bubbleImage:getHeight(), e.x, e.y, enemyImage:getWidth(), enemyImage:getHeight()) then
table.remove(bubbles, i) -- Remove bubble
table.remove(enemies, j) -- Remove enemy
break
end
end
end
end
function checkCollision(x1, y1, w1, h1, x2, y2, w2, h2)
return x1 < x2 + w2 and x2 < x1 + w1 and y1 < y2 + h2 and y2 < y1 + h1
end
shooting flag to prevent holding the space bar from spamming bubbles.Ensure your assets are correctly named and reside in the assets folder in the same directory as your main.lua. This structure will look like:
your_project/
main.lua
assets/
player.png
bubble.png
enemy.png
main.lua.love /path/to/your_project.This setup should work without runtime errors if your images are correctly set up. Adjust the paths accordingly if you store assets differently.
Creating a game similar to Bubble Bobble in PICO-8 involves writing code in Lua tailored specifically to PICO-8's unique environment, which includes limited screen size, color palette, and more. PICO-8 provides an easy way to make retro-style games with its built-in sprite editor, sound editor, and code editor.
Below is a simple prototype for a Bubble Bobble-like game in PICO-8. You can enhance it further by adding levels, scoring, sound effects, and other features.
Open PICO-8: Launch your PICO-8 environment.
Create a New Cartridge: Use CTRL+N or the F1 menu to create a new cartridge.
Sprites and Map: Use the sprite editor (F2) to create simple graphics for the player, bubbles, and enemies. Assign each graphic an index that you will reference in your code. Here's a simple set:
Define the Code: Use F4 to open the code editor and input the following Lua code:
-- Player setup
player = {x=64, y=64, dx=0, dy=0, sprite=1, can_shoot=true}
-- Entities
bubbles = {}
enemies = {}
-- Constants
MOVE_SPEED = 1.5
BUBBLE_SPEED = 2
ENEMY_SPEED = 0.7
SPAWN_TIME = 120
spawn_timer = 0
function _init()
create_enemy()
end
function _update()
-- Player movement
if btn(0) then player.dx = -MOVE_SPEED
elseif btn(1) then player.dx = MOVE_SPEED
else player.dx = 0 end
player.x = player.x + player.dx
-- Player shooting
if btn(4) and player.can_shoot then
shoot_bubble(player.x + 4, player.y)
player.can_shoot = false
elseif not btn(4) then
player.can_shoot = true
end
-- Update Bubbles
for i=#bubbles, 1, -1 do
local b = bubbles[i]
b.y = b.y - BUBBLE_SPEED
if b.y < 0 then
deli(bubbles, i)
end
end
-- Update Enemies
spawn_timer = spawn_timer + 1
if spawn_timer > SPAWN_TIME then
create_enemy()
spawn_timer = 0
end
for i=#enemies, 1, -1 do
local e = enemies[i]
e.y = e.y + ENEMY_SPEED
if e.y > 128 then
deli(enemies, i)
end
end
-- Collision Detection
check_collisions()
end
function _draw()
cls()
-- Draw Player
spr(player.sprite, player.x, player.y)
-- Draw Bubbles
for b in all(bubbles) do
spr(2, b.x, b.y)
end
-- Draw Enemies
for e in all(enemies) do
spr(3, e.x, e.y)
end
end
function shoot_bubble(x, y)
add(bubbles, {x=x, y=y})
end
function create_enemy()
local x = flr(rnd(128))
add(enemies, {x=x, y=0})
end
function check_collisions()
for i=#bubbles, 1, -1 do
local b = bubbles[i]
for j=#enemies, 1, -1 do
local e = enemies[j]
if b.x < e.x + 8 and b.x + 8 > e.x and b.y < e.y + 8 and b.y + 8 > e.y then
deli(bubbles, i)
deli(enemies, j)
break
end
end
end
end
Player Movement: Controlled with the left (0) and right (1) buttons. Can shoot bubbles upwards when pressing the Z button key (4).
Bubble and Enemy Updates: Bubbles move upwards and are culled if they exit the screen. Enemies spawn regularly and move downwards, being removed if they go off the screen.
Collision: A simple bounding box check removes bubbles and enemies if they collide.
Run the Game: Use CTRL+R to run your game and test the mechanics.
Sprites and Collisions: Make sure to use the correct sprite indices, and verify if they align with your designs in the sprite editor.
Enhancements: Use the built-in music editor or sound effect tool to create sounds, and continue refining gameplay by adjusting speeds, spawn times, collision logic, and add scoring or levels.
This setup is a solid foundation for a PICO-8 game, and you can expand this by adjusting different aspects of the gameplay, enhancing it with more features and content.
Not yet included in a published release.