Show me an example

duminică, 3 martie 2013

5 simple rules help you to programming in pygame.

Some rules when you want to use pygame:

1. Use hardware rendering :

>>> import pygame 
>>> show=pygame.display.set_mode((400,400), pygame.HWSURFACE | pygame.DOUBLEBUF)

2. Use function convert and load bmp file.

>>> im=pygame.image.load("9.bmp").convert()

... also you got error:

>>> im2=pygame.image.load("9.png")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
pygame.error: File is not a Windows BMP file

3. Don't use reserved words in your variables, like display, time ...

4. Be advice about low performance. For example: simple 2D game no need to use threads. Also bad configuration of game , wrong sprites , group classes used or using unoptimized images heavily and you will have a low performance.

5. Use profiler to test how much time your game spend on event handling, computing and drawing. Don't guess what is wrong?

miercuri, 19 septembrie 2012

Do you hear about PyGameZine

From official website I got this :

What is this all about? PyGameZine is all about pygame. An E-Zine which comes as a website, a downloadable PDF, and as python code. There are interviews with developers of pygames. There are articles on cutting edge Pygame techniques. To find out more about pygame, see pygame.org.

If you have more than $4.99 you can support for pygame.

If you want read it you need a user and password.

miercuri, 29 august 2012

pygame - test class show text "Hello World"

I wrote in my journal: Today I received an email in which I asked about python and how to display a text message. I hate this approach, especially on the internet there are hundreds of materials on this subject.

This made ​​me to remember my blog and I write a new tutorial on pygame.

I use the old class from last pygame tutorials , see here.

The source code it's pretty simple.

The news is function show_text().

To do this i use pygame functions, see pygame documentation.

Let's see the final source code.

import pygame
from pygame.locals import *

screen_mode = (640, 480)
color_black = 0, 0, 0

class Game:
 def __init__(self):
  pygame.init()
  self.screen = pygame.display.set_mode(screen_mode)
  pygame.display.set_caption("Pygame text window")

  self.quit = False
  
 def update(self):
  print "update function"
  self.show_text()
  return
 
 def draw(self):
  self.screen.fill(color_black)
  self.show_text()
  pygame.display.flip()

 def show_text(self):
  font = pygame.font.Font(None, 36)
  text = font.render("Hello World!", 1, (100, 100, 100))
  textpos = text.get_rect()
  textpos.centerx = self.screen.get_rect().centerx
  self.screen.blit(text, textpos)

 def mainLoop(self):

  while not self.quit:
   for event in pygame.event.get():
    if event.type == pygame.QUIT:
     self.quit = True
    elif  event.type  == pygame.MOUSEBUTTONDOWN:
     print pygame.mouse.get_pressed()
     
     self.update()
   self.draw()
 
if __name__ == '__main__':
 game = Game()
 game.mainLoop()

The result is this:

sâmbătă, 1 octombrie 2011

pygame - test mouse class - update

Today I will show how to test mouse with pygame. This is very simple , just you try to use MOUSEBUTTONDOWN. Also , the click mouse only registers once so you can use it with another event. If you don't want to use an event handler, you can check for input with:
pygame.mouse.get_pos() 
pygame.mouse.get_pressed()
I created a class called Game, which contains four functions. A function that needs to pay attention is mainLoop. This function deals with events in the following order. While there is no event QUIT then read events. If there is event QUIT or MOUSEBUTTONDOWN then is running this: Event QUIT the program is quit. Event MOUSEBUTTONDOWN print mouse events , next is use update function with print "update function". Press mouse buttons: left,right,mouse wheel ,spinning mouse wheel.
(1, 0, 0)
update function
(0, 0, 1)
update function
(0, 1, 0)
update function
(0, 0, 0)
update function
The code is show bellow.
import pygame
from pygame.locals import *

screen_mode = (640, 480)
color_black = 0, 0, 0

class Game:
 def __init__(self):
  pygame.init()
  self.screen = pygame.display.set_mode(screen_mode)
  pygame.display.set_caption("Pygame first window")
  self.quit = False
  
 def update(self):
  print "update function"
  return
  
 def draw(self):
  self.screen.fill(color_black)
  pygame.display.flip()
  
 def mainLoop(self):
  while not self.quit:
   for event in pygame.event.get():
    if event.type == pygame.QUIT:
     self.quit = True
    elif  event.type  == pygame.MOUSEBUTTONDOWN:
     print pygame.mouse.get_pressed()
     self.update()
   self.draw()
 
if __name__ == '__main__':
 game = Game()
 game.mainLoop()

duminică, 7 august 2011

pygame script tool - colors

This is a simple script to allow us to see all colors used by pygame.

import pygame
from pygame.locals import *
from pygame.color import THECOLORS
from time import sleep

def main():
    WINSIZE = 640,480
    pygame.init()
    screen = pygame.display.set_mode(WINSIZE,0,8)
    pygame.display.set_caption('Colors in pygame!')

    for i,j in THECOLORS.iteritems():
        print i
 screen.fill(THECOLORS[i])
 font = pygame.font.Font(None, 36)
 text = font.render(i, 1,(0,0,0),(100,100,100))
 sleep(1.1)
 textpos = text.get_rect()
 textpos.centerx = screen .get_rect().centerx
 screen.blit(text, textpos)
 pygame.display.update()

if __name__=="__main__":
    main()

The script is very simple, just try it.

pygame.display routines

Start pygame display:

C:\>python
Python 2.7 (r27:82525, Jul  4 2010, 09:01:59) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygame
>>> pygame.display.init()

pygame.display.Info() creates object with some data about hardware:

>>> pygame.display.Info()

Here is the interpretation of output.

hw: True if the display is hardware accelerated
wm: True if windowed display modes can be used
video_mem: The megabytes of video memory on the display. This is 0 if unknown
bitsize: Number of bits used to store each pixel
bytesize: Number of bytes used to store each pixel
masks: Four values used to pack RGBA values into pixels
shifts: Four values used to pack RGBA values into pixels
losses: Four values used to pack RGBA values into pixels
blit_hw: True if hardware Surface blitting is accelerated
blit_hw_CC: True if hardware Surface colorkey blitting is accelerated
blit_hw_A: True if hardware Surface pixel alpha blitting is accelerated
blit_sw: True if software Surface blitting is accelerated
blit_sw_CC: True if software Surface colorkey blitting is accelerated
blit_sw_A: True if software Surface pixel alpha blitting is acclerated
current_h, current_h: Width and height of the current video mode, or of the
desktop mode if called before the display.set_mode is called.

This will return the name of the currently running video driver.

>>> pygame.display.get_driver()
'directx'

Without arguments list_modes returns a list of possible dimensions

>>> pygame.display.list_modes()
[(1024, 600), (800, 600), (640, 480), (640, 400), (600, 1024), (600, 800), (512,
 384), (480, 640), (400, 640), (400, 300), (384, 512), (320, 240), (320, 200)]

How to query a specific display mode.

>>> full=pygame.FULLSCREEN | pygame.HWSURFACE | pygame.DOUBLEBUF
>>> pygame.display.mode_ok( [ 800,600 ], full, 32)
32

You can create such a script to help you.

>>> for i in pygame.display.list_modes():
...     a=pygame.display.mode_ok(i,full,16)
...     print "FULLSCREEN | HWSURFACE | DOUBLEBUF for "+str(i)+" x "+str(a)
...
FULLSCREEN | HWSURFACE | DOUBLEBUF for (1024, 600) x 16
FULLSCREEN | HWSURFACE | DOUBLEBUF for (800, 600) x 16
FULLSCREEN | HWSURFACE | DOUBLEBUF for (640, 480) x 16
FULLSCREEN | HWSURFACE | DOUBLEBUF for (640, 400) x 16
FULLSCREEN | HWSURFACE | DOUBLEBUF for (600, 1024) x 16
FULLSCREEN | HWSURFACE | DOUBLEBUF for (600, 800) x 16
FULLSCREEN | HWSURFACE | DOUBLEBUF for (512, 384) x 16
FULLSCREEN | HWSURFACE | DOUBLEBUF for (480, 640) x 16
FULLSCREEN | HWSURFACE | DOUBLEBUF for (400, 640) x 16
FULLSCREEN | HWSURFACE | DOUBLEBUF for (400, 300) x 16
FULLSCREEN | HWSURFACE | DOUBLEBUF for (384, 512) x 16
FULLSCREEN | HWSURFACE | DOUBLEBUF for (320, 240) x 16
FULLSCREEN | HWSURFACE | DOUBLEBUF for (320, 200) x 16

See more here.

luni, 21 martie 2011

Pygame and Android

What is Android?
Wikipedia say :
Android is an open-source software stack for mobile devices that includes an operating system, middleware and key applications.[5][6] Google Inc. purchased the initial developer of the software, Android Inc.

Python on Android?
Here I saw:
The Pygame Subset for Android is a port of a subset of Pygame functionality to the Android platform.
This pygame subset for Android , allow us creation of Android-specific games.
In the chapter "samples" comes only with just one classic sliding numbers puzzle.
The good thing is, it can run on PC.
I installed Fedora pygame with "yum install pygame" as root.
And I run the application example of "samples".
Here is the result:
It works great on Fedora.