Skip to content

Commit

Permalink
Merge pull request #443 from JD557/update-example-versions
Browse files Browse the repository at this point in the history
Update examples to 0.6.0-M1
  • Loading branch information
JD557 authored Nov 17, 2023
2 parents 510940a + a315bf0 commit dc0fb6d
Show file tree
Hide file tree
Showing 24 changed files with 115 additions and 168 deletions.
6 changes: 3 additions & 3 deletions examples/release/01-color-square.sc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//> using scala "3.3.1"
//> using lib "eu.joaocosta::minart::0.5.3"
//> using lib "eu.joaocosta::minart::0.6.0-M1"

/*
* Welcome to the minart tutorial!
Expand All @@ -18,8 +18,8 @@
*
* Since we want to work with graphics, we also need to import eu.joaocosta.minart.graphics._
*/
import eu.joaocosta.minart.backend.defaults._
import eu.joaocosta.minart.graphics._
import eu.joaocosta.minart.backend.defaults.given
import eu.joaocosta.minart.graphics.*

/*
* The Canvas.Settings are the settings of our window.
Expand Down
8 changes: 4 additions & 4 deletions examples/release/02-portable-color-square.sc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//> using scala "3.3.1"
//> using lib "eu.joaocosta::minart::0.5.3"
//> using lib "eu.joaocosta::minart::0.6.0-M1"

/*
* In this next example, we are going to do draw the same colored square, but now in a portable way.
Expand All @@ -8,9 +8,9 @@
/*
* We start with the same imports and canvas settings
*/
import eu.joaocosta.minart.backend.defaults._
import eu.joaocosta.minart.graphics._
import eu.joaocosta.minart.runtime._
import eu.joaocosta.minart.backend.defaults.given
import eu.joaocosta.minart.graphics.*
import eu.joaocosta.minart.runtime.*

val canvasSettings = Canvas.Settings(width = 128, height = 128, scale = Some(4))

Expand Down
8 changes: 4 additions & 4 deletions examples/release/03-fire-animation.sc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//> using scala "3.3.1"
//> using lib "eu.joaocosta::minart::0.5.3"
//> using lib "eu.joaocosta::minart::0.6.0-M1"

/*
* In the previous examples we just drew a static image on the screen.
Expand All @@ -13,9 +13,9 @@
* This time we also need to import the runtime. This package contains some helpful methods and objects to handle
* render loops, such as the LoopFrequency.
*/
import eu.joaocosta.minart.backend.defaults._
import eu.joaocosta.minart.graphics._
import eu.joaocosta.minart.runtime._
import eu.joaocosta.minart.backend.defaults.given
import eu.joaocosta.minart.graphics.*
import eu.joaocosta.minart.runtime.*

val canvasSettings = Canvas.Settings(width = 128, height = 128, scale = Some(4))

Expand Down
10 changes: 5 additions & 5 deletions examples/release/04-pointer.sc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//> using scala "3.3.1"
//> using lib "eu.joaocosta::minart::0.5.3"
//> using lib "eu.joaocosta::minart::0.6.0-M1"

/*
* Now that we learned the basics of animation, we can start to look at more dynamic applications.
Expand All @@ -11,10 +11,10 @@
* First our imports. Note the addition of eu.joaocosta.minart.input.
* We'll need this to handle our inputs, such as keyboard and mouse.
*/
import eu.joaocosta.minart.backend.defaults._
import eu.joaocosta.minart.graphics._
import eu.joaocosta.minart.input._
import eu.joaocosta.minart.runtime._
import eu.joaocosta.minart.backend.defaults.given
import eu.joaocosta.minart.graphics.*
import eu.joaocosta.minart.input.*
import eu.joaocosta.minart.runtime.*

/** Note the adition of clearColor here.
* This is the color of the canvas when nothing is drawn.
Expand Down
37 changes: 18 additions & 19 deletions examples/release/05-snake-game.sc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//> using scala "3.3.1"
//> using lib "eu.joaocosta::minart::0.5.3"
//> using lib "eu.joaocosta::minart::0.6.0-M1"

/*
* Now that we learned the basics of animation and input handling, we are almost ready to make a game.
Expand All @@ -12,30 +12,29 @@
*/
import scala.util.Random

import eu.joaocosta.minart.backend.defaults._
import eu.joaocosta.minart.graphics._
import eu.joaocosta.minart.input._
import eu.joaocosta.minart.runtime._
import eu.joaocosta.minart.backend.defaults.given
import eu.joaocosta.minart.graphics.*
import eu.joaocosta.minart.input.*
import eu.joaocosta.minart.runtime.*

/*
* Again, let's define our canvas settings.
* This time we'll use a smaller resolution with a larger scale.
*/
val canvasSettings = Canvas.Settings(width = 32, height = 32, scale = Some(16), clearColor = Color(0, 0, 0))

/**
* Then we define our game logic, this is an implementation of the classic snake game.
*/
/** Then we define our game logic, this is an implementation of the classic snake game.
*/
def mod(x: Int, y: Int): Int =
if (x >= 0) x % y
else mod(x + y, y)

final case class Position(x: Int, y: Int)

enum Direction(val x: Int, val y: Int) {
case Up extends Direction(0, -1)
case Down extends Direction(0, 1)
case Left extends Direction(-1, 0)
case Up extends Direction(0, -1)
case Down extends Direction(0, 1)
case Left extends Direction(-1, 0)
case Right extends Direction(1, 0)
}

Expand All @@ -51,15 +50,15 @@ final case class GameState(
lazy val boardHeight = board.size

lazy val nextState: GameState = {
val newApplePos =
val newApplePos =
if (applePos == snakeHead) Position(Random.nextInt(boardWidth - 1), Random.nextInt(boardHeight - 1))
else applePos
val newSnakeSize =
if (applePos == snakeHead) snakeSize + 1
else snakeSize
val newBoard = board
.updated(snakeHead.y, board(snakeHead.y).updated(snakeHead.x, snakeSize))
.map(_.map(life => Math.max(0, life - 1)))
.updated(snakeHead.y, board(snakeHead.y).updated(snakeHead.x, snakeSize))
.map(_.map(life => Math.max(0, life - 1)))
copy(
board = newBoard,
snakeHead = Position(mod(snakeHead.x + snakeDir.x, boardWidth), mod(snakeHead.y + snakeDir.y, boardHeight)),
Expand Down Expand Up @@ -92,8 +91,8 @@ val initialState = GameState(Vector.fill(canvasSettings.height)(Vector.fill(canv
* and it returns the next state.
*/
AppLoop
.statefulRenderLoop(
(state: GameState) => (canvas: Canvas) => {
.statefulRenderLoop((state: GameState) =>
(canvas: Canvas) => {
canvas.clear()

// We draw the snake body
Expand All @@ -108,7 +107,7 @@ AppLoop

// Then a nice red apple
canvas.putPixel(state.applePos.x, state.applePos.y, Color(255, 0, 0))

// Then redraw the canvas
canvas.redraw()

Expand All @@ -117,11 +116,11 @@ AppLoop
*/
if (state.gameOver) initialState
else state.updateSnakeDir(canvas.getKeyboardInput()).nextState
},
}
)
.configure(
canvasSettings,
LoopFrequency.fromHz(15), // 15 FPS
initialState // Notice that the configure function now needs an initialState
initialState // Notice that the configure function now needs an initialState
)
.run()
8 changes: 4 additions & 4 deletions examples/release/06-surface-blit.sc
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
//> using scala "3.3.1"
//> using lib "eu.joaocosta::minart::0.5.3"
//> using lib "eu.joaocosta::minart::0.6.0-M1"

/*
* Writing directly to a canvas pixel by pixel worked fine in the previous examples, but
* sometimes it's helpful to store some full images to draw.
* This is where surfaces come in.
*/

import eu.joaocosta.minart.backend.defaults._
import eu.joaocosta.minart.graphics._
import eu.joaocosta.minart.runtime._
import eu.joaocosta.minart.backend.defaults.given
import eu.joaocosta.minart.graphics.*
import eu.joaocosta.minart.runtime.*

val canvasSettings = Canvas.Settings(width = 128, height = 128, scale = Some(4))

Expand Down
8 changes: 4 additions & 4 deletions examples/release/07-canvas-settings.sc
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//> using scala "3.3.1"
//> using lib "eu.joaocosta::minart::0.5.3"
//> using lib "eu.joaocosta::minart::0.6.0-M1"

/** On some occasions, we might need to change our canvas settings.
* Here's how to do it.
*/
import eu.joaocosta.minart.backend.defaults._
import eu.joaocosta.minart.graphics._
import eu.joaocosta.minart.backend.defaults.given
import eu.joaocosta.minart.graphics.*
import eu.joaocosta.minart.input.KeyboardInput.Key
import eu.joaocosta.minart.runtime._
import eu.joaocosta.minart.runtime.*

/** Let's define some settings.
* Note that one of those will actually go fullScreen.
Expand Down
10 changes: 5 additions & 5 deletions examples/release/09-image.sc → examples/release/08-image.sc
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
//> using scala "3.3.1"
//> using lib "eu.joaocosta::minart::0.5.3"
//> using lib "eu.joaocosta::minart::0.6.0-M1"

/*
* It is sometimes convenient to load images from external resources.
* The minart-image library and graphics.image package allows one to do just that.
*/
import eu.joaocosta.minart.backend.defaults._
import eu.joaocosta.minart.graphics._
import eu.joaocosta.minart.graphics.image._
import eu.joaocosta.minart.runtime._
import eu.joaocosta.minart.backend.defaults.given
import eu.joaocosta.minart.graphics.*
import eu.joaocosta.minart.graphics.image.*
import eu.joaocosta.minart.runtime.*

val canvasSettings = Canvas.Settings(width = 128, height = 128, scale = Some(4))

Expand Down
56 changes: 0 additions & 56 deletions examples/release/08-pure-color-square.scala

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//> using scala "3.3.1"
//> using lib "eu.joaocosta::minart::0.5.3"
//> using lib "eu.joaocosta::minart::0.6.0-M1"

/*
* It can be quite cumbersome an ineficient to apply multiple transformations to a surface if we just use the getPixel
Expand All @@ -9,10 +9,10 @@
* intermediate results.
* This tutorial will show how to use those
*/
import eu.joaocosta.minart.backend.defaults._
import eu.joaocosta.minart.graphics._
import eu.joaocosta.minart.graphics.image._
import eu.joaocosta.minart.runtime._
import eu.joaocosta.minart.backend.defaults.given
import eu.joaocosta.minart.graphics.*
import eu.joaocosta.minart.graphics.image.*
import eu.joaocosta.minart.runtime.*

// First, let's load our example scala logo image
val canvasSettings = Canvas.Settings(width = 128, height = 128, scale = Some(4), clearColor = Color(0, 0, 0))
Expand Down
32 changes: 17 additions & 15 deletions examples/release/11-audio.sc → examples/release/10-audio.sc
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
//> using scala "3.3.1"
//> using lib "eu.joaocosta::minart::0.5.3"
//> using lib "eu.joaocosta::minart::0.6.0-M1"

/** Here we'll see how to generate and play audio
*
* Note: This is an experimental API, it might break in a future version
*/
import eu.joaocosta.minart.audio._
import eu.joaocosta.minart.graphics._
import eu.joaocosta.minart.runtime._
import eu.joaocosta.minart.input._
import eu.joaocosta.minart.backend.defaults._
import eu.joaocosta.minart.audio.*
import eu.joaocosta.minart.backend.defaults.given
import eu.joaocosta.minart.backend.subsystem.*
import eu.joaocosta.minart.graphics.*
import eu.joaocosta.minart.input.*
import eu.joaocosta.minart.runtime.*

/** First, let's define a simple song
*/
Expand Down Expand Up @@ -44,18 +45,19 @@ object Audio {

// Here we use `statelessAppLoop` so that we get an object with a `canvas` and an `audioPlayer`
AppLoop
.statelessAppLoop((system: Canvas with AudioPlayer) => {
.statelessAppLoop((system: CanvasSubsystem with AudioPlayerSubsystem) => {
import system._
// When someone presses "Space", we send our sound wave to the queue
if (system.getKeyboardInput().keysPressed.contains(KeyboardInput.Key.Space))
system.play(Audio.testSample)
if (canvas.getKeyboardInput().keysPressed.contains(KeyboardInput.Key.Space))
audioPlayer.play(Audio.testSample)
// When someone presses "Backspace", we stop the audio player
if (system.getKeyboardInput().keysPressed.contains(KeyboardInput.Key.Backspace))
system.stop()
system.clear()
if (canvas.getKeyboardInput().keysPressed.contains(KeyboardInput.Key.Backspace))
audioPlayer.stop()
canvas.clear()
// Paint green when nothing is playing and red otherwise
if (!system.isPlaying()) system.fill(Color(0, 128, 0))
else system.fill(Color(128, 0, 0))
system.redraw()
if (!audioPlayer.isPlaying()) canvas.fill(Color(0, 128, 0))
else canvas.fill(Color(128, 0, 0))
canvas.redraw()
})
.configure((Canvas.Settings(width = 128, height = 128), AudioPlayer.Settings()), LoopFrequency.hz60)
.run()
Loading

0 comments on commit dc0fb6d

Please sign in to comment.