Functions to open sockets, pass data with Another handy resource handling class is for network connections. For example, you could make a music class, with an _init_įunction that loads the sound (perhaps borrowing from a load_sound() function), a function to pause the music, and aįunction to restart. To give you more flexibility with more complex resources. You can also make resource handling classes, You can make similar functions for loading any other resources, such as loading sounds. Finally it returns the image object, and its rect. Tries to load the image, and convert any alpha regions so you can achieve transparency, and it returns a more human-readable error Theįunction assumes that all of your images are in a directory called data, and so it takes the filename and creates the full pathname,įor example data/ball.png, using the os module to ensure cross-platform compatibility. The first line of the function is a documentation string describing what the function does, and what object(s) it returns. Here we make a more sophisticated image loading function than the one provided by () load new image from a file (or file-like object). error, message : print 'Cannot load image:', fullname raise SystemExit, message return image, image. join ( 'data', name ) try : image = pygame. Take this example of an image loading function:ĭef load_png ( name ): """ Load image and return image object""" fullname = os. Writing resource handling functions gives you sophisticated, reusable code, and gives you moreĬontrol over your resources. Resources, those methods can take up more than one line, they can require consistent modification by yourself, and they often don't It's always a good idea to write your own resource handling functions,īecause although Pygame has methods for opening images and sounds, and other modules will have their methods of opening other To you to develop your own best practice and style. As with any good programming environment, it's up Together, and maybe have an object with which you can control all of your resources. You can of course create classes for these, so that you can group them Will, generally speaking, be resource handling functions. I generally put all my code of this nature first, in their own, classless functions these Were totally independent of any game logic or game objects, they were written as separate functions, and were written first so In the Line By Line Chimp example, the first code to be written was for loading images and sounds. #!/usr/bin/env python # Tom's Pong # A simple pong game with realistic physics and AI # Released under the GNU General Public License VERSION = "0.4" try : import sys import random import math import os import getopt import pygame from socket import * from pygame.locals import * except ImportError, err : print "couldn't load module. The code is fairly simple, so I won't bother explaining any of it: Then you can load modules, with some error checking so that Python doesn't print outĪ nasty traceback, which non-programmers won't understand. Want to give those will will be looking at it. The main source file, such as the name of the file, what it contains, the license it is under, and any other helpful info you might It's always a good idea to set a few things straight at the top of The first lines, and loading modules ¶įirst off, you need to start off your game and load up your modules. They will also include some simple but effective error handling, and any customisation you wish to provide on top of functions Will do all of the boring, generic tasks like loading modules, loading images, opening networking connections, playing music, and so The first sections of code are relatively simple, and, once written, can usually be reused in every game you consequently make.
0 Comments
Leave a Reply. |