Tiny linux games made with scripting languages

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
kaeza
Posts: 51
Joined: Thu 01 Dec 2011, 02:49

Tiny linux games made with scripting languages

#1 Post by kaeza »

Hello.

I created this thread so people can show some interesting games made using scripting languages.

The basic rules are:

- The code must be in a scripting language, be it bash, perl, lua, python, or whatever you can think of.

- The code must fit in a single post. You are not allowed to split the game into multiple posts or add attachments.

- Also related to the above rule, the less lines the better.

- The code you post here must be written by you, and by making it available here you put it in the public domain.

That's about it I think.

Heres one to get you started.

Code: Select all

#! /bin/sh

ntries=0;
targetnum=$(((RANDOM%10)+1));

while [ $ntries -lt 10 ]; do
    echo -n "Type a number between 1 and 10 inclusive:";
    read num;
    test "$num" || exit;
    num=$((num));
    ntries=$((ntries+1));
    if [ "$num" -eq $targetnum ]; then
        echo "Congrats pal! You got it in $ntries attempts!";
        exit;
    elif [ "$((num))" -gt $targetnum ]; then
        echo "Target number is lower.";
    else
        echo "Target number is higher.";
    fi
done

echo "You didn't make it in 10 attempts...";
[i]"Courage is not the absence of fear, but rather the judgement that something else is more important than fear."[/i] -- Ambrose Redmoon

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

Re: Tiny linux games

#2 Post by technosaurus »

kaeza wrote:

Code: Select all

#! /bin/sh

ntries=0;
targetnum=$(((RANDOM%10)+1));
...
$RANDOM is a bashism, so should be #! /bin/bash or something really hacky using /dev/random

space invaders would actually be possible in shell using color formatting and storing positions of objects in a bash array to store the positions/conditions of the "sprites" ... mazes, snake, tetris are the easier ones but defender and galaga are even scriptable

I have actually been contemplating writing a mini game engine in shell that simply modifies an svg image (I have have code for automatiacally refreshing an image when the file is modified as part of my simple icon tray - which, btw, I PD'd along with the most permissive license I could come up with for locations that don't recognize public domain ) Pretty much any 2d game you could think of could be rendered using a single svg thanks to "include", but I have not seen too many PD svgs around, so I have been collecting ISC/MIT/BSD/UIUC licensed ones that I come across.
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

kaeza
Posts: 51
Joined: Thu 01 Dec 2011, 02:49

#3 Post by kaeza »

okay, that would be cool.
But the idea of this post is to make something like Speedhack.

I'm working myself in some tiny platform game using ANSI escape sequences for graphics. I don't know if every (virtual) terminal out there supports these, but I'll post it when I fix some minor things.
[i]"Courage is not the absence of fear, but rather the judgement that something else is more important than fear."[/i] -- Ambrose Redmoon

User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

#4 Post by don570 »

A game I recommend is 'Columns'

J-K-L keys are used for movement

It's possible to run it in mini vMac emulation.

I put it in the archive package to download. Everything
needed is in the package. Follow instructions.

http://murga-linux.com/puppy/viewtopic.php?p=639450

_______________________________________

User avatar
darkcity
Posts: 2534
Joined: Sun 23 May 2010, 19:16
Location: near here
Contact:

#5 Post by darkcity »

can it run on Bash?

heres a pointless script-

Code: Select all

#! /bin/bash
#define board pieces
  strpieceX=".XXXXXXXXXXXXXXXXXXXXXXXXXXXX."
strpiece[0]=".X.....X......" && strpiece[1]="............X."
strpiece[2]=".X..X........." && strpiece[3]="......XX......"
strpiece[4]=".X..........X." && strpiece[5]=".........X..X."
strpiece[6]=".X............" && strpiece[7]="......X.....X."
strpiece[8]="......X..X...." && strpiece[9]="....X..X......"

#set board
for (( i=0; i <= 9; i++ ))
do
  stpiece[i]="X"${strpiece[$((RANDOM%10))]}${strpiece[$((RANDOM%10))]}"X"
done
#greeting
echo
echo Hello, I am the random Monkey Man!
echo
echo $strpieceX
for (( i=0; i <= 9; i++ ))
do
 printf ${stpiece[$i]}
 echo 
done
echo $strpieceX
echo 
#monkey business
echo -n "Can you guess the name of the map?  ";
read str;
if [ $((RANDOM%2)) -eq 0 ] 
then
  echo "I can not believe you guessed '"$str"'."
  if [ $((RANDOM%2)) -eq 0 ] ; then echo "very bad!"; fi
