Passa al contingut principal

Build a Snake Game – Interface Creation

In this tutorial series, you’ll learn how to create a game like Snake. The objective of the game is to grab the apples on screen to raise the score. Read on!




Step 1: Application Overview


Build a Snake Game

Using pre-made graphics we will code an entertaining game using Lua and the Corona SDK API’s.


The player will be able to hit a puck by dragging the paddle on the screen, you can modify the parameters in the code to customize the game.


Step 2: Target Device





The first thing we have to do is select the platform we want to run our app within, this way we’ll be able to choose the size for the images we will use.


The iOS platform has these characteristics:



  • iPad 1/2: 1024x768px, 132 ppi

  • iPad 3: 2048×1536, 264 ppi

  • iPhone/iPod Touch: 320x480px, 163 ppi

  • iPhone 4/iPod Touch: 960x640px, 326 ppi

  • iPhone 5/iPod Touch: 1136×640, 326 ppi


Because Android is an open platform, there are many different devices and resolutions. A few of the more common screen characteristics are:



  • Asus Nexus 7 Tablet: 800x1280px, 216 ppi

  • Motorola Droid X: 854x480px, 228 ppi

  • Samsung Galaxy SIII: 720x1280px, 306 ppi


In this tutorial, we’ll be focusing on the iOS platform with the graphic design, specifically developing for distribution to an iPhone/iPod touch, but the code presented here should apply to Android development with the Corona SDK as well.




Step 3: Interface





A simple and friendly interface will be used. This involves multiple shapes, buttons, bitmaps and more.


The interface graphic resources necessary for this tutorial can be found in the attached download.




Step 4: Export Graphics





Depending on the device you have selected, you may need to export the graphics in the recommended ppi, you can do that in your favorite image editor.


I used the Adjust Size… function in the Preview app on Mac OS X.


Remember to give the images a descriptive name and save them in your project folder.




Step 5: Sound





We’ll use Sound Effects to enhance the feeling of the game, the sounds used in this app were generated by AS3SFXR.




Step 6: App Configuration


An external file will be used to make the application go fullscreen across devices, the config.lua file. This file shows the original screen size and the method used to scale that content in case the app is run in a different screen resolution.



application =
{
content =
{
width = 320,
height = 480,
scale = "letterbox"
},
}



Step 7: Main.lua


Let’s write the application!


Open your prefered Lua editor (any Text Editor will work, but you won’t have syntax highlighting) and prepare to write your awesome app. Remember to save the file as main.lua in your project folder.




Step 8: Code Structure


We’ll structure our code as if it were a Class. If you know ActionScript or Java, you should find the structure familiar.



Necesary Classes
Variables and Constants
Declare Functions
contructor (Main function)
class methods (other functions)
call Main function



Step 9: Hide Status Bar



display.setStatusBar(display.HiddenStatusBar)

This code hides the status bar. The status bar is the bar on top of the device screen that shows the time, signal, and other indicators.




Step 10: Background





A simple graphic is used as the background for the application interface, the next line of code stores it.



-- Graphics
-- [Background]
local bg = display.newImage('bg.png')



Step 11: Title View





This is the Title View, it will be the first interactive screen to appear in our game, these variables store its components.



-- [Title View]
local titleBg
local playBtn
local creditsBtn
local titleView



Step 12: Credits View





This view will show the credits and copyright of the game, this variable will be used to store it.



-- [CreditsView]
local creditsView



Step 13: Game Background





This image will be placed on top of our previous background. The following lines also store the graphics for the on-screen pad.



-- [Game Background]
local gameBg
-- [Pad]
local up
local left
local down
local right



Step 14: Apple





This is the apple graphic, referenced in the next variable. Grabbing these items will increase the snake size/parts.



-- [Apple]
local apple



Step 15: Head





The first part of the snake, it will be in the stage at start. A hit area will be created on top of it and both will be grouped in the head variable.



-- Head
local headGFX
local headHitArea
local head



Step 16: Snake Part





This graphic will be added each time the snake eats an apple.




Step 17: Score


The next line handles the textfield that will display the scores.



-- Score
local score



Step 18: Variables


This are the variables we’ll use, read the comments in the code to know more about them.



-- Variables
local dir --current direction of the snake
local started --used to start the timer
local timerSrc
local speed = 500
local mConst = 17 --# of pixels to move every timer count
local apples --apples group
local lastPart --last part added to snake
local firstPart
local parts -- parts group
local current = 0 --a number assigned to each part



Step 19: Declare Functions


