Basic drawing
Syntax
screen |
= |
display. |
set_mode |
((640,480)) |
||||
draw. |
rect( |
screen, |
(255,0,0), |
(10, |
10, |
100, |
100), |
4) |
Drawing in pygame revolves around the idea of a surface. A surface is a block of memory which can be updated by the draw commands and then, when drawing is complete, be sent to the graphics card. When doing any command using the draw object, you must always pass through a surface.
Some sample code to try –
from pygame import *
init()
# we only need a screen to get access to the events!
screen = display.set_mode((640, 480)) # tuple
display.set_caption('Graphics')
endProgram = False
while not endProgram:
# pygame event loop
for e in event.get():
if e.type == QUIT:
endProgram = True
# draw stuff
# Red Green Blue (RGB)
# 0 - 254
screen.fill((100,100,200))
# rectangle
# X, Y, Width, Height
draw.rect(screen,(255,0,0), (10,10,100,100), 4)
# fill rectangle
draw.rect(screen,(255,0,0), (200,10,100,100))
# circle
draw.ellipse(screen, (0,255,0), (10, 200, 80, 80) , 4)
# circle
draw.ellipse(screen, (0,255,0), (200, 200, 80, 80))
# update the screen!
display.update()
Rectangles form a large part of pygame and it is worth learning more about them. Also, it is also important to learn about surfaces as they provide a lot of the depth that pygame provides.
Helpful links
http://pygametutorials.wikidot.com/book-surf – Introduction to surfaces
http://www.pygame.org/docs/tut/newbieguide.html Newbie guide to pygame











