Jaseman's Python Lessons


61 posts   Page 1 of 3   1, 2, 3
by Jaseman » Sun Jan 29, 2012 5:21 pm
Inspired by Liam Fraser's fantastic video tutorials on YouTube:

http://www.youtube.com/user/Ra.....iTutorials ....  I've started putting together some lessons of my own, to help beginners get to grips with programming.

Before you start with these, you will need to have Geany, and Pygame installed on your computer or virtual machine.

The lessons can be downloaded from this link:

https://skydrive.live.com/redir.aspx?cid=f41ca64cc38a2ec5&resid=F41CA64CC38A2EC5!182&parid=F41CA64CC38A2EC5!117

The lessons are in .png image format.  You can either view them straight from my Skydrive folder, or download them/print them out as you wish.

I will also include a copy of the completed source code, as well as any other files that may be required.

I shall let you know here when new lessons have been added.

Please let me know if you find these helpful.  I will try to make them as understandable as I can.  If you don't understand something, or get stuck, let me know, and I will try to help you.

NOW GET CODING!

All the best.

Jaseman
Posts: 302
Joined: Tue Jan 10, 2012 12:59 pm
by 1aws » Sun Jan 29, 2012 7:55 pm
Excellent tutorials Jason. Keep up the good work. I'm looking forward to your next offerings.
Posts: 37
Joined: Fri Dec 30, 2011 8:43 pm
by Jaseman » Mon Jan 30, 2012 1:33 pm
Lesson Three is now ready:

https://skydrive.live.com/redir.aspx?cid=f41ca64cc38a2ec5&resid=F41CA64CC38A2EC5!227&parid=F41CA64CC38A2EC5!182

It was quite challenging getting the maths right on this one.

You might not understand some of it, but follow it through and hopefully it will start to make sense.

We are learning about using time to control the sequence of events.
Posts: 302
Joined: Tue Jan 10, 2012 12:59 pm
by meltwater » Mon Jan 30, 2012 4:04 pm
Any chance you can have a short description of each tutorial?

Would help decide which ones to follow and which ones I'd rather skip etc.
______________
http://www.themagpi.com/
A Magazine for Raspberry Pi Users
Read Online or Download for Free.

My new book: goo.gl/dmVtsc

Meltwater's Pi Hardware - pihardware.com

Like the MagPi? @TheMagP1 @TheMagPiTeam
User avatar
Posts: 993
Joined: Tue Oct 18, 2011 11:38 am
by gimliflea » Mon Jan 30, 2012 5:10 pm
I've worked through lessons 1 & 2 and can't get 2 to work. Is this me being hopeless, or is there a bit missing. i have compared my file with your .txt and they seem to be the same. Time warp changes but there does not seem to be any code to update the secs day years.

Is this an exercise for the noob or a little omission? :-)
Posts: 76
Joined: Fri Nov 18, 2011 4:14 pm
by Jaseman » Mon Jan 30, 2012 6:01 pm
Posts: 302
Joined: Tue Jan 10, 2012 12:59 pm
by Jaseman » Mon Jan 30, 2012 6:08 pm
gimliflea said:


I've worked through lessons 1 & 2 and can't get 2 to work. Is this me being hopeless, or is there a bit missing. i have compared my file with your .txt and they seem to be the same. Time warp changes but there does not seem to be any code to update the secs day years.

Is this an exercise for the noob or a little omission? :-)

I'm guessing that you missed something.  Did you put this line in?:

gameseconds +=timewarp

Posts: 302
Joined: Tue Jan 10, 2012 12:59 pm
by gimliflea » Mon Jan 30, 2012 6:14 pm
You guess right. Thanks
Posts: 76
Joined: Fri Nov 18, 2011 4:14 pm
by Jaseman » Tue Jan 31, 2012 12:48 am
Bonus reference material:

http://thepythongamebook.com/en:start

I found this website to be a very good reference point.
Posts: 302
Joined: Tue Jan 10, 2012 12:59 pm
by meltwater » Tue Jan 31, 2012 9:15 am
Jaseman said:


