Mind 0.2.4 Tutorial

Note

This tutorial covers using each module one by one but it doesn’t cover making game with entire library.

Mind.Knowledge

Knowledge is part of Mind for saving data (with .knw extension)

Note

This module currently isn’t very useful (mostly because of bugs) but it might be soon.

For beginning import Knowledge:

>>> from Mind import Knowledge

Then initialize basic class for saving data:

>>> data = Knowledge.Knowledge("test")

Then try this few lines (these adds data to data class):

>>> data['player'] = 5
>>> data[25] = 8
>>> data[21.753] = 'exit'
>>> data["something"] = [1, "X", [12.3, "Y"], 3]
>>> data
21.753 : exit
player : 5
something : [1, 'X', [12.3, 'Y'], 3]
25 : 8
>>> data[25]
8

Now try to save data and load:

>>> data.save_data()
>>> data = Knowledge.load("test")
>>> data
something : [1, 'X', [12.32, 'Y'], 3]
25 : 8
21.793000000000006 : exit
player : 5

Warning

Saving decimal numbers isn’t very correct and maximum is 3 decimals!

See also

Mind.Test.

Mind.Orientation

Orientation is part of library for maps.

At begining:

>>> from Mind import Orientation

We could first create map:

>>> Map = Orientation.MAP(1000, 1000)

Then we could add few points:

>>> A = Orientation.point(100, 100, Map, "A")
>>> B = Orientation.point(200, 200, Map, "B")
>>> C = Orientation.point(300, 200, Map, "C")

Note

If point is placed out of Map (here 1000 width and 1000 height) it will cause MapError!

When point is created it automatically goes to map. Test it:

>>> print(Map)
Map 1000x1000:
1. A @ 100, 100
2. B @ 200, 200
3. C @ 300, 200

We could add some group of points:

>>> Group1 = Orientation.group_of_points(Map, "Some", B, C)

And try to print Map now:

>>> print(Map)
Map 1000x1000:
1. A @ 100, 100
2. B @ 200, 200
3. C @ 300, 200
4. Some group [B @ 200, 200; C @ 300, 200]

If we want for points (or any other map object) to not add to Map, we should set quiet to True (e.g.: point(400, 300, Map, quiet=True)).

Now it’s time for some rects:

>>> Rect1 = Orientation.rect(100, 100, 100, 100, Map)
>>> print(Map)
1. A @ 100, 100
2. Some group [B @ 200, 200; C @ 300, 200]
3. B @ 200, 200
4. C @ 300, 200
5. Unknown rect 100X100 @ 100, 100

Rects support some interesting things:

>>> A in Rect1
True
>>> B in Rect1
True
>>> C in Rect1
False
>>> Group1 in Rect1
False
>>> Orientation.group_of_points(Map, "Some", A, B) in Rect1
True
>>> Rect1 in Rect1
True
>>> Orientation.rect(150, 100, 100, 100, Map) in Rect1
False
I think you got the point but in case you didn’t:
  • If point is in inside rect or on edges of rect then point in rect is True.
  • If point in rect is True for all points in group_of_points then group_of_points in rect is True.
  • If all parts of rect1 are in rect2 then rect1 in rect2 is True.
  • In all examples if it isn’t True it’s False.

Library for now also has line, line-seg, ray, and direction classes but they still don’t have support in Tiled maps loading, so this tutorial doesn’t cover them.

Important thing is ext_obj class. It’s any Map object but with some additional properties:

>>> p1 = Orientation.ext_obj(point(10, 10, Map, "some", quiet=True), "weird", "very strange", type="non-normal")

Now you may ask why do you need all that? You maybe think: “It would probably be better if I do my map exactly for my game, only map loading is problem.

Because of that I will now start explaining using Tiled map (.tmx extension).

Note

For following examples you need to have tiledtmxloader if you don’t have it some classes won’t initialize

We probably need to set the map:

>>> Map = Orientation.tiled_map("first_level")
<tiledtmxloader.tmxreader.TileSet object at 0x01C9B3D0>
SpriteLayer: Sprite Cache hits: 0

Now we could print map (I will show just beginning of output because it have 103 lines):

