joi, 15 august 2013

PyGame - first interface - part 4

This is another tutorial about pygame.
Tha main goal of this tutorial is to make one box with some text for your application or game.
First i import the pygame module and I set two colors : black and green:
import pygame
import pygame.font
import pygame.event
import pygame.draw
import string
from pygame.locals import *
Tne next step is to make a function to create the display box.
def display_box(screen,mess):
        fontobject = pygame.font.Font(None,18)
        pygame.draw.rect(screen,black,((screen.get_width() / 2) - 100,
                (screen.get_height() / 2) - 10,200,20), 0)
        pygame.draw.rect(screen,green,((screen.get_width() / 2) - 101,
                (screen.get_height() / 2) - 11,200,20), 1)
        if len(mess) != 0:
                screen.blit(fontobject.render(mess, 1, (25,255,25)),
                ((screen.get_width() / 2) - 100, (screen.get_height() / 2) - 9))
Let's start the pygame window and display the box.
if __name__ == '__main__':
        pygame.display.set_caption("Hello world")
        screen = pygame.display.set_mode((320,240))
        mess = []
        while 1:
                display_box(screen,"Hello world text")
                for event in pygame.event.get():
                        if event.type in (pygame.QUIT,pygame.KEYDOWN,pygame.MOUSEBUTTONDOWN):
                                raise SystemExit
The result of this source of code can be see in the next image.

You can also change this source of code to make editbox or another GUI elements.

2 comentarii:

tom arnall spunea...

i wondering if the writer has tested this. it doesn't work on my phone nor my tablet. gives garbage on screen. i think the main problem is that ...Font(None... will work on PC systems but not android. not sure why yet.

Rene Villalta spunea...

Some help with windows, and I cant find PGS4A.

Trimiteți un comentariu