Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411488 Posts in 69377 Topics- by 58433 Members - Latest Member: Bohdan_Zoshchenko

April 29, 2024, 04:09:45 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Starting with Python and Pygame
Pages: [1] 2
Print
Author Topic: Starting with Python and Pygame  (Read 7642 times)
Lucaz
Level 6
*


Indier than thou


View Profile WWW
« on: August 21, 2008, 03:59:03 PM »

I want to learn to program games, but I have few to none experience with programming. I heard recommendations so I am going to start learning Python, and then Pygame. So I want to know if it's actually a good choice for starting.
Logged

jonny
Level 0
**



View Profile WWW
« Reply #1 on: August 21, 2008, 05:47:59 PM »

I've never used Python myself, but I've heard many good things about it. My first language was C++, but there's a bigger learning curve and you'd have to work harder to see be able to see the fruits of you effort.

By learning Python you'd also be learning many universal programming concepts which you could really apply to any other language you might use in the future. The more languages you learn, the easier it is to learn the next language. But most importantly, it has to be fun. If programming is only a painful process, there's a very good chance you'd give up before your first game is able to see the light.

So yeah, I'd say go for it. If Python is too hard, just move to a higher level. There are many game making programs which allow you to create games with basically no real programming knowledge at all.
« Last Edit: August 21, 2008, 05:55:31 PM by jonny » Logged
george
Level 7
**



View Profile
« Reply #2 on: August 21, 2008, 06:42:27 PM »

So yeah, I'd say go for it. If Python is too hard, just move to a higher level. There are many game making programs which allow you to create games with basically no real programming knowledge at all.

Totally seconded. Lucaz, a few months ago I was basically in the same boat as you, and I started learning Python. Python is a fun language and I think not that hard to learn, but if as you say you want to learn to program games, at first I would see about programming a game, and not programming a game, if you catch my drift. As you go learn the techniques, but I think it's valuable to get to do the thing itself quickly to see if you like doing it.
Logged
Lucaz
Level 6
*


Indier than thou


View Profile WWW
« Reply #3 on: August 22, 2008, 03:36:49 PM »

I have already used game making programs, and I have some idea of programming, so I'll go straight to Python. I'll put some time learning the basics, and when I feel like I'm done there, I'll start trying and experimenting with Pygame. I'm looking forward for a good experience. Thanks for the advice.
Logged

Akhel
Level 10
*****



View Profile
« Reply #4 on: August 22, 2008, 03:38:03 PM »

I'm currently learning Python and PyGame and enjoying it. Here's how I started (worked for me):

  • Start by reading the official Python tutorial. It's quite easy to understand, and you'll learn all the basics of the language.
  • I've never seen a very good introduction to PyGame, so you have to learn whatever you can from wherever you can. There's a book called Beginning Game Development with Python and PyGame which is pretty good. Also check the tutorials on the PyGame website and whatever open source apps you can find.
  • Make lots of little, even if useless, programs and games. Every bug you find and manage to get rid of will teach you something nice.

I've released my first (and very simple) PyGame game as open source. If you'd like, you can get the code here; I think it's pretty well commented. Try changing little parts of the code and seeing what happens. Hope you can learn something from it.

Good luck!
Logged
Cymon
Level 9
****


Computer Kid


View Profile WWW
« Reply #5 on: August 22, 2008, 04:46:18 PM »

I've released my first (and very simple) PyGame game as open source. If you'd like, you can get the code here; I think it's pretty well commented. Try changing little parts of the code and seeing what happens. Hope you can learn something from it.

Good luck!
Can you include an executable? (Haha)

YOu know, if I ever did add expand Cymon's Games I think Python would be the next step.
Logged

Cymon's Games, free source code, tutorials, and a new game every week!
Follow me on twitter
jonny
Level 0
**



View Profile WWW
« Reply #6 on: August 22, 2008, 07:07:23 PM »

Kao, I checked out your source. Python looks like a very cool language! Nice and high level. It reminds me a bit of JavaScript, except with a lighter syntax. Do you know if supports anonymous functions?

at first I would see about programming a game, and not programming a game, if you catch my drift.

I agree. Just focus on finishing a game and don't worry too much about how you do it. That's a big problem I have. I focus too much on whether or not my code is good and I end up losing sight of my ultimate goal: to create a game. It's important to remember that while it's good to maintain good coding practices, the code itself is simply a means to an end, and not the end itself. This isn't a lesson that everybody has to learn, but it's certainly something that I personally need to come to terms with.
« Last Edit: August 22, 2008, 07:36:18 PM by jonny » Logged
Ragzouken
Level 0
***



View Profile WWW
« Reply #7 on: August 23, 2008, 02:21:05 AM »

There are anonymous functions of sorts. They can only contain one expression which the result of which is the return value. There's not much use for them though as these two are identical:
Code:
identity = lambda x: x
Code:
def identity(x):
    return x
You only lose the binding to a name, which isn't worth the trouble really. Some common uses of lambda like using an operator on two given arguments are implemented in the operator module which makes it even more unessecary.

As for Python and PyGame, I'd say it's a great place to start. It's what I'm using at the moment, it's easy to get started and I don't feel limited in what I can do even now that I've worked a lot of it out.
Logged

Akhel
Level 10
*****



View Profile
« Reply #8 on: August 23, 2008, 03:21:11 AM »

Can you include an executable? (Haha)

Sure, you can get it here. Smiley
Logged
jonny
Level 0
**



View Profile WWW
« Reply #9 on: August 23, 2008, 08:06:00 AM »

There are anonymous functions of sorts. They can only contain one expression which the result of which is the return value.

Hmm, well that's not extremely useful.

Another language that I've been looking into is Ruby. It's similar to Python in many ways but also takes some concepts from Lisp and Pearl. It's a bit more flexible than Python in many aspects. It also has a game development library inspired by PyGame called Rubygame. Might be worth looking into. I'm afraid of using languages that make my life easier. I might become lazy...
Logged
qubodup
Level 1
*


icons?


View Profile WWW
« Reply #10 on: August 23, 2008, 08:24:16 AM »

I want to learn to program games, but I have few to none experience with programming. I heard recommendations so I am going to start learning Python, and then Pygame. So I want to know if it's actually a good choice for starting.
Python is one of the three good languages for starting to program (along with ruby and lua)

I never used Pygame, but perhaps Pyglet is interesting for you. It seems to be a minimal alternative to Pygame.

The project is kind of cool, as this video of programming space invaders in 40 minutes shows. (Yes I am easily impressed by stuff like that.)

For learning python, I recommend any of the tutorials listed on the python documentation page and additionally always keep a tab open with the official python documentation whenever you need additional info on a topic.


Can someone maybe quickly explain what a main loop is? (Or link to a good explanation..)
Logged
Akhel
Level 10
*****



View Profile
« Reply #11 on: August 23, 2008, 09:04:40 AM »

Unless I'm confusing things (if I am, someone please correct me), main loop is a loop that runs over and over until your program is closed; it has instructions for capturing input from the user, drawing sprites, refreshing the screen and so on. Here's a simplified version of my game's loop:

Code:
while 1:

    tick clock

    count dead and stuck tanks
    if only one tank is alive:
            end game
            congratulate the winner
    if all living tanks are stuck:
            end game in a draw

    update each tank

    blit the background
    blit the map
    blit the tanks

    blit other stuff

    get events
        if the event is a mouse click and it is the player's moving phase:
            move the player
        if the event is a mouse click and it is the player's shooting phase:
            shoot
        (similar operations for debug keys)
        if the event is the Esc key being pressed down:
            close the game

        if the event is the user clicking on the 'close' button:
            close the game

    if it is the player's moving phase:
        show the moving cursor and the player's possible moves
    if it is the player's shooting phase:
        show the shooting cursor

    blit text

    update the display

    if it is the AI's moving phase:
        decide where to move and do so
    if it is the AI's shooting phase:
        decide who to shoot and do so

It is quite rudimentary and not very organized, but it should be clear enough Tongue
Logged
Ragzouken
Level 0
***



View Profile WWW
« Reply #12 on: August 23, 2008, 10:28:08 AM »

There are anonymous functions of sorts. They can only contain one expression which the result of which is the return value.

Hmm, well that's not extremely useful.

What do you want to use anonymous functions for anyway?
Logged

jonny
Level 0
**



View Profile WWW
« Reply #13 on: August 23, 2008, 11:06:08 AM »

Unless I'm confusing things (if I am, someone please correct me), main loop is a loop that runs over and over until your program is closed
That is correct.

What do you want to use anonymous functions for anyway?
Many things, more than I could probably think of at the moment. It's just a very useful tool to have. Pretty much all my JavaScript scripts are designed around anonymous functions. It doesn't really enable you to do anything that you couldn't already accomplish with OOP concepts like polymorphism or procedural concepts like switch/if statements and function pointers, but it simply provides an alternative to all those solutions which in many cases will create better, cleaner, and much more flexible code.

In the area of GUIs I can particular see anonymous functions being quite useful, since so much of GUIs are designed around attaching specific behaviors to general concepts, like a button.