Declare all functions as local at the start.



-- Functions
local Main = {}
local startButtonListeners = {}
local showCredits = {}
local hideCredits = {}
local showGameView = {}
local gameListeners = {}
local movePlayer = {}
local hitTestObjects = {}
local update = {}



Step 20: Constructor


Next we’ll create the function that will initialize all the game logic:



function Main()
-- code...
end



Step 21: Add Title View


Now we place the TitleView in the stage and call a function that will add the tap listeners to the buttons.



function Main()
titleBg = display.newImage('titleBg.png', display.contentCenterX - 100.5, 40.5)
playBtn = display.newImage('playBtn.png', display.contentCenterX - 27, display.contentCenterY + 10)
creditsBtn = display.newImage('creditsBtn.png', display.contentCenterX - 48, display.contentCenterY + 65)
titleView = display.newGroup(titleBg, playBtn, creditsBtn)
startButtonListeners('add')
end



Next Time…


In this part of the series you’ve learned the interface and the basic setup of the game. In the next and final part of the series, we’ll handle the Snake movement, collision detection, and the final steps to take prior to app release like testing, creating a start screen, adding an icon and, finally, building the app. Stay tuned for the final part!







via Mobiletuts+ http://mobile.tutsplus.com/tutorials/corona/corona-sdk-build-a-snake-game-interface-creation/

Comentaris

Entrades populars d'aquest blog

Learn Composition from the Photography of Henri Cartier-Bresson

“Do you see it?” This question is a photographic mantra. Myron Barnstone , my mentor, repeats this question every day with the hopes that we do “see it.” This obvious question reminds me that even though I have seen Cartier-Bresson’s prints and read his books, there are major parts of his work which remain hidden from public view. Beneath the surface of perfectly timed snap shots is a design sensibility that is rarely challenged by contemporary photographers. Henri Cartier-Bresson. © Martine Franck Words To Know 1:1.5 Ratio: The 35mm negative measures 36mm x 24mm. Mathematically it can be reduced to a 3:2 ratio. Reduced even further it will be referred to as the 1:1.5 Ratio or the 1.5 Rectangle. Eyes: The frame of an image is created by two vertical lines and two horizontal lines. The intersection of these lines is called an eye. The four corners of a negative can be called the “eyes.” This is extremely important because the diagonals connecting these lines will form the breakdown ...

El meu editor de codi preferit el 2024, que això ja se sap que va canviant 😄

Visual Code Visual Code és un editor de codi font lleuger, però potent que s’executa al teu escriptori i està disponible per a Windows, macOS i Linux. Compta amb suport integrat per a JavaScript, TypeScript i Node.js i té un ric ecosistema d’extensions per a altres llenguatges i entorns d’execució (com C++, C#, Java, Python, PHP, Go, .NET).  És una eina ideal per a desenvolupar i depurar aplicacions web i en el núvol. Per què Visual Code? Visual Code té molts avantatges com a editor de codi font, com per exemple: És gratuït, ràpid i fàcil d’instal·lar i actualitzar. Té un ampli ecosistema d’extensions que et permeten afegir funcionalitats i personalitzar la teva experiència de desenvolupament. Té un suport integrat per a molts llenguatges i entorns d’execució, i et permet depurar i executar el teu codi des del mateix editor. Té una interfície senzilla i elegant, amb diferents temes i modes de visualització. Té un sistema de sincronització de configuracions que et permet guardar les...

Las Mejores Aplicaciones Gratis para iPad de 2012

Las Mejores Aplicaciones Gratis para iPad de 2012 : ¿No tienes ni un duro? No te preocupes, pues hoy os traemos una extensa selección de las mejores apps gratuitas que puedes conseguir en la App Store para que llenes tu iPad de calidad, sin gastar nada de nada.   ¿Estás buscando juegos o apps gratis para tu iPad? En la App Store hay más de 500,000 apps y juegos, y una gran cantidad de ellos está disponible de forma totalmente gratuita. Aquí vamos con la selección de las mejores Apps gratis para iPad (todos los modelos), organizada por categoría. ¿Estás preparado? Las Mejores Apps Gratis de Redes Sociales para iPad Nombre Facebook Gratis Categoría Redes sociales Facebook es la red social más famosa del mundo , con casi mil millones de usuarios. Su app para iPad ha tardado, pero aquí está. Nombre Twitter Gratis Categoría Redes sociales Twitter es la red de microblogging por excelencia. La forma más rápida y directa de informar y mantenerse informado de las cosa...