File: chess.py

package info (click to toggle)
python-rx 4.0.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,056 kB
  • sloc: python: 39,070; javascript: 77; makefile: 24
file content (102 lines) | stat: -rw-r--r-- 2,639 bytes parent folder | download
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import sys
from os.path import dirname, join

import pygame

from reactivex import operators as ops
from reactivex.scheduler.mainloop import PyGameScheduler
from reactivex.subject import Subject

# FORMAT = '%(asctime)-15s %(threadName)s %(message)s'
# logging.basicConfig(format=FORMAT, level=logging.DEBUG)
# log = logging.getLogger('Rx')


def main():
    pygame.init()

    size = 500, 500
    screen = pygame.display.set_mode(size)
    pygame.display.set_caption("Rx for Python rocks")

    black = 0, 0, 0
    background = pygame.Surface(screen.get_size())
    background.fill(black)  # fill the background black
    background = background.convert()  # prepare for faster blitting

    scheduler = PyGameScheduler(pygame)

    mousemove = Subject()

    color = "white"
    base = dirname(__file__)
    files = [
        join(base, img % color)
        for img in [
            "chess_rook_%s.png",
            "chess_knight_%s.png",
            "chess_bishop_%s.png",
            "chess_king_%s.png",
            "chess_queen_%s.png",
            "chess_bishop_%s.png",
            "chess_knight_%s.png",
            "chess_rook_%s.png",
        ]
    ]
    images = [pygame.image.load(image).convert_alpha() for image in files]

    old = [None] * len(images)
    draw = []
    erase = []

    def handle_image(i, image):
        imagerect = image.get_rect()

        def on_next(ev):
            imagerect.top = ev[1]
            imagerect.left = ev[0] + i * 32

            if old[i]:
                erase.append(old[i])
            old[i] = imagerect.copy()
            draw.append((image, imagerect.copy()))

        def on_error(err):
            print("Got error: %s" % err)
            sys.exit()

        mousemove.pipe(ops.delay(0.1 * i, scheduler=scheduler)).subscribe(
            on_next, on_error=on_error
        )

    for i, image in enumerate(images):
        handle_image(i, image)

    while True:
        for event in pygame.event.get():
            if event.type == pygame.MOUSEMOTION:
                pos = event.pos
                mousemove.on_next(pos)
            elif event.type == pygame.QUIT:
                sys.exit()

        if len(draw):
            update = []
            for rect in erase:
                screen.blit(background, (rect.x, rect.y), rect)
                update.append(rect)

            for image, rect in draw:
                screen.blit(image, rect)
                update.append(rect)

            pygame.display.update(update)
            pygame.display.flip()
            draw = []
            erase = []

        scheduler.run()


if __name__ == "__main__":
    main()