Python PyGame: 2

Python PyGame: 2

In my first blog I covered the basics of getting PyGame installed on your Widnows machine with Python 2.7. We also went over the core methodology of IDEA: ALTER presented in the “Game Programming The Express Line to Learning” book I’m using to start this series. For review:

  • IDEA (the game framework)
  • 1.Import & Initialize
  • 2.Display Configuration
  • 3.Entities
  • 4.Action (broken into the ALTER steps)
    • ALTER
    • 1.Assign values to key variables
    • 2.Loop (infinite w/breakout clause)
    • 3.Timer (setup framerate)
    • 4.Event Handling
    • 5.Refresh Display

Today I’m going to take us a step further into the game development world with three programs. All of these are covered in great depth in the book, so, my purpose is simple introdcution to the concepts and you can walk through it with me if you like. I am a believer that if you do anything 10,000 times you’ll be much farther advanced than today, so, it may feel tedious to type the code out, but over time, you’ll engrain it into your brain.

The book itself is written largely towards total beginners to programming, so, I am sparsely choosing which ones I do. The three programs today are drawCircles, mouseEvents, and Paint. So, you’ll see how to draw stuff on your canvas, interact with the user through keyboard and mouse, and then put it all together.

Let’s check out the first program, drawCircles:

Two points of interest. We’re using PyGame’s draw command to create a lot of circles of various colors and starting at different points. Secondly, we’re demonstrating out ability to save files back to a folder (in this case we’re converting the screen into a bitmap). One additional thing is the use of a function call as opposed to just doing everything in one go. It’s not so impressive here, but, it will be more handy when we begin writing larger programs or using object oriented techniques.

Go ahead and run this and look in your file folder. You’ll find this image:

Personally, whenever I get a chance to draw things I inevitably want to draw fractals like it’s 1980. I’ll post a small addendum later to do that.

Okay, so the second program is called It grabs a few user events and prints them to the console. Let’s take a look at the code:

So, you can see the IDEA:ALTER is still working for us here (it will keep working for us too). Outside of the normal stuff, we’re using the PyGame Event Handler to grab when the user hits the Close button, presses a key down, or clicks the mouse. One thing of interest is that the simple pygame.KEYDOWN event is all you need to create a keylogger and steal people’s passwords all day long. There’s a reason Python is a popular language among black and white hat hackers. It’s easy, light, and fast.

Run this program and you’ll be able to see the console spit out everything you do:

That’s good fun…

Okay now let’s put them together into a Paint program. This silly task shows us a lot of good features of game programming. Let’s take a look at the first part where we define some functions:

The first function is interfacing with the input from the user to determine what to do. Primarily what color and line width to use for the paint brush. The second function is reporting back what color and width the user has chosen. Setting the MyFont to the “None” of SysFont means it will use the default font on whatever system is running the program. That’s a handy thing to keep in mind.
Okay, the second part defines the main function:

So once we are inside the main loop, we check first to see if the user has quit, and then handle the mouse and keyboard. If any key is pressed we update the values of myData. If the mouse is held down we use pygame’s draw command and feed it MyData’s variables. Result? Paint:

Okay, so, that’s it for today. With these kind of fundamentals we are ready to tackle some bigger concepts and move towards more object oriented games. See you next time.

Leave a comment


Email(will not be published)*


Your comment*

Submit Comment

© Copyright Duke Hall - Designed by Pexeto