[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Re: game design techniques



Josh Close wrote:

Has anyone read the "game programming gems" book series? Would these be helpful?

Any help would be appreciated!

-Josh


Hi Josh,

I wanted to type some advice for you this afternoon already, but didn't, because i don't have any real good links etc. at hand.

I'll just throw some random thoughts on you, maybe they can get you started / in the right direction.

small games / experiments (no title screen, etc) are organized like this (i do it that way, it's quite generic, i think)


<imports>
import os, pygame, from pygame.locals import *, etc, whichever modules you need
maybe a good order to do so is python modules first (os, sys, math...), then API's (pygame, whatever), then possibly other files from your project
</imports)

<constants>
maybe you want some variables to be changed easily, or have them handy for access, etc. In other languages there are constants which cannot be changed after creation, in python this isn't possible. A convention often used for variables is ALL_UPPERCASE_CHARACTERS to distinguish them from other variables. Use them like:

VERSION = 0.1
DATA_PATH = "data"
RESOLUTION = (640, 480)
MAX_BULLETS = 10
MAX_SPRITES = 50
DEFAULT_HAIRCOLOR = "Blonde"
</constants>

<init>
init pygame, screen, load images / sprites, sounds, whatever
make / initialize variables such as

ufo = loadimage("ufo.png")
star = loadimage ("star.png")
x = 320
y = 200
xspeed = 0.
yspeed = 0.
quit = False
</init>

<main loop>
an inner loop "where all the magic happens" (tm).
something like:

quit = False # belongs to init, actually, see above
while quit == False:
get_events()
calculate_stuff()
paint_stuff()

actually, in simple programs you don't have to use functions / classes / whatever to break everything up, do it as you learn more about what happens where, just do it all in the main loop first.

for moving something around the screen smoothly, i'd suggest you use
a) variables to hold the current position of your object on the screen (st like x and y will do),
b) two variables like xspeed and yspeed to save the current speed of the object in each direction.

<events>
for getting events, just look at the pygame tutorials and docs, it's just a little bit you need at first.
make something like if Left Arrow is pressed, subtract a certain value from xspeed (xspeed going negative means moving to the left, then), if Right Arrow is pressed, add the same value to xspeed. Do similar for Up and Down Arrow keys and yspeed.

add something like if Key = ESC: quit = True (remember our loop variable)
</events>

<calculate_stuff>
easy, do
x = x + xspeed #(or x += xspeed, that's shorter)
y = y + yspeed

but you wanted to make a wall somewhere, didn't you? ok, do them along the sides of the screen, so our ufo doesn't get lost in space (tm).

so do if x >= 640 or x <= 0 (or whatever, screensize) then:
xspeed *= -1 (negate x, so the move direction is reversed)
x + (2 * xspeed) # this is so rounding errors don't get in our way too often and we don't run into the boundary in the next frame again

same thing for y (except boundary values).

</calculate_stuff>

<paint>
screen.blit(ufo, (x,y)) # i don't know the right code and i don't wanna look, check the docs, but i guess you know it already
</paint>
</main loop>

------------------------------------

Take care to know what your variable types are. Remember, any calculation with a floating number results in a float, every other does NOT. You know, like 3 * ( 2 / 3) results to 0. I guess it can bite you if you haven't programmed in a more low-level language before.

Just start with something like this and expand and experiment. I did. Some cool stuff you can make from this is maybe a shooter, a breakout game, a snake/nibbles, or a tron, an easy overhead racer, for an instance. Or just make an array of ufo's and change their speed by random values in the main loop, whatever.

I wrote some tips about better scrolling in a post some days ago, look for the "Hardware Accellerated scrolling Text" thread.

Last but not least, there are many tutorials online for things like collision detection, etc. Often times they're not written for python, but for C/C++ or other languages. This shouldn't stop you going over them and grab the basics and use them in your code, it's often just a couple of ideas or techniques which are easily understandable.

i hope i hit the spot somewhere, i'm not quite sure i did, i hope you can use something of this stuff.

Greets to Jack Nutting who posted quicker than i, he mentioned roughly the same things.

I hope thee fare well (i hope this was english)

Daniel

On Sun, 5 Sep 2004 01:23:37 -0500, Josh Close <narshe@gmail.com> wrote:

I'm starting to do some game design for the first time. I can get
things to move around the screen, but I don't really know what I'm
doing. What are some of the game design standards? Probably for just
2D games to start.

How do you organize things? How do you get objects to move smoothly?
How do you get objects to move at different speeds? How do you get
objects to not go past, say, a wall?

Are there any good websites or books that would have some standards
for this sort of thing? Like a tutuorial or something? I've looked at
all the pygame docs examples, and that doesn't do much right now.

Thanks.

-Josh