Skip to content

Commit 571e820

Browse files
committed
initial commit
0 parents  commit 571e820

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+1849
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__pycache__/

COPYING

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
2+
Version 2, December 2004
3+
4+
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
5+
6+
Everyone is permitted to copy and distribute verbatim or modified
7+
copies of this license document, and changing it is allowed as long
8+
as the name is changed.
9+
10+
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
11+
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
12+
13+
0. You just DO WHAT THE FUCK YOU WANT TO.
14+

DejaVuSansMono.ttf

327 KB
Binary file not shown.

animation.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
2+
import pygame
3+
4+
5+
6+
7+
class Animation(object):
8+
__slots__ = ('_count', '_sprite', '_current', '_idx', '_width', '_height', '_duration', '_tick', '_once', '_done')
9+
def __init__(self, sprite: pygame.Surface, count: int = 1, fps: int = 8, once: bool = False) -> None:
10+
super().__init__()
11+
self._count = count
12+
self._sprite = sprite
13+
self._current = None
14+
self._once = once
15+
self._done = False
16+
self._idx = 0
17+
self._width = self._sprite.get_width() // count
18+
self._height = self._sprite.get_height()
19+
self._duration = 1000 // fps
20+
self._tick = 0
21+
22+
def update(self, delta: int) -> None:
23+
if self._done:
24+
return
25+
self._tick -= delta
26+
reclip = False
27+
while self._tick <= 0:
28+
reclip = True
29+
self._idx += 1
30+
self._tick += self._duration
31+
if reclip:
32+
if self._idx >= self._count:
33+
self._idx %= self._count
34+
self._done = self._once
35+
self._current = self._sprite.subsurface((self._width * self._idx, 0, self._width, self._height))
36+
37+
def render(self, target: pygame.Surface, x: int, y: int) -> None:
38+
if self._current is None:
39+
return
40+
target.blit(self._current, (x, y))
41+
42+
width = property(lambda s: s._width)
43+
height = property(lambda s: s._height)
44+
surface = property(lambda s: s._current)
45+
done = property(lambda s: s._done)
46+
47+
48+
49+
50+
51+
52+
53+
if __name__ == '__main__':
54+
import sys
55+
try:
56+
fn = sys.argv[1]
57+
cnt = int(sys.argv[2])
58+
except:
59+
fn = 'test.png'
60+
cnt = 6
61+
def load_image(filename: str) -> pygame.Surface:
62+
return pygame.image.load(filename).convert_alpha()
63+
pygame.init()
64+
screen = pygame.display.set_mode((480, 320))
65+
clock = pygame.time.Clock()
66+
running = True
67+
delta = 0
68+
a = Animation(load_image(fn), cnt, 12)
69+
while running:
70+
# INPUT
71+
for e in pygame.event.get():
72+
if e.type == pygame.QUIT:
73+
running = False
74+
continue
75+
# UPDATE
76+
a.update(delta)
77+
# RENDER
78+
screen.fill((0,0,0))
79+
a.render(screen, 5, 5)
80+
#pygame.display.flip()
81+
pygame.display.update()
82+
# TICK
83+
delta = clock.tick(30)

animations.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
2+
import enum
3+
from enum import auto
4+
import os
5+
6+
import pygame
7+
8+
import animation
9+
10+
DIRECTORY = '~/Bilder/coder/export'
11+
12+
_surfaces = {}
13+
class SurfaceType(enum.Enum):
14+
VIM = auto()
15+
ATOM = auto()
16+
EMACS = auto()
17+
INTELLIJ = auto()
18+
NANO = auto()
19+
VSCODE = auto()
20+
21+
BUG_RED_1 = auto()
22+
BUG_RED_2 = auto()
23+
BUG_GREEN_1 = auto()
24+
BUG_GREEN_2 = auto()
25+
BUG_YELLOW_1 = auto()
26+
BUG_YELLOW_2 = auto()
27+
BUG_GRAY_1 = auto()
28+
BUG_GRAY_2 = auto()
29+
30+
TRY_EXCEPT = auto()
31+
CATCH_EXCEPTION = auto()
32+
33+
FOR_LOOP = auto()
34+
35+
FLOATINGPOINTERROR = auto()
36+
INDEXERROR = auto()
37+
KEYERROR = auto()
38+
MEMORYERROR = auto()
39+
NOTIMPLEMENTEDERROR = auto()
40+
OVERFLOWERROR = auto()
41+
RECURSIONERROR = auto()
42+
RUNTIMEERROR = auto()
43+
TYPEERROR = auto()
44+
45+
def get_filename(self):
46+
if self == Surfaces.VIM: fn = 'vim.png'
47+
elif self == Surfaces.ATOM: fn = 'atom.png'
48+
elif self == Surfaces.EMACS: fn = 'emacs.png'
49+
elif self == Surfaces.INTELLIJ: fn = 'intellij.png'
50+
elif self == Surfaces.NANO: fn = 'nano.png'
51+
elif self == Surfaces.VSCODE: fn = 'vscode.png'
52+
elif self == Surfaces.BUG_RED_1: fn = 'bug-red-1.png'
53+
elif self == Surfaces.BUG_RED_2: fn = 'bug-red-2.png'
54+
elif self == Surfaces.BUG_GREEN_1: fn = 'bug-green-1.png'
55+
elif self == Surfaces.BUG_GREEN_2: fn = 'bug-green-2.png'
56+
elif self == Surfaces.BUG_YELLOW_1: fn = 'bug-yellow-1.png'
57+
elif self == Surfaces.BUG_YELLOW_2: fn = 'bug-yellow-2.png'
58+
elif self == Surfaces.BUG_GRAY_1: fn = 'bug-gray-1.png'
59+
elif self == Surfaces.BUG_GRAY_2: fn = 'bug-gray-2.png'
60+
elif self == Surfaces.TRY_EXCEPT: fn = 'try-except.png'
61+
elif self == Surfaces.CATCH_EXCEPTION: fn = 'catch-exception.png'
62+
elif self == Surfaces.FOR_LOOP: fn = 'for-loop.png'
63+
elif self == Surfaces.FLOATINGPOINTERROR: fn = 'floatingpointerror.png'
64+
elif self == Surfaces.INDEXERROR: fn = 'indexerror.png'
65+
elif self == Surfaces.KEYERROR: fn = 'keyerror.png'
66+
elif self == Surfaces.MEMORYERROR: fn = 'memoryerror.png'
67+
elif self == Surfaces.NOTIMPLEMENTEDERROR: fn = 'notimplementederror.png'
68+
elif self == Surfaces.OVERFLOWERROR: fn = 'overflowerror.png'
69+
elif self == Surfaces.RECURSIONERROR: fn = 'recursionerror.png'
70+
elif self == Surfaces.RUNTIMEERROR: fn = 'runtimeerror.png'
71+
elif self == Surfaces.TYPEERROR: fn = 'typeerror.png'
72+
else raise Exception('Unknown Surface Type: {}'.format(str(self)))
73+
return os.path.join(DIRECTORY, fn)
74+
75+
def get_surface(self):
76+
if self in _surfaces:
77+
return _surfaces[self]
78+
surface = pygame.image.load(self.get_filename()).convert_alpha()
79+
_surfaces[self] = surface
80+
return surface
81+
82+
83+
class Player(animation.Animation):
84+
def __init__(self, player_type: SurfaceType):
85+
super().__init__(player_type.get_surface(), 1, 1, False)
86+
87+
class Bug(animation.Animation):
88+
def __init__(self, bug_type: SurfaceType):
89+
super().__init__(bug_type.get_surface(), 3, 12, False)
90+
91+
class TryExcept(animation.Animation):
92+
def __init__(self):
93+
super().__init__(SurfaceType.TRY_EXCEPT.get_surface(), 1, 1, False)
94+
95+
class CatchException(animation.Animation):
96+
def __init__(self, x: int, y: int):
97+
super().__init__(SurfaceType.CATCH_EXCEPTION.get_surface(), 8, 20, True)
98+
self._x = x
99+
self._y = y
100+
101+
def render(self, target, *args):
102+
super().render(target, self._x, self._y)
103+
104+
class Snippet(animation.Animation):
105+
def __init__(self, snippet_type: SurfaceType):
106+
super().__init__(snippet_type.get_surface(), 1, 1, False)
107+
108+
class Error(animation.Animation):
109+
def __init__(self, error_type: SurfaceType):
110+
super().__init__(error_type.get_surface(), 1, 1, False)
111+
112+

background.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
2+
from typing import List
3+
4+
import pygame
5+
6+
from collections import namedtuple
7+
import random
8+
9+
class Background(object):
10+
__slots__ = ('_chars', '_surfaces', '_tick', '_width', '_height', '_max_idx', '_min_new', '_max_new')
11+
12+
class Char(object):
13+
__slots__ = ('x', 'y', 'v', 'surface')
14+
def __init__(self, x, y, velocity, surface) -> None:
15+
super().__init__()
16+
self.x = int(x)
17+
self.y = y
18+
self.v = velocity
19+
self.surface = surface
20+
21+
def __init__(self, w: int, h: int) -> None:
22+
super().__init__()
23+
self._chars = [] # type: List[Char]
24+
self._surfaces = [] # type: List[chr, pygame.Surface]
25+
self._max_idx = len(self._surfaces)
26+
self._tick = 0
27+
self._width = w
28+
self._height = h
29+
self._min_new = max(1, w // 120)
30+
self._max_new = max(2, w // 30)
31+
self._init_surfaces()
32+
33+
def _init_surfaces(self):
34+
CHARS = '#+!{}[]&%$?' + ''.join(map(chr, range(ord('a'), ord('z')))) + ''.join(map(chr, range(ord('A'), ord('Z'))))
35+
font = pygame.font.Font('DejaVuSansMono.ttf', 12)
36+
surface = font.render(CHARS, True, (70, 150, 25), None).convert_alpha()
37+
width = surface.get_width() // len(CHARS)
38+
height = surface.get_height()
39+
for i in range(len(CHARS)):
40+
subsurface = surface.subsurface((i * width, 0, width, height))
41+
self._surfaces.append(subsurface)
42+
self._max_idx = len(self._surfaces) - 1
43+
44+
def update(self, delta: int) -> None:
45+
self._tick -= delta
46+
if self._tick <= 0:
47+
for i in range(random.randint(self._min_new, self._max_new)):
48+
c = Background.Char(random.randint(1, self._width), -10, .15 + (random.random() * .2), self._surfaces[random.randint(0, self._max_idx)])
49+
self._chars.append(c)
50+
self._tick = 125
51+
self._chars = [c for c in self._chars if c.y < self._height]
52+
for c in self._chars:
53+
c.y += (c.v * delta)
54+
55+
def render(self, target: pygame.Surface, x: int, y: int) -> None:
56+
target.fill((30, 30, 30))
57+
for c in self._chars:
58+
target.blit(c.surface, (c.x, int(c.y)))
59+
60+
61+
62+
if __name__ == '__main__':
63+
SIZE = 600, 600
64+
pygame.init()
65+
screen = pygame.display.set_mode(SIZE, pygame.DOUBLEBUF)
66+
clock = pygame.time.Clock()
67+
running = True
68+
delta = 0
69+
b = Background(*SIZE)
70+
fpss = []
71+
min_fps = -1000
72+
while running:
73+
# INPUT
74+
for e in pygame.event.get():
75+
if e.type == pygame.QUIT:
76+
running = False
77+
continue
78+
# UPDATE
79+
b.update(delta)
80+
# RENDER
81+
b.render(screen, 5, 5)
82+
#pygame.display.flip()
83+
pygame.display.update()
84+
# TICK
85+
delta = clock.tick(1000)
86+
#fps = 1000 / delta
87+
#if len(fpss) > 100:
88+
# print('avg fps {:.1f} min fps: {:.1f}'.format(sum(fpss) / 100, min(fpss)))
89+
# if min_fps < 0:
90+
# min_fps = 1000 # hack to ignore the first run
91+
# elif min(fpss) < min_fps:
92+
# min_fps = min(fpss)
93+
# fpss = []
94+
#fpss.append(fps)
95+
#print('MIN: {:.2f}'.format(min_fps))

bugs.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import pygame
2+
import consts
3+
import color
4+
from typing import List
5+
6+
class Bugs(object):
7+
def __init__(self, color, x, y) -> None:
8+
self._rect = None # type: pygame.Rect
9+
self._textsurface = None
10+
self._myfont = None
11+
self._x = x
12+
self._y = y
13+
self._col = color
14+
self._is_killed = False # type: bool
15+
16+
def render(self, screen):
17+
self._rect = pygame.Rect(self._x, self._y, consts.BUG_W, consts.BUG_H)
18+
pygame.draw.rect(screen, self._col, self._rect, 0)
19+
20+
def is_collided(self, sprite: pygame.Rect):
21+
if self._rect != None:
22+
return self._rect.colliderect(sprite)
23+
else: return False
24+
25+
26+
class BugManager(object):
27+
def __init__(self) -> None:
28+
self._bugs = []
29+
self._snippets = None # type: list
30+
31+
def add_bug(self, color, x, y):
32+
self._bugs.append(Bugs(color, x, y))
33+
34+
def inject_snippets(self, snippets: List[pygame.Rect]) -> None:
35+
self._snippets = snippets
36+
37+
def render(self, screen):
38+
for bug in self._bugs:
39+
for snip in self._snippets:
40+
if snip._rect != None:
41+
if bug.is_collided(snip._rect):
42+
bug._is_killed = True
43+
snip._is_killed = True
44+
bug.render(screen)
45+
self._bugs = list(filter(lambda b: not b._is_killed, self._bugs)) # some weird stuff from lukas ask lukas
46+

0 commit comments

Comments
 (0)