Hier werden die Unterschiede zwischen zwei Versionen gezeigt.
Beide Seiten der vorigen Revision Vorhergehende Überarbeitung Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
ss20:neg_utility [2020/08/26 20:59] srather [Sourcecode] |
ss20:neg_utility [2020/09/11 14:28] (aktuell) srather [draw] |
||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
- | [[nichteuklidische_geometrie|Back to project page]] | + | [[nichteuklidische_geometrie|← Back to project page]] |
====== Sourcecode ====== | ====== Sourcecode ====== | ||
- | Download sourcecode from Git repository: | + | Download the release version here:\\ |
+ | {{:ss20:neg-world-engine.zip}} | ||
- | [[https://gitlab.tubit.tu-berlin.de/srather/NEG-World-Engine.git|https://gitlab.tubit.tu-berlin.de/srather/NEG-World-Engine.git]] | + | Or download the newest version from the Git repository:\\ |
+ | [[https://gitlab.tubit.tu-berlin.de/srather/NEG-World-Engine.git]] | ||
==== Folder Structure: ==== | ==== Folder Structure: ==== | ||
- | **[[neg_sourcecode|→ Root]]:** [[neg_sourcecode#readme|README]], [[neg_sourcecode#main|main]] | + | **[[neg_sourcecode|Root]]:** [[neg_sourcecode#readme|README]], [[neg_sourcecode#main|main]] |
- | * **[[neg_datatypes|→ Datatypes]]:** [[neg_datatypes#vector|Vector]], [[neg_datatypes#matrix|Matrix]] | + | * **[[neg_datatypes|Datatypes]]:** [[neg_datatypes#vector|Vector]], [[neg_datatypes#matrix|Matrix]] |
- | * **[[neg_drawables|→ Drawables]]:** [[neg_drawables#drawable|drawable]], [[neg_drawables#ray|Ray]], [[neg_drawables#polygon|Polygon]], [[neg_drawables#floor|Floor]], [[neg_drawables#portal|Portal]] | + | * **[[neg_drawables|Drawables]]:** [[neg_drawables#drawable|drawable]], [[neg_drawables#ray|Ray]], [[neg_drawables#polygon|Polygon]], [[neg_drawables#floor|Floor]], [[neg_drawables#portal|Portal]] |
- | * **[[neg_utility|↓ Utility]]:** [[neg_utility#colors|colors]], [[neg_utility#generate|generate]], [[neg_utility#draw|draw]] | + | * **[[neg_utility|Utility]]:** [[neg_utility#colors|colors]], [[neg_utility#generate|generate]], [[neg_utility#draw|draw]] |
===== Utility ===== | ===== Utility ===== | ||
Zeile 71: | Zeile 73: | ||
</file> | </file> | ||
- | [[neg_utility#sourcecode|Back to top]] | + | [[#top|↑ Back to top]] |
---- | ---- | ||
Zeile 82: | Zeile 84: | ||
from matrix import * | from matrix import * | ||
- | def polygon(edges, center, radius, phase=0, M=Matrix((1,0),(0,1)), color=(0,0,0)): | + | |
+ | def polygon(edges, center, radius, phase=0, M=((1,0),(0,1)), color=(0,0,0)): | ||
""" | """ | ||
Return a regular polygon. | Return a regular polygon. | ||
Zeile 97: | Zeile 100: | ||
x = radius * math.sin((n + phase) * math.tau / edges) | x = radius * math.sin((n + phase) * math.tau / edges) | ||
y = radius * math.cos((n + phase) * math.tau / edges) | y = radius * math.cos((n + phase) * math.tau / edges) | ||
- | poly.append(Vector(*center) + M.prod(Vector(x, y))) | + | poly.append(Vector(*center) + Matrix(*M).prod(Vector(x, y))) |
return poly | return poly | ||
Zeile 128: | Zeile 131: | ||
def border(polygon): | def border(polygon): | ||
+ | """ | ||
+ | Turn a polygon into a border. | ||
+ | (split it into its sides) | ||
+ | """ | ||
+ | |||
bor = [] | bor = [] | ||
for i, _ in enumerate(polygon): | for i, _ in enumerate(polygon): | ||
bor.append(Polygon(polygon[i-1], polygon[i])) | bor.append(Polygon(polygon[i-1], polygon[i])) | ||
return bor | return bor | ||
+ | |||
def door(start, end, part): | def door(start, end, part): | ||
+ | """ | ||
+ | Turn a wall into a door. | ||
+ | (make a hole in the middle with relative size 'part') | ||
+ | """ | ||
+ | |||
return (Polygon(start, start+(1-part)/2*(end-start)), Polygon(end+(1-part)/2*(start-end), end)) | return (Polygon(start, start+(1-part)/2*(end-start)), Polygon(end+(1-part)/2*(start-end), end)) | ||
</file> | </file> | ||
- | [[neg_utility#sourcecode|Back to top]] | + | [[#top|↑ Back to top]] |
---- | ---- | ||
Zeile 150: | Zeile 164: | ||
from portal import * | from portal import * | ||
- | def player(screen, offset, player): | + | |
+ | def player(screen, offset, size): | ||
+ | """ | ||
+ | Draw the player as a white circle in the middle of the screen. | ||
+ | """ | ||
rect = Vector(*screen.get_rect()[2:]) | rect = Vector(*screen.get_rect()[2:]) | ||
- | # pygame.draw.aaline(screen, colors.BRIGHT_RED, round((rect + Vector(player, player)) / 2 - offset), round((rect - Vector(player, player)) / 2 - offset)) | + | pygame.gfxdraw.aacircle(screen, *round(rect / 2), size // 2, colors.WHITE) |
- | # pygame.draw.aaline(screen, colors.BRIGHT_RED, round((rect + Vector(player, -player)) / 2 - offset), round((rect - Vector(player, -player)) / 2 - offset)) | + | pygame.gfxdraw.filled_circle(screen, *round(rect / 2), size // 2, colors.WHITE) |
- | pygame.gfxdraw.aacircle(screen, *round(rect / 2), player // 2, colors.WHITE) | + | |
- | pygame.gfxdraw.filled_circle(screen, *round(rect / 2), player // 2, colors.WHITE) | + | |
- | def world(screen, wrld, offset, depth, M=Matrix((1,0),(0,1))): | + | def world(screen, wrld, offset, depth): |
+ | """ | ||
+ | Draw all objects in 'wrld' in the right order with their shadows | ||
+ | and portal worlds to the maximum recursion-'depth'. | ||
+ | """ | ||
+ | |||
+ | # put all objects in a tuple with their distance to be sortable | ||
dists = [] | dists = [] | ||
for object in wrld: | for object in wrld: | ||
dists.append((object.dist_to(offset), object)) | dists.append((object.dist_to(offset), object)) | ||
+ | # draw the furtheset object first | ||
for object in reversed(sorted(dists)): | for object in reversed(sorted(dists)): | ||
if isinstance(object[1], drawable): | if isinstance(object[1], drawable): | ||
+ | # draw order: shadow, portal world, object | ||
object[1].draw_shadow(screen, offset, offset) | object[1].draw_shadow(screen, offset, offset) | ||
if depth > 0 and type(object[1]) is Portal: | if depth > 0 and type(object[1]) is Portal: | ||
Zeile 173: | Zeile 198: | ||
- | chars = { | + | def debug(screen, y, info, color): |
- | '': [0b10101010, 0b01010101, 0b10101010, 0b01010101, 0b10101010, 0b01010101], | + | """ |
- | '0': [0b00111110, 0b01000101, 0b01001001, 0b01010001, 0b00111110, 0b00000000], | + | Draw debug information 'info' to the top right corner. |
- | '1': [0b01000000, 0b01000000, 0b01111111, 0b01000010, 0b01000000, 0b00000000], | + | 'y' is the textline and 'color' the color. |
- | '2': [0b01000110, 0b01001001, 0b01001001, 0b01010001, 0b01100010, 0b00000000], | + | """ |
- | '3': [0b00110110, 0b01001001, 0b01001001, 0b01000001, 0b00100010, 0b00000000], | + | |
- | '4': [0b01111111, 0b00010001, 0b00010010, 0b00010100, 0b00011000, 0b00000000], | + | # binary representation of some chars |
- | '5': [0b00111001, 0b01000101, 0b01000101, 0b01000101, 0b00100111, 0b00000000], | + | chars = { |
- | '6': [0b00110000, 0b01001001, 0b01001001, 0b01001010, 0b00111100, 0b00000000], | + | '': [0b10101010, 0b01010101, 0b10101010, 0b01010101, 0b10101010, 0b01010101], |
- | '7': [0b00000111, 0b00001001, 0b01110001, 0b00000001, 0b00000011, 0b00000000], | + | '0': [0b00111110, 0b01000101, 0b01001001, 0b01010001, 0b00111110, 0b00000000], |
- | '8': [0b00110110, 0b01001001, 0b01001001, 0b01001001, 0b00110110, 0b00000000], | + | '1': [0b01000000, 0b01000000, 0b01111111, 0b01000010, 0b01000000, 0b00000000], |
- | '9': [0b00011110, 0b00101001, 0b01001001, 0b01001001, 0b00000110, 0b00000000], | + | '2': [0b01000110, 0b01001001, 0b01001001, 0b01010001, 0b01100010, 0b00000000], |
- | '.': [0b01100000, 0b00000000], | + | '3': [0b00110110, 0b01001001, 0b01001001, 0b01000001, 0b00100010, 0b00000000], |
- | ',': [0b11100000, 0b00000000], | + | '4': [0b01111111, 0b00010001, 0b00010010, 0b00010100, 0b00011000, 0b00000000], |
- | '+': [0b00001000, 0b00001000, 0b00111110, 0b00001000, 0b00001000, 0b00000000], | + | '5': [0b00111001, 0b01000101, 0b01000101, 0b01000101, 0b00100111, 0b00000000], |
- | '-': [0b00001000, 0b00001000, 0b00001000, 0b00001000, 0b00001000, 0b00000000], | + | '6': [0b00110000, 0b01001001, 0b01001001, 0b01001010, 0b00111100, 0b00000000], |
- | ' ': [0b00000000, 0b00000000, 0b00000000], | + | '7': [0b00000111, 0b00001001, 0b01110001, 0b00000001, 0b00000011, 0b00000000], |
- | 'F': [0b00000001, 0b00001001, 0b00001001, 0b00001001, 0b01111111, 0b00000000], | + | '8': [0b00110110, 0b01001001, 0b01001001, 0b01001001, 0b00110110, 0b00000000], |
- | 'P': [0b00000110, 0b00001001, 0b00001001, 0b00001001, 0b01111111, 0b00000000], | + | '9': [0b00011110, 0b00101001, 0b01001001, 0b01001001, 0b00000110, 0b00000000], |
- | 'S': [0b00110001, 0b01001001, 0b01001001, 0b01001001, 0b01000110, 0b00000000], | + | '.': [0b01100000, 0b00000000], |
- | 'X': [0b01100011, 0b00010100, 0b00001000, 0b00010100, 0b01100011, 0b00000000], | + | ',': [0b11100000, 0b00000000], |
- | 'Y': [0b00000011, 0b00000100, 0b01111000, 0b00000100, 0b00000011, 0b00000000], | + | '+': [0b00001000, 0b00001000, 0b00111110, 0b00001000, 0b00001000, 0b00000000], |
- | } | + | '-': [0b00001000, 0b00001000, 0b00001000, 0b00001000, 0b00001000, 0b00000000], |
+ | ' ': [0b00000000, 0b00000000, 0b00000000], | ||
+ | 'F': [0b00000001, 0b00001001, 0b00001001, 0b00001001, 0b01111111, 0b00000000], | ||
+ | 'P': [0b00000110, 0b00001001, 0b00001001, 0b00001001, 0b01111111, 0b00000000], | ||
+ | 'S': [0b00110001, 0b01001001, 0b01001001, 0b01001001, 0b01000110, 0b00000000], | ||
+ | 'X': [0b01100011, 0b00010100, 0b00001000, 0b00010100, 0b01100011, 0b00000000], | ||
+ | 'Y': [0b00000011, 0b00000100, 0b01111000, 0b00000100, 0b00000011, 0b00000000], | ||
+ | } | ||
- | def debug(screen, y, fps, color): | ||
pos = screen.get_rect()[2] - 4 | pos = screen.get_rect()[2] - 4 | ||
- | for char in reversed(str(fps)): | + | for char in reversed(str(info)): |
for line in chars.get(char, chars.get(char.upper(), chars[''])): | for line in chars.get(char, chars.get(char.upper(), chars[''])): | ||
for i in range(8): | for i in range(8): | ||
Zeile 208: | Zeile 239: | ||
</file> | </file> | ||
- | [[neg_utility#sourcecode|Back to top]] | + | [[#top|↑ Back to top]] |