From ef56d9a254d5441bac327074806f54f561878d94 Mon Sep 17 00:00:00 2001 From: Sarah C <144068104+iamawatermelo@users.noreply.github.com> Date: Tue, 4 Feb 2025 00:43:36 +0000 Subject: [PATCH] space game :3 --- pets/sarahs_space_game/LICENSE.md | 13 ++ pets/sarahs_space_game/Makefile | 7 + pets/sarahs_space_game/README.md | 10 ++ pets/sarahs_space_game/game.py | 198 +++++++++++++++++++++++++++ pets/sarahs_space_game/output.bmp | Bin 0 -> 2102 bytes pets/sarahs_space_game/spaceship.bmp | Bin 0 -> 2110 bytes pets/sarahs_space_game/spaceship.png | Bin 0 -> 1262 bytes 7 files changed, 228 insertions(+) create mode 100644 pets/sarahs_space_game/LICENSE.md create mode 100644 pets/sarahs_space_game/Makefile create mode 100644 pets/sarahs_space_game/README.md create mode 100644 pets/sarahs_space_game/game.py create mode 100644 pets/sarahs_space_game/output.bmp create mode 100644 pets/sarahs_space_game/spaceship.bmp create mode 100644 pets/sarahs_space_game/spaceship.png diff --git a/pets/sarahs_space_game/LICENSE.md b/pets/sarahs_space_game/LICENSE.md new file mode 100644 index 00000000..5c93f456 --- /dev/null +++ b/pets/sarahs_space_game/LICENSE.md @@ -0,0 +1,13 @@ + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + Version 2, December 2004 + + Copyright (C) 2004 Sam Hocevar + + Everyone is permitted to copy and distribute verbatim or modified + copies of this license document, and changing it is allowed as long + as the name is changed. + + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. You just DO WHAT THE FUCK YOU WANT TO. diff --git a/pets/sarahs_space_game/Makefile b/pets/sarahs_space_game/Makefile new file mode 100644 index 00000000..66c875c7 --- /dev/null +++ b/pets/sarahs_space_game/Makefile @@ -0,0 +1,7 @@ +help: + echo "make images - turn .pngs into .bmps" + +images: spaceship.bmp + +%.bmp: %.png + magick $< +dither -colors 2 -type palette BMP3:$@ \ No newline at end of file diff --git a/pets/sarahs_space_game/README.md b/pets/sarahs_space_game/README.md new file mode 100644 index 00000000..d2487041 --- /dev/null +++ b/pets/sarahs_space_game/README.md @@ -0,0 +1,10 @@ +# sarahs_space_game + +Procedurally generated space game. + +NOTE: To run this game, please install CyrilSLi's fork of +Blinka_Displayio_PyGameDisplay. + +``` +pip install git+https://github.com/CyrilSLi/Blinka_Displayio_PyGameDisplay +``` \ No newline at end of file diff --git a/pets/sarahs_space_game/game.py b/pets/sarahs_space_game/game.py new file mode 100644 index 00000000..9ea99b01 --- /dev/null +++ b/pets/sarahs_space_game/game.py @@ -0,0 +1,198 @@ +""" +sarahs_space_game + +Copyright © 2024 Sarah C +This work is free. You can redistribute it and/or modify it under the +terms of the Do What The Fuck You Want To Public License, Version 2, +as published by Sam Hocevar. See the LICENSE.md file for more details. +""" + +from math import cos, pi, sin, sqrt +from random import randint +from time import monotonic, sleep +import sys +from blinka_displayio_pygamedisplay import PyGameDisplay +import pygame +import displayio + +IS_CIRCUITPYTHON = sys.implementation.name == "circuitpython" + +if IS_CIRCUITPYTHON: + def dummy(*_args, **_kwargs): + pass + + debug = dummy + warning = dummy + error = dummy +else: + import logging + logging.basicConfig(level=logging.DEBUG) + logger = logging.getLogger(__name__) + + debug = logger.debug + warning = logger.warning + error = logger.error + +ACCELERATION_FACTOR = 1.2 +ACCELERATION_CONSTANT = 400 +ACCELERATION_CEILING = 5000 +ROTATIONAL_VELOCITY_FACTOR = 2 +VELOCITY_DECAY_FACTOR = -0.7 +VELOCITY_DECAY_CONSTANT = 490 +DISPLAY_SCALING_FACTOR = 3 +WIDTH = 128 + +spaceship = displayio.OnDiskBitmap("spaceship.bmp") + + +class Palette: + NOT_BLACK = 0x313950 + LESS_BLACK = 0x434B65 + EVEN_LESS_BLACK = 0x5B647E + NOT_WHITE = 0xCBD8FF + +# Setup and initialisation code +display = PyGameDisplay( + width=WIDTH*DISPLAY_SCALING_FACTOR, + height=WIDTH*DISPLAY_SCALING_FACTOR, + hw_accel=False +) +splash = displayio.Group(scale=DISPLAY_SCALING_FACTOR) +display.show(splash) + +dt = 0 # in ms + +bitmap = displayio.Bitmap(WIDTH*2, WIDTH*2, 2) +for i in range((WIDTH*2)**2): + bitmap[i // (WIDTH*2), i % (WIDTH*2)] = randint(0, 2) + +# hundredths of a pixel per sec +velocity_x = 0 +velocity_y = 0 +orientation = 0 +global_x = 0 +global_y = 0 + +def create_bg(): + global global_x + global global_y + global velocity_x + global velocity_y + + color_palette = displayio.Palette(3) + color_palette[0] = Palette.NOT_BLACK + color_palette[1] = Palette.LESS_BLACK + color_palette[2] = Palette.EVEN_LESS_BLACK + + factor_x = 1 if velocity_x < 0 else -1 + factor_y = 1 if velocity_x < 0 else -1 + + velocity_x += VELOCITY_DECAY_CONSTANT * dt * factor_x + velocity_y += VELOCITY_DECAY_CONSTANT * dt * factor_y + velocity_x *= 1 + VELOCITY_DECAY_FACTOR * dt + velocity_y *= 1 + VELOCITY_DECAY_FACTOR * dt + + print(f"velocity {velocity_x} {velocity_y}") + + global_x += velocity_x * dt + global_y += velocity_y * dt + + whole_x = global_x // 100 + whole_y = global_y // 100 + + tile_width = WIDTH // 2 + + viewport_x = int(whole_x % tile_width) + viewport_y = int(whole_y % tile_width) + + tile_x = int((whole_x // tile_width) % 4) + tile_y = int((whole_y // tile_width) % 4) + + sprite = displayio.TileGrid( + bitmap, + pixel_shader=color_palette, + x=viewport_x - tile_width, + y=viewport_y - tile_width, + tile_width=tile_width, + tile_height=tile_width, + width=4, + height=4 + ) + + for x in range(4): + for y in range(4): + sprite[x, y] = (((y - tile_y) % 4) * 4) + ((x - tile_x) % 4) + + return sprite + + +def render(group: displayio.Group): + group.append(create_bg()) + + color_palette = displayio.Palette(2) + color_palette.make_transparent(0) + color_palette[1] = Palette.NOT_WHITE + + player = displayio.TileGrid( + spaceship, + x=(WIDTH//2)-16, + y=(WIDTH//2)-16, + tile_width=32, + tile_height=32, + width=17, + height=1, + default_tile=16, + pixel_shader=color_palette + ) + + player[0] = int(orientation // (pi / 8)) + + group.append(player) + + + +last_it = monotonic() +last_dt_it = last_it +while True: + # Time tracking nonsense + cur_it = monotonic() + rt = (cur_it - last_it) * 1000 + dt = (cur_it - last_dt_it) + if rt > (1000/60): + warning(f"took {rt:1f}ms - over target") + else: + debug(f"took {rt:1f}ms") + + last_dt_it = monotonic() + + wait_time = (1/60) - (cur_it - last_it) + + if wait_time > 0: + debug(f"sleeping {wait_time*1000:1f}ms") + sleep(wait_time) + + last_it = monotonic() + + keys = pygame.key.get_pressed() + + if keys[pygame.K_LEFT] and keys[pygame.K_RIGHT]: + # And who said trigonometry wasn't useful! + velocity = sqrt(velocity_x**2 + velocity_y**2) + velocity_mult_coef = (velocity * (1 + (ACCELERATION_FACTOR * dt))) + ACCELERATION_CONSTANT * dt + velocity_x += sin(orientation) * velocity_mult_coef + velocity_y += cos(orientation) * velocity_mult_coef + velocity_x = max(min(velocity_x, ACCELERATION_CEILING), -ACCELERATION_CEILING) + velocity_y = max(min(velocity_y, ACCELERATION_CEILING), -ACCELERATION_CEILING) + + elif keys[pygame.K_LEFT]: + orientation = (orientation + (ROTATIONAL_VELOCITY_FACTOR * dt)) % (pi * 2) + + elif keys[pygame.K_RIGHT]: + orientation = (orientation - (ROTATIONAL_VELOCITY_FACTOR * dt)) % (pi * 2) + + group = displayio.Group(scale=DISPLAY_SCALING_FACTOR) + render(group) + display.show(group) + + if display.check_quit(): + break diff --git a/pets/sarahs_space_game/output.bmp b/pets/sarahs_space_game/output.bmp new file mode 100644 index 0000000000000000000000000000000000000000..14060208a66d14d88e11e66026449d0f6015372f GIT binary patch literal 2102 zcmeIyWtSFI7zNN57!V{RrMpvd0O@XNK|my=8$`N8TDp;L5ELX;5QmZurBf;C?%2cd zFPOd7xzAnq`F1|sl2wAE2Hpw|3IsB_rFxeLfp-h|h+r9-&_P5Z5kzF7@BvYY z#)m{F1~G}nN5m!$afwHK67VqzNkn3jkd$O3Cj}`o(^=R6P@Y8H+1D&y3w5; z^rRQP=|f-o(VqbfWDtWH!ce|r7{eLCNJcT5F?`Qh#xb4=Ok@(1nZi`2F`XIAWEMX# zn>oy79`jkiLKd-@B`jqb%UQuneqkoD z!ghABlU?lQFZQsPeeCA|2RX!Hj&PK}`G;d1=L9D?#c9rPmUEov0vGw0|G30uu5guW v{Lgi6aFbiy<_>qc$9*2~kVib`2~T;(b6)V0SG*?d2M9mI8whXUeQe+jzf1u^ literal 0 HcmV?d00001 diff --git a/pets/sarahs_space_game/spaceship.bmp b/pets/sarahs_space_game/spaceship.bmp new file mode 100644 index 0000000000000000000000000000000000000000..3c4323ebbfb7b46f6da7c6572a1529929e3f8425 GIT binary patch literal 2110 zcmeH|v1%JZ5Qg_8(54V_d&WWzq)1Ao@)g1o>GKZi+zQfM5v+TFJU|}jia?4$YL^EI zsa&X3A*_7g?%8ha2MF<+xt;xY{+YR%Ir+oK%U2<-OP=r41|hs=EBH&RzgOofiTd&8 zhuh2gpDR$7c_4<}(^V*%r}5Z=uR@SNLya8+hlI~azsBsb<1t72<&egv$LkQbcOjgE zP1Yd{_b5GofqH_Bz{3s^V97sk!8a(_U;PQ;{qGRQYxu;8-^qiVme`y%nP1#+cOX~J zzdCHW8)5tQO9<-`>2c>d_WUI|UE%W-TjR&C*b--pUE=Ele8~&SFG*@b%vnYHU`K2> z*O@=7Gyi&m&m*z0or0AcY~j>1bbL%tnp8{q-#AwnVB}+c$5jZoQ)0cze6V5%w&u=z z`DKKI-kKA7IYbr+*g_oXu$znHtK)pnugrnpiW;tbclmR1>=MrIS-yKCj-M3YIKw8z zHGlJ{Xr6ibe#Y5pi}?QJQM6#@Wqo|l`7*z^e0@$C3i)e=^S;WseCu$)&brv2Q~L4g zF4x`u+lR5W(-yvU_H5nlpMBW=%ze4%Ux3?p`Od+3Lr#Z0U-o$-hK+pZAl^Rm&l=`( zmFLd+bq<{m=W>fr=gj$X?no)k;Q%}5GIn{+36YolJ@MN7U04g}d`Lddz5KNB=XX&w q_|3?}v*@zl8A#&S`5c=EldS0DnN_SF|55mz51MBTU*Lba0)GJpyN#Rx literal 0 HcmV?d00001 diff --git a/pets/sarahs_space_game/spaceship.png b/pets/sarahs_space_game/spaceship.png new file mode 100644 index 0000000000000000000000000000000000000000..ac7565ec79b63674d6d432c06df4c9b7783fdf8f GIT binary patch literal 1262 zcmaJ>|2q>193IIrsiutO+u9Cy(y82tA=bi5l(Lvo*T^+jDrL;re7RCILX#}CB|1!I zRy#KJ)M>aGDvN5K4l%J!n$c`KcYi?d5AT=f^LgGM->V10Lo9)tfB*o%5)+D!1OSYo zD;{cQy24w)&k!qOloA<&0X?AWK1~K)fv{lX=KAzXrj9@ zi1%PZPwpJ8(5tkXCBOIUJl)n0DlQ?u@6n8_3zrryjy8116{Sv$-t;i6ybWVQ%8arM z0o6F{Crv09eU5m=e$UqvWxK#W6Lp%$QsME=loo#7E`mg=Z&Os)=DHZvgD>Lm&(MDw z{I26hmY!=8INxA>viXrR_G12L-7w=1Y9sYsdRY?WLa#EWy?O}Ykx6Rb%-@L?9nYS+ zhMq0hBYe~W_mKsi`~BGBOL9c)c#pcCRTDP|nzd=tJNY;XCZrbx38a=Y##UEf5BS;R zpWh{lbDE;NyvL$pFx5uLXC2NKlAI#s>$d+JXXV{E;y8ousTgP|>F<-_sWqx2bII zl+$}u%nVVYc)5?7!c(Zz-QBCzcL ztpaqEfVzHfGQS}q&(V@V2YA@cpAt1zmk%s^ZOZf8aG*D{Dse8RTO&fI)3Tk-5{rW9 zx;8DV|DR`T9D=L7-x^qzg|UTLI!7u5@i2kz;ScvFK9-^%iX8Y9Z})|p&SSRXW%=q4 zNW^_iV`TyIt08o^#%)fLJ8;j1G-i|iT8Lbt{%8b2JaIFfXCArClOa*`L*1+WM|e1u z7>E)`V&BiT%A<@@fq__((i-s*9wmY>9;BsVb7>XCU$_`ojj#qiU9Bl{l z6$BRFLz^G!b7z_#>EUZl9-6FDb~V!rzs`FKd|BL&F^8X&>uuy2xYlG$cn2+x`#iGJ zAXwaHkz_r?PD3#E;;!>TtbHUtw$834XY^}`T&>h|F=+?9WF15m8`Qt=Y|z`)LzA_h z#2|P9g9zaL3@eu?t)PTPCB^$%MACeOZ1Ser>CZG(z3~@tX0kR3Z^!>Z+OogYn*Z_F zxwg8|Yhg>xZ!CtzS3yV)YkK!9ZH6g7>U5MNY^G-R&!R5{s$8Vva$~gBd5?+-4QqdX z2ZcU^ok_o~Loo5l{)&SZojl>}S&Ec4J-n*)#w92DxsA3kb#c6F4#YRD^wPT)rehQQ zR@gCM1h2q#%inWRCuIYmaUh;ks;qE>4h5DTG|w-FCj=wxaO(Qz1bGS(YxmarOUzwh z`7rUm+$bwv6M(_?<28iEeri)0HNLzmRpMvFF40NX0w9fGZL1XZtv&hw_NLMhET#+1 Rya1~IGG