Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --# Main
- -- App Project
- topScore = 0
- lives = nil
- -- Use this function to perform your initial setup
- function setup()
- lives = 3
- topScore = readLocalData("highScore", 0)
- Scene("main", MainMenu)
- Scene.Change("main")
- displayMode(FULLSCREEN)
- end
- -- This function gets called once every frame
- function draw()
- -- This sets a dark background color
- strokeWidth(5)
- background(0, 186, 255, 255)
- Scene.Draw()
- -- This sets the line thickness
- -- Do your drawing here
- end
- function touched(touch)
- Scene.Touched(touch)
- end
- --# SplashScreen
- SplashScreen = class()
- function SplashScreen:init(x)
- -- you can accept and set parameters here
- self.x = x
- end
- function SplashScreen:draw()
- -- Codea does not automatically call this method
- end
- function SplashScreen:touched(touch)
- -- Codea does not automatically call this method
- end
- --# MainMenu
- MainMenu = class()
- local setting
- local play
- local backGround
- function MainMenu:init()
- -- you can accept and set parameters here
- setting = Button("Dropbox:setting icon", vec2(950, 75))
- play = Button("iOS Button Pack:Yellow Move Scene Forward Button", vec2(WIDTH/2+150, 350))
- end
- function MainMenu:draw()
- -- Codea does not automatically call this method
- -- sprite("Dropbox:background", WIDTH/2, HEIGHT/2, 2048, 1536)
- background(227, 80, 79, 255)
- fontSize(100)
- text("GIANT", WIDTH/2, 650)
- text("KITTEN", WIDTH/2, 511)
- setting:draw()
- play:draw()
- rect(800, 550, 300, 500)
- fontSize(50)
- fill(255, 255, 255, 255)
- text("HS = ", 920, 680)
- text(topScore, 910, 600)
- fontSize(25)
- text("INSTRUCTIONS", 150, 400)
- text("______________", 150, 399)
- text("Drag the character to hit", 150, 350)
- text("the answers to the given", 150, 325)
- text("math problems. Dont hit", 150, 300)
- text("the wrong answer or let", 150, 275)
- text("it hit the edge of", 150, 250)
- text("the screen.", 150, 225)
- --sprite("Dropbox:background", 950, 75)
- end
- function MainMenu:touched(touch)
- -- Codea does not automatically call this method
- setting:touched(touch)
- play:touched(touch)
- end
- --# Options
- Options = class()
- function Options:init(x)
- -- you can accept and set parameters here
- self.x = x
- end
- function Options:draw()
- -- Codea does not automatically call this method
- end
- function Options:touched(touch)
- -- Codea does not automatically call this method
- end
- --# Credits
- Credits = class()
- function Credits:init(x)
- -- you can accept and set parameters here
- self.x = x
- end
- function Credits:draw()
- -- Codea does not automatically call this method
- end
- function Credits:touched(touch)
- -- Codea does not automatically call this method
- end
- --# LevelSelect
- LevelSelect = class()
- function LevelSelect:init(x)
- -- you can accept and set parameters here
- self.x = x
- end
- function LevelSelect:draw()
- -- Codea does not automatically call this method
- end
- function LevelSelect:touched(touch)
- -- Codea does not automatically call this method
- end
- --# Level1
- Level1 = class()
- function Level1:init(x)
- -- you can accept and set parameters here
- self.x = x
- end
- function Level1:draw()
- -- Codea does not automatically call this method
- end
- function Level1:touched(touch)
- -- Codea does not automatically call this method
- end
- --# Level2
- Level2 = class()
- function Level2:init(x)
- -- you can accept and set parameters here
- self.x = x
- end
- function Level2:draw()
- -- Codea does not automatically call this method
- end
- function Level2:touched(touch)
- -- Codea does not automatically call this method
- end
- --# Level3
- Level3 = class()
- function Level3:init(x)
- -- you can accept and set parameters here
- self.x = x
- end
- function Level3:draw()
- -- Codea does not automatically call this method
- end
- function Level3:touched(touch)
- -- Codea does not automatically call this method
- end
- --# GameOver
- GameOver = class()
- function GameOver:init(x)
- -- you can accept and set parameters here
- self.x = x
- end
- function GameOver:draw()
- -- Codea does not automatically call this method
- end
- function GameOver:touched(touch)
- -- Codea does not automatically call this method
- end
- --# WinScreen
- WinScreen = class()
- function WinScreen:init(x)
- -- you can accept and set parameters here
- self.x = x
- end
- function WinScreen:draw()
- -- Codea does not automatically call this method
- end
- function WinScreen:touched(touch)
- -- Codea does not automatically call this method
- end
- --# HelperClass
- -- HelperClass
- -- Created by: Mr Coxall
- -- Created on: Nov 2014
- -- Created for: ICS2O
- -- This is a collection of classes and a scene management system,
- -- to help in the aid of coding.
- -- Button Class
- -- This class simplifies the process of checking if a sprite button is pressed or not
- -- Then you can ask the button if it is being touched or was ended.
- -- Works best with vector sprite buttons, since it enlarges them when they are touched
- --
- -- Parameters to pass in:
- -- 1. Untouched button image name
- -- 2. A vector2 that is the location of the button
- Button = class()
- function Button:init(buttonImage, buttonPosition)
- -- accepts the button image and location to draw it
- self.buttonImage = buttonImage
- self.buttonLocation = buttonPosition
- self.buttonTouchScale = 1.15
- self.buttonImageSize = vec2(spriteSize(self.buttonImage))
- self.currentButtonImage = self.buttonImage
- self.buttonTouchedImage = resizeImage(self.buttonImage, (self.buttonImageSize.x*self.buttonTouchScale), (self.buttonImageSize.y*self.buttonTouchScale))
- self.selected = false
- end
- function Button:draw()
- -- Codea does not automatically call this method
- pushStyle()
- pushMatrix()
- noFill()
- noSmooth()
- noStroke()
- sprite(self.currentButtonImage, self.buttonLocation.x, self.buttonLocation.y)
- popMatrix()
- popStyle()
- end
- function Button:touched(touch)
- -- local varaibles
- local currentTouchPosition = vec2(touch.x, touch.y)
- -- reset touching variable to false
- self.selected = false
- if (touch.state == BEGAN) then
- if( (self.buttonLocation.x - self.buttonImageSize.x/2) < currentTouchPosition.x and
- (self.buttonLocation.x + self.buttonImageSize.x/2) > currentTouchPosition.x and
- (self.buttonLocation.y - self.buttonImageSize.y/2) < currentTouchPosition.y and
- (self.buttonLocation.y + self.buttonImageSize.y/2) > currentTouchPosition.y ) then
- self.currentButtonImage = self.buttonTouchedImage
- --print("Now touching! - began")
- else
- self.currentButtonImage = self.buttonImage
- --print("Not touching - began")
- end
- end
- if (touch.state == MOVING) then
- if( (self.buttonLocation.x - self.buttonImageSize.x/2) < currentTouchPosition.x and
- (self.buttonLocation.x + self.buttonImageSize.x/2) > currentTouchPosition.x and
- (self.buttonLocation.y - self.buttonImageSize.y/2) < currentTouchPosition.y and
- (self.buttonLocation.y + self.buttonImageSize.y/2) > currentTouchPosition.y ) then
- self.currentButtonImage = self.buttonTouchedImage
- --print("Now touching! - moving")
- else
- self.currentButtonImage = self.buttonImage
- --print("Not touching - moving")
- end
- end
- if (touch.state == ENDED) then
- if( (self.buttonLocation.x - self.buttonImageSize.x/2) < currentTouchPosition.x and
- (self.buttonLocation.x + self.buttonImageSize.x/2) > currentTouchPosition.x and
- (self.buttonLocation.y - self.buttonImageSize.y/2) < currentTouchPosition.y and
- (self.buttonLocation.y + self.buttonImageSize.y/2) > currentTouchPosition.y ) then
- self.selected = true
- --print("Activated button")
- end
- self.currentButtonImage = self.buttonImage
- end
- end
- function resizeImage(img, width, height)
- -- function from
- -- http://codea.io/talk/discussion/3490/importing-pics-from-dropbox/p1
- local newImg = image(width,height)
- setContext(newImg)
- sprite( img, width/2, height/2, width, height )
- setContext()
- return newImg
- end
- -- Dragging Object Class
- -- This class simplifies the process of dragging and dropping objects
- -- You can have several objects interacting, but it is not mulit-touch
- --
- -- Parameters to pass in:
- -- 1. Object image name
- -- 2. A vector2 that is the location of the button
- -- 3. Optional object id
- SpriteObject = class()
- function SpriteObject:init(objectImage, objectStartPosition, objectID)
- self.objectImage = objectImage
- self.objectStartLocation = objectStartPosition
- self.ID = objectID or math.random()
- self.objectCurrentLocation = self.objectStartLocation
- self.objectImageSize = vec2(spriteSize(self.objectImage))
- self.selected = false
- self.dragOffset = vec2(0,0)
- self.draggable = true
- -- yes, the following does need to be global to the entire program
- -- this is the only way (easy way!) to move things around and no get
- -- several object "attached" to each other
- -- this way just the top object in the stack moves
- DRAGGING_OBJECT_MOVING = nil or DRAGGING_OBJECT_MOVING
- end
- function SpriteObject:draw()
- -- Codea does not automatically call this method
- pushStyle()
- pushMatrix()
- noFill()
- noSmooth()
- noStroke()
- sprite(self.objectImage, self.objectCurrentLocation.x, self.objectCurrentLocation.y)
- popMatrix()
- popStyle()
- end
- function SpriteObject:touched(touch)
- -- Codea does not automatically call this method
- -- local varaibles
- local currentTouchPosition = vec2(touch.x, touch.y)
- -- reset touching variable to false
- self.selected = false
- if (touch.state == BEGAN and self.draggable == true) then
- if( (self.objectCurrentLocation.x - self.objectImageSize.x/2) < currentTouchPosition.x and
- (self.objectCurrentLocation.x + self.objectImageSize.x/2) > currentTouchPosition.x and
- (self.objectCurrentLocation.y - self.objectImageSize.y/2) < currentTouchPosition.y and
- (self.objectCurrentLocation.y + self.objectImageSize.y/2) > currentTouchPosition.y ) then
- -- if the touch has began, we need to find delta from touch to center of object
- -- since will need it to reposition the object for draw
- -- subtracting 2 vec2s here
- self.dragOffset = self.objectCurrentLocation - currentTouchPosition
- DRAGGING_OBJECT_MOVING = self.ID
- end
- end
- if (touch.state == MOVING and self.draggable == true) then
- if( (self.objectCurrentLocation.x - self.objectImageSize.x/2) < currentTouchPosition.x and
- (self.objectCurrentLocation.x + self.objectImageSize.x/2) > currentTouchPosition.x and
- (self.objectCurrentLocation.y - self.objectImageSize.y/2) < currentTouchPosition.y and
- (self.objectCurrentLocation.y + self.objectImageSize.y/2) > currentTouchPosition.y ) then
- -- only let it move if self.draggable == true
- if (self.draggable == true) then
- -- add the offset back in for its new position
- if (self.ID == DRAGGING_OBJECT_MOVING) then
- self.objectCurrentLocation = currentTouchPosition + self.dragOffset
- end
- end
- end
- end
- if (touch.state == ENDED and self.draggable == true) then
- DRAGGING_OBJECT_MOVING = nil
- end
- -- this checks for if you have just touched the image
- -- you will have to release and re-touch for this to be activated again
- if (touch.state == BEGAN) then
- if( (self.objectCurrentLocation.x - self.objectCurrentLocation.x/2) < currentTouchPosition.x and
- (self.objectCurrentLocation.x + self.objectCurrentLocation.x/2) > currentTouchPosition.x and
- (self.objectCurrentLocation.y - self.objectCurrentLocation.y/2) < currentTouchPosition.y and
- (self.objectCurrentLocation.y + self.objectCurrentLocation.y/2) > currentTouchPosition.y ) then
- self.selected = true
- --print("Activated button")
- end
- end
- end
- function SpriteObject:isTouching(otherSpriteObject)
- -- this method checks if one dragging object is touching another dragging object
- local isItTouching = false
- if( (self.objectCurrentLocation.x + self.objectImageSize.x/2) > (otherSpriteObject.objectCurrentLocation.x - otherSpriteObject.objectImageSize.x/2) and
- (self.objectCurrentLocation.x - self.objectImageSize.x/2) < (otherSpriteObject.objectCurrentLocation.x + otherSpriteObject.objectImageSize.x/2) and
- (self.objectCurrentLocation.y - self.objectImageSize.y/2) < (otherSpriteObject.objectCurrentLocation.y + otherSpriteObject.objectImageSize.y/2) and
- (self.objectCurrentLocation.y + self.objectImageSize.y/2) > (otherSpriteObject.objectCurrentLocation.y - otherSpriteObject.objectImageSize.y/2) ) then
- -- if true, then not touching
- isItTouching = true
- end
- return isItTouching
- end
- -- SceneManager
- --
- -- This file lets you easily manage different scenes
- -- Original code from Brainfox, off the Codea forums
- Scene = {}
- local scenes = {}
- local sceneNames = {}
- local currentScene = nil
- setmetatable(Scene,{__call = function(_,name,cls)
- if (not currentScene) then
- currentScene = 1
- end
- table.insert(scenes,cls)
- sceneNames[name] = #scenes
- Scene_Select = nil
- end})
- --Change scene
- Scene.Change = function(name)
- currentScene = sceneNames[name]
- scenes[currentScene]:init()
- if (Scene_Select) then
- Scene_Select = currentScene
- end
- collectgarbage()
- end
- Scene.Draw = function()
- pushStyle()
- pushMatrix()
- scenes[currentScene]:draw()
- popMatrix()
- popStyle()
- end
- Scene.Touched = function(t)
- if (scenes[currentScene].touched) then
- scenes[currentScene]:touched(t)
- end
- end
- Scene.Keyboard = function()
- if (scenes[currentScene].keyboard) then
- scenes[currentScene]:keyboard(key)
- end
- end
- Scene.OrientationChanged = function()
- if (scenes[currentScene].orientationChanged) then
- scenes[currentScene]:orientationChanged()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement