Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import SpriteKit
- import GameplayKit
- class GameScene: SKScene, SKPhysicsContactDelegate {
- //Enum con las distintas colisiones
- enum Collisions: UInt32 {
- case Fly = 1
- case Object = 2
- case Breach = 4
- }
- //Variables de todos los elementos del juego
- var fly = SKSpriteNode()
- var background = SKSpriteNode()
- var land = SKSpriteNode()
- var tube1 = SKSpriteNode()
- var tube2 = SKSpriteNode()
- var scoreLabel = SKLabelNode()
- var score = 0
- var gameOverLabel = SKLabelNode()
- var gameOver = false
- var timer = Timer()
- var play = false
- func setupGame() {
- //MOSCA:
- //Texturas
- let flyTexture1 = SKTexture(imageNamed: "fly1")
- let flyTexture2 = SKTexture(imageNamed: "fly2")
- let flyTexture3 = SKTexture(imageNamed: "fly3")
- //Movimiento
- let animationFly = SKAction.animate(with: [flyTexture1,flyTexture2, flyTexture3], timePerFrame: 0.1)
- let moveFly = SKAction.repeatForever(animationFly)
- fly = SKSpriteNode(texture: flyTexture1)
- fly.position = CGPoint(x: self.frame.midX - 150, y: self.frame.midY)
- fly.zPosition = -1
- fly.run(moveFly)
- //Físicas
- fly.physicsBody = SKPhysicsBody(circleOfRadius: flyTexture1.size().height / 2)
- fly.physicsBody!.isDynamic = false
- fly.physicsBody!.allowsRotation = false
- fly.physicsBody!.categoryBitMask = Collisions.Fly.rawValue
- fly.physicsBody!.collisionBitMask = Collisions.Object.rawValue | Collisions.Fly.rawValue
- fly.physicsBody!.contactTestBitMask = Collisions.Object.rawValue | Collisions.Breach.rawValue
- self.addChild(fly)
- //FONDO:
- //Textura
- let backgroundTexture = SKTexture(imageNamed: "background")
- //Animación para de movimiento constante
- let backgroundMove1 = SKAction.moveBy(x: -backgroundTexture.size().width, y: 0, duration: 8)
- let backgroundMove2 = SKAction.moveBy(x: backgroundTexture.size().width, y: 0, duration: 0.0)
- let backgroundAnimation = SKAction.repeatForever(SKAction.sequence([backgroundMove1, backgroundMove2]))
- //Bucle con el que situamos la imagen repetidamente en la pantalla
- var i : CGFloat = 0
- while i < 5 + self.frame.size.width / (backgroundTexture.size().width) {
- background = SKSpriteNode(texture: backgroundTexture)
- background.position = CGPoint(x: i * backgroundTexture.size().width, y: -self.frame.midY)
- background.size.height = self.size.height
- background.run(backgroundAnimation)
- background.zPosition = -2
- self.addChild(background)
- i += 1
- }
- //SUELO:
- //Textura
- let landTexture = SKTexture(imageNamed: "land")
- //Animación para de movimiento constante
- let landMove1 = SKAction.moveBy(x: -landTexture.size().width, y: 0, duration: 2)
- let landMove2 = SKAction.moveBy(x: landTexture.size().width, y: 0, duration: 0.0)
- let landAnimation = SKAction.repeatForever(SKAction.sequence([landMove1, landMove2]))
- //Bucle con el que situamos la imagen repetidamente en la pantalla
- var j : CGFloat = 0
- while j < 5 + self.frame.size.width / (landTexture.size().width) {
- land = SKSpriteNode(texture: landTexture)
- land.position = CGPoint(x: j * land.size.width - 500, y: -self.size.height / 2)
- land.run(landAnimation)
- land.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: self.frame.size.width, height: landTexture.size().height))
- land.physicsBody!.isDynamic = false
- land.physicsBody!.categoryBitMask = Collisions.Object.rawValue
- land.physicsBody!.collisionBitMask = Collisions.Object.rawValue
- land.physicsBody!.contactTestBitMask = Collisions.Fly.rawValue
- land.zPosition = -1
- self.addChild(land)
- j += 1
- }
- //Puntuación
- scoreLabel.fontName = "Helvetica"
- scoreLabel.fontSize = 60
- scoreLabel.text = "0"
- scoreLabel.position = CGPoint(x: self.frame.midX, y: self.frame.height / 2 - 70)
- self.addChild(scoreLabel)
- }
- //Crear los tubos y su aparición aleatoria
- @objc func createTube(){
- //TUBOS:
- let breachHeight = UInt(self.frame.size.height / 3)
- let y = UInt(arc4random()) % breachHeight
- //Texturas
- let tubeTexture1 = SKTexture(imageNamed: "tube1")
- let tubeTexture2 = SKTexture(imageNamed: "tube2")
- let tubeGroup = SKNode()
- tubeGroup.position = CGPoint(x: self.frame.width + 300, y: self.frame.minY)
- tubeGroup.zPosition = -1
- tube1 = SKSpriteNode(texture: tubeTexture1)
- tube1.size.height = self.frame.size.height / 1.5
- tube1.position = CGPoint(x: 0, y: CGFloat(y))
- tube1.physicsBody = SKPhysicsBody(rectangleOf: tube1.size)
- tube1.physicsBody!.isDynamic = false
- tube1.physicsBody!.categoryBitMask = Collisions.Object.rawValue
- tube1.physicsBody!.collisionBitMask = Collisions.Object.rawValue
- tube1.physicsBody!.contactTestBitMask = Collisions.Fly.rawValue
- tubeGroup.addChild(tube1)
- tube2 = SKSpriteNode(texture: tubeTexture2)
- tube2.size.height = self.frame.size.height / 1.5
- tube2.position = CGPoint(x: 0, y: CGFloat(y) + tube1.size.height + CGFloat(breachHeight))
- tube2.physicsBody = SKPhysicsBody(rectangleOf: tube2.size)
- tube2.physicsBody!.isDynamic = false
- tube2.physicsBody!.categoryBitMask = Collisions.Object.rawValue
- tube2.physicsBody!.collisionBitMask = Collisions.Object.rawValue
- tube2.physicsBody!.contactTestBitMask = Collisions.Fly.rawValue
- tubeGroup.addChild(tube2)
- let tubeMove = SKAction.moveBy(x: -2 * self.frame.width, y: 0.0, duration: TimeInterval(8))
- let tubeSequence = SKAction.sequence([tubeMove])
- tubeGroup.run(tubeSequence)
- //Añadir brecha
- let breach = SKNode()
- breach.position = CGPoint(x: 0, y: CGFloat(y) + tube1.size.height)
- //Añadir las físicas
- breach.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: tubeTexture1.size().width, height: CGFloat(breachHeight)))
- breach.physicsBody!.isDynamic = false
- breach.run(tubeMove)
- breach.physicsBody?.categoryBitMask = Collisions.Breach.rawValue
- breach.physicsBody?.collisionBitMask = Collisions.Breach.rawValue
- breach.physicsBody?.contactTestBitMask = Collisions.Fly.rawValue
- tubeGroup.addChild(breach)
- self.addChild(tubeGroup)
- }
- override func didMove(to view: SKView) {
- //Añadimos la física al mundo
- self.physicsWorld.gravity = CGVector(dx: 0.0, dy: -5.0)
- //Llamamos a la función para la configuración del juego
- setupGame()
- }
- func didBegin(_ contact: SKPhysicsContact) {
- if !gameOver{
- if contact.bodyA.categoryBitMask == Collisions.Breach.rawValue || contact.bodyB.categoryBitMask == Collisions.Breach.rawValue {
- score += 1
- scoreLabel.text = String(score)
- }else{
- //Añadir game over
- gameOverLabel.fontName = "Helvetica"
- gameOverLabel.fontSize = 50
- gameOverLabel.text = "Game Over! Pulsa para comenzar"
- gameOverLabel.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
- self.addChild(gameOverLabel)
- self.speed = 0
- gameOver = true
- play = false
- timer.invalidate()
- }
- }
- }
- override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
- if !gameOver {
- if !play {
- createTube()
- timer = Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(GameScene.createTube), userInfo: nil, repeats: true)
- play = true
- }
- fly.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
- fly.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 60))
- fly.physicsBody?.isDynamic = true
- } else {
- score = 0
- self.speed = 1
- gameOver = false
- self.removeAllChildren()
- setupGame()
- }
- }
- func rotacion(min: CGFloat, max: CGFloat, valor: CGFloat) -> CGFloat {
- if valor > max {
- return max
- }else if valor < min {
- return min
- }else {
- return valor
- }
- }
- override func update(_ currentTime: TimeInterval) {
- fly.zRotation = self.rotacion(min: -0.5, max: 0.5, valor: (fly.physicsBody?.velocity.dy)! * (fly.physicsBody!.velocity.dy < 0 ? 0.003 : 0.001))
- }
- }
Add Comment
Please, Sign In to add comment