🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Discover Python and Patterns (1): Start

Published November 07, 2019
Advertisement

In this series, I propose an introduction to programming for beginners. To do so, I choose the Python language, which is today one of the best (if not the best) language to discover programming. And, as you may guess, we’ll program games

Before starting anything we need a development environment. There are many possibilities with Python, I propose here one of the most popular: Anaconda with Spyder. Anaconda is a tool that contains Python with many libraries and applications. The default package has a lot of them, including Spyder. Later, it is easy to add/remove/upgrade these libraries and applications. Spyder is an IDE dedicated to Python, its use is simple and easy.

To get Anaconda, download the Python 3 distribution here. Run the installer, and be patient… Installation takes a while. At the end of the installation, you can check the boxes if this anaconda will be the main Python env you will use. Otherwise, don’t touch anything, you can easily run programs from Spyder without these options. Then, if you are using Windows, search “Spyder” in the start menu, and you should see something like this:

Spyder

The left part is the current Python program:


# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script
"""

 

These lines are only comments, they don’t do anything if you run it. The first line starting with a ‘#’ is a single line comment, and the following ones are multi-lines comments, starting and ending with a triple double quote.

The upper right part shows some help – there is other tabs we’ll see later.

The lower right part shows the console – this is where messages are printed.

Create your first program

Let’s create now a new program from the menu File / New File… A new tab appears in the top left part, with already some content: this is also comments, they will not lead to any action. Type print(“You win !”) right after, and you should get something like:


# -*- coding: utf-8 -*-
"""
Created on Wed Oct  2 21:49:54 2019

@author: phylyp
"""

print("You win !")

 

Save it from the menu File / Save, for instance with the file name “win.py”. Your interface should look like the following one:

Spyder first program

This first program is a really easy game: nothing to do you always win

Run your first program

To run this program, click the play icon in the top bar:

Spyder run

You should see the following dialog. Check “Remove all variables before execution”, it’ll prevent unexpected errors later:

Spyder run properties

Then, in the console in the bottom right part, you should see the following new lines:


In [1]: runfile('C:/dev/win.py', wdir='C:/dev')
You win !

In [2]:

 

Your Spyder interface should look like this one:

Spyder run first program

The first line In [1]: runfile('C:/dev/win.py', wdir='C:/dev') is a command Spyder typed itself to run the python program “C:\dev\win.py” in the directory “C:\dev” (Spyder uses slash ‘/’ instead of ‘\’, it works the same). Do not worry about this line.

Print function

The second line...

Continue reading this post on https://learngameprog.com...

0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement