python tutorial

Post here about scripting and programming for HaloPC (audio, network, ai, etc.)
Post Reply

Would you like me to do a python tutorial?

Yes
2
50%
No
0
No votes
Why the hell would we need a python tutorial
2
50%
 
Total votes: 4

sparkicks





Posts: 151
Joined: Sat May 15, 2004 8:07 am

python tutorial

Post by sparkicks »

i really have no idea why a programming forum is here and it has vb tutorials in it unless you can some how edit halo by reverse engineering but if anyone wants i could make a tutorial on python programming (dont flame me if that would be useless cus everyone needs vb) but if you want it i can give you a push start
Last edited by sparkicks on Sat Feb 26, 2005 7:37 am, edited 1 time in total.
~{DE}~Demolisher





Posts: 100
Joined: Thu Dec 02, 2004 8:35 am
Location: MS,USA
Contact:

Post by ~{DE}~Demolisher »

Well any tutorial helps.
Signature exceeded 75KB.
sparkicks





Posts: 151
Joined: Sat May 15, 2004 8:07 am

Post by sparkicks »

well ok here we go (i might change bits later)

ok python= programming language which you type in code into something like notepad and it makes it into code but you can do graphics aswell if you get pygame and easygame (ill explain later) when you run it the python module will get the code out of the text pad you just wrote it in and run it... so as from request i have just explained what python is (as if anyone didnt know.... this is the programming section)
ok first note . i have just begun programming so i can only give you a push start. and second i make mistakes so if i left out a bit in a code point it out
First thing is to download python 2.3 ( not 2.4 2.3 ok?)www.python.org and the reason its 2.3 i want you to download is that if your going to make games or any graphics with python you will need ... sort of a plugin for it. the too programs you will need are easygame and pygame.
search the net for those and pygame comes with an install file and easygame ; well put all the files in the easygame folder you download in the python folder

Just as a note to people. programming is not about learning commands that make you do cool stuff. programming is being creative about the commands you have to make the program do what you want to do.



ok now first open python IDLE and you will see loads of version stuff at the top and a space to type under neath. now first off when you write the code python will run the code so it is python that is doing the work and running it. to make it a program as such you should compile it which basicly takes python code that runs it and sticks it to the program and makes it an exe. now we will start codding.

Ok i will say stright off that my website teaches you to program so if i left anything out go to www.sparkicks.com and learn to program page.

ok now go to file and new window and save that as : whatever you want here.py
now type in

Code: Select all

 print "hello world" 
and press f5 or go to run and run module. it prints hello world. now that is the first step that usually all programming tutorials take. (the hello world bit) they usually use that to show the print command.
now , to explain something new. There is a thing called a varible. it is basicly like x and y. it represents things. only in maths you dont know what it is. its like a box and you can store something in it and look at it during the program and change it. so to make a varible we say
variblenamehere= "hello world"

Code: Select all

 hehehellowhateveryouwant= "this is what is in the varible"
Now you will notice at this stage that i used "" around my thing to say . well this is because i want the program to know it is words im saying it to write not a varible. if i say

Code: Select all

 print hehehellowhateveryouwant
it will say what the varible --- is equal to. if i said

Code: Select all

 print "hehehellowhateveryouwant"
it would actually print the word (well not exactly a proper word now but anyway) hehehellowhateveryouwant. so whatever is in a "" is called a string. so try this code

Code: Select all

 whatever1= "yes i am good"
whatever2= "yea yea yea whatever"
heheidontknowwhattocallthisone= "yawn"
print "this is a string and im going to print a varible now"
print whatever1
print whatever2
print heheidontknowwhattocallthisone 
*lets breath out* ok now that we have that done i want you to fiddle around with printing varibles and strings and one more thing. to print 2 things on the same line say

Code: Select all

print "string here " + "another string " + andvariblehere
another thing. varibles cant have spaces in them and they must start with a letter (the letter part i strongly believe but not 100% sure)
ok so once you have done all that fiddling we will be doing an if command
the way it works its

Code: Select all

 if hehe== "yes":
      print "hehe is yes"
Main Mistakes :
no : at the end of an if command
to say something is = to somethin just use one = if you are saying if it is equal to something you say ==. the other thing to add to an if command is the else command. if whatever the condition is , is not met then do this
eg

Code: Select all

 answer= "yes"
if answer == "yes"
       print "ans is yes"
else:
     print "ans is not yes ans is something else"
now usually i would tell people at the start of the program but .. well the only excuse is that i forgot.
usually you would have loads of lines of code when you get programing and its confusing when you want to edit a bit of it. so first make your work spaced out and leave nots after code

Code: Select all

 varible2= "the house" # this is the varible that says what item i have in my bag....
so use # to write a line that is not included in the code but is still there you can use it after a code or on a new line, your choice
now back to the if command. you may want to check a number of things lets say you were at a hotel and you wanted to ask was there a double bed. and if there wasnt ask for a single bed and then if nither of those worked a couch? well here is how
elif
its like else but if the if or elif is true that is asked above it then it doesnt ask

Code: Select all

answer= "ok"
if answer == "yes ":
     print "why thank you"
elif answer== "mabey":
     print "thanks"
elif answer == "ok":
     print "sure"
elif answer== "fine":
     print "*sign* ok"
else :
      print "why didnt you say something positive ?!?!?!"

now did you realise that by the time it got to elif answer == "ok" and so on that after it finished out that command it finished. it didnt go onto the next elif or else.try it now and see also you dont need to do answer== every time and a print everytime you could make it make lets say eg

Code: Select all

make= "yes"
if make== "yes":
     new= "well whatever you want it to be"
elif new= "well whatever you want it to be"
    print "oh you already have a varible called new"




sry i got to go now ill be back later to write more
Last edited by sparkicks on Sat Feb 26, 2005 7:36 am, edited 2 times in total.
~{DE}~Demolisher





Posts: 100
Joined: Thu Dec 02, 2004 8:35 am
Location: MS,USA
Contact:

Post by ~{DE}~Demolisher »

Ok also what is python i jsut begun programming in vb to but i have never heard of python.
Signature exceeded 75KB.
sparkicks





Posts: 151
Joined: Sat May 15, 2004 8:07 am

Post by sparkicks »

ok now back to the tutorial



ok we now what to have an input in your program so we will make a simple code that asks us our name and then says hi your name here

Code: Select all

name= raw_input ("Your name please: ")
print "well hello " name
now when you run the program and it will look something like this
Your name please: Sparkicks
Well hello sparkicks

so now i will explain what we wrote.The varible is defined when you type in something so the input() is the command now i added in raw_ to make it a string no matter if you type in a number and i added a string inside the brackets to add a few words to the line before it asks you for something if you didnt then it would just be blank waiting for you to type something in. try it actually and see for yourself. now we will make a adding program
now first i want you to minimize this and try to figure out how to do it yourself and if you really cant figure it out then read on
















ok so you're really stuck? or you already figured it out =)
ok so we go like this

Code: Select all

thingone=input("first subject to add: ")
thingtwo=input (" right this is your second: ")
answer =thingone + thingtwo
print answer
and there you are
One last thing before we go to graphics is the loops
the first loop is the while loop and it will loop whatever you have in it if the statement is meat so look at this
thing="yes"

Code: Select all

while  thing== "yes":
       print "thing is yes"
        thing=raw_imput("thing is: )
now as long as you type in yes it will keep going
if you want it to keep going forever without having a statement you can use

Code: Select all

while 1:
next loop is a loop for lists which i havnt covered cus... i eh forgot..*woops* but if you had a command where you wanted to print everything in the list you would do

Code: Select all

for madeupvaribleforthisloop in listofshoppingitems:
     print madeupvaribleforthislist
and it would print all the things you added to the list called listofshoppingitems
usually people use for n in list and i dont know why but n is a very popular thing. where n is by the way you can put anything you want. it just stands for the item in the list you are dealing with. when the loop ends it grabs a new item and makes it whatever you put where i had madeupvaribleforthisloop it isnt still there after the loop though
to add something to a list ....i cant remember so someone else pm me so i can add it to here .

now graphics!!!

ok first i suppose you have followed the steps i mentioned about pygame and easygame.
so we will start our code with

Code: Select all

from easygame import *
right. well that imports ... its like importing a plugin. it gets easygame into your program and gets the easygame window up
right now what we will do is draw a circle

Code: Select all

  
from easygame import *
circle((100,200) ,10)



now what that does is it draws a circle that has the location of 100,200 100 is the x and 200 y and 10 refers to the size of that circle

ok now if your havnt programmed ever before you are probally looking for a move command right? i know i was and a lot of people do but im sorry to say as i have said at the top of this tutorial . you have to be creative . so try to think how we will move the circle before you read on.

right well you probally guessed it if you thought about it (or probally didnt) but anyway getting back on track . we have to wipe of the circle off the screen and draw another one right beside it. how we do that is we make it a tempory drawing so when we clear anything temport it will go and second we make the x and y location a varible. so eg

Code: Select all

xlocation=100
ylocation=200
cleartemp()
circle((xlocation,ylocation),20 , temp=1)
xlocation=xlocation + 10
circle((xlocation,ylocation),20 , temp=1)
ONE THING : LISTIN UP AND READ THIS PART!!:
dont post here untill i have my stuff done cus then ill have to make a new post for the tutorial just pm me to say how crap/useless it was

one more thing .... ne chance of making it a stickey cus i hate to see my hard work go to waste
Last edited by sparkicks on Thu Feb 17, 2005 2:04 am, edited 6 times in total.
sparkicks





Posts: 151
Joined: Sat May 15, 2004 8:07 am

Post by sparkicks »

can someone stickey it? it has no pictures because what i wrote would be basicly just in the picture with a window around it
[CL]9mm-Man




Wordewatician 500

Posts: 773
Joined: Tue May 04, 2004 7:14 am
Location: Uranus

Post by [CL]9mm-Man »

I think you should give a description about what Python is...Also you can make Halo programs in VB...Trust me :D
Your image cannot exceed 400x200 or 50kb
sparkicks





Posts: 151
Joined: Sat May 15, 2004 8:07 am

Post by sparkicks »

[CL]9mm-Man wrote: you can make Halo programs in VB...Trust me :D
ok what the hell does that mean. halo programs? did i ever say u couldnt ? and what do u mean by halo programs
Post Reply