Did you put the event loop in? This is a code sample of a very basic pygame program ...
import pygame
from pygame.locals import *
yellow = (255,255,0) # RGB color tuple
# initialize screen
pygame.init()
screen = pygame.display.set_mode((350, 250))
pygame.display.set_caption('Basic Pygame program')
# fill background
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill(yellow)
# display some text
font = pygame.font.Font(None, 36)
text = font.render("Hello from Monty PyGame", 1, (10, 10, 10))
textpos = text.get_rect()
textpos.centerx = background.get_rect().centerx
background.blit(text, textpos)
# blit everything to the screen
screen.blit(background, (0, 0))
pygame.display.flip()
# event loop
while 1:
for event in pygame.event.get():
if event.type == QUIT:
raise SystemExit
screen.blit(background, (0, 0))
pygame.display.flip()
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
This would be a very basic pygame program, see if you can get that to work:
# fooling around with pygame --> draw/fill some rectangles
import pygame as pg
# initiate pygame first
pg.init()
#create a 400x300 window
screen = pg.display.set_mode((400, 300))
# give it a title
pg.display.set_caption('Draw/fill rectangles using pygame')
# some color constants
white = 0xFFFFFF
red = 0xFF0000
green = 0x00FF00
blue = 0x0000FF
yellow = 0xFFFF00
#draw/fill a 77x33 rectangle in position x=250, y=50 (NW corner)
screen.fill(white, (250, 50, 77, 33))
# more rectangles, vary color, size, position ...
screen.fill(red, (30, 20, 70, 120))
screen.fill(red, (140, 70, 90, 80))
screen.fill(green, (150, 80, 70, 60))
screen.fill(yellow, (200, 170, 150, 60))
screen.fill(blue, (70, 200, 100, 70))
# hey, nothing gets displayed until one updates the screen
pg.display.update()
# create the event loop to get things going
# and specify an exit action (clicking on the window x)
while True:
for event in pg.event.get():
if event.type == pg.QUIT:
raise SystemExit
Ene Uran
Posting Virtuoso
1,723 posts since Aug 2005
Reputation Points: 625
Solved Threads: 213
If you want the window to stay open and allow for a graceful exit procedure, you need to set up a simple event loop at the end of the program:
# a simple pygame example
# creates a white circle on a black background
# the event loop makes exit from the program easier
import pygame
pygame.init()
# create a 300 x 300 display window
win = pygame.display.set_mode((300, 300))
white = (255, 255, 255)
center = (100, 100)
radius = 20
pygame.draw.circle(win, white, center, radius)
# this puts the circle on the display window
pygame.display.flip()
# event loop and exit conditions (windows x click)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
raise SystemExit
sneekula
Nearly a Posting Maven
2,427 posts since Oct 2006
Reputation Points: 961
Solved Threads: 212
Are you guys putting the code into the interactive python interpreter or are you creating a separate .py file. When I started my first pygame project, I tried putting it in the interpreter and got an Not Responding window. Of course I just started python and pygame so what do I know :P.
Why not try to run it from IDEs like Drpython, or Wing 101 and tell us the progress
evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417