Difference between revisions of "User:Saka/Gemini Pages/Tut:FPS"

From LifeWiki
Jump to navigation Jump to search
m (better words... ... ...)
Line 1: Line 1:
Ah! You wants to Pythoning? Well, young lad, look no further than your computer. Here you will learn the basics of Python-scripting in Golly™.
Ah! You want to write some Python? Well, look no further. Here you will learn the basics of Python-scripting in Golly™. Note that, while this won't function as a complete tutorial for Python, you should be able to still follow a bit even if you have no Python knowledge.




== Things to Install ==
== Things to Install ==
Wait! You need to pack some stuff for the trip. You need:
Wait! You need to prepare some stuff first. You need:
*[[Golly]]
*[[Golly]]
*Python 2.7+ (But not Python 3, since Golly doesn't support Python 3 as of now...)
*Python 2.7+ (But not Python 3, since Golly doesn't support Python 3 as of now...)
Line 12: Line 12:


== What are we Going to Make? ==
== What are we Going to Make? ==
For this tutorial we are going to make a very simple script that detects the [[period]] of the object on screen. Golly already has a built-in script for this exact thing (See oscar.py)but we are going to make it anyway because why not. For this script, it will only detect the period of an oscillator, not a spaceship, because that's too complicated for a simple person like me. But who am I? <s>And where is I?</s>
For this tutorial we are going to make a very simple script that detects the [[period]] of the object on screen. Golly already has a built-in script for this exact thing (See oscar.py)but we are going to make it anyway because why not. We will start by detecting the period of an oscillator, then move on to making it able to detect the period of spaceships.




=== The Idea ===
=== The Idea ===
The idea of this script is to take a the pattern and run it until it either repeats or crosses the set border of time. You'll understand later (hopefully) once we get into making the script. Let's draw an outline of our program in a flow chart (Note: I have no idea how to draw a flow chart):<br/>
The idea of this script is to take a the pattern and run it until it either repeats or crosses the set border of time. You'll understand later (hopefully) once we get into making the script. Let's draw an outline of our program in a flow chart (Note: The author has no idea how to properly draw a flow chart):<br/>
[[File:Flow1.png]]<br/>
[[File:Flow1.png]]<br/>
So there, we have the flow our program is going to go in. Now we just need to translate Ingleesh to Pythonese. To start, open a new Python IDLE window.
So there, we have the flow our program is going to go in. Now we just need to translate that to Python. To start, open a new Python IDLE window, or any text editor, in fact.


== Let's get making! ==
== Let's get scripting! ==
Horray! It's fun time!


=== Step 1: Setting things up ===
=== Step 1: Setting things up ===
Let's set things up.
Let's set things up.


Believe it or not, Golly has it's own Python module! It is used to make scripting easier and contains other useful non-Golly features such as easy dialog boxes without the need of the bulky tkinter commands. To import the Golly module, simply type
Believe it or not, Golly has it's own Python module (A python module is basically a set of functions)! It is used to make scripting easier and contains other useful non-Golly-related features such as easy dialog boxes without the need of the bulky tkinter commands. To import the Golly module, simply type


<pre>
<pre>
Line 32: Line 31:
</pre>
</pre>


The 'as g' is not needed, but will make it easier. Commands in that are packed into the Golly module can be found in Golly's help files.
The 'as g' is not needed, but will make typing out the commands (Which are usually used a lot in Golly scripts) less tedious. Commands in that are packed into the Golly module can be found in Golly's help files under Help -> Python Scripting.
 
We'll also need a variable for the period of the pattern, so put this in as well:
<pre>
period = 0
</pre>


=== Step 2: Is there a pattern? ===
=== Step 2: Is there a pattern? ===
There is a very simple way to test if the pattern is empty in Golly. If you read the Golly help files, you probably have found it already. If you haven't, it's <pre>g.empty()</pre>
There is a very simple way to test if the pattern is empty in Golly. If you read the Golly help files, you probably have found it already. If you haven't, it's <pre>g.empty()</pre>
The g.empty() command returns a simple true or false. It will return true if the pattern is empty and false otherwise...
The g.empty() command returns a simple True or False. It will return True if the pattern is empty and False otherwise. So, we first want to check if there even ''is'' a pattern to check the period of. Let's do that by using the Python if statement:
<pre>
if g.empty():
    g.warn("No pattern!")
    g.exit()
</pre>
This also shows us two other Golly commands:
*'''g.warn()''' opens a dialog box with the classing yellow triangle warning symbol with the specified message.
*'''g.exit()''' stops the script.

Revision as of 13:24, 20 May 2020

Ah! You want to write some Python? Well, look no further. Here you will learn the basics of Python-scripting in Golly™. Note that, while this won't function as a complete tutorial for Python, you should be able to still follow a bit even if you have no Python knowledge.


Things to Install

Wait! You need to prepare some stuff first. You need:

  • Golly
  • Python 2.7+ (But not Python 3, since Golly doesn't support Python 3 as of now...)
  • Computer
  • Keyboard
  • Mouse (Optional)

If you are on a Mac, Python should already be installed (But alas, the author of this article doesn't have a "Mac", so the author is relying on information from others.). Once you have packed your stuff and prepared you can start your Pythoning adventure!

What are we Going to Make?

For this tutorial we are going to make a very simple script that detects the period of the object on screen. Golly already has a built-in script for this exact thing (See oscar.py)but we are going to make it anyway because why not. We will start by detecting the period of an oscillator, then move on to making it able to detect the period of spaceships.


The Idea

The idea of this script is to take a the pattern and run it until it either repeats or crosses the set border of time. You'll understand later (hopefully) once we get into making the script. Let's draw an outline of our program in a flow chart (Note: The author has no idea how to properly draw a flow chart):
Flow1.png
So there, we have the flow our program is going to go in. Now we just need to translate that to Python. To start, open a new Python IDLE window, or any text editor, in fact.

Let's get scripting!

Step 1: Setting things up

Let's set things up.

Believe it or not, Golly has it's own Python module (A python module is basically a set of functions)! It is used to make scripting easier and contains other useful non-Golly-related features such as easy dialog boxes without the need of the bulky tkinter commands. To import the Golly module, simply type

import golly as g

The 'as g' is not needed, but will make typing out the commands (Which are usually used a lot in Golly scripts) less tedious. Commands in that are packed into the Golly module can be found in Golly's help files under Help -> Python Scripting.

We'll also need a variable for the period of the pattern, so put this in as well:

period = 0

Step 2: Is there a pattern?

There is a very simple way to test if the pattern is empty in Golly. If you read the Golly help files, you probably have found it already. If you haven't, it's

g.empty()

The g.empty() command returns a simple True or False. It will return True if the pattern is empty and False otherwise. So, we first want to check if there even is a pattern to check the period of. Let's do that by using the Python if statement:

if g.empty():
    g.warn("No pattern!")
    g.exit()

This also shows us two other Golly commands:

  • g.warn() opens a dialog box with the classing yellow triangle warning symbol with the specified message.
  • g.exit() stops the script.