Coder+Dojo+Python+Code+examples

3rd Week
We start with another piece of code downloaded from http://codentronix.com/2011/04/20/simulation-of-3d-point-rotation-with-python-and-pygame/ This shows a slightly more complicated version of the cube rotation program we played with last week, Here we make a new data structure called faces, and the draw the edges of the cube on a face by face basis. The rest of the cube generation rotation and viewing code is the same.

The reason for drawing the cube on a face by face basis, is that we can use the faces later to determine which faces are closest to us, and by filling them in last, we can solve the hidden surfaces problem. I'll talk about this in class.

With this we want to play first of all. So: Try the following: Change the colors of the cube edges.

Change the size of the cube sides, Can you use decimal points in the numbers here?

Change the speed of rotation, making each of the axis rotate at different speeds.

A lot trickier, can you make two cubes?

This will involve going into the code at the head of the program and putting in another 8 points for the corners of the new cube, and then going into the list of sides, and working out what the next six sides, the sides of the new cube will be. After that everything works, but I found the process took a bit of time.

This introduces the concept of Refactoring code. This code is just fine for one cube, but it becomes quite messy when we go to the two cube solution. Can you see how we might rewrite this code so that its easier to make more cubes?

Ideally we would like a function like MakeCube(Location,Side1=1,Side2=1,Side3=1,RotX=0,RotY=0,RotZ=0)

This will not be easy, but might be something to look at for next week. Some more versions of the code. More detailed notes on these will be added later: The code file below is my simple version of the wireframe cube slightly colored with the code somewhat simplified. The vertices are now defined using a variable a rather than the value 1 which was hard coded in the original file. This will allow us to change the size of the cube by changing the value of this variable rather that hard coding it in 24 places. The results of running this are identical to those of the previous version of the file, but we have now refactored the code so as to allow for easier modification later. The version below plays around with the dimensions of the cube to make it into a cuboid, slows down the rotation speed so that its easier to see whats going on, and by changing some of the numbers in the section defining vertices, changes the connectivity of the cube edges, resulting in a cuboid with some diagonals added and some edges missing. This version has solid multicolored faces This would allow us to MakeCube(location) at the location given by Location, and optionally change the length of the sides, and the angle at which the cube is sitting. This would make the code a lot easier to use, and more readable too.

Blender (and python)
Start by finding, downloading and installing blender. On windows machines there is a self extracting script, for OS10 and linux based systems it arrives as a zip archive which you have to unzip. Then you just run the program.

Instructions for Making a cube rotate in blender, try following this video: media type="youtube" key="UVeya_IWPPU" width="420" height="315" Blender stuff rotating a cube Finally to connect back to running python, heres a stack overflow discussion on running a python script called Draw.Car! http://stackoverflow.com/questions/11604548/running-python-script-in-blender 2nd Week 2015 We are going to start with a file for rotating a set of dots. This is downloaded from this website: http://codentronix.com/2011/04/20/simulation-of-3d-point-rotation-with-python-and-pygame/

1st Week 2015 This was the talk, its in powerpoint, which you may ar maynot have on your computer. The python I discussed first was loops: like media type="custom" key="27117238" m) Heres another version of the code, but this time we have added a While loop that does the same thing media type="custom" key="27117410" We next looked at defining a function, and setting up default input values for some of the variables.

The final part of python code we looked at was defining a class. An example of that code is here: media type="custom" key="27117434" Week 6 Here is the code for Jea n Luc's version of the sprite collector program. note you will have to have the image files for the knight and the dragon in the same directory as the code for it to work right! Dragon Knight

The program where we replace blocks by knights and dragons. Try running the code. The critical piece of code in this version is these lines

if color == RED: self.image = pygame.image.load("knight_01.png").convert else: self.image = pygame.image.load("dragon.png").convert self.image.set_colorkey(WHITE) Jean Luc went over these in class, What happens here is we test the colour of the block we are creating, and if it is the red block, it gets replaced by the knight image, otherwise it gets replaced by a dragon. One other line here is this one: self.image.set_colorkey(WHITE) What this does is it sets the white parts of the mage thats read in to be transparent, and this makes the result when we have a lot od dragons look a lot more realistic.
 * 1) self.image.fill(color) text from arcade games website

To try for Saturday: Look at the code running in circles in week 5 below. See if you can convert some of the walls in our maze runner program into sprites and make them move in circles. Think about: Possibly loading images to replaces the walls in the middle of the rooms. I have a vision of the third room with snakes swinging around in cirlces!

Running in Circles with a knight. In this program, we have altered the running in Circles program so that the player is now a knight! A JellyFish And its accompanying file: Maze runner with Jelly Fish:survival mode!

Week 5

Alternative version of with multicoloured targets.



Find where this code was changed, and try to understand it. The next thing we discussed was reading in an image instead of creating a sprite as a square This code version below should show you how this is done



Here we read in an image GreenGlob.png each time we create a block, and so the screen is now full of images instead of blocks. To make this nicer, we want to read in different images for the sprites we are eating and for ourselves. Think about how we might do this. Also for this code to run, you need to have a .png file called GreenGlob.png in the python directory where your program lives. You can either make a file using paint or some other program, or you can download a .png file and rename it. If you cant find one, try right clicking on the image infront of the Week5 caption above and saving it in the python directory. This should work.

The third thing we discussed was posting a score to the pygame window where we play the game as opposed to placing it in the IDLE window.



This code places it as a caption Again this should show one way of doing it. In this case we post it as a caption at the top of the pygame window. Finally, Here's a link to a more interesting version of this code: [|ProgramArcadeGames.com/python_examples/f.php?file=sprite_circle_movement.py]

This code is on thee website ProgramArcadeGames.com Which is where a lot of the programs we have modified here came from. That version will need a little editing before it will run, you may have to remove line numbers! I have a cleaner version of the code here: Just to keep everything in focus. Our long term plan is to modify code from each of these and put it back into our maze runner program so that we end up with the walls in the program moving and interacting with our sprite.

Walking through the original sprite_collect_blocks.py code at the top we have two lines: import pygame import random These are pulling in the pygame library, and pulling in a library called random. Most of what we do here in the program involves using bits out of these libraries.

Next we define colours BLACK, WHITE, and RED These are defined in terms of sets of three numbers which tell the computer, what color to put on the sreeen when we refer to red, White or Black

We then have a line which starts with the word class This is where we make the blocks. The next section after this sets up pygame, and defines the screen size for the game window. You can play with the numbers here and see how the change things.

After this we set up a list for all the blocks we make.

After this we make 50 blocks, and put them in the list. This line

for i in range(50): Says we make 50 blocks. We move down and make the red block next.

Week 4

Down load this file as we will also try playing with this one. Our aim at the end is to combine this with the maze runner to make a game.



We want to look at the code in more detail trying to understand bits of its structure. We will also play with it to see if we can make it more fun.

l =Week 3 = Today we will be working within the IDLE with the above File

We sre looking at code and trying to identify bits and pieces of whats going on. Our aim is to look at control structures and the idea of a function, and we want to see some before we build them ourselves. There is a video about the above code which is worth watching here: code http://youtu.be/5-SbFanyUkQ code

=Week 1 =

What do you need to install on your laptop?  - Python 3.x ([])  - Pygame (not needed 1st week)  ([]) Other resources : ebook "Invent your ownComputer Games"  ([]) At the end of this we had a working copy of python installed on our laptops.

** For the MAC people: **
Ok folks, to do before Saturday

If you have a MAC, we want you to install Virtualbox software. You should be able to download it from here:

[] To install this you may have to enable instalation of software not directly downloaded from the Apple store. This software is from Oracle, which is a large reputable company sot its robably safe to do this now.

What this does is make your Mac run a ring fenced version of another OS which will be easier to run python and Pygame on. Once you have virtual box installed on your Mac, we can give you a version of Linux, with Python and Pygame which will run on top of the virtual box software.

We started the IDLE development environment, which opened up a new window. and we were able to check by running the import pygame command, that pygame had arrived safely. After this we finally got to play with python. We added two numbers in the command window 2 + 3 and when we hit return, the computer printed 5 And we ran the command print(“Hello World”) To run the code here, just hit the run button, below! <media type="custom" key="26660788" We then went to the file menu and chose to create a new file We entered this command into the field we created, and used either f5 or the run command from the top menu bar to run this file. The computer then prompted us to save the file, which we did by giving it a file name, and it that ran the file. We saw that it gave us much the same output as we had got when we ran the command interactively. We then went back into the file and changed the command to print(“Hello World) Here we have left out the " at the end of the "Hello World And when we ran this file the computer complained. media type="custom" key="26660872" We went back in and fixed the error. The next thing I introduced was the use of triple quotes “”” for really long lines””” and we discussed how you could use this for your game introduction.
 * What we did next**

I next introduced the idea of a variable called x via the command x = 2, and then print(2*x) which gave us 4 I then changed x by writing x=”Sam “ on the command line and then asked you to guess what I would get if I ran print(100*x) When we ran it we got a whole load of Sam Sam Sam Sam Sam….. media type="custom" key="26660920" The last thing we did was to set y = “Fred” and then use the following command print(20*(x+y)) which printed a whole string of Sam Fred Sam Fred Sam Fred….. Final example code from class: media type="custom" key="26660960"

Chalenge
The following piece of code gets you to give the program your name and then prints it out. media type="custom" key="26661060" Alter the code so that it prints out some thing like the Sam Fred Sam Fred sequence, but with your name instead of Sam's