Active Topics

 


Reply
Thread Tools
Posts: 67 | Thanked: 34 times | Joined on Apr 2011 @ Co. Wicklow, Ireland
#1
Dear all,

I am teaching myself python and pygame.

I took a simple example and modified it in order to understand how it works. My source code is here:

import pygame
import pygame.gfxdraw # not sure why needed
import random
pygame.init() # initialize the pygame module
screen = pygame.display.set_mode((800,450)) # set the window size
pygame.display.set_caption('Circles') # set the window title
pygame.mouse.set_visible(0) # hide the mouse pointer
screen.fill((0, 0, 0)) # fill the ascreen in black
while 1:
for event in pygame.event.get(): # read all the events
if event.type == pygame.QUIT: sys.exit() # was the x pressed
r=random.randint(0,255)
g=random.randint(0,255)
b=random.randint(0,255)
x=random.randint(0,800)
y=random.randint(0,450)
d=random.randint(0,800)
s = pygame.Surface(screen.get_size(), pygame.SRCALPHA, 32) # set the surface
pygame.gfxdraw.filled_circle(s, x, y, d, (r, g, b)) # draw a circle on the surface
screen.fill((0, 0, 0)) # clear the screen so all other objects removed
screen.blit(s, (0, 0)) # blit draws the screen
pygame.display.flip() # display the screen

My questions are:

1) If I import pygame, why do I need to seperately import pygame.gfxdraw?
2) Why do I need to set the surface and what does this do exactly?
3) Why do I need a screen.blit?

Hope someone can help so I can progress my learning!
 
Khertan's Avatar
Posts: 1,012 | Thanked: 817 times | Joined on Jul 2007 @ France
#2
1) pygame.gfxdraw is needed because not all module of pygame are imported by default
2) Surface is where you draw, ... imagine it that you draw in a buffer.
3) To transfer the buffer to the real screen. (to simplify as it s still a buffer)

I suggest you to read the documentation and particulary the examples :
http://www.pygame.org/docs/

And also this beginner tutorial : http://www.learningpython.com/2006/0...game-part-one/
 

The Following 2 Users Say Thank You to Khertan For This Useful Post:
Reply


 
Forum Jump


All times are GMT. The time now is 09:37.