Python Games – Week 4: Lists and For Loops

We learned about some new concepts today in the Python Games group – multi-line strings, lists, for loops, methods, elif statements and the dictionary data type.

We had a quick look at the Hangman Game from Invent Your Own Computer Games with Python (http://inventwithpython.com/chapter9.html). We covered Lists which are a way of storing a number of values in one variable and had some fun writing code snippets mainly based around X-Factor. We looked at some new ways of working with strings called multi-line strings.  We also learned about new types of functions called methods and a new type of loop called a for loop. At the end of the session we started to get to grips with Pygame. Our code snippets are available here and my slides from today are here python session_4

ModderDojo Athenry Topic 6: JavaScript Operators and ScriptCraftJS Drone Functions

Our mods:

Download them here: https://cdathenry.wordpress.com/2013/11/10/modderdojo-athenry-our-scriptcraftjs-minecraft-mods/

Operators:

Operators in any programming language are used when you want to calculate something new: they operate on values. variables, or expressions to produce a new value.

Since ScriptCraftJS is built on the JavaScript langauge, it uses standard JavaScript operators. As it happens, many other programming languages (including C, C++ and Java) use the same operators or very similar ones.

JavaScript Operators

Drone Functions:

As we have seen before, in ScriptCraft you use a drone to do your building for you. The drone has functions that are part of it.

Here are some of the main drone functions that are useful when building your mods:

ScriptCraft Drone Functions

You can find lots more about these and other functions in the ScriptCraft API Reference:
https://github.com/walterhiggins/ScriptCraft/blob/master/docs/API-Reference.md

ModderDojo Topic 5: Moving from Scratch to JavaScript

GeneralFeaturesOfProgrammingLanguages

Reminder: JavaScript is a well-established programming language, mainly used in web development. ScriptCraft is a Minecraft mod that allows you to write JavaScript code for building structures in Minecraft and writing new Minecraft mods. (So it’s a mod for creating other mods.)

Steps 1 and 2: Try out ScriptCraft commands and write simple mods

Please look back to last week’s notes for now to do this: https://cdathenry.wordpress.com/2013/10/05/modderdojo-week-1-getting-started-with-scriptcraft-and-javascript/

Step 3: Comparing JavaScript to Scratch

Some people criticise Scratch as being “childish”, but I don’t agree. While it is designed so that even 8 year olds can use it, it is still has all of the key features of ‘adult’ programming languages, as listed in the image at the top of this post.

(Technically, any programming language with variables, decision and loops is Turing Complete.)

This means that, if you already know how to write a Scratch programs that use these features, you will be able to apply that knowledge to any other language, such as JavaScript. The syntax of JavaScript is different, but it uses the same computational thinking.

Variables-Operators

Loops

Decisions

Notes:

  • Even though they have basic ideas in common, every programming language has its own specific commands that relate to its purpose: Scratch is focused on 2D games and animations, while ScriptCraft is focused on operating inside Minecraft, and JavaScript generally is used for interactive websites.
  • the echo command that features in these slides is not a standard JavaScript command, it is just used in ScriptCraft to display things on your screen in Minecraft.  Everything else is standard JavaScript.

Scratch Advanced – Week 4 – Cryptography the Caesar Cipther

This week we created on of the most famous ciphers the Caesar Cipher. This cipher is named after the Roman emperor Julius Caesar.

The Caesar cipher works by shifting each letter of the alphabet by a specific number.

In the example of the Caesar Cipher below the letters in the PlainText alphabet have been shifted by three places.

Cipher shift

The letter a becomes D, the letter g becomes J and so on.

Using a Caesar Cipher with an offset of three the words Coder Dojo Athenry would become FRGHU GRMR DWKHQUB

The notes for this session on how to create a Caesar Cipher using Scratch are here: Scratch Encryption project Ceasar Cipher

Scratch Beginners – Week 4, Animation

In this session we improved on our previous Ghostcatcher game by adding animation.

bigideas

We did this by switching between costumes. Rather than using sprites with various costumes already created, we drew our own.

animation

For the first time, we used the broadcast block, to communicate between different parts of the code.

broadcast

A PDF version of the notes from this weeks session can be found here: CDA-S3-Challenge04-BetterGhostCatcher.pdf

Python Beginners – Week 4: Adventure Game

This week we looked at creating a game from the beginning and at the steps we might go through to complete it. We looked at flowcharts and pseudocode and then we broke down the program into bite size pieces and wrote functions for these. We got about half way through this and hopefully we’ll complete it next week. Here is a screenshot of what we have completed so far.

screenshotHere are my slides from this week. python session4

Beginners Scratch – Challenge 4 – Create a GhostBuster Game!

This week, our lead mentor was Martha Madden, who blogs at kidswithskills.wordpress.com.

The challenge was to develop a game with a PacMan character, controlled by the mouse, who has to catch ghosts that move at random.

Coding concepts that we discussed included:

  1. Design of programs
  2. Loops
  3. Decisions
  4. Using the Paint Editor in Scratch

Here are the notes from the day (PDF): CDA-S2-Challenge04-GhostCatcher.pdf

If you would like us to send you the PowerPoint slides of my notes, get in touch via twitter or by leaving a comment.

 

Advanced Scratch – Week 4 – Truchet Tiling.

We were supposed to do this last week, but we had so much fun with the Spirals that we didn’t have enough time.

What we are going to learn this week.

  • Recap on Saving Images created in Scratch to Picture files on your Computer.
  • Drawing an accurate image – this is important for Truchet Tiling.
  • Recap on Exporting Sprites.
  • Truchet Tiling – these are cool…:)

