ThorPy

A GUI library for pygame

Tutorials - Integration of ThorPy into a pygame code

This short tutorial is for pygame users who already have a code written and that simply want to include some elements from ThorPy. However, if you are dealing with many buttons and other widgets from ThorPy, your code will a lot simpler and clearer if you use menus provided by ThorPy, as explained in other tutorials.

Pygame code

Let us work on a simple pygame code in which a red rect is drawn on the screen. Each time user press the left arrow key, the rect goes on the left.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#Tutorial : how to use ThorPy with a pre-existing code - step 0
import pygame, thorpy
pygame.init()
pygame.key.set_repeat(300, 30)
screen = pygame.display.set_mode((400,400))
screen.fill((255,255,255))
rect = pygame.Rect((0, 0, 50, 50))
rect.center = screen.get_rect().center
clock = pygame.time.Clock()

pygame.draw.rect(screen, (255,0,0), rect)
pygame.display.flip()

#when left arrow is pressed, the red rect goes to the left
playing_game = True
while playing_game:
    clock.tick(45)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            playing_game = False
            break
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                pygame.draw.rect(screen, (255,255,255), rect)
                pygame.display.update(rect)
                rect.move_ip((-5,0))
                pygame.draw.rect(screen, (255,0,0), rect)
                pygame.display.update(rect)

pygame.quit()

Integration of ThorPy elements

Now, assume you want to use, let's say, a slider in your code. And also a button for leaving the application. All that stored in a box. Then, you have to create those three elements, as done at lines 15-17. Until here, nothing new from previous tutorials. However, you may wonder how to tell your code to use these elements, since we only learned to use elements on menus until now. The answer is simple : you will use a menu anyway (line 19), but you won't launch it. Instead, in your main loop, you will simply have to add the line menu.react(event). That's all!

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#Tutorial : how to use ThorPy with a pre-existing code - step 1
import pygame, thorpy
pygame.init()
pygame.key.set_repeat(300, 30)
screen = pygame.display.set_mode((400,400))
screen.fill((255,255,255))
rect = pygame.Rect((0, 0, 50, 50))
rect.center = screen.get_rect().center
clock = pygame.time.Clock()

pygame.draw.rect(screen, (255,0,0), rect)
pygame.display.flip()

#declaration of some ThorPy elements ...
slider = thorpy.SliderX.make(100, (12, 35), "My Slider")
button = thorpy.make_button("Quit", func=thorpy.functions.quit_func)
box = thorpy.Box.make(elements=[slider,button])
#we regroup all elements on a menu, even if we do not launch the menu
menu = thorpy.Menu(box)
#important : set the screen as surface for all elements
for element in menu.get_population():
    element.surface = screen
#use the elements normally...
box.set_topleft((100,100))
box.blit()
box.update()

playing_game = True
while playing_game:
    clock.tick(45)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            playing_game = False
            break
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                pygame.draw.rect(screen, (255,255,255), rect) #delete old
                pygame.display.update(rect)
                rect.move_ip((-5,0))
                pygame.draw.rect(screen, (255,0,0), rect) #drat new
                pygame.display.update(rect)
        menu.react(event) #the menu automatically integrate your elements

pygame.quit()

Note that you can use elements as you would in a 'pure' ThorPy code, i.e adding reactions to the elements the normal way, etc...