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

Re: [pygame] looking for pygame minimal examples



On Wed, 8 Oct 2008 12:26:15 -0700, "Ian Mallett" <geometrian@xxxxxxxxx>
wrote:
> On Wed, Oct 8, 2008 at 8:54 AM, kschnee <kschnee@xxxxxxxxxx> wrote:
>>
>> <code>
>> ## Create an SDL window for graphics, and draw a blue square.
>> import pygame
>> screen = pygame.display.set_mode((800,600))
>> pygame.draw.rect((42,42,200,200),(0,0,255),0)) ## Parameter order might
> be
>> wrong
>> pygame.display.update()
>> </code>
> 
> pygame.draw.rect(screen,(0,0,255),(42,42,200,200),0)

Ah, right, I forgot the surface reference (you don't have to draw to the
screen, but can draw to any surface object) and reversed the color and rect
parameters.

Some of the other things being asked about are basically one-or-two-liners.
Loading and displaying an image (by one method), for instance:
pic = pygame.image.load("filename.png").convert() ## convert() is optional
pic.set_colorkey((r,g,b)) ## Treat this color as transparent; good for
images on a background marked with a weird color like (255,0,255)
screen.blit(pic,(100,0)) ## Draw it at (100,0)

For a sprite you can use general Python knowledge to make a class that has
references to a loaded image and to a set of coordinates, then use the
logic section of whatever main loop you've established to define rules for
where the sprites move to, and to draw each sprite wherever its coordinates
say. The pygame.sprite module contains methods for logical groupings of
sprites and there's been discussion about collision detection methods. I
don't like the sprite module myself, because it doesn't do much and it
assumes that screen coordinates are the same as world coordinates. Instead
I built a sprite module focused on defining a set of rect objects
representing the frames of animation within a larger image, then having my
sprite objects reference which frame should be displayed.

For a main game loop you need some kind of loop that draws everything,
handles events, and does some game logic like deciding what moves where and
whether the game is over yet.

In general you should look at the documentation on pygame.org, and try the
built-in help commands help(thing_you_want_help_on) or dir(thing) to see
what functions are available and how they work.