Modules in Python

When I programmed Factory512 all of the code was in one long file (1897 lines in fact) which meant that it took a while to scroll through. When I started my next game I did the same but by the time it was over 3000 lines I started to wonder if there was was a better way to deal with lots of code. Daddy looked it up and told me about modules. Modules allow you to have code in separate files that all run together. You just have to import the module at the start of the larger programme and then you can treat it like function (Except that that you must go into another file to see it). And it gets compiled too!(Compiled python runs faster) Yay!

The Post in Which I Explain The Post in Which Daddy Helps me With Arrays.

Several people have indicated that my last post was pretty technical, so let me see if I can explain it differently…

It’s like this: Imagine that Daddy and I are walking down the road. I have a notebook that I can write stuff in.

First Daddy and I walk past house number 64. Daddy tells me to “Write down the number of the house we just walked past”, so I write “…the number of the house we just walked past”.

Then we walk past house number 66 and Daddy tells me to “write down the number of the house we just walked past”, so again I write “…the number of the house we just walked past”.

THEN we walk past house number 68 and Daddy tells me again to “write down the number of the house we just walked past”, so then there’s a plot twist and I write “…the number of the house we just walked past”.

Finally we walk past house number 70 and Daddy asks what I’ve written down so I look at the notebook and see:

  • …the number of the house we just walked past.
  • …the number of the house we just walked past.
  • …the number of the house we just walked past.

so I look at the number of the house we just walked past and see that it’s 70. I say to Daddy “70 70 70” and he gets annoyed.

Now replace me with my computer and Daddy with me and that is what I was talking about in the last post.

The Post in Which Daddy Helps Me With Arrays.

I am making a grid based game where you have to move big blocks around and onto buttons and stuff like that. I also wanted the player to be able to undo their moves if they made a mistake, so I needed a big array that would remember the positions of the blocks and then if the player pressed the undo button, it would move the blocks (and the player) back to their earlier positions. However, the position of the block was also an array because some blocks could be odd shapes and I needed to store all of that information as well. So basically I had a huge array of all the information that ever was in a smaller array that was the position and shape of oddly shaped blocks.

It didn’t work.

I tried to fix it but it just made the code messier and I really couldn’t figure it out. I decided to do another, better and simpler version of it and that didn’t work ether. It seemed like while the actual position of the block moved, it would change bits of the array that saved previous positions, but it would only do it in specific spots. I figured out that it would only do it when I undid moves. I found the exact line that would change one list and for some reason the other one changed. I stared at that line for quite awhile until Daddy looked at it. He went online and found out that somehow instead of the code putting the data of the array at that time in the other array, it was REFERRING to the array of the current position. So instead of reading data of the array at the time when I wanted to go back to, the code was just looking at what the blocks current position is.

I fixed it by adding [:] to the right lines and it worked!…almost. I just had to write a bit more code and fix a few more glitches, and then it worked. The end.

Blog and a Videogame.

Recently I have been working on my blog and making it look a bit better. I want to write more posts by myself and more often.

About a month ago I finished programming one of my longest videogames yet and Daddy posted it to a site called for me. The game is called Factory 512 and I made it with Python 2. It’s the first platformer that I made with all my own code.

The hardest part was making gravity work properly and the player stop when they collide with solid objects. I made my own physics for the game because I wanted more control over it. I know that when the player jumps, they go up exactly 7 pixels on the first frame and the distance they go up decreases every frame after that (even into negatives) until they hit the ground (or they get to terminal velocity which is -14 pixels per frame). Not only does the programme check collisions every frame but it also checks in between frames to prevent a fast moving object from flying right through a wall or something (although sometimes it doesn’t bother if the player is in between tiles and has no chance of hitting anything).

At first the wires between the levers and gates were very thin but after people tested the game I realized that they needed to be thicker. Unfortunately I couldn’t make them light up when powered. Maybe next time I do something similar I’ll have interactive wires.

The game is about a robot escaping from a factory – for freedom and happiness! But it is very challenging.

Click here to go to to download the game.

Next time I may or may not be talking about what I am programming now.


Yesterday at the library I demonstrated my latest programme on the projector at the library and then described how I coded it.  Question and answer followed where I was able to explain how and why I did the manipulations I did.

Tech Club

Yesterday there were two more new introductions to our coding club.  It was exciting to spend time tweaking the computers.

I spent most of my Minecraft time perfecting an igloo with my friend S!

Tech Club

Today at our weekly Tech Club we had probably the most other kids come out ever!  Hard to imagine that the first many weeks it was just Daddy and me.

Daddy thinks we had at least eleven other kids playing Minecraft and Scratch!
Afterwards Daddy had an appointment, so I went with my friend C! and his Grandma to Chapters, where Mama caught up with us and brought me home.

It was almost overwhelming!  Whew!