Develop a string avoider prototype with Corona SDK

Read all posts about "" game

As you know, Stringy game was built starting from this prototype: Develop a Flash game like String Avoider – AS3 version – and more!.

Christian Wisniewski made a Corona version of the prototype to be played on mobile devices as you can see here:

With this main.lua file:

-- Declaring Variables
-- Stage Width and Height
local _W,_H = display.contentWidth,display.contentHeight

local tailLength = 5
local tailNodes = 200
local head = display.newCircle(_W/2,_H/2,10)
local nodes = {}

for i=1,tailNodes,1 do
	nodes[i] = {}
	nodes[i].x = head.x
	nodes[i].y = head.y
end

-- Creating Display Groups
local tailGroup = display.newGroup()

local function startDrag(event)
	-- Store the Target (the Head) in a variable
	local t = event.target
	if event.phase == "began" then
		-- When the touch started, set the Focus on the Head
		display.getCurrentStage():setFocus( t )
		t.isFocus = true
		-- Store initial Position of the Head
		t.x0 = event.x - t.x
        	t.y0 = event.y - t.y
	elseif t.isFocus then
		if event.phase == "moved" then
			-- While moving the head
			-- remove every drawn line from the group
			for i=tailGroup.numChildren,1,-1 do
				tailGroup:remove(i)
			end
			
			-- Move the Head
			t.x = event.x - t.x0
			t.y = event.y - t.y0
			
			-- Create starting line and insert it into the group
			local line = display.newLine(head.x,head.y,nodes[1].x,nodes[1].y)
			tailGroup:insert(line)
			line.width = 20
			
			nodes[1].x = head.x
			nodes[1].y = head.y
			
			-- Loop through every tailNode and save new positions in table
			-- Note: LUA tables start with an index of 1 and not 0!
			for i=2,tailNodes,1 do
				local nodeAngle = math.atan2(nodes[i].y-nodes[i-1].y,nodes[i].x-nodes[i-1].x);
				nodes[i].x = nodes[i-1].x+tailLength*math.cos(nodeAngle)
				nodes[i].y = nodes[i-1].y+tailLength*math.sin(nodeAngle)
				
				-- You could use
				-- line:append(nodes[i].x,nodes[i].y)
				-- but this wouldn't allow you to alter each segment (fading the color, reducing linewidth etc.)
				
				-- Creating new line segments
				local nLine = display.newLine(nodes[i-1].x,nodes[i-1].y,nodes[i].x,nodes[i].y)
				-- c will store the colorvalue - starting from 255 and ending with 0 (white to black)
				local c = 255-i/tailNodes*255
				nLine:setColor(c)
				-- width will start at 20 and fade out to 0
				nLine.width = line.width-i/tailNodes*line.width
				
				-- Important: insert the new Line segments into the table!
				tailGroup:insert(nLine)
			end
		elseif event.phase == "ended" or event.phase == "cancelled" then
			-- Remove the focus
			display.getCurrentStage():setFocus( t, nil )
			t.isFocus = false
		end
	end
end

-- Create touch Eventlistener
head:addEventListener("touch",startDrag)

During next days I will improve the prototype, maybe this could be a start for a mobile version of Stringy game.

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

214 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers