1
|
|
|
#!/usr/bin/env python |
2
|
|
|
""" |
3
|
|
|
Sample pack showcasing the abilities of TDL |
4
|
|
|
|
5
|
|
|
This example is not finished quite yet |
6
|
|
|
""" |
7
|
|
|
|
8
|
|
|
import sys |
9
|
|
|
|
10
|
|
|
import random |
11
|
|
|
|
12
|
|
|
sys.path.insert(0, '../') |
13
|
|
|
import tdl |
14
|
|
|
|
15
|
|
|
sampleIndex = 0 |
16
|
|
|
|
17
|
|
|
class SampleApp(tdl.event.App): |
18
|
|
|
name = '' |
19
|
|
|
|
20
|
|
|
def key_UP(self, event): |
21
|
|
|
global sampleIndex |
22
|
|
|
sampleIndex = (sampleIndex - 1) % len(samples) |
23
|
|
|
|
24
|
|
|
def key_DOWN(self, event): |
25
|
|
|
global sampleIndex |
26
|
|
|
sampleIndex = (sampleIndex - 1) % len(samples) |
27
|
|
|
|
28
|
|
|
class TrueColorSample(SampleApp): |
29
|
|
|
name = 'True Colors' |
30
|
|
|
|
31
|
|
|
def update(self, deltaTime): |
32
|
|
|
width, height = samplewin.getSize() |
33
|
|
|
for x in range(width): |
34
|
|
|
for y in range(height): |
35
|
|
|
char = random.getrandbits(8) |
36
|
|
|
samplewin.drawChar(x, y, char, (255, 255, 255), (0, 0, 0)) |
37
|
|
|
|
38
|
|
|
class NoiseSample(SampleApp): |
39
|
|
|
name = 'Noise' |
40
|
|
|
SPEED = 3 |
41
|
|
|
|
42
|
|
|
NOISE_KEYS = {'1': 'PERLIN', '2': 'SIMPLEX', '3': 'WAVELET'} |
43
|
|
|
MODE_KEYS = {'4': 'FLAT', '5': 'FBM', '6': 'TURBULENCE'} |
44
|
|
|
|
45
|
|
|
def __init__(self): |
46
|
|
|
self.noiseType = 'PERLIN' |
47
|
|
|
self.noiseMode = 'FLAT' |
48
|
|
|
self.x = 0 |
49
|
|
|
self.y = 0 |
50
|
|
|
self.z = 0 |
51
|
|
|
self.zoom = 4 |
52
|
|
|
self.generateNoise() |
53
|
|
|
|
54
|
|
|
def generateNoise(self): |
55
|
|
|
self.noise = tdl.noise.Noise(self.noiseType, self.noiseMode, seed=42) |
56
|
|
|
|
57
|
|
|
def ev_KEYDOWN(self, event): |
58
|
|
|
if event.char in self.NOISE_KEYS: |
59
|
|
|
self.noiseType = self.NOISE_KEYS[event.char] |
60
|
|
|
print('noise set to %s' % self.noiseType) |
61
|
|
|
if event.char in self.MODE_KEYS: |
62
|
|
|
self.noiseMode = self.MODE_KEYS[event.char] |
63
|
|
|
print('mode set to %s' % self.noiseMode) |
64
|
|
|
self.generateNoise() |
65
|
|
|
|
66
|
|
|
def update(self, deltaTime): |
67
|
|
|
self.x += self.SPEED * deltaTime# * self.zoom |
68
|
|
|
self.y += self.SPEED * deltaTime# * self.zoom |
69
|
|
|
self.z += deltaTime / 4 |
70
|
|
|
|
71
|
|
|
width, height = samplewin.getSize() |
72
|
|
|
for x in range(width): |
73
|
|
|
for y in range(height): |
74
|
|
|
val = self.noise.getPoint((x + self.x) / width * self.zoom, |
75
|
|
|
(y + self.y) / height * self.zoom, |
76
|
|
|
self.z) |
77
|
|
|
bgcolor = (int(val * 255),) * 2 + (min(255, int(val * 2 * 255)),) |
78
|
|
|
samplewin.drawChar(x, y, ' ', (255, 255, 255), bgcolor) |
79
|
|
|
|
80
|
|
|
WIDTH, HEIGHT = 80, 40 |
81
|
|
|
SAMPLE_WINDOW_RECT = (20, 10, 46, 20) |
82
|
|
|
|
83
|
|
|
FONT = '../fonts/X11/8x13.png' |
84
|
|
|
|
85
|
|
|
if __name__ == '__main__': |
86
|
|
|
tdl.setFont(FONT) |
87
|
|
|
console = tdl.init(WIDTH, HEIGHT, renderer='opengl') |
88
|
|
|
samplewin = tdl.Window(console, *SAMPLE_WINDOW_RECT) |
89
|
|
|
|
90
|
|
|
samples = [cls() for cls in [TrueColorSample, NoiseSample]] |
91
|
|
|
|
92
|
|
|
while 1: |
93
|
|
|
console.clear() |
94
|
|
|
samples[sampleIndex].runOnce() |
95
|
|
|
for i, sample in enumerate(samples): |
96
|
|
|
bgcolor = (0, 0, 0) |
97
|
|
|
if sampleIndex == i: |
98
|
|
|
bgcolor = (0, 0, 192) |
99
|
|
|
console.drawStr(0, -5 + i, '%s' % sample.name, (255, 255, 255), bgcolor) |
100
|
|
|
console.drawStr(0, -1, '%i FPS' % tdl.getFPS()) |
101
|
|
|
tdl.flush() |
102
|
|
|
|