sâmbătă, 28 martie 2009

PyGame and OpenGL - part 1

OpenGL is an application programming interface. The PyOpenGL modules has many functions and several matrices for working with 3D graphics. This is one example how to use PyOpenGL. I've successfully tested it under Linux.

import os
import sys
import pygame
from pygame import *
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *

def init_display(w, h):
 pygame.display.set_mode( (w,h) , pygame.OPENGL | pygame.DOUBLEBUF )
 glViewport(0, 0, w, h)
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

 glEnable(GL_LIGHTING)

 glEnable(GL_LIGHT0)        

 glLight(GL_LIGHT0, GL_POSITION,  (0, 0, 1, 0))    
 glMatrixMode(GL_PROJECTION);

 glLoadIdentity();

 gluOrtho2D(-1, 1, -1, 1);

 glMatrixMode(GL_MODELVIEW);

def draw():
 glClearColor(0.0, 0.0, 0.0, 1.0)
 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
 glColor(0,1,0)
 glLight(GL_LIGHT0, GL_POSITION,  (1, 1, 1, 0))
 glMaterial(GL_FRONT, GL_AMBIENT, (0.1, 0.1, 0.1, 1.0))    

 glMaterial(GL_FRONT, GL_DIFFUSE, (1.0, 1.0, 1.0, 1.0))
 glutSolidTorus(0.1,0.5,24,24)
 pygame.display.flip()

def main():
 pygame.init()
 glutInit()
 init_display(800, 600)
 while 1:
  event=pygame.event.poll ()
  draw()
  if event.type is KEYDOWN:
   if event.key is K_ESCAPE:
    sys.exit(0)
main()

vineri, 27 februarie 2009

How I used time on pygame.

This is simple example. The program show some random colors.

import time
import random
import pygame

pygame.init()

def main():
    for i in range(60):
        screen = pygame.display.set_mode((800,600))
        pygame.display.set_caption("Some random colors!")

        bg = pygame.Surface(screen.get_size())
        bg = bg.convert()
        bg.fill((random.randint(1,255),random.randint(1,255),random.randint(1,255)))
        clock = pygame.time.Clock()
        Go = True
        while Go:
            clock.tick(2)
            screen.blit(bg,(0,0))
            pygame.display.flip()
            time.sleep(6)
            Go = False

if __name__ == "__main__":
    main()
As you see , try to create one loop:

        clock = pygame.time.Clock()
        Go = True
        while Go:
            clock.tick(2)
            ...  
            some code line 
            ...
            time.sleep(6)
            Go = False

miercuri, 18 februarie 2009

Sounds in pygame.

First of all you need two files. This is "load.py".

import os
import sys
import pygame
from pygame import mixer
pygame.mixer.init() 
def snd(nume):
    numeintreg = os.path.join("sunete",nume)
    sound = pygame.mixer.Sound(numeintreg)
    return sound
This is "main.py". The sounds "o.wav" can be any sound file.

import os
import sys
import pygame
from pygame import *
import load
def main():
     ocean=load.snd("o.wav")
     ocean.play()
main()

Showing pictures on pygame

First of all you need two files. This is "load.py"

import os
import sys
import pygame
def img(nume):
    numeintreg = os.path.join("date",nume)
    image = pygame.image.load(numeintreg)
    image = image.convert()
    return image
This is "main.py". The image "bk2.bmp" can be any image.
import os
import sys
import pygame
import load
def main():
    pygame.init()
    ecran = pygame.display.set_mode((640, 480))
    fundal = load.img('bk2.bmp')
    ecran.blit(fundal, (0, 0))
    pygame.display.flip()
main()

marți, 17 februarie 2009

Keyboard on pygame

This is a short example of how to use the keyboard in pygame.

import os
import sys
import pygame
from pygame import *
def main():
   pygame.init()
   ecran = pygame.display.set_mode((640, 480))
   ruleaza= True
   action = "Help!"
   while ruleaza is True:
       for event in pygame.event.get():
           if event.type == QUIT:
               ruleaza == False
           elif event.type == KEYDOWN:
               if event.key == K_ESCAPE:
                   ruleaza = False
               elif event.key == K_LEFT:
                   action="L"
               elif event.key == K_RIGHT:
                   action="R"
           print action
main()

luni, 9 februarie 2009

About PYGAME

Yet another powerful open source 2D game engine.Pygame is a set of modules allows you to create fully featured games and multimedia programs in the python language. Pygame is portable and runs on every platform and operating system.

Pygame is free. Released under the LGPL licence, you can create open source, free, freeware, shareware, and commercial games with it. See the licence for full details.