>>> print(Map)
1. Unknown rect 20X500 @ 0, 0; {type: None}
2. Unknown rect 1980X20 @ 20, 0; {type: None}
3. Unknown rect 1980X20 @ 20, 480; {type: None}

You may notice that print output looks like Map print output and all objects are converted to ext_obj.

I think it’s best to not work in python shell.

We should first create main pygame loop (It doesn’t use Mind but we’ll need it), something like this:

import pygame
from Mind import Orientation

pygame.init()

screen = pygame.display.set_mode((500, 500))

running = True
while running:
   for event in pygame.event.get():
       if event.type == pygame.QUIT:
           running = False

   screen.fill((255, 255, 255))

   pygame.display.flip()

pygame.quit()

I asume that you understand everything in that code.

Next we should add Map initialization and Map blitting:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import pygame
from Mind import Orientation

pygame.init()

screen = pygame.display.set_mode((500, 500))

Map = Orientation.tiled_map("first_level")

Map.set_screen(screen)

running = True
while running:
   for event in pygame.event.get():
       if event.type == pygame.QUIT:
           running = False

   screen.fill((255, 255, 255))

   Map.blit()

   pygame.display.flip()

pygame.quit()

Line 8: We loaded map from file first_level.tmx ( .tmx extension automatically added by programm).

Line 10: We seted our screen from line 6 to the Map

Line 20: Programm blits Map.

Now we have camera with center on point (0, 0) of the map (uper left corner).

But what if we want to have camera somewhere else? Then we should set map position:

 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
import pygame
from Mind import Orientation

pygame.init()

screen = pygame.display.set_mode((500, 500))

Map = Orientation.tiled_map("first_level")

Map.set_screen(screen)

Map.set_camera_pos(200, 200)

running = True
while running:
   for event in pygame.event.get():
       if event.type == pygame.QUIT:
           running = False

   screen.fill((255, 255, 255))

   Map.blit()

   pygame.display.flip()

pygame.quit()

Line 12: We set center position of camera to 200, 200.

If you edge is True in Map.set_camera_pos (which is by default) then map won’t be blitted outside the edges.

If we want to have map which will move when we press arrows we need to use moving_map:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import pygame
from Mind import Orientation

pygame.init()

screen = pygame.display.set_mode((500, 500))

Map = Orientation.moving_map("first_level", 500, 500, screen)

running = True
while running:
   for event in pygame.event.get():
       if event.type == pygame.QUIT:
           running = False

   screen.fill((255, 255, 255))

   Map.blit()

   pygame.display.flip()

pygame.quit()

Line 15: We changed Map class to moving_map

For moving map we need Map.move(x, y):

 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
import pygame
from Mind import Orientation

pygame.init()

pygame.display.set_caption("test, arrows for moving")
screen = pygame.display.set_mode((400, 400))

Map = Orientation.moving_map("first_level", 500, 500, screen)

running = True
while running:
   for event in pygame.event.get():
       if event.type == pygame.QUIT:
           running = False
       elif event.type == pygame.KEYDOWN:
           if event.key == pygame.K_UP:
               Map.move(0, -10)
           if event.key == pygame.K_RIGHT:
               Map.move(10, 0)
           if event.key == pygame.K_DOWN:
               Map.move(0, 10)
           if event.key == pygame.K_LEFT:
               Map.move(-10, 0)

   screen.fill((255, 255, 255))

   Map.blit()

   pygame.display.flip()

pygame.quit()

Besides we added title in line 6, we seted that when pressing UP map y decreases for 10 and so on.

Now try to move map.

Left and right works but up doesn’t. That’s because in begining Map center is in point (500, 500) and when you press key up it’s in point (500, 490) but it looks same because in both situations map would be blitted out of Map but it automatically blits on edge.

We should use set_position() and get_camera_pos() methods for fixing that:

 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
import pygame
from Mind import Orientation

pygame.init()

pygame.display.set_caption("test, arrows for moving")
screen = pygame.display.set_mode((400, 400))

Map = Orientation.moving_map("first_level", 500, 500, screen)
Map.set_position(*Map.get_camera_pos())

