Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
My first Gideros game: Guess a number, all text — Gideros Forum

My first Gideros game: Guess a number, all text

MagnusviriMagnusviri Member
edited March 2012 in Code snippets
Ok, this is litterally just an experiment. I was seeing in how few lines I could write this. Turns out it takes a lot of lines because I have to emulate a console. I should probably just create a real GUI. I'm hesitant to post such messy code but I'm just going to take the plunge.

I'm curious what others think and if anyone has suggestions how I could make it simpler so that I could teach it to people who have never programmed before.
Console = Core.class(Sprite)
function Console:init()
	self.xbeg = 0
	self.ybeg = 0
	self.x = 0
	self.y = 0
	self.xinc = 7
	self.yinc = 12
end
function Console:incrementY(increment)
	self.y = self.y + increment
	if self.y > 320 then
		self:setY(self:getY()-increment)
	end
	self.x = self.xbeg
end
function Console:printTF(message)
	local textfield = TextField.new(nil, message)
	textfield:setPosition(self.x, self.y)
	self:addChild(textfield)
	return textfield
end
function Console:printN(message)
	if message ~= "" then
		local textfield = self:printTF(message)
	end
	self:incrementY(self.yinc)
	return textfield
end
function Console:print(message)
	local textfield = self:printTF(message)
	self.x = self.x+self.xinc*string.len(message)
	return textfield
end
function Console:printB(message)
	local textfield = TextField.new(nil, message)
	local paddingY = 14
	local paddingX = 7
	local width = textfield:getWidth()+paddingX*2
	local height = textfield:getHeight()+paddingY*2
 
	local x = self.x
	local y = self.y+paddingY
 
	self.x = self.x+width+paddingX/2
	local shape = Shape.new()
	shape:setFillStyle(Shape.SOLID, 0xffff00, 1.0)
	shape:beginPath(Shape.NON_ZERO)
	shape:moveTo(0, 0)
	shape:lineTo(0, -height)
	shape:lineTo(width, -height)
	shape:lineTo(width, 0)
	shape:lineTo(0, 0)
	shape:closePath()
	shape:endPath()
	shape:setPosition(x, y)
	textfield:setPosition(paddingX, -paddingY)
	shape:addChild(textfield)
	self:addChild(shape)
	return shape
end
 
console = Console.new()
stage:addChild(console)
console:setPosition(3, 10)
 
math.randomseed( os.time() )
 
function makeAGuess(textfield, event)
	if textfield:hitTestPoint(event.x, event.y) then
		guess = textfield.number
		guessesTaken = guessesTaken + 1
 
		if guess == secretNumber then
			console:printN(textfield.number.." is right! You took this many guesses: "..guessesTaken..".")
			console:printN("Let's play again.")
			startGame()
		else				
			if guess > secretNumber then
				console:printN(textfield.number.." is too high.")
			elseif guess < secretNumber then
				console:printN(textfield.number.." is too low.")
			end
			if guessesTaken > 5 then
				console:printN("You took too long. The number was "..secretNumber)
				console:printN("Let's play again.")
				startGame()
			end
 
		end	
 
		event:stopPropagation()
	end
end
 
function startGame()
	secretNumber = math.random( 20 )
	console:printN("I am thinking of a number between 1 and 20.")
	console:printN("")
	local button
	for ii=1,20,1 do
		button = console:printB(ii)
		button.number = ii
		button:addEventListener(Event.MOUSE_DOWN, makeAGuess, button)
	end
	console:incrementY(button:getHeight())
	guessesTaken = 0
end
 
startGame()

Likes: atilim

+1 -1 (+1 / -0 )Share on Facebook

Comments

  • ar2rsawseenar2rsawseen Maintainer
    edited March 2012
    Good one. http://builtwithgideros.com/ ? :D

    Hmm,
    I'd created one text field showing number and two arrows to increase it and decrese it and a button to take a guess.

    And why do you need to save all previous game information on screen?

    But yeah, this kind of interaction would probably require some code
  • Ok I know I made it really weird. I'm coding on my iPad using Textastic, uploading to Dropbox, and I've customized my Gideros exported project so that it downloads the file from Dropbox and then runs it. The problem is that there is absolutely no debugging and so when I have an error it just blinks off. I found I needed to print out a lot of information like in a console so I made something that will do that. I am probably going to find some way to shake the phone or some secret gesture that will get the console to show and then it will only be for my eyes.

    I've already started to make a semi gui with shapes.

    Another reason I don't have any graphics is because I haven't customized my Gideros app on my phone with anything but downloading one lua file. I hope to eventually have it download a whole project, kinda like what Gideros Player does with a desktop computer. Don't ask me why I don't program when I'm in front of my laptop/desktop. I just do different things when I'm in front of them and I do Gideros stuff when I've got my iPad in front of me.
  • @magnusviri Would it be possible for you to share the project you use to download and execute lua main from Dropbox? I am also interested to develop on my iPad at the office :)

    Thanks!
  • @grotly here is a version. Pretty rough. You have to set the URL in the Settings App.

    http://dl.dropbox.com/u/10761501/blog/GiderosAppExport.zip
Sign In or Register to comment.