• Create Account

## Python List Comprehensions! (And Python / Pygame Update!)

Posted by , in Beginners, Development 20 June 2013 - · 856 views

Some news:

I finished Code Academy's Python track and most of their advanced non-track courses.

I've started hacking around with Pygame.

I'm finally making games again!

That last piece of news might be a surprise. I've spent most of the last few months furthuring myself as a programmer and trying to re-learn the basics. There's still a huge amount of information I don't know about Python, however I know enough to make games, so that's what I'm doing.

Currently I'm working on Pong, and I'll update you guys on that whenever I ask a question on gamedev.

Now on to: Python List Comprehensions.

List comprehensions are a nice little tool in Python which add syntactic sugar. Like lambdas, they aren't needed however their existence makes programmers lives a little easier. Let's start by comparing traditional Python list syntax with List Comprehension Syntax:

```my_list = []
for index in range(1,101):
if index % 2 == 0:
my_list.append(index ** 2)
```
List comprehension:
```my_list = [x**2 for x in range(1,101) if x % 2 == 0]
```
Just look at how much less space list comprehensions take up to do the same thing as a traditional for loop.

If you can't tell, the List Comprehension says that for every number in the range from 1-101, if x can be evenly divided by two then append x squared to my_list. Although you could accomplish this with a traditional python loop, the list comprehension is easier and just looks nicer.

What is the syntax?

my_list - empty list to store values from list comprehension in
for x in range(1,101) - values to check if statement against.
if x % 2 == 0 - If statement. All values from for statement are checked here.
x ** 2 - The value of this expression (it can be any expression) is appended to my_list when the if statement is true.

Let's look at a two dimensional example:

```my_list = []
current_list = []
for first in range(10):
if y % 3 == 0:
for second in range(first,first + 20,2):
current_list.append(second)
else:
my_list.append(current_list)
current_list = []
```
List Comprehension:
```my_list = [[x for x in range(y, y + 20, 2)] for y in range(10) if y % 3 == 0]
```
As you can see, it is a lot easier to use a list comprehension.

I hope that you now have a basic understanding of Python list comprehensions. They are a very useful tool !

Cheers !

## [Beginners] How To Move From Tutorials To Your Own Game Part 1

Posted by , in Beginners 26 October 2012 - · 774 views

One thing I see constantly on these forums are people who are having trouble actually making a game. They understand the language and library they're using, however making their own game is hard because they are so used to either tutorials, or they don't understand the large amount of knowledge you need to have to even get started. This series of articles aim is to resolve that issue.

A game needs a State Machine.

State machines allow your program to do different things depending on whether you're in the main-menu, playing the game, paused, or the game is over. Let's look at two common types:

1) Enumerated State Machines

```//STATE MACHINE DECLARATION
InCredits,
Playing,
InGameOver,
Exiting }
//GAME LOOP
{
}
{
//Check for Button Clicks
}
else if (MyGameState == InCredits)
{
//Display Credits
}
else if (MyGameState == Playing)
{
//Game Logic
//Game Draw
}
//////////////////////////////////
// You Get The Idea //
//////////////////////////////////
```

Normally these are an enumeration that is equal to whatever State your game is in. However sometimes you want a more flexible system. When you want that you can use:

2) Objective State Machine

An Objective State Machine is an object that will handle whatever Logic and Rendering it needs depending on it's state and Updates itself. Here's an example of one:

```class StateMachine
{
public:
void HandleLogic();
void HandleRendering();
private:
bool Playing;
bool Exiting;
Window * DrawWindow;
Event * CheckEvent;
GameLogic MyLogic;
GameDraw MyDraw;
}
```

Of course this code isn't really though through, but it should give you an idea.

That's it for today, if you have questions ask below. A great area of reference for this if you want to read further is LazyFoo!

S M T W T F S
123
4 5 678910
11121314151617
18192021222324
25262728293031