Pages

vineri, 9 august 2013

PyGame : First interface - part 3.

Today I will show you just a few tips about pygame window.
If we want to put the window in the center of the screen we can use this:
import os
os.environ['SDL_VIDEO_CENTERED'] = '1'
Most of the pygame windows are made by :
def main():
  background_colour = (25,25,255)
  screen = pygame.display.set_mode((320,240))
  screen.fill(background_colour)
This will create this window:

Now if you don't need a title window, like the next image...

... then you need to use this source code :
  screen = pygame.display.set_mode((320,240),NOFRAME)

luni, 24 iunie 2013

PyGame : The pygame module come without font module.

Today I try to see some old python source code.
I got this error and seam many python users have the same error.
NotImplementedError: font module not available
(ImportError: No module named font)
It's the last release of pygame.
When I try to config the pygame I got this
pygame-1.9.1release]$ python2.7 config.py
Using UNIX configuration...


Backup existing "Setup" file [Y/n]:

Hunting dependencies...
sh: smpeg-config: command not found
WARNING: "smpeg-config" failed!
SDL     : found 1.2.13
FONT    : not found
IMAGE   : found
MIXER   : not found
SMPEG   : not found
PNG     : found
JPEG    : found
SCRAP   : found
PORTMIDI: not found
PORTTIME: not found


Warning, some of the pygame dependencies were not found. Pygame can still
compile and install, but games that depend on those missing dependencies
will not run. Would you like to continue the configuration? [Y/n]:
Not only the FONT is not here. I will try to fix this issue.

duminică, 3 martie 2013

PyGame : 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 the error:
>>> im2=pygame.image.load("9.png")
Traceback (most recent call last):
  File "", line 1, in 
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, a simple 2D game no needs to use threads. Also the bad configuration of game, wrong sprites, group classes used or using unoptimized images heavily and you will have a low performance.
5. Use the profiler to test how much time your game spend on event handling, computing, and drawing. Don't guess what is wrong?

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 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: