Skip to content

Instantly share code, notes, and snippets.

@MageJohn
Last active September 30, 2018 10:24
Show Gist options
  • Select an option

  • Save MageJohn/d9fee1983f5810951472f21a3473fa41 to your computer and use it in GitHub Desktop.

Select an option

Save MageJohn/d9fee1983f5810951472f21a3473fa41 to your computer and use it in GitHub Desktop.
Simple snake game in Wren, for the TIC-80 fantasy console. Play with `tic80 snake.wren`, or place in the console's filesystem and load from there. Bear in mind there are rough edges, obvious bugs, and missing features. The basic game is there, though.
// title: Snake
// author: MageJohn
// desc: Snake game programmed in Wren
// script: wren
import "random" for Random
var HEAD = 256
var CORNER = 257
var BODY = 258
var TAIL = 259
var APPLE = 260
// Screen is 30x17 sprites
var GRID_WIDTH = 30
var GRID_HEIGHT = 17
var SCREEN_WIDTH = 240
var SCREEN_HEIGHT = 136
var TEXT_HEIGHT = 6
class XYcompare {
static xyequal(list1, list2) {
return (list1[0] == list2[0] && list1[1] == list2[1])
}
static xynotequal(list1, list2) {
return (list1[0] != list2[0] && list1[1] != list2[1])
}
}
class Snake{
construct new() {
_movements = {
0: [ 0, -1],
1: [ 0, 1],
2: [-1, 0],
3: [ 1, 0]
}
_segments = [[15, 8], [16, 8], [17, 8], [18, 8]]
_dir = _movements[2]
_new_dir = _dir
_speed = 30 // ticks before update
_logger = Logger.new(this.type.name)
_extend = false
}
speed { _speed }
speed=(value) { _speed=value }
segments { _segments }
input() {
var dir = null
for (i in 0...4) {
if (TIC.btn(i)) {
dir = _movements[i]
if ( !(_dir[0] + dir[0] == 0 && _dir[1] + dir[1] == 0) ) {
_new_dir = dir
}
}
}
}
move() {
_dir = _new_dir
var headPos = _segments[0][0..-1] // copy of first element
headPos[0] = headPos[0] + _dir[0]
headPos[1] = headPos[1] + _dir[1]
_segments.insert(0, headPos)
if (!_extend) {
_segments.removeAt(-1)
}
_extend = false
}
collision {
for (seg in _segments[1..-1]) {
if (XYcompare.xyequal(_segments[0], seg)) {
return true
}
}
if ((_segments[0][0] == -1 || _segments[0][0] == GRID_WIDTH) ||
(_segments[0][1] == -1 || _segments[0][1] == GRID_HEIGHT)) {
return true
}
return false
}
draw() {
var seg
var going
var coming
var rotation
var flip
// The keys are hashable representation of the values from _movements.
// The elements have 1 added to them.
var rotations = {
"10": 1,
"12": 3,
"01": 0,
"21": 2
}
var flips = {
"1001": 1,
"1021": 0,
"1201": 0,
"1221": 1,
"0110": 0,
"0112": 2,
"2110": 2,
"2121": 0
}
// TIC.spr id x y [colorkey=-1] [scale=1] [flip=0] [rotate=0] [w=1 h=1]
TIC.spr(
HEAD,
_segments[0][0] * 8,
_segments[0][1] * 8,
0, 1, 0,
rotations["%(_dir[0]+1)%(_dir[1]+1)"]
)
for (i in 1..(_segments.count - 2)) {
seg = _segments[i]
going = this.direction(_segments[i-1], seg)
coming = this.direction(seg, _segments[i+1])
if (XYcompare.xynotequal(coming, going)) { // wren can't compare list equality?!
rotation = rotations["%(going[0]+1)%(going[1]+1)"]
flip = flips["%(going[0]+1)%(going[1]+1)%(coming[0]+1)%(coming[1]+1)"]
TIC.spr(
CORNER,
seg[0] * 8,
seg[1] * 8,
0, 1,
flip,
rotation
)
} else {
TIC.spr(
BODY,
seg[0] * 8,
seg[1] * 8,
0, 1, 0,
going[0] != 0 ? 0 : 1
)
}
}
seg = _segments[-1] // tail
going = this.direction(_segments[-2], seg)
TIC.spr(
TAIL,
seg[0] * 8,
seg[1] * 8,
0, 1, 0,
rotations["%(going[0]+1)%(going[1]+1)"]
)
}
extend() {
_extend = true
}
direction(seg1, seg2) {
return [seg1[0] - seg2[0], seg1[1] - seg2[1]]
}
}
class Apple {
construct new() {
_random = Random.new()
this.move()
}
location { _location }
move() {
_location = [_random.int(GRID_WIDTH), _random.int(GRID_HEIGHT)]
}
draw() {
TIC.spr(
APPLE,
_location[0] * 8,
_location[1] * 8,
0
)
}
}
class Game is TIC{
construct new(){
_logger = Logger.new(this.type.name)
this.newGame()
}
newGame() {
TIC.cls(0)
_snake = Snake.new()
_apple = Apple.new()
_ticks = 0
_mode = "playing"
_objects = [_snake, _apple]
}
playing() {
// game objects update
_ticks = _ticks + 1
_snake.input()
if (_ticks % _snake.speed == 0) {
_snake.move()
}
if (XYcompare.xyequal(_snake.segments[0], _apple.location)) {
_snake.extend()
_apple.move()
if (_snake.segments.count % 3 == 0) {
_snake.speed = (_snake.speed * 0.7).ceil
}
}
// screen update
TIC.cls(0)
for (ob in _objects) {
ob.draw()
}
if (_snake.collision) {
_mode = "game over"
return
}
}
gameOver() {
// print offscreen to get text width
var width = TIC.print("GAME OVER", SCREEN_WIDTH, SCREEN_HEIGHT)
TIC.print("GAME OVER", ((SCREEN_WIDTH - width) / 2).ceil, ((SCREEN_HEIGHT - TEXT_HEIGHT) / 2).ceil)
for (i in 0..8) {
if (TIC.btn(i)) {
_mode = "new game"
return
}
}
}
TIC() {
if (_mode == "playing") {
this.playing()
} else if (_mode == "game over") {
this.gameOver()
} else if (_mode == "new game") {
this.newGame()
}
}
}
class Logger {
construct new(location) {
_loc = location
}
log(message) {
System.print(_loc + ": " + message)
}
}
// <SPRITES>
// 000:000000000000bbb000bb2bbb0bbbbb5b0bbbbbb500bb2bbb0000bbb000000000
// 001:000000005bbbb000bbbbbb005b5bbbb0b5b5bbb0bbbb5bb055b5bbb005bb5b50
// 002:000000005bbbbbb5bbbbbbbb5b5b5b5bb5b5b5b5bbbbbbbb5bbbbbb500000000
// 003:0000000000000000bb000000bbbbb000bbbbbb00bb0000000000000000000000
// 004:0000000000000000000000000006600000066000000000000000000000000000
// </SPRITES>
// <WAVES>
// 000:00000000ffffffff00000000ffffffff
// 001:0123456789abcdeffedcba9876543210
// 002:0123456789abcdef0123456789abcdef
// </WAVES>
// <SFX>
// 000:000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000304000000000
// </SFX>
// <PALETTE>
// 000:140c1c44243430346d4e4a4e854c30346524d61800757161597dced27d2c8595a16daa2cd2aa996dc2cadad45edeeed6
// </PALETTE>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment