Flask pyhton framework and Scirra. An experiment.

Discussion and feedback on Construct 2

Post » Fri Dec 16, 2011 9:58 pm

Hey everyone, I have been lurking for a while but would like to share my enthusiasm for Construct 2. I am about to embark on a project using Scirra to develop a front end for an app with a python powered back end.

I am a novice construct user and my java script is limited, however I have been using python for a while now and I am creating the back end with the Flask pyhton framework.

I would like to share my experiences during the project if anyone is interested.
B
16
S
2
G
4
Posts: 67
Reputation: 3,295

Post » Fri Dec 16, 2011 10:10 pm

I've been working on something similar tbh although I've been using the twisted libraries for python. Makes a lot of the networking aspects nice. although I'm not sure how the event driven aspect with work with Construct2 atm as far as network based communication.
B
5
G
2
Posts: 18
Reputation: 1,286

Post » Thu Dec 29, 2011 11:41 am

Just resurrecting this, the project is underway now so expect some more info soon.

B
16
S
2
G
4
Posts: 67
Reputation: 3,295

Post » Thu Dec 29, 2011 3:38 pm

Please do let us know how it goes - if there are any obvious missing features to make this work we might be able to add them in to a future release!
Scirra Founder
B
359
S
214
G
72
Posts: 22,946
Reputation: 178,468

Post » Thu Jan 26, 2012 10:03 am

Hey Ashley and co,

Apologies for leaving this thread for so long. Been some crazy stuff going on so have been looking after my boy full time for a while.

I have made some wonderful progress so far and I think flask and construct are a lush match for anyone wanting to move away from php or who already have python experience from construct 1.

I will write a complete tutorial to help people get started when I have the time however I will share some of the possibilities now.

Also I have a missing feature for you Ashley: The ability to set custom save paths for the runtime file so that it doesn't need to be manually edited, no biggie but it speeds things up:

On a flask site the folder structure is like this:

app.py (in the root)
/templates (folder for templates, ie this is where html of the game lives)
/static (all static files so I have the basic runtime files in here then subfolders /images and /media)

My app looks like this:

app.py
/static
/static/allthejquerybits.js
/static/mygamename/runtime and appcache.js
/static/mygamename/media/myaudio.ogg
/static/mygamename/images/myimages.png
/templates/game.html

I edit the html to reflect this and when I export the project I set the right paths for images and media so I don't have to do it manually for every file in the runtime js.

As far as things you can easily do (I will make a full tutorial on flask soon enough) here is a quick one.

On an ubuntu dev box install pip:

sudo apt-get install python-pip
sudo pip install flask

Create a directory called "mygame" to store your files.
make two sub directories called "static" and "templates" we will come back to these.

Make a new file called app.py in your mygame directory.

paste this into it:

###################################
#import your dependancies
from flask import Flask, request, render_template

#Set key for encrypting cookies
SECRET_KEY = 'development key'
#Turn on debug mode
DEBUG = True

#define app
app = Flask(__name__)
app.debug = DEBUG
app.secret_key = SECRET_KEY

#create our method called funnysentance
#Start with defining the route
@app.route('/funnysentance')
#define your function
def funnysentance():
    #fetch arguments from url using request.args.get
    a = request.args.get('a', None)
    b = request.args.get('b', None)
    #make the funny sentance
    c = a+" make me happy in the "+b
    #return the result to the browser/ajax
    return c

#render your game on a url
@app.route("/mygame")
def mygame():
    #render the html
    return render_template('mygame.html')
    
#set up the app to run
if __name__ == '__main__':
    app.run(host='0.0.0.0')

########################

Open a command prompt on the path of this file and type python app.py

Open a browser and put this in the url bar:
http://127.0.0.1:5000/funnysentance?a=eggs&b=morning

You will get your sentance using the variables parameters in the url.

Make a simple game with a button and a text box, when the button is clicked it makes an ajax call to:
http://127.0.0.1:5000/funnysentance?a=eggs&b=morning

and sets the text in the text box to AJAX.lastData

Export the files making sure you put your template renamed mygame.html into the "templates" folder

Put the c2runtime files in the "static" folder and make sure you change the path of c2runtime.js in your mygame.html file. If you do use images and sounds be sure to place their folders in the "static" folder. When you export the project append static to the paths of media and images and this will make sure you have the right paths in runtime.js.

Still with me?

Good

Navigate to http://127.0.0.1:5000/mygame and you should see your game, click the button and you should get a sentance in your text box using the url encoded variables.

If you are using a headless server not an ubuntu desktop vm just navigate to http://serverip:5000/mygame and you should see it.

Cool I should get some actual work for money done now. Hope you guys enjoyed this. I will do a bigger tutorial involving databases and other fun things as soon as poss.

Mike


B
16
S
2
G
4
Posts: 67
Reputation: 3,295

Post » Thu Feb 16, 2012 8:56 am

The project is moving on a little. I wanted to package the front end for android so tried directly exporting to phone gap. However it uses oauth and sessions so it was a no go. With a little effort however I managed to wrap the app, served by flask into a webview using the Android Hello Web tutorial.

So far so good, the game is not finished yet but is nearly there.

I will post a webview tutorial soon for anyone who wants to wrap a server dependent app in its own webview or you can just head to the Android documentation and follow the tuts.
B
16
S
2
G
4
Posts: 67
Reputation: 3,295


Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 8 guests