Sign in to follow this  

pygame tutorials

This topic is 4106 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
-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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
But I added pygame.display.flip(), heres the code:
import pygame, sys,os
from 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 = 50
y = 100
while 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()

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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=True
while 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

Share this post


Link to post
Share on other sites

This topic is 4106 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this