Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions source-code/ninja-thief/episode-1/32blit-lua/assets.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# 32blit pack --force --config assets.yml --output assets

background.blim:
type: raw_binary
assets/background.png:

spritesheet.blim:
type: raw_binary
assets/spritesheet.png:
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions source-code/ninja-thief/episode-1/32blit-lua/constants.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Constants = {
-- Screen size in pixels
SCREEN_WIDTH = 160,
SCREEN_HEIGHT = 120,

-- Actual game area in pixels
GAME_WIDTH = 120,
GAME_HEIGHT = 120,

-- Each sprite on the spritesheet is 8x8 pixels
SPRITE_SIZE = 8,
}

12 changes: 12 additions & 0 deletions source-code/ninja-thief/episode-1/32blit-lua/metadata.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# 32blit metadata --config metadata.yml --metadata-file ninja_thief.blmeta

title: Ninja Thief
description: A tutorial game for the 32blit and PicoSystem. Collect all the coins while avoiding the enemy ninjas!
author: ThePythonator
splash:
file: assets/logo.png
icon:
file: assets/icon.png
version: v1.0.0
category: game
url: https://github.com/32blit/32blit-tutorials-contrib
55 changes: 55 additions & 0 deletions source-code/ninja-thief/episode-1/32blit-lua/ninja_thief.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require "constants"

-- Outside of our functions, just after the includes
background = nil

-- Setup your game here
function init()
-- Set the resolution to 160x120
set_screen_mode(ScreenMode.lores)

-- Load the background from assets.cpp
background = Surface.load("assets/background.blim")

-- Load the spritesheet
screen.load_sprites("assets/spritesheet.blim")
end

-- This function is called to perform rendering of the game.
function render(time)
-- Clear the screen
screen.pen = Pen(0, 0, 0)
screen.clear()

-- Draw the entire background image onto the screen at (0, 0)
screen.blit(background, Rect(0, 0, Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT), Point(0, 0))

-- Set the text colour to white
screen.pen = Pen(255, 255, 255)

-- Render the placeholder score text
screen.text("Score: 0", minimal_font, Point(2, 2))

-- Example primitive shape drawing code:
--[[
-- Draw a red (filled) rectangle
screen.pen = Pen(255, 0, 0)
screen.rectangle(Rect(10, 10, 50, 20))

-- Draw a blue (filled) circle
screen.pen = Pen(0, 0, 255)
screen.circle(Point(20, 40), 10)

-- Draw a green diagonal line from top left to bottom right
screen.pen = Pen(0, 255, 0)
screen.line(Point(0, 0), Point(120, 120))
]]--

-- Draw the first blue player sprite (index 32) at (50, 50)
screen.sprite(32, Point(50, 50))
end

-- This is called to update your game state.
function update(time)

end
9 changes: 9 additions & 0 deletions source-code/ninja-thief/episode-2/32blit-lua/assets.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# 32blit pack --force --config assets.yml --output assets

background.blim:
type: raw_binary
assets/background.png:

spritesheet.blim:
type: raw_binary
assets/spritesheet.png:
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
55 changes: 55 additions & 0 deletions source-code/ninja-thief/episode-2/32blit-lua/constants.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
Constants = {
-- Screen size in pixels
SCREEN_WIDTH = 160,
SCREEN_HEIGHT = 120,

-- Actual game area in pixels
GAME_WIDTH = 120,
GAME_HEIGHT = 120,

-- Each sprite on the spritesheet is 8x8 pixels
SPRITE_SIZE = 8,
}

-- Offset of game area from top left corner
Constants.GAME_OFFSET_X = (Constants.SCREEN_WIDTH - Constants.GAME_WIDTH) / 2
Constants.GAME_OFFSET_Y = (Constants.SCREEN_HEIGHT - Constants.GAME_HEIGHT) / 2

-- Sprite data, including indices to use for rendering
Constants.Sprites = {
-- Offset of the red ninja sprites from the blue ninja sprites
RED_OFFSET = 4,

-- We'll only be using PLAYER_IDLE for now
PLAYER_IDLE = 32,
PLAYER_CLIMBING_IDLE = 33,
PLAYER_CLIMBING_1 = 34,
PLAYER_CLIMBING_2 = 35,
PLAYER_WALKING_1 = 40,
PLAYER_WALKING_2 = 41,
PLAYER_JUMPING_UP = 42,
PLAYER_JUMPING_DOWN = 43,

LADDER = 11,

COIN = 19,
GEM = 18,

-- These will be used to draw a border either side of the screen, to make the game area 120x120
BORDER_LEFT = 10,
BORDER_FULL = 9,
BORDER_RIGHT = 8,
}

-- Player data such as speeds
Constants.Player = {
-- Speeds are measured in pixels per second
MAX_SPEED = 50.0,

JUMP_SPEED = 125.0,
}

-- Environment data such as gravity strength
Constants.Environment = {
GRAVITY_ACCELERATION = 375.0,
}
12 changes: 12 additions & 0 deletions source-code/ninja-thief/episode-2/32blit-lua/metadata.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# 32blit metadata --config metadata.yml --metadata-file ninja_thief.blmeta