else
  echo -n "Correct answer..."
  if [ $((RANDOM%2)) -eq 0 ] 
  then
    echo "but no gold for you, sorry."
  else
    echo "take 10 golden coins, well done, your welcome."    
  fi
fi
echo Now be on your way!!!
echo 

User avatar
SFR
Posts: 1800
Joined: Wed 26 Oct 2011, 21:52

#6 Post by SFR »

How about Mastermind?
I'm not a big fan of this game, but I can't come up with anything more interesting right now...
Here's my crude, textual version:

Code: Select all

#!/bin/bash

# Simple implementation of Mastermind by SFR'2012

# ===========================================
SET=A-F			# Set of letters, max. A-Z
LENGHT=4		# Lenght of the code, max. 10
ATTEMPTS=12		# Number of tries
# ===========================================

C='!!!!!!!!!!'
W='??????????'
U='..........'
LEN=$(($LENGHT-1))

for i in `seq 0 $LEN`; do CODE[$i]=`cat /dev/urandom | tr -dc $SET | head -c 1`; done

clear
echo "The $LENGHT-character code out of $SET set has been generated."
echo "The code may contain repetitions."
echo "Try to guess it within $ATTEMPTS attempts!"
echo
echo "! - indicates the existence of a correct letter in the correct position."
echo "? - indicates the existence of a correct letter in the wrong position."
echo
echo "Lenght: $LENGHT   Set: $SET   Attempts: $ATTEMPTS"
echo

N=1

while [ $N -le $(($ATTEMPTS)) ]; do

  while true; do
    read -p "Attempt $N: " KEY
    [ "$KEY" = "." ] && exit
    TEST=`echo $KEY | tr [:lower:] [:upper:] | tr -dc $SET`
    [ "${#TEST}" -eq $LENGHT ] && break
    echo -e "\033[2A"
  done

  CODE_TEMP=( "${CODE[@]}" )
  for i in `seq 0 $LEN`; do KEY_TEMP[$i]=`echo "${TEST:$i:1}"`; done

  CORRECT_POS=0
  for i in `seq 0 $LEN`; do
    if [ "${CODE_TEMP[$i]}" = "${KEY_TEMP[$i]}" ]; then
      CORRECT_POS=$(($CORRECT_POS+1))
      CODE_TEMP[$i]=".1"; KEY_TEMP[$i]=".2"
    fi
  done
  
  WRONG_POS=0
  for i in `seq 0 $LEN`; do
    for j in `seq 0 $LEN`; do
      if [ "${CODE_TEMP[$i]}" = "${KEY_TEMP[$j]}" ]; then
        WRONG_POS=$(($WRONG_POS+1))
        CODE_TEMP[$i]=".1"; KEY_TEMP[$j]=".2"
      fi
    done
  done
  
  [ "${#N}" -eq 1 ] && N="0"$N
  echo -ne "\n\033[2A"$N") "${TEST[@]}" : "
  echo "$C" | head -c $CORRECT_POS; echo "$W" | head -c $WRONG_POS; echo "$U" | head -c $(($LENGHT-$CORRECT_POS-$WRONG_POS))
  echo "           "
  
  [ "$CORRECT_POS" = "$LENGHT" ] && echo -e "\nCongratulations! You made it in $N attempt!" && exit
  N=$((10#$N+1))
done

echo -ne "\nSorry, you didn't make it. The code was: "
echo "${CODE[@]:0}" | tr -d " "
exit
Greetings!
[color=red][size=75][O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource[/size][/color]
[b][color=green]Omnia mea mecum porto.[/color][/b]

User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

#7 Post by L18L »

SFR wrote:How about Mastermind?
OK, let´s play
$HOME/my-applications/bin/mastermind wrote:The 4-character code out of A-F set has been generated.
The code may contain repetitions.
Try to guess it within 12 attempts!

! - indicates the existence of a correct letter in the correct position.
? - indicates the existence of a correct letter in the wrong position.

Lenght: 4 Set: A-F Attempts: 12

01) AAAA : ....
02) BCDE : !?..
03) FFFF : !...
04) CDEF : ??..
05) DDDD : !!..
06) FDDE : !??.
07) DDFB : !!??
08) DFBD : !!??
09) DFCC : !!..
10) DFDB : !!!!

Congratulations! You made it in 10 attempt!
#

User avatar
Moose On The Loose
Posts: 965
Joined: Thu 24 Feb 2011, 14:54

Tiny linux games

#8 Post by Moose On The Loose »

kaeza wrote:Hello.