Recap on Saving Images.

First you need to run you Scratch Project in Full Screen mode.Then you press the Print Screen button, this should be on the top right of your keyboard somewhere 🙂 Nothing appears to happen when you press that button, but in the background the screen has been saved to your clipboard (same as copy and past).

Then, from the Start menu, go to Accesories and Paint, when Paint is open you cna just Paste in your picture. You will have to edit it a little as you will have the full screen in there!

Sorry, that was for the Windows Users, I’m not sure what it is on a Mac 😦

Truchet Tiling.

These are designs based on work done by Sébastien Truchet

From Wikipeadia:

One particular pattern that he studied involved square tiles split by a diagonal line into two triangles, decorated in contrasting colors. By placing these tiles in different orientations with respect to each other, as part of a square tiling, Truchet observed that many different patterns could be formed.”
We can do them quite easily in Scratch, there are just a coule of key things that need to be done correctly.

Drawing an accurate image.

Start a new Scratch Project, delete the poor Cat as he is fed up being in every project and draw your own Sprite.

The key here is that your Sprite must obey the following rules:

  1. It must be perfectly Square.
  2. Any Lines that you draw must touch the edge of the Square at the exact Centre.

I did this in a few stages.

First draw a Square, in black, with a Vertical Line and a Horizontal Line, it should look something like this, don’t make it too big please.

Advanced Tip 1 -If you hold down the shift key when drawing the square in the Paint Editor, Scratch will force it to be a Square, you wont have to guess. This also applies to circles, if you hold the shift key down they will be perfectly round.

Advanced Tip 2 – If you draw a small square using tip 1, you an then use the stamp tool to make it into four perfect squares.

Now you can draw your own lines from the centre of each side, these don’t need to be accurate as long as they go to the centre of each side, in fact the more wibbly they are the better, then rub out the black lines, either use the rubber or the fill tool. So you will go through this…

To the final image which should look like this…

Now when you are back in your Scratch Project you will need to make a note of how big your Sprite is as you will need this information later…

And finally we can get to some coding. Basically you are going to go to the Top Left of the Screen STAMP your sprite, move it along the the right, so it is next to the first STAMPING, turn it round by 90,180 or 270 degrees and STAMP it again.You keep repeating this until you get to the far right of the screen, you then start another line and repeat until the Screen is full.

This means that your one little Sprite that looks pretty boring…

Can make a random picture which is quite interesting and could be used as a Maze in a game for example.

The Code you will need.

Remember, the screen is 480 wide by 360 tall, and you should have the dimensions of your Sprite from earlier as well.

If you drag your Sprite to the Top Left of the Screen you can see what your Starting X and Y coordinates should be…

You can then put these numbers into your code, which should give you somethign like this…

Next, you need to work out how many times you can STAMP this Sprite across the screen and down the screen.

480 / Size of your Sprite – This will give you the number of times you can go across – we can call this number – columns

360 / Size of your Sprite – This will give you the number of Rows you will be able to make across the screen – we can call this number – rows

Advanced Tip 3 – These numbers are not critical you may want to round up, or even add one to whatever you get to make sure the screen is completely filled.

So make a couple of variables, rows and columns and put them into your script as well, somethign like this…

We now need a two loops (repeat blocks) one inside the other, I did the columns inside the rows, if you do this it will make it easier. For each column we need to STAMP the Sprite and for each Row we need to go back to the left of the screen and down one row.

We know where the left is, in my case it is -210, and we know that the Sprite is roughly 60 high, so we need to drop down 60 or so as well. I found that to make sure teh edges joined up nicely that it was better to overlap by a couple of Pixels, that is why I used 58 as the number of Pixels to drop down.

Like this…

Finally you need to STAMP your Sprite and turn it round ready for the next STAMP. You will need to use a random number here, so it will turn 90, 180 or 270 degrees. So a random number between 1 and 3  multiplied by 90. Once you have the Sprite turned you will also need to move it along so it will STAMP next to the last STAMP. Again this is where you use the size of your Sprite that you noted down earlier, and once again, you might need to reduce it by a little bit in order to get the lines joining up nicely.

Once you have this working, you can change your Sprite to somehting different, as long as you stick with the two golden rules above you should still get interesting patterns, here are some ot the other ones that I have done.

This Sprite…

Makes this Maze…

Remeber, the trick with this is to get your little Sprite nice and accurate, everything else will then just fall into place.

Try colouring in either the centre or the two corners, see what patterns that can make. Something like these:

Advanced Tip 4 – Don’t forget to Export this Sprite and keep it with your Button Sprite you Exported last week. We are going to use these building blocks later on to build a Game.