title: Ninja Thief
description: A tutorial game for the 32blit and PicoSystem. Collect all the coins while avoiding the enemy ninjas!
author: ThePythonator
splash:
file: assets/logo.png
icon:
file: assets/icon.png
version: v1.0.0
category: game
url: https://github.com/32blit/32blit-tutorials-contrib
59 changes: 59 additions & 0 deletions source-code/ninja-thief/episode-2/32blit-lua/ninja.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
require "constants"

HorizontalDirection = {
LEFT = -1,
RIGHT = 1,
}

VerticalDirection = {
UP = -1,
DOWN = 1,
}

Ninja = {
position_x = 0,
posiiton_y = 0,
velocity_x = 0,
velocity_y = 0,

facing_direction = HorizontalDirection.LEFT,
}

function Ninja:new(x, y)
local o = {position_x = x, position_y = y}

setmetatable(o, self)
self.__index = self
return o
end

function Ninja:update(dt)
-- Before we update the position, update the velocity
self.velocity_y = self.velocity_y + Constants.Environment.GRAVITY_ACCELERATION * dt

-- Move the ninja
self.position_x = self.position_x + self.velocity_x * dt
self.position_y = self.position_y + self.velocity_y * dt

-- Update direction the ninja is facing (only if the player is moving)
if self.velocity_x < 0.0 then
self.facing_direction = HorizontalDirection.LEFT
elseif self.velocity_x > 0.0 then
self.facing_direction = HorizontalDirection.RIGHT
end
end

function Ninja:render()
-- If ninja is travelling left, flip the image horizontally
local transform = SpriteTransform.NONE
if self.facing_direction == HorizontalDirection.LEFT then
transform = SpriteTransform.HORIZONTAL
end

screen.sprite(Constants.Sprites.PLAYER_IDLE, Point(math.floor(self.position_x + 0.5) + Constants.GAME_OFFSET_X, math.floor(self.position_y + 0.5) + Constants.GAME_OFFSET_Y), transform)
end

function Ninja:jump(jump_speed)
-- Upwards is negative
self.velocity_y = -jump_speed
end
56 changes: 56 additions & 0 deletions source-code/ninja-thief/episode-2/32blit-lua/ninja_thief.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require "constants"
require "player_ninja"

-- Our global variables are defined here
background = nil

last_time = 1.0

-- Temporary player object for testing
player = PlayerNinja:new(50, 50)

-- Setup your game here
function init()
-- Set the resolution to 160x120
set_screen_mode(ScreenMode.lores)

-- Load the background from assets.cpp
background = Surface.load("assets/background.blim")

-- Load the spritesheet
screen.load_sprites("assets/spritesheet.blim")
end

-- This function is called to perform rendering of the game.
function render(time)
-- Clear the screen
screen.pen = Pen(0, 0, 0)
screen.clear()

-- Draw the entire background image onto the screen at (0, 0)
screen.blit(background, Rect(0, 0, Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT), Point(0, 0))

-- Set the text colour to white
screen.pen = Pen(255, 255, 255)

-- Render the placeholder score text
screen.text("Score: 0", minimal_font, Point(2, 2))

-- Render the player
player:render()
end

-- This is called to update your game state.
function update(time)
-- Calculate change in time (in seconds) since last frame
local dt = (time - last_time) / 1000.0
last_time = time

-- Limit dt
if dt > 0.05 then
dt = 0.05
end

-- Update the player
player:update(dt)
end
30 changes: 30 additions & 0 deletions source-code/ninja-thief/episode-2/32blit-lua/player_ninja.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require "ninja"

PlayerNinja = Ninja:new()

function PlayerNinja:update(dt)
-- If nothing is pressed, the player shouldn't move
self.velocity_x = 0.0

-- Note: "else if" isn't used, because otherwise the sprite will still move when both buttons are pressed
-- Instead, we add/subtract the velocity, so if both are pressed, nothing happens
if buttons.state & Button.LEFT ~= 0 then
self.velocity_x = self.velocity_x - Constants.Player.MAX_SPEED
end
if buttons.state & Button.RIGHT ~= 0 then
self.velocity_x = self.velocity_x + Constants.Player.MAX_SPEED
end

-- Handle jumping
-- Note that we use buttons.pressed, which only contains the buttons just pressed (since the last frame)
if buttons.pressed & Button.A ~= 0 then
self:jump(Constants.Player.JUMP_SPEED)
end

Ninja.update(self, dt)

-- Temporary addition to stop player falling off bottom of screen
if self.position_y > Constants.GAME_HEIGHT - Constants.SPRITE_SIZE then
self.position_y = Constants.GAME_HEIGHT - Constants.SPRITE_SIZE
end
end
9 changes: 9 additions & 0 deletions source-code/ninja-thief/episode-3/32blit-lua/assets.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# 32blit pack --force --config assets.yml --output assets

background.blim:
type: raw_binary
assets/background.png:

spritesheet.blim:
type: raw_binary
assets/spritesheet.png:
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading