Skip to content

Instantly share code, notes, and snippets.

@litan
litan / arduino-sample.kojo
Created Sep 12, 2017
Controlling an arduino board
View arduino-sample.kojo
// #include ~/kojo-includes/ka-bridge.kojo
def setup() {
pinMode(2, INPUT)
pinMode(3, OUTPUT)
pinMode(4, OUTPUT)
pinMode(5, OUTPUT)
}
def loop() {
@litan
litan / simple-game.kojo
Last active Sep 12, 2017
A simple game
View simple-game.kojo
// a simple game - you need to keep the rectangle within the canvas
// the rectangle moves and grows in size; its speed goes up as its size increases
// you can rotate it by pressing the 'P' key; you can make it smaller by clicking on it
cleari()
drawStage(black)
val rect = fillColor(lightGray) * penColor(lightGray) -> PicShape.rect(40, 60)
draw(rect)
animate {
rect.translate(1, 0)
rect.scale(1.001)
@litan
litan / shape-tree.kojo
Created Sep 12, 2017
A tree with shapes
View shape-tree.kojo
Shape.clear()
setBackgroundH(black, blue)
val (height, breadth) = (100, 20)
def stem = Shape.rectangle(breadth, height).outlined(lightGray).filled(lightGray)
def tree(n: Double): Shape = {
if (n == 0)
stem
else
// on2 places shapes on each other without centering
tree(n-1) .rotated(30) .scaled(0.7) .translated(0, height * 0.95) on2
@litan
litan / shape-face.kojo
Created Sep 12, 2017
A ‘shape’ drawing
View shape-face.kojo
Shape.clear()
def r(w: Int, h: Int) = Shape.rectangle(w, h) .outlined(black)
def sq(l: Int) = r(l, l)
def vgap(l: Int) = Shape.gap(0, l)
def hgap(l: Int) = Shape.gap(l, 0)
val eyes = sq(50) beside hgap(100) beside sq(50) filled(lightGray)
val nose = r(30, 100) .filled(orange)
val mouth = r(100, 20) .filled(red)
View repeat-pattern.kojo
clear()
setSpeed(fast)
setPenColor(darkGray)
repeat(18) {
forward(100)
right(100)
}
@litan
litan / ScalariformTokenMaker.scala
Created Nov 21, 2012
A Scalariform based Lexer for RSyntaxTextArea
View ScalariformTokenMaker.scala
/*
* Copyright (C) 2012 Lalit Pant <pant.lalit@gmail.com>
*
* The contents of this file are subject to the GNU General Public License
* Version 3 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.gnu.org/copyleft/gpl.html
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
@litan
litan / LSystem-Kojo.scala
Created May 20, 2012
Playing with LSystems in Kojo
View LSystem-Kojo.scala
case class LSystem(axiom: String, angle: Double, len: Int = 100, sf: Double = 0.6)(rules: PartialFunction[Char, String]) {
var currVal = axiom
var currGen = 0
def evolve() {
currGen += 1
currVal = currVal.map { c =>
if (rules.isDefinedAt(c)) rules(c) else c
}.mkString.replaceAll("""\|""" , currGen.toString)
}