Anonymous functions are a paramount feature in Ruby, except Ruby calls them "blocks" and handles them a little differently.

One such application of anonymous functions that comes to mind is iterating through all the elements of a collection. This is something that any programmer will find themselves doing a lot, so it's important there exists a clean solution.

The C++/STL way of doing it is quite verbose and gets tiring to write over and over again:
Code:
for (vector<int>::iterator i = listOfNumbers.begin(); i != listOfNumbers.end(); ++ i) {
    // do something with *i
}

In a language which supports anonymous functions, the same thing can be accomplished with significantly less characters. Here's how you could iterate through a collection in JavaScript:
Code:
listOfNumbers.foreach(function(i) {
    // Do something with i
});

That's just one situation where anonymous functions prove to be very useful. Just as with every knew programming concept, at first it's hard to see in your mind where it might be useful, but sooner or later you'll come across a problem and think "Oh, so that's why they put this in the language." Then you'll think about how you'd have to accomplish the same think in a language that didn't have as many features, and on one hand you'll be glad because you're life just become a little easier, but on the other hand I start to worry that I might never be able to program in C++ ever again because it's just too damn primitive!
 Tongue
« Last Edit: August 23, 2008, 11:25:42 AM by jonny » Logged
Ragzouken
Level 0
***



View Profile WWW
« Reply #14 on: August 23, 2008, 01:12:18 PM »

I guess I'm a bit shaky on what anonymous functions really are then. You can use python functions to do the same sort of thing:
Code:
def dosomething(item):
    print i

map(dosomething, listOfNumbers)
Though purists would say that's an abuse of map Tongue In this particular example you really only need to do:
Code:
for item in listOfNumbers:
    dosomething(item)
or even just write the code inline:
Code:
for item in listOfNumbers:
    print item
All functions (I think) in python are first class, so you can define them within other functions and also pass them around like any other object, and I can't see much else you'd need. Of course I've never really used anonymous functions so I really wouldn't know Tongue
Logged

jonny
Level 0
**



View Profile WWW
« Reply #15 on: August 23, 2008, 03:09:18 PM »

I guess I'm a bit shaky on what anonymous functions really are then. You can use python functions to do the same sort of thing:
Code:
def dosomething(item):
    print i

map(dosomething, listOfNumbers)
Well, that's more like the function pointers I was talking about in C, or maybe delegates in C#. Anonymous functions in the simplest sense are functions that aren't bound to any name, but instead are passed around by "value", or probably more correctly by reference. As I said, anonymous functions don't ultimately allow you to do anything different, they just make it easier to do the same things.

Well,
All functions (I think) in python are first class, so you can define them within other functions and also pass them around like any other object, and I can't see much else you'd need. Of course I've never really used anonymous functions so I really wouldn't know Tongue
Well, I don't think they could qualify as fully first-class because you cannot create full new functions at run-time. If a languages treats functions as first-class objects then that means you are able to create new functions at run-time without any restrictions (Pythons anonymous functions have restrictions, so they aren't completely first-class), i.e., you can even pass them as arguments return them as values.

Code:
function saysSomething(something) {
    return function() {
        alert(something);
    }
}
var func = saysSomething("Hello");
func();
In this specific case, we have a function that returns another functions. The functions "saysSomething" creates and returns another function (at runtime) which will, when called, print out the the screen whatever you originally passed to saysSomething, which in this case is "Hello".

However, if Python allows you to declare functions within other functions, then this sort of thing might be possible. The returned function here is called a "closure", because it is evaluated within the scope of the function that it was created in. Closures can be very useful. Or maybe it's just that without closures, anonymous functions would be less useful...
Another example would be a button object.
Code:
new Button(function() {
    alert("Hey there!");
});

new Button(function() {
    alert("Hello World!");
});
This illustrates how anonymous functions can be particularly useful when used in event based system, such as GUIs. In this example, when the user clicked the button, the function passed as an argument would be called. Now, it would be entirely possible to produce the same results without anonymous functions, but it would be arguably messier. This is only two buttons, but some GUI applications have hundreds of buttons and many other even more complex controls.
Code:
function onClick() {
    alert("Hey there!");
}
new Button(onClick);

// Already have a function called "onClick", so we need to come up with a new name...
function onClick2() {
    alert("Hello World!");
}
new Button(onClick2);

function onClick3 { ...
That's another way of doing it with still using callback methods to handle events, but without using anonymous functions. It's not as clean, and will get even messier as the project gets bigger.

Another way to accomplish the same thing is the OO method.
Code:
class Button {
    virtual function onClick();
}

class Button1 inherits Button {
    function onClick() {
        alert("Hey there!");
    }
}

class Button2 inherits Button {
    function onClick() {
        alert("Hello World!");
    }
}
The object oriented way of doing the same thing (rather, one of the OO ways) is also fairly clean, but is definitely more verbose than the anonymous function way. In C++ a common convention is storing class definitions in separate header files, which doubles the amount of files in your project making it harder to maintain.

Yet another way (last one Tongue) to accomplish the same thing:
Code:

function onClick(button) {
    switch (button) {
        case "button1":
            alert("Hey There!");
            break;
        case "button2":
            alert("Hello World!");
            break;
    }
}

new Button(onClick, "button1");
new Button(onClick, "button2");
This last way is a fairly common way of accomplish event based architectures. It's clean and simple, and doesn't require too much code. However, a problem with this method is that you'll find yourself spending a lot of time jumping back and forth between places in your project since all the controls are created in one place, and their behaviors are defined in another. This could actually be considered either a good thing or a bad thing, depending on how you want to organize your code. Another problem with this method is that it tends to create very large functions, since lots of different behaviors are handled in a smaller amount of functions. The other two methods are better in this respect because each behavior gets its own function.

So there you have it. Three ways of doing the same thing thing, each with the same end, just different means of achieve that end. If your project is small, the means usually doesn't really matter. However, as the scope of your project gets bigger, it generally gets exponentially more complex unless your start creating your code on a higher level. Languages like C or C++ have a harder time doing this because they are static, fairly strict, and inherently low level languages, so you have to do a lot more initial work to achieve the high levels of programming that you desire. If you do a good enough job then you don't really need anonymous functions, but it's usually not a trivial matter to "do a good enough job" when it comes to simulating high level concepts in a low level language. The results are usually slightly less than ideal, which still gets the job done, but requires more pain, headaches and hacks than one would feel comfortable dealing with on a regular basis (these problems are amplified or reduced depending on how much of a purist you are. Wink)

With higher level languages, you get most (if not all) of the higher level constructs right out of the box, reducing total amount initial of work needed by the programmers by a large amount. That means, in general, there's less "distance" (i.e., lines of code), between where you are, and where you want to be, which, when it comes to coding, is always a good thing.

Hehe, I feel like I just wrote an essay. Tongue I kinda got a little side tracked (well, maybe just too abstract), and I don't even know if that answered your question. But maybe now you have a better idea of why anonymous functions might be useful.
« Last Edit: August 23, 2008, 03:19:06 PM by jonny » Logged
Ragzouken
Level 0
***



View Profile WWW
« Reply #16 on: August 23, 2008, 03:53:57 PM »

However, if Python allows you to declare functions within other functions, then this sort of thing might be possible.

Ah, sorry for my bad use of terminology, you can indeed declare functions (and classes ;o) within other functions. I think all functions in python are really declared at run time.
Logged

jonny
Level 0
**



View Profile WWW
« Reply #17 on: August 23, 2008, 04:11:10 PM »

No, that's cool. I don't know why I placed emphasis on declare, define is also a good word.

I did some reading and it seems that Python does include closures for nested functions and nested classes (well, class methods, so I guess just functions). So that's cool.
Logged
Dayv
Level 0
**



View Profile
« Reply #18 on: August 24, 2008, 07:27:22 AM »

I want to learn to program games, but I have few to none experience with programming. I heard recommendations so I am going to start learning Python, and then Pygame. So I want to know if it's actually a good choice for starting.

I love Python and it's a great language.  But I came to it after years of programming C++ and other languages.  I was also taught how to program at college and university so it's difficult for me to say how easy it is to get started with from zero knowledge.  But it is completely free so there's nothing to stop you from trying.

A common criticism of self-taught programmers is that they tend to reinforce bad habits by learning in isolation.  If at all possible, see if you can get someone to program along with.  Often you will find that you will find the answer to your problem just by explaining what you are trying to do to someone else aka cardboard programmer.

Good luck.
Logged
ImaginaryThomas
Level 1
*


Reor, Creo, Incito


View Profile WWW
« Reply #19 on: August 25, 2008, 08:26:18 AM »

I'm going to also recommend the book "Getting started with python and pygame" or whatever. I picked it up and It has a lot of stuff for beginners. I skipped over most of it because I'm an experienced coder but the author does a good job of getting you up to speed.

I've also just started with pygame and it's great for someone who's dabbled in code before but for someone who hasn't done the stuff before I'd agree that you should pick a higher level system like RPGmaker or Megazeux/ZZT (Do those exist anymore?)
Logged

Pages: [1] 2
Print
Jump to:  

Theme orange-lt created by panic