I created this thread so people can show some interesting games made using scripting languages.
This isn't completely tiny but here it is complete with a GUI.
It plays tictactoe and does a fairly good job of it.

Code: Select all


#/bin/bash

DF=/tmp/tic
BB="X23456789"
W=false

function tot() {
  Y=$(echo "${BB:$2:1}${BB:$3:1}${BB:$4:1}" | sed "s/[1-9]//g;s/X/+1/g;s/O/+10/g")
  case $(( 0 $Y )) in
    0) eval $1=1
      ;;
    1) eval $1=2
      ;;
    10) eval $1=2
      ;;
    2) eval $1=1000
      W=true
      ;;
    20) eval $1=10
      ;;
    30) echo "You win!"
      exit
      ;;
    *) eval $1=0
      ;;
    esac
  }

function but() {
  local Z
  if [[ "$1" > "A" ]] ; then
    Z=text
  else
    Z=button
    fi
    
  echo "<$Z><label>$1</label></$Z>"
  }

function dia() {
  local I
  local J
  echo "<vbox>"
  if [[ "$1" != "" ]] ; then
    echo "<text><label>$1</label></text>"
    fi
  echo "<hbox>"
  
  for I in 0 1 2 ; do
    echo '<vbox homogeneous="true">'
    for J in $I $(( $I + 3)) $(( $I + 6 )) ; do
      but ${BB:$J:1}
      done
    echo "</vbox>"
    done

  echo "</hbox>"    

  if [[ "$2" != "" ]] ; then
    echo "<button><label>$2</label></button>"
    fi
      
  echo "</vbox>"
  }

MX="your move"
while true ; do
  if ( echo $BB | grep -v -r -q "[1-9]" ) ; then
    dia "Its a draw" "OK" >$DF
  else
    dia "$MX" >$DF
    fi
  M=$(gtkdialog3 -f /tmp/tic)
  if ! (echo "$M" | grep -q "[1-9]") ; then
    exit
    fi 
  M=${M/EXIT/M}
  eval $M
  BB=${BB/$M/O}
  for X in "A 0 1 2" "B 3 4 5" "C 6 7 8" "D 0 3 6" "E 1 4 7" "F 2 5 8" "G 0 4 8" "H 2 4 6" ; do
    tot $X
    done
  M=$( echo "$((2 + $A + $D + $G )) 1 ${BB:0:1}
$(( $A + $E )) 2 ${BB:1:1}
$((2 + $A + $F + $H )) 3 ${BB:2:1}
$(( $B + $D )) 4 ${BB:3:1}
$(( $B + $E +$G + $H )) 5 ${BB:4:1}
$(( $B + $F )) 6 ${BB:5:1}
$((2 + $C + $D + $H )) 7 ${BB:6:1}
$(( $C + $E )) 8 ${BB:7:1}
$((2 + $C + $F + $G )) 9 ${BB:8:1}
" | grep -r "[1-9]\$" | sort -n | tail -1 | cut -d\  -f2 )
  MX="I moved $M"
  BB=${BB/$M/X}
  if $W ; then
    dia "I win at $M" "OK" >$DF
    gtkdialog3 -f /tmp/tic
    exit
    fi
  done
  


User avatar
darkcity
Posts: 2534
Joined: Sun 23 May 2010, 19:16
Location: near here
Contact:

#9 Post by darkcity »

seems like line 22 isn't needed ; -)

User avatar
Moose On The Loose
Posts: 965
Joined: Thu 24 Feb 2011, 14:54

#10 Post by Moose On The Loose »

darkcity wrote:seems like line 22 isn't needed ; -)
I would be nice if you quoted a bit of what you are reacting to. I went and looked at my code and lines 21-23 do serve an important purpose. I assumed that the user would look at the code before running it. Those lines serve to give a false hope of winning.

User avatar
darkcity
Posts: 2534
Joined: Sun 23 May 2010, 19:16
Location: near here
Contact:

#11 Post by darkcity »

fair play ; -)

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#12 Post by technosaurus »

http://murga-linux.com/puppy/viewtopic.php?t=80352
I thought outside the box a bit and just created a poor man's scriptable game framework... would be cool to have some kind of a contest
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

kaeza
Posts: 51
Joined: Thu 01 Dec 2011, 02:49

#13 Post by kaeza »

Sory for the delay guys, I exceeded my bandwidth :(
Really nice games.
Been a bit busy "working", so I didn't have time to finish anything.
A new game coming soon...
[i]"Courage is not the absence of fear, but rather the judgement that something else is more important than fear."[/i] -- Ambrose Redmoon

Post Reply