Menu

Main Menu
Talk Get Daily Search

Member's Online

    User Name
    Password

    python/pygame help needed

    Reply
    mmarks | # 1 | 2011-06-20, 11:45 | Report

    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!

    Edit | Forward | Quote | Quick Reply | Thanks

     
    Khertan | # 2 | 2011-06-20, 12:48 | Report

    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/

    Edit | Forward | Quote | Quick Reply | Thanks
    The Following 2 Users Say Thank You to Khertan For This Useful Post:
    Captwheeto, pelago

     
vBulletin® Version 3.8.8
Normal Logout