pygame tutorials

Started by
8 comments, last by mikeman 17 years, 7 months ago
I've began to load images and create images with pygame, but I can't seem to find any tutorials to help me go to the next step. The tutorials of pygame.org will show the basics, then jump into some new concepts without properly explaining them. Has anyone been able to find any good tutorials on pygame that will guide me through each step?
Advertisement
So what are you looking to do with PyGame? Maybe you already know how to do what you want to do....
Rob Loach [Website] [Projects] [Contact]
Well, I guess I would like to know how to properly move a sprite across the screen. I am able to do this, but not without creating a new image everytime the KEYDOWN event happens.
You're absolutely correct, that is the wrong way to do it. :) Here's the general order to do things:

-Create the window, and along with it, the screen
-Load an image to a surface
-while loop
--if keydown...
---move x position or w/e
--blit the surface onto the screen at the x and y coordinates of your character
--flip the buffers
-Create the window, and along with it, the screen ~ check
-Load an image to a surface ~ check
-while loop ~ check
--if keydown... ` check
---move x position or w/e ~ check
--blit the surface onto the screen at the x and y coordinates of your character ~ check
--flip the buffers ~ ?

I've done everything you explained up until 'flip the buffers'. Could you explain how to do this and what it means?
Quote:
...but I can't seem to find any tutorials to help me go to the next step...


Where have you searched? This is the main doc page from pygame.org. The very first tutorial(Introduction to PyGame) does pretty much what you want(moves a ball around). It explains the program line-to-line, even the buffer flipping(line25: pygame.display.flip) you're wondering about. Also, there are various examples with source included in your pygame distribution(for example, moveit.py) you can check out. Of course, if you find a command you don't understand, use the reference, right below the tutorials. It's organized per module, with a convenient general description right besides the routine/class and a detailed explanation offered if you click on it. So all you need to do is click on "Display" and "pygame.display.flip". Or you can even type in your interpreter 'help(pygame.display.flip). In any case, this is what you'll get:

Quote:
pygame.display.flip()->None

update the display.

This will update the contents of the entire display. If your display mode is using the flags pygame.HWSURFACE and pygame.DOUBLEBUF, this will wait for a vertical retrace and swap the surfaces. If you are using a different type of display mode, it will simply update the entire contents of the surface.


In other words, what you're drawing will not show unless you call display.flip() in each frame.
But I added pygame.display.flip(), heres the code:
import pygame, sys,osfrom pygame.locals import *pygame.init()window = pygame.display.set_mode((1024,768), FULLSCREEN)screen = pygame.display.get_surface()the_sprite_file = os.path.join("pics", "guy.gif")sprite_surface = pygame.image.load(the_sprite_file)screen.blit(sprite_surface, (50,100))pygame.display.flip()x = 50y = 100while True:    for event in pygame.event.get():         if event.type == KEYDOWN and event.key == 27:             pygame.quit()         elif event.type == QUIT:             pygame.quit()         if event.type == KEYDOWN and event.key == 100:             x = x + 25             screen.blit(sprite_surface, (x, 100))             pygame.display.flip()

Alright, never mind, I got it working with screen.fill(black) with black - 0, 0, 0...I get the concept of it, but is there a chart or something of other colors I can reference to?
You'll learn the colors quick enough. If you want to, you can open up MS Paint or similiar, double click on a color to change it, and click on "Define Custom Colors>>", then select a color, and type its red green and blue values into your program.
The colors are just RGB(Red-Green-Blue) triplets.

Btw, your program does not terminate properly. pygame.quit() does not exit your infinite loop, not does it terminate the program. It just cleans up the pygame library. The loop will continue and when it attempts to execute the next "pygame.event.get()" it will terminate abnormaly with an exception. The loop should look something like this:

game_running=Truewhile game_running:    for event in pygame.event.get():         if event.type == KEYDOWN and event.key == 27:	     game_running=False         elif event.type == QUIT:             game_running=False         if event.type == KEYDOWN and event.key == 100:             x = x + 25             screen.blit(sprite_surface, (x, 100))             pygame.display.flip()pygame.quit()#we have exited the loop, cleanup pygame

This topic is closed to new replies.

Advertisement