[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [pygame] Simple question



Hi,

Below is a very simple script with comments, as you requested.
If you're new to python you should also check out the python tutorial; and if you're new to game programming, you should check out some of the web sites about game programming out there. I don't know which one to point you to, http://www.gamedev.net/ has some interesting articles aimed towards beginners, you may also take a look at the linux game development center which has a lot of link to all kinds of resources (http://lgdc.sunsite.dk/). http://www.gamasutra.com is much more business-oriented. Umm, what else ? If you know C, you may have a look at the SDL doc at http://www.libsdl.org

HTH, sylvain

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

#first we import all the stuff needed
#pygame.locals holds all the constants used throughout the program

import pygame
from pygame.locals import *

#the following statement tells pygame to setup itself correctly
pygame.init()

#the following variable is a tuple which holds the color we'll use to cover the screen
#for more info on tuples check the python tutorial
myColor = (150,150,150)

#we set up a video mode for the pygame window
#here we just choose the size, you can pass different flags
#if you want hardware acceleration, double-buffering...
#you'll have to check the pygame docs for details
screen = pygame.display.set_mode((640, 480))

#the following line sets the title of the pygame window
pygame.display.set_caption('simple display test')

#the easiest way to fill the screen with a given color is to draw
#a rectangle which covers the whole screen. What we do below is we create a Surface,
#a pygame object which is basically a rectangle filled with pixels. We want our surface
#to have the screen size, so we use the get_size() method to get it.
#Now that we have our surface (called "back"), we use its convert() method to have it use
#the same pixel format as the current display mode. It's not needed but highly recommended
#as it speeds up drawing a lot.
back = pygame.Surface(screen.get_size())
back = back.convert()

#we finally fill our surface with pixels of the color we defined earlier.
back.fill(myColor)

#below is the only function defined in this script, the one which actually makes everything happen.
#the "while 1:" makes sure we run in a loop. What the loop does:
#It checks the event queue, where all keyboard, mouse and system events go. The for loop checks them all
#and looks for quit event (which is generated when you close the pygame window) or escape key event.
#If it encounters any of these it returns from the main function, thus ending the script.
#If no event is detected, the script blits ("glue down") our colored back surface on the screen surface,
#starting from the top left corner of screen. We then call the pygame.display.flip() method
#to update what is on the screen.


def main():
while 1:
for event in pygame.event.get():
if event.type == QUIT:
return
elif event.type == KEYDOWN and event.key == K_ESCAPE:
return
screen.blit(back, (0, 0))
pygame.display.flip()

#here we call the main() function we defined earlier and enter the "game loop".
main()



At 15:57 28/03/03 +0000, you wrote:
Hi!

I'm brazilian and I don't speak english, but I try.

Recently I decide write school games using python.

But I never write games in any language.

I have the tutorial of pygame(include all functions of all modules) and I
continue completly lost.

I program in PHP, ASP, VB, Perl and C, but never with games.

So, I need some help to start my first screen.

I just need create one screen with one color.

I can do this, but my screen stay blink all the time.

Somebody can send to me one example(with coments) about create one screen with
some color(what module i need use, what functions, what make that functions).

Is just for start. I'm completely lost in concepts about game develop.

Thanks!
Alexander
Brazil - Rio de Janeiro-RJ