running = True
while running:
   for event in pygame.event.get():
       if event.type == pygame.QUIT:
           running = False
       elif event.type == pygame.KEYDOWN:
           if event.key == pygame.K_UP:
               Map.move(0, -10)
           if event.key == pygame.K_RIGHT:
               Map.move(10, 0)
           if event.key == pygame.K_DOWN:
               Map.move(0, 10)
           if event.key == pygame.K_LEFT:
               Map.move(-10, 0)

   Map.set_position(*Map.get_camera_pos())
   screen.fill((255, 255, 255))

   Map.blit()

   pygame.display.flip()

pygame.quit()

Line 10: We define that center of map is center of map inside the edges.

Line 26: Like in line 9, but this line executes while game is running.

Now it’s time for some object to appear. We’ll define it as letter ‘A’:

 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
from Mind import Orientation
import pygame

pygame.init()

screen = pygame.display.set_mode((400, 400))
pygame.display.set_caption("test, arrows for moving")

Map = Orientation.moving_map("first_level", 500, 500, screen)

Map.set_position(*Map.get_camera_pos())

font = pygame.font.SysFont(None, 50)
a = font.render('A', 1, (255, 0, 0))
p = Orientation.map_obj(*Map.get_position() + (Map, a))

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                p.move(0, -10)
            if event.key == pygame.K_RIGHT:
                p.move(10, 0)
            if event.key == pygame.K_DOWN:
                p.move(0, 10)
            if event.key == pygame.K_LEFT:
                p.move(-10, 0)

    Map.set_position(p.x, p.y)
    screen.fill((255, 255, 255))

    Map.blit()

    p.blit()

    pygame.display.flip()

pygame.quit()

Lines 13-15: Initialization of object.

Lines 24, 26, 28, 30: Instead of moving map we move object.

Line 32: Sets player pos as map center position.

Line 37: Blits player onto screen.

Note

We don’t need Mind.Orientation.moving_map anymore and in further examples it will be replaced with Mind.Orientation.tiled_map.

Map initialization has got some more advenced options but I’m not going to show them because they aren’t necessary.

Note

I might extend this tutorial (for Mind.Orientation), but feel free to extend this tutorial (and send me source) or create completely new tutorial.

Mind.Imagination

Imagination is part of library for Main Menu.

Note

Because this all classes need pygame initialization, it would be pointless to do this in shell.

I supose you have code similiar to this (and understand it):

from Mind import Imagination
import pygame

screen = pygame.display.set_mode((800, 500))
running = True

pygame.init()

font = pygame.font.SysFont(None, 50)

while running:
    event_get = pygame.event.get()
    for event in event_get:
        if event.type == pygame.QUIT:
            running = False

    screen.fill((255, 255, 255))

    pygame.display.flip()

pygame.quit()

Next step would be code like this:

 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
from Mind import Imagination
import pygame

screen = pygame.display.set_mode((800, 500))
running = True

pygame.init()

font = pygame.font.SysFont(None, 50)

Main_menu = Imagination.Vertical_menu(Imagination.PLACE(True), 150)
keyboard = Main_menu.get_keyboard()
keyboard.extend([(pygame.K_ESCAPE, "quit")])

Main_menu.add_option(Imagination.text_option(font, "Start", (255, 0, 0), Main_menu), True)
Main_menu.add_option(Imagination.text_option(font, "Options", (0, 255, 0), Main_menu))
Main_menu.add_option(Imagination.text_option(font, "Quit", (0, 0, 255), Main_menu))
Main_menu.set_options()

while running:
    if keyboard.keys["quit"]:
        running = False

    screen.fill((255, 255, 255))

    Main_menu.blit()

    pygame.display.flip()

pygame.quit()

Line 11: We create Main Menu which is on active place (but that place doesn’t matter because it’s only one) and has 150 pixels between each option (but it currently doesn’t have options).

Lines 12-13: We get keyboard from Main Menu and then add (pygame.K_ESCAPE, "quit") to it. So basically if we press esc keyboard["quit"] would be True .

Lines 15-17: We add option to Main Menu which uses font from line 9, have text “Start”, is red, it’s Main Menu is one from line 11 and is option on which index is. And we also add two similar options with some different attributes.

Line 18: We finish our options defining. You don’t have to know what this does but it must be on end of options defining.

Lines 21-22: If keyboard quit is on (so if we pressed esc) then game will stop running.

Line 26: Main Menu blits on screen.

When you run it and hit space/enter one option will disappear. If you press ecs game quits.

If we want options to become black when index come to option we could do this:

 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
from Mind import Imagination
import pygame

screen = pygame.display.set_mode((800, 500))
running = True

pygame.init()

font = pygame.font.SysFont(None, 50)

Main_menu = Imagination.Vertical_menu(Imagination.PLACE(True), 150)
keyboard = Main_menu.get_keyboard()
keyboard.extend([(pygame.K_ESCAPE, "quit")])

Main_menu.add_option(Imagination.text_option(font, "Start", (255, 0, 0), Main_menu, pos_do=Imagination.ch_color((0, 0, 0))), True)
Main_menu.add_option(Imagination.text_option(font, "Options", (0, 255, 0), Main_menu, pos_do=Imagination.ch_color((0, 0, 0))))
Main_menu.add_option(Imagination.text_option(font, "Quit", (0, 0, 255), Main_menu, pos_do=Imagination.ch_color((0, 0, 0))))
Main_menu.set_options()

while running:
    if keyboard.keys["quit"]:
        running = False

    screen.fill((255, 255, 255))

    Main_menu.blit()

    pygame.display.flip()

pygame.quit()

Basically when index come to option its color changes to 0, 0, 0 (black).

But when index is not on option it’s still black, so we should define what happens when we go away from option. To do this we need anti_pos_do attribute:

 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
from Mind import Imagination
import pygame

screen = pygame.display.set_mode((800, 500))
running = True

pygame.init()

font = pygame.font.SysFont(None, 50)

Main_menu = Imagination.Vertical_menu(Imagination.PLACE(True), 150)
keyboard = Main_menu.get_keyboard()
keyboard.extend([(pygame.K_ESCAPE, "quit")])

Main_menu.add_option(Imagination.text_option(font, "Start", (255, 0, 0), Main_menu, pos_do=Imagination.ch_color((0, 0, 0)), anti_pos_do=Imagination.reset()), True)
Main_menu.add_option(Imagination.text_option(font, "Options", (0, 255, 0), Main_menu, pos_do=Imagination.ch_color((0, 0, 0)), anti_pos_do=Imagination.reset()))
Main_menu.add_option(Imagination.text_option(font, "Quit", (0, 0, 255), Main_menu, pos_do=Imagination.ch_color((0, 0, 0)), anti_pos_do=Imagination.reset()))
Main_menu.set_options()

while running:
    if keyboard.keys["quit"]:
        running = False

    screen.fill((255, 255, 255))

    Main_menu.blit()

    pygame.display.flip()

pygame.quit()

Now it’s time to add Game object. It is object can change which menu will be blitted.

 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
from Mind import Imagination
import pygame

screen = pygame.display.set_mode((800, 500))

pygame.init()

font = pygame.font.SysFont(None, 50)

Places = [Imagination.PLACE(True)]

Game = Imagination.Game(Places[0])

Main_menu = Imagination.Vertical_menu(Places[0], 150)
keyboard = Main_menu.get_keyboard()
keyboard.extend([(pygame.K_ESCAPE, "quit")])

Main_menu.add_option(Imagination.text_option(font, "Start", (255, 0, 0), Main_menu, pos_do=Imagination.ch_color((0, 0, 0)), anti_pos_do=Imagination.reset()), True)
Main_menu.add_option(Imagination.text_option(font, "Options", (0, 255, 0), Main_menu, pos_do=Imagination.ch_color((0, 0, 0)), anti_pos_do=Imagination.reset()))
Main_menu.add_option(Imagination.text_option(font, "Quit", (0, 0, 255), Main_menu, pos_do=Imagination.ch_color((0, 0, 0)), anti_pos_do=Imagination.reset()))
Main_menu.set_options()

Main_menu.set_game(Game)

while Game.run():
    if keyboard.keys["quit"]:
        Game.kill()

    screen.fill((255, 255, 255))

    Game.blit()

    pygame.display.flip()

pygame.quit()

Line 10: Now we need that place multiple times.

Line 12: We initialize game object with default place to Places[0]. We also use Places[0] for Main menu in line 14.

Line 23: We set Game`` as Main_menu game.

Lines 25 and 27: We use Game running instead of our.

Now we will add that game exits when we press quit:

 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
from Mind import Imagination
import pygame

screen = pygame.display.set_mode((800, 500))

pygame.init()

font = pygame.font.SysFont(None, 50)

Places = [Imagination.PLACE(True)]

Game = Imagination.Game(Places[0])

Main_menu = Imagination.Vertical_menu(Places[0], 150)
keyboard = Main_menu.get_keyboard()
keyboard.extend([(pygame.K_ESCAPE, "quit")])

Main_menu.add_option(Imagination.text_option(font, "Start", (255, 0, 0), Main_menu, pos_do=Imagination.ch_color((0, 0, 0)), anti_pos_do=Imagination.reset()), True)
Main_menu.add_option(Imagination.text_option(font, "Options", (0, 255, 0), Main_menu, pos_do=Imagination.ch_color((0, 0, 0)), anti_pos_do=Imagination.reset()))
Main_menu.add_option(Imagination.text_option(font, "Quit", (0, 0, 255), Main_menu, Imagination.Quit, pos_do=Imagination.ch_color((0, 0, 0)), anti_pos_do=Imagination.reset()))
Main_menu.set_options()

Main_menu.set_game(Game)

while Game.run():
    if keyboard.keys["quit"]:
        Game.kill()

    screen.fill((255, 255, 255))

    Game.blit()

    pygame.display.flip()

pygame.quit()

We define on line 20 that when we press quit game quits. Note that it’s not same as Game.kill() on line 27 because of way of execution (it’s complicated, but just don’t use Game.kill() in option or Imagination.Quit outside option). Also for this ability you need to have lines: 12, 23 and 25.

We will now add option menu. If we have two menues we should have places and second place should be off.

 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
from Mind import Imagination
import pygame

screen = pygame.display.set_mode((800, 500))

pygame.init()

font = pygame.font.SysFont(None, 50)

Places = [Imagination.PLACE(True)] + [Imagination.PLACE()]

Game = Imagination.Game(Places[0])

Main_menu = Imagination.Vertical_menu(Places[0], 150)
keyboard = Main_menu.get_keyboard()
keyboard.extend([(pygame.K_ESCAPE, "quit")])

Main_menu.add_option(Imagination.text_option(font, "Start", (255, 0, 0), Main_menu, pos_do=Imagination.ch_color((0, 0, 0)), anti_pos_do=Imagination.reset()), True)
Main_menu.add_option(Imagination.text_option(font, "Options", (0, 255, 0), Main_menu, Imagination.link(Places[1]), pos_do=Imagination.ch_color((0, 0, 0)), anti_pos_do=Imagination.reset()))
Main_menu.add_option(Imagination.text_option(font, "Quit", (0, 0, 255), Main_menu, Imagination.Quit, pos_do=Imagination.ch_color((0, 0, 0)), anti_pos_do=Imagination.reset()))
Main_menu.set_options()

Main_menu.set_game(Game)

Options = Imagination.Vertical_menu(Places[1], 150, keyboard=keyboard)
Options.add_option(Imagination.text_option(font, "Sound", (255, 255, 0), Options, pos_do=Imagination.ch_color((0, 0, 0)), anti_pos_do=Imagination.reset()), True)
Options.add_option(Imagination.text_option(font, "Back", (255, 0, 255), Options, Imagination.link(Places[0]), pos_do=Imagination.ch_color((0, 0, 0)), anti_pos_do=Imagination.reset()))
Options.set_options()

Options.set_game(Game)

while Game.run():
    if keyboard.keys["quit"]:
        Game.kill()

    screen.fill((255, 255, 255))

    Game.blit()

    pygame.display.flip()

pygame.quit()

Line 10: Now we need two places.

Line 25: We create Options menu just like Main Menu, but this is on second place and have keyboard from Main Menu.

Lines 26-30: Just like in Main Menu, Back option have link to first place.

For now this turtorial ended, next version will probably have some new things.

Note

Althrought I will continue writting this tutorial, you may expand it or create completely new tutorial (your will probably be way better) and send me link (jakov.manjkas@gmail.com).

Table Of Contents

Previous topic

Mind.Imagination

This Page