Guest User

Untitled

a guest
Jul 21st, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.97 KB | None | 0 0
  1. import SpriteKit
  2. import GameplayKit
  3.  
  4. class GameScene: SKScene, SKPhysicsContactDelegate {
  5.  
  6. //Enum con las distintas colisiones
  7. enum Collisions: UInt32 {
  8. case Fly = 1
  9. case Object = 2
  10. case Breach = 4
  11. }
  12.  
  13. //Variables de todos los elementos del juego
  14. var fly = SKSpriteNode()
  15. var background = SKSpriteNode()
  16. var land = SKSpriteNode()
  17. var tube1 = SKSpriteNode()
  18. var tube2 = SKSpriteNode()
  19. var scoreLabel = SKLabelNode()
  20. var score = 0
  21. var gameOverLabel = SKLabelNode()
  22. var gameOver = false
  23. var timer = Timer()
  24. var play = false
  25.  
  26.  
  27. func setupGame() {
  28.  
  29. //MOSCA:
  30. //Texturas
  31. let flyTexture1 = SKTexture(imageNamed: "fly1")
  32. let flyTexture2 = SKTexture(imageNamed: "fly2")
  33. let flyTexture3 = SKTexture(imageNamed: "fly3")
  34. //Movimiento
  35. let animationFly = SKAction.animate(with: [flyTexture1,flyTexture2, flyTexture3], timePerFrame: 0.1)
  36. let moveFly = SKAction.repeatForever(animationFly)
  37. fly = SKSpriteNode(texture: flyTexture1)
  38. fly.position = CGPoint(x: self.frame.midX - 150, y: self.frame.midY)
  39. fly.zPosition = -1
  40. fly.run(moveFly)
  41. //Físicas
  42. fly.physicsBody = SKPhysicsBody(circleOfRadius: flyTexture1.size().height / 2)
  43. fly.physicsBody!.isDynamic = false
  44. fly.physicsBody!.allowsRotation = false
  45.  
  46. fly.physicsBody!.categoryBitMask = Collisions.Fly.rawValue
  47. fly.physicsBody!.collisionBitMask = Collisions.Object.rawValue | Collisions.Fly.rawValue
  48. fly.physicsBody!.contactTestBitMask = Collisions.Object.rawValue | Collisions.Breach.rawValue
  49.  
  50. self.addChild(fly)
  51.  
  52. //FONDO:
  53. //Textura
  54. let backgroundTexture = SKTexture(imageNamed: "background")
  55. //Animación para de movimiento constante
  56. let backgroundMove1 = SKAction.moveBy(x: -backgroundTexture.size().width, y: 0, duration: 8)
  57. let backgroundMove2 = SKAction.moveBy(x: backgroundTexture.size().width, y: 0, duration: 0.0)
  58. let backgroundAnimation = SKAction.repeatForever(SKAction.sequence([backgroundMove1, backgroundMove2]))
  59.  
  60. //Bucle con el que situamos la imagen repetidamente en la pantalla
  61. var i : CGFloat = 0
  62. while i < 5 + self.frame.size.width / (backgroundTexture.size().width) {
  63. background = SKSpriteNode(texture: backgroundTexture)
  64. background.position = CGPoint(x: i * backgroundTexture.size().width, y: -self.frame.midY)
  65. background.size.height = self.size.height
  66. background.run(backgroundAnimation)
  67. background.zPosition = -2
  68. self.addChild(background)
  69.  
  70. i += 1
  71. }
  72.  
  73. //SUELO:
  74. //Textura
  75. let landTexture = SKTexture(imageNamed: "land")
  76. //Animación para de movimiento constante
  77. let landMove1 = SKAction.moveBy(x: -landTexture.size().width, y: 0, duration: 2)
  78. let landMove2 = SKAction.moveBy(x: landTexture.size().width, y: 0, duration: 0.0)
  79. let landAnimation = SKAction.repeatForever(SKAction.sequence([landMove1, landMove2]))
  80.  
  81. //Bucle con el que situamos la imagen repetidamente en la pantalla
  82. var j : CGFloat = 0
  83. while j < 5 + self.frame.size.width / (landTexture.size().width) {
  84. land = SKSpriteNode(texture: landTexture)
  85. land.position = CGPoint(x: j * land.size.width - 500, y: -self.size.height / 2)
  86.  
  87. land.run(landAnimation)
  88.  
  89. land.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: self.frame.size.width, height: landTexture.size().height))
  90. land.physicsBody!.isDynamic = false
  91.  
  92. land.physicsBody!.categoryBitMask = Collisions.Object.rawValue
  93. land.physicsBody!.collisionBitMask = Collisions.Object.rawValue
  94. land.physicsBody!.contactTestBitMask = Collisions.Fly.rawValue
  95.  
  96. land.zPosition = -1
  97. self.addChild(land)
  98.  
  99. j += 1
  100.  
  101. }
  102.  
  103. //Puntuación
  104. scoreLabel.fontName = "Helvetica"
  105. scoreLabel.fontSize = 60
  106. scoreLabel.text = "0"
  107. scoreLabel.position = CGPoint(x: self.frame.midX, y: self.frame.height / 2 - 70)
  108. self.addChild(scoreLabel)
  109.  
  110.  
  111. }
  112.  
  113. //Crear los tubos y su aparición aleatoria
  114. @objc func createTube(){
  115. //TUBOS:
  116.  
  117. let breachHeight = UInt(self.frame.size.height / 3)
  118. let y = UInt(arc4random()) % breachHeight
  119.  
  120.  
  121.  
  122. //Texturas
  123. let tubeTexture1 = SKTexture(imageNamed: "tube1")
  124. let tubeTexture2 = SKTexture(imageNamed: "tube2")
  125.  
  126.  
  127. let tubeGroup = SKNode()
  128.  
  129.  
  130. tubeGroup.position = CGPoint(x: self.frame.width + 300, y: self.frame.minY)
  131.  
  132. tubeGroup.zPosition = -1
  133.  
  134.  
  135. tube1 = SKSpriteNode(texture: tubeTexture1)
  136. tube1.size.height = self.frame.size.height / 1.5
  137. tube1.position = CGPoint(x: 0, y: CGFloat(y))
  138. tube1.physicsBody = SKPhysicsBody(rectangleOf: tube1.size)
  139. tube1.physicsBody!.isDynamic = false
  140.  
  141. tube1.physicsBody!.categoryBitMask = Collisions.Object.rawValue
  142. tube1.physicsBody!.collisionBitMask = Collisions.Object.rawValue
  143. tube1.physicsBody!.contactTestBitMask = Collisions.Fly.rawValue
  144.  
  145.  
  146. tubeGroup.addChild(tube1)
  147.  
  148.  
  149.  
  150. tube2 = SKSpriteNode(texture: tubeTexture2)
  151. tube2.size.height = self.frame.size.height / 1.5
  152. tube2.position = CGPoint(x: 0, y: CGFloat(y) + tube1.size.height + CGFloat(breachHeight))
  153. tube2.physicsBody = SKPhysicsBody(rectangleOf: tube2.size)
  154. tube2.physicsBody!.isDynamic = false
  155.  
  156. tube2.physicsBody!.categoryBitMask = Collisions.Object.rawValue
  157. tube2.physicsBody!.collisionBitMask = Collisions.Object.rawValue
  158. tube2.physicsBody!.contactTestBitMask = Collisions.Fly.rawValue
  159.  
  160. tubeGroup.addChild(tube2)
  161.  
  162.  
  163. let tubeMove = SKAction.moveBy(x: -2 * self.frame.width, y: 0.0, duration: TimeInterval(8))
  164.  
  165. let tubeSequence = SKAction.sequence([tubeMove])
  166.  
  167. tubeGroup.run(tubeSequence)
  168.  
  169.  
  170. //Añadir brecha
  171. let breach = SKNode()
  172. breach.position = CGPoint(x: 0, y: CGFloat(y) + tube1.size.height)
  173.  
  174.  
  175. //Añadir las físicas
  176. breach.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: tubeTexture1.size().width, height: CGFloat(breachHeight)))
  177. breach.physicsBody!.isDynamic = false
  178. breach.run(tubeMove)
  179. breach.physicsBody?.categoryBitMask = Collisions.Breach.rawValue
  180. breach.physicsBody?.collisionBitMask = Collisions.Breach.rawValue
  181. breach.physicsBody?.contactTestBitMask = Collisions.Fly.rawValue
  182.  
  183. tubeGroup.addChild(breach)
  184.  
  185. self.addChild(tubeGroup)
  186.  
  187.  
  188. }
  189.  
  190. override func didMove(to view: SKView) {
  191. //Añadimos la física al mundo
  192. self.physicsWorld.gravity = CGVector(dx: 0.0, dy: -5.0)
  193.  
  194. //Llamamos a la función para la configuración del juego
  195. setupGame()
  196.  
  197.  
  198. }
  199.  
  200. func didBegin(_ contact: SKPhysicsContact) {
  201.  
  202. if !gameOver{
  203. if contact.bodyA.categoryBitMask == Collisions.Breach.rawValue || contact.bodyB.categoryBitMask == Collisions.Breach.rawValue {
  204.  
  205. score += 1
  206. scoreLabel.text = String(score)
  207.  
  208. }else{
  209.  
  210. //Añadir game over
  211. gameOverLabel.fontName = "Helvetica"
  212. gameOverLabel.fontSize = 50
  213. gameOverLabel.text = "Game Over! Pulsa para comenzar"
  214. gameOverLabel.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
  215. self.addChild(gameOverLabel)
  216. self.speed = 0
  217. gameOver = true
  218. play = false
  219. timer.invalidate()
  220.  
  221. }
  222. }
  223. }
  224.  
  225. override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  226.  
  227. if !gameOver {
  228.  
  229. if !play {
  230.  
  231. createTube()
  232. timer = Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(GameScene.createTube), userInfo: nil, repeats: true)
  233. play = true
  234. }
  235. fly.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
  236. fly.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 60))
  237. fly.physicsBody?.isDynamic = true
  238.  
  239.  
  240. } else {
  241.  
  242. score = 0
  243. self.speed = 1
  244. gameOver = false
  245. self.removeAllChildren()
  246. setupGame()
  247. }
  248.  
  249. }
  250.  
  251. func rotacion(min: CGFloat, max: CGFloat, valor: CGFloat) -> CGFloat {
  252.  
  253. if valor > max {
  254. return max
  255.  
  256. }else if valor < min {
  257. return min
  258.  
  259. }else {
  260. return valor
  261.  
  262. }
  263. }
  264. override func update(_ currentTime: TimeInterval) {
  265.  
  266. fly.zRotation = self.rotacion(min: -0.5, max: 0.5, valor: (fly.physicsBody?.velocity.dy)! * (fly.physicsBody!.velocity.dy < 0 ? 0.003 : 0.001))
  267.  
  268. }
  269. }
Add Comment
Please, Sign In to add comment