How"s this for you?

https://skydrive.live.com/redir.aspx?cid=f41ca64cc38a2ec5'resid=F41CA64CC38A2EC5!255'parid=F41CA64CC38A2EC5!182
Open the index html file.


Excellent!  Thank you.
______________
http://www.themagpi.com/
A Magazine for Raspberry Pi Users
Read Online or Download for Free.

My new book: goo.gl/dmVtsc

Meltwater's Pi Hardware - pihardware.com

Like the MagPi? @TheMagP1 @TheMagPiTeam
User avatar
Posts: 993
Joined: Tue Oct 18, 2011 11:38 am
by Jaseman » Tue Jan 31, 2012 1:07 pm
Lesson Four is done, and I think you're going to like it!

https://skydrive.live.com/redir.aspx?cid=f41ca64cc38a2ec5&resid=F41CA64CC38A2EC5!182&parid=F41CA64CC38A2EC5!117

Anyone managed to get through Lesson Three yet?  I haven't had any comments about it.
Posts: 302
Joined: Tue Jan 10, 2012 12:59 pm
by playmobot » Tue Jan 31, 2012 9:17 pm
You should host your index.html file i on web site. If you need a free host ive had success with 000webhost.com in the past.
Posts: 12
Joined: Wed Aug 24, 2011 9:25 pm
by Jaseman » Wed Feb 01, 2012 10:21 am
playmobot said:


You should host your index.html file i on web site. If you need a free host ive had success with 000webhost.com in the past.



It's a good idea.  I haven't really thought much about promoting these tutorials to a wider audience.  I might end up getting bombarded with questions and requests 'How can I do this?' or 'Why isn't this working?'.

For now I'm just going to keep it within this little community. However feel free to share the link to my Skydrive folder with others.

I would really appreciate feedback.  I may slip up somewhere, or miss telling you about an important step in the process.

You have to understand that I'm not a serious programmer - I'm learning as I go along, and sharing that experience with you guys.  I used to play around with AMOS basic on the Amiga years ago and this is really similar.

But don't think that my examples are 'Best Practice' when it comes to programming.  There may be more efficient ways of doing things.

I've got lots of ideas and concepts - and it's those which I'm sharing with you.  It might stir up some ideas for you, and I would love it if I manage to help some people to get started, so that they can find their own way and write even better code for themselves.

When I've been scouting around for examples of code, I always find that the example is over-complicated, when all I want is a simple straight forward demo.  The reference guides tend to be: COMMAND NAME (PARAMETERS) - but it doesnt give you any example of parameters or syntax.
Posts: 302
Joined: Tue Jan 10, 2012 12:59 pm
by gimliflea » Wed Feb 01, 2012 11:36 am
Thanks Jaseman I'm finding these very helpful. So far all the bugs have been self inflicted. I have been working towards learning python for sometime and find your approach helpful as I find I can now understand the code pretty well.

The pygame book referred to above is helpful  for a more theoretical understanding, but I am one of those learners who likes to have practice first and theory later.

Thanks again and keep them coming.
Posts: 76
Joined: Fri Nov 18, 2011 4:14 pm
by Jaseman » Wed Feb 01, 2012 5:44 pm
gimliflea said:


Thanks Jaseman I'm finding these very helpful. So far all the bugs have been self inflicted. I have been working towards learning python for sometime and find your approach helpful as I find I can now understand the code pretty well.

The pygame book referred to above is helpful  for a more theoretical understanding, but I am one of those learners who likes to have practice first and theory later.

Thanks again and keep them coming.



That's good to hear.  Lesson five is uploaded:

https://skydrive.live.com/redir.aspx?cid=f41ca64cc38a2ec5&resid=F41CA64CC38A2EC5!284&parid=F41CA64CC38A2EC5!182
Posts: 302
Joined: Tue Jan 10, 2012 12:59 pm
by Jaseman » Thu Feb 02, 2012 7:38 pm
I have a little dillema now.

You see, over the last 2 days I totally re-wrote my program from scratch.  It's much better now, and some of the things I've put in my tutorials are no longer relevant.

So, I'm wondering whether to redo the five tutorials from scratch, replacing them with a much improved version, or to leave them as is, and just post the code for my new program and say... Take a look at this.

I think rewriting will be the best option, as I won't be happy leaving a tutorial out there that I'm not completely happy with.

At the same time I don't want to confuse the people that have already followed the tutorials or are part way through.

I thought of another idea, which is to use my chat room, and schedule a 'live' lesson.  I should be able to post the code straight into the chat room and you could paste it into Geany.  This way you could ask questions if you get stuck, and maybe we can work together, as I'm sure you will be able to put ideas my way too.  I quite like the idea of being part of a live coding group, perhaps meeting up online at a certain hour of the evening.

What should I do?
Posts: 302
Joined: Tue Jan 10, 2012 12:59 pm
by gimliflea » Thu Feb 02, 2012 8:53 pm
Hi Jaseman

I worked thro lesson five. I had to put add an "int" as below.
Lesson 5 slide 10
line 130 needs to read sunx=int(...
 
Your experience of re-writing after a first hack at it is not at all unusual. You could turn it 
into an interesting teaching point about about how more thinking and designing before
 coding saves work in the long run.
And an explanation of why version two is so much better.
It all rather depends on where you want to go with this.
 
I don't like the idea of pasting the code your .png format is good because it does not allow
 that. Typing the code in gets the syntax into your fingers and greatly helps the learning.
 
I  would like the chance to have a live chat sometimes. But would probably not make it on
a regular basis.
 
As a bit of background I am semi-retired so not the target audience. I spent the eighties
as an assembler programmer in industry, until my life took me in other directions. I have
always had a problem with High level languages but the launch of the rpi has inspired me
to get going again.
 
Posts: 76
Joined: Fri Nov 18, 2011 4:14 pm
by gimliflea » Thu Feb 02, 2012 8:55 pm
How do you get this forum to format properly?
Posts: 76
Joined: Fri Nov 18, 2011 4:14 pm
by Jaseman » Fri Feb 03, 2012 4:55 pm
Here's the updated version of my demo

https://skydrive.live.com/redir.aspx?cid=f41ca64cc38a2ec5&resid=F41CA64CC38A2EC5!308&parid=F41CA64CC38A2EC5!182

You'll need the image 'scrollyback.png' to be saved in the same folder as the demo.py file.

My next plan is to add some purple mountains to sit between the grass and sky.

I've added a variable called machine speed.  You have to use # to comment out as appropriate, because I've noticed that the speed it runs at varies wildly from one computer to another.  This won't be a problem once we all have Raspberries, because we will all have the same hardware.

When you run the program, and it gets past the sunrise, press h for help screen and r to restart.  Some other keys are mentioned on the help screen.  The position of your mouse at the time you press the keys makes a difference.  Put the mouse pointer towards the top for low numbers, and move it lower for high values.

The biggest difference is that now everything has it's own surface, and I've gradient filled the backgrounds with rectangles.  I've added a fade in text and a scrolly text at the start, and a randomly generated star field is drawn onto the darkness surface.  I've abandoned the years/days and seconds, in favour of a different approach.  You place the sun somewhere and then give it a target destination to achieve.  I've also introduced a runstage variable as a way of controlling the transition from one section of the demo to the next... IE runstage 0 is the fade in text, runstage 1 for scrolly text, runstage 2 for sunrise and runstage 3 for sunset.

If you've done lessons 1-5 and look through the code, hopefully you can work out what is going on in this latest version.

I'll let you know what's going on with the lessons soon.  I'll keep making them as png images.  I agree that when you are learning, it's better if you have to type in the code yourself rather than just copy & paste it.  That way you might retain some of the knowledge.
Posts: 302
Joined: Tue Jan 10, 2012 12:59 pm
by Jaseman » Fri Feb 03, 2012 5:09 pm
Oh and by the way....

The screen size is now set to 1280x512.

If you are running inside an Oracle VirtualBox, go to the start menu and pick Preferences>Monitor Settings.  Change the resolution to 1280x900..ish

Then hold down the right CTRL key on your keyboard and press C to switch your display to scale mode.

If your monitor is set to less than 1280 width - increase it!  If you can't increase it, get a better monitor!
Posts: 302
Joined: Tue Jan 10, 2012 12:59 pm
by Jaseman » Sat Feb 04, 2012 10:17 pm
I've now installed Python, Pygame and Geany onto my 32-bit Windows 7 PC.

Oh boy!, does it run better than through that Oracle Virtual Box?  It's only about 20 times faster.

Geany is buggy though in Windows.  I'm still using it to edit the files, but to run them, I go to the command prompt and do... cd\python32

Then I type in filename.py to launch the program.

Here's how to install it for Windows:

WINDOWS:

If you have 32-bit windows, download 'Python 3.22 Windows x86 MSI Installer' or for 64-bit download 'Python 3.22 X86-64 MSI Installer' from http://python.org/download/

Then go to http://pygame.org/download.shtml and download pygam-1.9.2a0.win32-py3.2.msi

Next go to http://www.geany.org/Download/Releases

Download geany-021_setup.exe - Full installer including GTK 2.16

Right click on Computer, Advanced System Settings, Environment Variables.  In System variables, edit the path and add ;c:\python32 onto the end of the path.

Make sure that c:\python32\python.exe is the default program for opening .py files.

Create a subfolder under c:\python32 to store your programs.

I'll migrate back across to Linux if I ever get my hands on a Raspberry Pi.
Posts: 302
Joined: Tue Jan 10, 2012 12:59 pm
by stormy1 » Sun Feb 05, 2012 6:19 am
Jaseman said:



Then go to http://pygame.org/download.shtml and download pygam-1.9.2a0.win32-py3.2.msi


Had problems finding that file it is here:

http://pygame.org/ftp/

not on the main download page
Posts: 60
Joined: Fri Jan 06, 2012 3:44 am
by stormy1 » Sun Feb 05, 2012 6:21 am
Would it possible to get a zip or gz file with all the lessons in it rather than downloading them one by one?
Posts: 60
Joined: Fri Jan 06, 2012 3:44 am
by Jaseman » Sun Feb 05, 2012 1:15 pm
stormy1 said:


Would it possible to get a zip or gz file with all the lessons in it rather than downloading them one by one?



Sure.  That's easily done.  Since I'm going to be recreating all the lessons from scratch, I'll make sure to upload a zip with everything contained within.  I'll also have them as seperate downloads, because some people will only be interested in a particular command or feature.

I'm thinking that I should possibly wait until I have a Raspberry so that the lessons can be geared towards that.  It's easier than trying to cater for all the different hardware/os variations that people might be using.  I'm eager to know what the performance of python will be like.  I'll put stuff out there though in the meantime, because I'm learning new things at a rapid pace now, so keep checking here for new stuff.

Cheers.
Posts: 302
Joined: Tue Jan 10, 2012 12:59 pm
by Jaseman » Mon Feb 06, 2012 1:11 am
I've added a new program 'fadetext.py':

https://skydrive.live.com/redir.aspx?cid=f41ca64cc38a2ec5&resid=F41CA64CC38A2EC5!312&parid=F41CA64CC38A2EC5!182

Some instructions are included.  I've gone right back to basics for this one, because I've covered some new things here, like running your program from the command prompt rather than from within Geany.

Hopefully it will work okay for you.

I noticed that I named it fadetext.py in Windows and fadetxt.py in linux - a mistake on my part - but it doesnt really matter what you call it.

All it does is fade in/out some text like an intro to a Hollywood movie or a game IE 'Jaseman Presents....'

Using this method, you can break down a big program into smaller parts and create a batch/bash script to run each little program in sequence.
Posts: 302
Joined: Tue Jan 10, 2012 12:59 pm