Advertisement
Guest User

I'm cool

a guest
Nov 26th, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.71 KB | None | 0 0
  1.  
  2. --# Main
  3. -- App Project
  4. topScore = 0
  5. lives = nil
  6.  
  7. -- Use this function to perform your initial setup
  8.  
  9. function setup()
  10. lives = 3
  11. topScore = readLocalData("highScore", 0)
  12. Scene("main", MainMenu)
  13. Scene.Change("main")
  14. displayMode(FULLSCREEN)
  15. end
  16.  
  17. -- This function gets called once every frame
  18. function draw()
  19. -- This sets a dark background color
  20. strokeWidth(5)
  21. background(0, 186, 255, 255)
  22. Scene.Draw()
  23.  
  24. -- This sets the line thickness
  25.  
  26. -- Do your drawing here
  27.  
  28. end
  29.  
  30. function touched(touch)
  31.  
  32. Scene.Touched(touch)
  33.  
  34. end
  35. --# SplashScreen
  36. SplashScreen = class()
  37.  
  38. function SplashScreen:init(x)
  39. -- you can accept and set parameters here
  40. self.x = x
  41. end
  42.  
  43. function SplashScreen:draw()
  44. -- Codea does not automatically call this method
  45. end
  46.  
  47. function SplashScreen:touched(touch)
  48. -- Codea does not automatically call this method
  49. end
  50.  
  51. --# MainMenu
  52. MainMenu = class()
  53.  
  54. local setting
  55. local play
  56. local backGround
  57. function MainMenu:init()
  58. -- you can accept and set parameters here
  59.  
  60.  
  61. setting = Button("Dropbox:setting icon", vec2(950, 75))
  62. play = Button("iOS Button Pack:Yellow Move Scene Forward Button", vec2(WIDTH/2+150, 350))
  63.  
  64. end
  65. function MainMenu:draw()
  66. -- Codea does not automatically call this method
  67. -- sprite("Dropbox:background", WIDTH/2, HEIGHT/2, 2048, 1536)
  68.  
  69. background(227, 80, 79, 255)
  70.  
  71. fontSize(100)
  72. text("GIANT", WIDTH/2, 650)
  73. text("KITTEN", WIDTH/2, 511)
  74. setting:draw()
  75. play:draw()
  76.  
  77. rect(800, 550, 300, 500)
  78. fontSize(50)
  79. fill(255, 255, 255, 255)
  80. text("HS = ", 920, 680)
  81. text(topScore, 910, 600)
  82.  
  83. fontSize(25)
  84. text("INSTRUCTIONS", 150, 400)
  85. text("______________", 150, 399)
  86.  
  87. text("Drag the character to hit", 150, 350)
  88. text("the answers to the given", 150, 325)
  89. text("math problems. Dont hit", 150, 300)
  90. text("the wrong answer or let", 150, 275)
  91. text("it hit the edge of", 150, 250)
  92. text("the screen.", 150, 225)
  93.  
  94.  
  95. --sprite("Dropbox:background", 950, 75)
  96.  
  97. end
  98.  
  99. function MainMenu:touched(touch)
  100. -- Codea does not automatically call this method
  101.  
  102. setting:touched(touch)
  103. play:touched(touch)
  104.  
  105. end
  106.  
  107. --# Options
  108. Options = class()
  109.  
  110. function Options:init(x)
  111. -- you can accept and set parameters here
  112. self.x = x
  113. end
  114.  
  115. function Options:draw()
  116. -- Codea does not automatically call this method
  117. end
  118.  
  119. function Options:touched(touch)
  120. -- Codea does not automatically call this method
  121. end
  122.  
  123. --# Credits
  124. Credits = class()
  125.  
  126. function Credits:init(x)
  127. -- you can accept and set parameters here
  128. self.x = x
  129. end
  130.  
  131. function Credits:draw()
  132. -- Codea does not automatically call this method
  133. end
  134.  
  135. function Credits:touched(touch)
  136. -- Codea does not automatically call this method
  137. end
  138.  
  139. --# LevelSelect
  140. LevelSelect = class()
  141.  
  142. function LevelSelect:init(x)
  143. -- you can accept and set parameters here
  144. self.x = x
  145. end
  146.  
  147. function LevelSelect:draw()
  148. -- Codea does not automatically call this method
  149. end
  150.  
  151. function LevelSelect:touched(touch)
  152. -- Codea does not automatically call this method
  153. end
  154.  
  155. --# Level1
  156. Level1 = class()
  157.  
  158. function Level1:init(x)
  159. -- you can accept and set parameters here
  160. self.x = x
  161. end
  162.  
  163. function Level1:draw()
  164. -- Codea does not automatically call this method
  165. end
  166.  
  167. function Level1:touched(touch)
  168. -- Codea does not automatically call this method
  169. end
  170.  
  171. --# Level2
  172. Level2 = class()
  173.  
  174. function Level2:init(x)
  175. -- you can accept and set parameters here
  176. self.x = x
  177. end
  178.  
  179. function Level2:draw()
  180. -- Codea does not automatically call this method
  181. end
  182.  
  183. function Level2:touched(touch)
  184. -- Codea does not automatically call this method
  185. end
  186.  
  187. --# Level3
  188. Level3 = class()
  189.  
  190. function Level3:init(x)
  191. -- you can accept and set parameters here
  192. self.x = x
  193. end
  194.  
  195. function Level3:draw()
  196. -- Codea does not automatically call this method
  197. end
  198.  
  199. function Level3:touched(touch)
  200. -- Codea does not automatically call this method
  201. end
  202.  
  203. --# GameOver
  204. GameOver = class()
  205.  
  206. function GameOver:init(x)
  207. -- you can accept and set parameters here
  208. self.x = x
  209. end
  210.  
  211. function GameOver:draw()
  212. -- Codea does not automatically call this method
  213. end
  214.  
  215. function GameOver:touched(touch)
  216. -- Codea does not automatically call this method
  217. end
  218.  
  219. --# WinScreen
  220. WinScreen = class()
  221.  
  222. function WinScreen:init(x)
  223. -- you can accept and set parameters here
  224. self.x = x
  225. end
  226.  
  227. function WinScreen:draw()
  228. -- Codea does not automatically call this method
  229. end
  230.  
  231. function WinScreen:touched(touch)
  232. -- Codea does not automatically call this method
  233. end
  234.  
  235. --# HelperClass
  236. -- HelperClass
  237.  
  238. -- Created by: Mr Coxall
  239. -- Created on: Nov 2014
  240. -- Created for: ICS2O
  241. -- This is a collection of classes and a scene management system,
  242. -- to help in the aid of coding.
  243.  
  244.  
  245. -- Button Class
  246.  
  247. -- This class simplifies the process of checking if a sprite button is pressed or not
  248. -- Then you can ask the button if it is being touched or was ended.
  249. -- Works best with vector sprite buttons, since it enlarges them when they are touched
  250. --
  251. -- Parameters to pass in:
  252. -- 1. Untouched button image name
  253. -- 2. A vector2 that is the location of the button
  254.  
  255. Button = class()
  256.  
  257. function Button:init(buttonImage, buttonPosition)
  258. -- accepts the button image and location to draw it
  259.  
  260. self.buttonImage = buttonImage
  261. self.buttonLocation = buttonPosition
  262.  
  263. self.buttonTouchScale = 1.15
  264. self.buttonImageSize = vec2(spriteSize(self.buttonImage))
  265. self.currentButtonImage = self.buttonImage
  266. self.buttonTouchedImage = resizeImage(self.buttonImage, (self.buttonImageSize.x*self.buttonTouchScale), (self.buttonImageSize.y*self.buttonTouchScale))
  267. self.selected = false
  268. end
  269.  
  270. function Button:draw()
  271. -- Codea does not automatically call this method
  272.  
  273. pushStyle()
  274. pushMatrix()
  275. noFill()
  276. noSmooth()
  277. noStroke()
  278.  
  279. sprite(self.currentButtonImage, self.buttonLocation.x, self.buttonLocation.y)
  280.  
  281. popMatrix()
  282. popStyle()
  283. end
  284.  
  285. function Button:touched(touch)
  286. -- local varaibles
  287. local currentTouchPosition = vec2(touch.x, touch.y)
  288.  
  289. -- reset touching variable to false
  290. self.selected = false
  291.  
  292. if (touch.state == BEGAN) then
  293. if( (self.buttonLocation.x - self.buttonImageSize.x/2) < currentTouchPosition.x and
  294. (self.buttonLocation.x + self.buttonImageSize.x/2) > currentTouchPosition.x and
  295. (self.buttonLocation.y - self.buttonImageSize.y/2) < currentTouchPosition.y and
  296. (self.buttonLocation.y + self.buttonImageSize.y/2) > currentTouchPosition.y ) then
  297.  
  298. self.currentButtonImage = self.buttonTouchedImage
  299. --print("Now touching! - began")
  300. else
  301. self.currentButtonImage = self.buttonImage
  302. --print("Not touching - began")
  303. end
  304. end
  305.  
  306. if (touch.state == MOVING) then
  307. if( (self.buttonLocation.x - self.buttonImageSize.x/2) < currentTouchPosition.x and
  308. (self.buttonLocation.x + self.buttonImageSize.x/2) > currentTouchPosition.x and
  309. (self.buttonLocation.y - self.buttonImageSize.y/2) < currentTouchPosition.y and
  310. (self.buttonLocation.y + self.buttonImageSize.y/2) > currentTouchPosition.y ) then
  311.  
  312. self.currentButtonImage = self.buttonTouchedImage
  313. --print("Now touching! - moving")
  314. else
  315. self.currentButtonImage = self.buttonImage
  316. --print("Not touching - moving")
  317. end
  318. end
  319.  
  320. if (touch.state == ENDED) then
  321. if( (self.buttonLocation.x - self.buttonImageSize.x/2) < currentTouchPosition.x and
  322. (self.buttonLocation.x + self.buttonImageSize.x/2) > currentTouchPosition.x and
  323. (self.buttonLocation.y - self.buttonImageSize.y/2) < currentTouchPosition.y and
  324. (self.buttonLocation.y + self.buttonImageSize.y/2) > currentTouchPosition.y ) then
  325.  
  326. self.selected = true
  327. --print("Activated button")
  328. end
  329.  
  330. self.currentButtonImage = self.buttonImage
  331. end
  332. end
  333.  
  334. function resizeImage(img, width, height)
  335. -- function from
  336. -- http://codea.io/talk/discussion/3490/importing-pics-from-dropbox/p1
  337.  
  338. local newImg = image(width,height)
  339. setContext(newImg)
  340. sprite( img, width/2, height/2, width, height )
  341. setContext()
  342. return newImg
  343. end
  344.  
  345.  
  346. -- Dragging Object Class
  347. -- This class simplifies the process of dragging and dropping objects
  348. -- You can have several objects interacting, but it is not mulit-touch
  349. --
  350. -- Parameters to pass in:
  351. -- 1. Object image name
  352. -- 2. A vector2 that is the location of the button
  353. -- 3. Optional object id
  354.  
  355.  
  356. SpriteObject = class()
  357.  
  358. function SpriteObject:init(objectImage, objectStartPosition, objectID)
  359.  
  360. self.objectImage = objectImage
  361. self.objectStartLocation = objectStartPosition
  362. self.ID = objectID or math.random()
  363.  
  364. self.objectCurrentLocation = self.objectStartLocation
  365. self.objectImageSize = vec2(spriteSize(self.objectImage))
  366. self.selected = false
  367. self.dragOffset = vec2(0,0)
  368. self.draggable = true
  369. -- yes, the following does need to be global to the entire program
  370. -- this is the only way (easy way!) to move things around and no get
  371. -- several object "attached" to each other
  372. -- this way just the top object in the stack moves
  373. DRAGGING_OBJECT_MOVING = nil or DRAGGING_OBJECT_MOVING
  374. end
  375.  
  376. function SpriteObject:draw()
  377. -- Codea does not automatically call this method
  378.  
  379. pushStyle()
  380. pushMatrix()
  381. noFill()
  382. noSmooth()
  383. noStroke()
  384.  
  385. sprite(self.objectImage, self.objectCurrentLocation.x, self.objectCurrentLocation.y)
  386.  
  387. popMatrix()
  388. popStyle()
  389.  
  390. end
  391.  
  392. function SpriteObject:touched(touch)
  393. -- Codea does not automatically call this method
  394.  
  395. -- local varaibles
  396. local currentTouchPosition = vec2(touch.x, touch.y)
  397.  
  398. -- reset touching variable to false
  399. self.selected = false
  400.  
  401. if (touch.state == BEGAN and self.draggable == true) then
  402. if( (self.objectCurrentLocation.x - self.objectImageSize.x/2) < currentTouchPosition.x and
  403. (self.objectCurrentLocation.x + self.objectImageSize.x/2) > currentTouchPosition.x and
  404. (self.objectCurrentLocation.y - self.objectImageSize.y/2) < currentTouchPosition.y and
  405. (self.objectCurrentLocation.y + self.objectImageSize.y/2) > currentTouchPosition.y ) then
  406. -- if the touch has began, we need to find delta from touch to center of object
  407. -- since will need it to reposition the object for draw
  408. -- subtracting 2 vec2s here
  409. self.dragOffset = self.objectCurrentLocation - currentTouchPosition
  410. DRAGGING_OBJECT_MOVING = self.ID
  411. end
  412. end
  413.  
  414. if (touch.state == MOVING and self.draggable == true) then
  415. if( (self.objectCurrentLocation.x - self.objectImageSize.x/2) < currentTouchPosition.x and
  416. (self.objectCurrentLocation.x + self.objectImageSize.x/2) > currentTouchPosition.x and
  417. (self.objectCurrentLocation.y - self.objectImageSize.y/2) < currentTouchPosition.y and
  418. (self.objectCurrentLocation.y + self.objectImageSize.y/2) > currentTouchPosition.y ) then
  419. -- only let it move if self.draggable == true
  420. if (self.draggable == true) then
  421. -- add the offset back in for its new position
  422. if (self.ID == DRAGGING_OBJECT_MOVING) then
  423. self.objectCurrentLocation = currentTouchPosition + self.dragOffset
  424. end
  425. end
  426. end
  427. end
  428.  
  429. if (touch.state == ENDED and self.draggable == true) then
  430. DRAGGING_OBJECT_MOVING = nil
  431. end
  432.  
  433. -- this checks for if you have just touched the image
  434. -- you will have to release and re-touch for this to be activated again
  435. if (touch.state == BEGAN) then
  436. if( (self.objectCurrentLocation.x - self.objectCurrentLocation.x/2) < currentTouchPosition.x and
  437. (self.objectCurrentLocation.x + self.objectCurrentLocation.x/2) > currentTouchPosition.x and
  438. (self.objectCurrentLocation.y - self.objectCurrentLocation.y/2) < currentTouchPosition.y and
  439. (self.objectCurrentLocation.y + self.objectCurrentLocation.y/2) > currentTouchPosition.y ) then
  440.  
  441. self.selected = true
  442. --print("Activated button")
  443. end
  444. end
  445. end
  446.  
  447. function SpriteObject:isTouching(otherSpriteObject)
  448. -- this method checks if one dragging object is touching another dragging object
  449.  
  450. local isItTouching = false
  451.  
  452. if( (self.objectCurrentLocation.x + self.objectImageSize.x/2) > (otherSpriteObject.objectCurrentLocation.x - otherSpriteObject.objectImageSize.x/2) and
  453. (self.objectCurrentLocation.x - self.objectImageSize.x/2) < (otherSpriteObject.objectCurrentLocation.x + otherSpriteObject.objectImageSize.x/2) and
  454. (self.objectCurrentLocation.y - self.objectImageSize.y/2) < (otherSpriteObject.objectCurrentLocation.y + otherSpriteObject.objectImageSize.y/2) and
  455. (self.objectCurrentLocation.y + self.objectImageSize.y/2) > (otherSpriteObject.objectCurrentLocation.y - otherSpriteObject.objectImageSize.y/2) ) then
  456. -- if true, then not touching
  457. isItTouching = true
  458. end
  459.  
  460. return isItTouching
  461. end
  462.  
  463.  
  464. -- SceneManager
  465. --
  466. -- This file lets you easily manage different scenes
  467. -- Original code from Brainfox, off the Codea forums
  468.  
  469. Scene = {}
  470. local scenes = {}
  471. local sceneNames = {}
  472. local currentScene = nil
  473.  
  474. setmetatable(Scene,{__call = function(_,name,cls)
  475. if (not currentScene) then
  476. currentScene = 1
  477. end
  478. table.insert(scenes,cls)
  479. sceneNames[name] = #scenes
  480. Scene_Select = nil
  481. end})
  482.  
  483. --Change scene
  484. Scene.Change = function(name)
  485. currentScene = sceneNames[name]
  486. scenes[currentScene]:init()
  487. if (Scene_Select) then
  488. Scene_Select = currentScene
  489. end
  490.  
  491. collectgarbage()
  492. end
  493.  
  494. Scene.Draw = function()
  495. pushStyle()
  496. pushMatrix()
  497. scenes[currentScene]:draw()
  498. popMatrix()
  499. popStyle()
  500. end
  501.  
  502. Scene.Touched = function(t)
  503. if (scenes[currentScene].touched) then
  504. scenes[currentScene]:touched(t)
  505. end
  506. end
  507.  
  508. Scene.Keyboard = function()
  509. if (scenes[currentScene].keyboard) then
  510. scenes[currentScene]:keyboard(key)
  511. end
  512. end
  513.  
  514. Scene.OrientationChanged = function()
  515. if (scenes[currentScene].orientationChanged) then
  516. scenes[currentScene]:orientationChanged()
  517. end
  518. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement