Raspberry Pi Demo today

21/06/2012

Computer Club is on today!

Come to room C6 (Mr. Simon’s room) at the end of the day to see a Raspberry Pi working.

A computer for £25. Can be used as a low-cost media centre (1080p output over HDMI).

Learn programming, play games, etc.

Image


Some notes on GUI programming

18/06/2012

Baby Steps

This is another little idea I have found while researching CoderDojo.

The next step to take should always be as small as possible.

 – Yes, as small as possible.

In other words, when you are writing some code, test whether or not it does what you expect. If something isn’t right, fix it before moving on.

 

GUI Programming

When you create a GUI, you are basically making a window with a grid in it. You decide where to position the widgets (labels, text boxes, buttons etc.) inside the grid and create them accordingly.

For instance:

# gui1.py : demonstrates the grid in a GUI
# Import the Tk gui-making features
from tkinter import *

# create the “root” window – within which
# everything else will be built.
root = Tk()
root.title(“GUI example”)
root.geometry(“50×100”)
app = Frame(root)
app.grid()

# setting up the GUI
# make some buttons
bttn1 = Button(app, text = “0,0”)
bttn1.grid(row = 0, column = 0)
bttn2 = Button(app, text = “0,1”)
bttn2.grid(row = 0, column = 1)
bttn3 = Button(app, text = “0,2”)
bttn3.grid(row = 0, column = 2)
bttn4 = Button(app, text = “1,0”)
bttn4.grid(row = 1, column = 0)
bttn5 = Button(app, text = “1,1”)
bttn5.grid(row = 1, column = 1)
bttn6 = Button(app, text = “1,2”)
bttn6.grid(row = 1, column = 2)
bttn7 = Button(app, text = “2,0”)
bttn7.grid(row = 2, column = 0)
bttn8 = Button(app, text = “2,1”)
bttn8.grid(row = 2, column = 1)
bttn9 = Button(app, text = “2,2”)
bttn9.grid(row = 2, column = 2)

# Start the main-loop (like in a game).
root.mainloop()

Alternative: You can download a program that does this (and a little bit more) here. Have a look at the code and make some changes – (eg, change what is printed when the button is clicked).

This will set up a little grid of buttons that should look like this:

Hopefully this will help you to visualise the way we are creating the GUI for lovegui.py.

If you want, you can cut and paste this code into a new file in Python IDLE and check out how it works and see what happens if you make some changes.

Moved from top of post!

Pair Programming

I’m keen for you to have a go at pair programming this week.  The basic idea is that two people use one computer. You could compare it to flying a plane or driving a rally car.  One person is “driving”, the other is “co-pilot”. The driver will type the code and operate the computer, the co-pilot will stay focused, read the code, spot errors and offer suggestions. As I have mentioned, this approach is used in “real” software development. If you want to read more, you can look at this webpage: Pair Programming.

Pair Programming is also part of the methodology of the CoderDojo movement. You might be interested in reading more about this also. It was started in Ireland by a Sixth-former who was keen to help young people to learn programming.

Skills

We’ve also been discussing the skills that we use when programming. If you are interested in what sort of skills programming develops, you might want to look at this page (from Learn Python The Hard Way). The author does a good job of explaining why excellent literacy skills are important for coders.


Some Print formatting in Python

28/05/2012

Some ways of working with strings

I’m haven’t got time today to write very much on this blog. So …

Here’s a text file you can download which has a Python program that uses a few of the different ways that we can work with strings.

What I’d like you to do is to edit this with IDLE. Make some changes and have a play with the way the different printing commands and options work.

Things to try:

See if you can use the multi-line printing approach to make some ascii art.

Then you could try wrting a program that asks the user a series of questions and then prints output which is formatted in an interesting way.

Can you use these methods to create a table with headings and neat layout?

happy hacking

antiloquax


I’m gonna do an internet …

20/05/2012

HTML

I thought today we’d have a look at some HTML and how to make simple web-pages!

HTML (or Hypertext Markup Language) was created by Tim Berners-Lee, the scientist who created the World Wide Web. The first version of HTML was published in June 1993.

I’m sure many of you will have made websites before, but it’s likely that you will have used a web-design package like “FrontPage” or “Dreamweaver” that allows you to build pages without actually typing in the HTML tags.

First, I’d like you to go to this page and have a look at an extremely simple web-page.

I’ll explain here as well, just in case things go wrong :).

Open “Notepad” (we don’t want to use a Word-processor as that will add formatting and mess up our HTML).

Save it as “index.html”.

Type the following in your new document:

 

For some reason the school computers don’t let you view the source of web-pages. I am going to make a sheet with some of the simple html tags I used on the two web-pages you can see here.
 

If you get finished, I’d like you to do some research into Google’s Summer of Code. Maybe this is something you’d like to do, when you are a bit older (it’s for over-18s).

mt


The Killers (Example of good dialogue).

12/05/2012

The Killers  by Ernest Hemingway

The door of Henry’s lunchroom opened and two men came in. They sat down at the counter.

“What’s yours?” George asked them.

“I don’t know,” one of the men said. “What do you want to eat, Al?”

“I don’t know,” said Al. “I don’t know what I want to eat.”

Outside it was getting dark. The streetlight came on outside the window. The two men at the counter read the menu. From the other end of the counter Nick Adams watched them. He had been talking to George when they came in.

“I’ll have a roast pork tenderloin with apple sauce and mashed potatoes,” the first man said.

“It isn’t ready yet.”

“What the hell do you put it on the card for?”

“That’s the dinner,” George explained. “You can get that at six o’clock.”

George looked at the clock on the wall behind the counter.

“It’s five o’clock.”

“The clock says twenty minutes past five,” the second man said.

“It’s twenty minutes fast.”

“Oh, to hell with the clock,” the first man said. “What have you got to eat?”

“I can give you any kind of sandwiches,” George said. “You can have ham and eggs,bacon and eggs, liver and bacon, or a steak.”

“Give me chicken croquettes with green peas and cream sauce and mashed potatoes.”

“That’s the dinner.”

“Everything we want’s the dinner, eh? That’s the way you work it.”

“I can give you ham and eggs, bacon and eggs, liver—”

“I’ll take ham and eggs,” the man called Al said. He wore a derby hat and a black overcoat buttoned across the chest. His face was small and white and he had tight lips. He wore a silk muffler and gloves.

“Give me bacon and eggs,” said the other man. He was about the same size as Al. Their faces were different, but they were dressed like twins. Both wore overcoats too tight for them. They sat leaning forward, their elbows on the counter.

“Got anything to drink?” Al asked.

“Silver beer, bevo, ginger-ale,” George said.

 “I mean you got anything to drink?”

“Just those I said.”

“This is a hot town,” said the other. “What do they call it?”

“Summit.”

“Ever hear of it?” Al asked his friend.

“No,” said the friend.

“What do they do here nights?” Al asked.

“They eat the dinner,” his friend said. “They all come here and eat the big dinner.”


Bat and Ball Game

15/04/2012

Pygame provides us with lots of ready-made tools for making games. If you haven’t already installed Pygame, you’ll find information about installing it on their website.

Liam Fraser’s video tutorials on YouTube are really great place to start learning about Pygame. The other tutorial I have found most helpful is this one by Professor Paul Craven.

Before we start working on the game, we are going to look at a very simple template for Pygame programs (download “template.py“).

If you run the program, you’ll see a simple Pygame window like this:

So, let’s see how we made it.

The first proper line of the program imports pygame itself. Then we start the game-engine with “pygame.init()”. Next we assign a list of numbers to the variable: “green”. The numbers refer to how much red, green and blue we want. This colour had no red or blue and the maximum value for green. So it is a nice deep green. Why not have a play with these numbers and make the window a different colour?

The next lines set up the Pygame window. For my example, it was 200 pixels wide and 150 tall. The example you downloaded was a slightly bigger window. The next line puts some text in the the window bar. You can change this to whatever you like. Here’s the next bit:

We put the value “False” into a variable called “done” and make a Pygame timer to control the speed of the main loop. The most important bit comes now (drumroll) it’s the main game loop.

This simple bit of code is the going to become very familiar to you if you want to make games using Pygame. The “while” loop will keep going until the variable “done” contains “False”.  Then we fill the screen with green. The next lines check for input from the user – if we click the close “x” on the Pygame window, the program will exit. The number in the “clock.tick()” command sets the Frames Per Second of the game (how fast it goes). The next line “updates” or flips the display. This basically  makes anything we have drawn to the screen actually appear. If you leave this out, nothing happens (mind you – nothing much happens in this program – yet!). Finally, outside the “while” loop, we close Pygame.

Right – now we need to get something moving on the screen and a bat! I’ll show you the code bit by bit. You can edit the template file (save it as something like “bat&ball.py first). Then, when you have finished, you can play the game.

You need to download the bat and ball images (bat and ball).

 

Here’s the first part of the program:

So, the first new thing here is another line with “import” in it. This is just to make it easier for ourselves – we won’t have to type out the full paths to some of the Pygame methods we’ll use later on. We also set up more colours, so you can easily change the background colour.

The “screen.get_rect()” command makes a special type of object out of the screen. This makes it easier for us to do a lot of things (like find out it’s size and position). After giving the window a title, we make the normal mouse pointer invisible. The next four lines load up some images in a Pygame format. and makes “rect”s from them. The bit where it says “convert_alpha” makes sure images with transparent parts are displayed properly.

We set the speed of the ball to one pixel right and one down. Then we use the “rect” commands to put the bat at the middle of the screen, 50 pixels up from the bottom. And we make a timer.

Now we enter the main loop of the game. First we fill the screen with white (change it to another colour if you like). Then there is the part which closes the game when we click the cross. The next bit finds the position of the mouse and changes the x co-ordinate of the bat (we don’t want the bat moving up and down as well!). The position of the bat is a list with two numbers in it. The x co-ordinate is the first one (which the computer thinks of as zero – that’s why it’s “position[0]”). You could see what happens if you delete the “[0]” or change it to “[1]”.  The “move()” method is a helpful thing we can do to rect objects – it just moves the ball 1 pixel right and one down (because we set speed to [1,1]).

“Collision detection” is when we check if two things are hitting each other. We could just do a simple collision detection, but then we’d get some problems – the ball would sort of get stuck as it collided with the bat over and over. If you want to see this, change this section to this:

and see what happens if you get the edge of the bat near the ball.

Anyway, in the proper code, I first check to see if the bottom of the ball (ballrect.bottom)  is touching the top of the bat (batrect.top). Next I check if the ball is between the ends of the bat. (I’ve added the “-3” and “+3” bit to avoid making the ball bounce when we hit the transparent corners of the square in which the ball is drawn.)

This is what our “ball” really looks like. The grey checked bits are transparent! You could see what happens if you remove the “-3” and “+3” bits or change the numbers.

The next bit is to make the ball “bounce” if it hits 3 edges of the screen. So if the left side of the ball is at less than x co-ordinate “0” or more than the width of the screen, it switches the speed to the opposite direction (eg one pixel left and one up for the first bounce). We do the same for the top – but not the bottom. If you don’t hit the ball, it’s game over!

The “blit” commands draw the bat and the ball in their new positions and the clock is set to 40. You can change this if you want the game to run faster. Then we do “pygame.display.flip()” to make these change get displayed.

And that’s it.

If you get this far, try changing some stuff. You could download a different image to use as the ball, or the bat! You could also use a picture for the background instead of a solid colour – ask me how in class!

antiloquax


The Destructors

26/03/2012

Graham Greene’s classic story The Destructors.

Please read this story. Click on the link to open the pdf file.

We’ve looked at two stories in which the characters do something challenging in order to prove something to themselves (and perhaps others). In the next few lessons I want you to plan and write a story in which a young person does something unusual and difficult.

Make a start by thinking about characters and setting.