Compare commits

...

4 commits
demo ... main

Author SHA1 Message Date
5c773fa3db Fixed another stuuuuubid crash. 2024-07-16 13:11:22 +02:00
19210b0032 Added some special settings. 2024-07-16 13:06:27 +02:00
d5caa29017 Added some comments. 2024-07-16 10:31:34 +02:00
95440471c4 Made the random bool function faster. 2024-07-16 08:56:51 +02:00

View file

@ -4,17 +4,19 @@ import pygame
from pygame._sdl2 import Window from pygame._sdl2 import Window
from dataclasses import dataclass from dataclasses import dataclass
from wobbl_tools.data_file import load_dataclass_json, save_dataclass_json from wobbl_tools.data_file import load_dataclass_json, save_dataclass_json
from random import choice from random import getrandbits
def true_false_random(): def true_false_random():
return choice([True, False]) return getrandbits(1) == 0
@dataclass @dataclass
class Settings: class Settings:
fps: int = 60 fps: int = 60
window_size: tuple = (1000, 600) window_size: tuple = (1000, 600)
slipperiness_left: int = 1
slipperiness_right: int = 1
class FallingSandParticle: class FallingSandParticle:
@ -32,38 +34,38 @@ class FallingSandParticle:
old_pos = self.pos old_pos = self.pos
ox, oy = old_pos ox, oy = old_pos
if oy >= self.app.sand_surface.get_height() - 4: if oy >= self.app.sand_surface.get_height() - self.app.max_height:
self.app.falling_sand_particles.remove(self) self.app.falling_sand_particles.remove(self)
return return
x, y = self.pos x, y = self.pos
if self.app.matrix[ox, oy + 1] == self.app.sand_surface.map_rgb(self.app.gray): if self.app.matrix[ox, oy + 1] == self.app.sand_surface.map_rgb(self.app.gray): # fall down
y += 1 y += 1
elif ( elif (
self.app.matrix[ox - 1, oy + 2] == self.app.sand_surface.map_rgb(self.app.gray) self.app.matrix[ox - 1, oy + self.app.slipperiness_left] == self.app.sand_surface.map_rgb(self.app.gray)
and self.app.matrix[ox + 1, oy + 2] == self.app.sand_surface.map_rgb(self.app.gray) and self.app.matrix[ox + 1, oy + self.app.slipperiness_right] == self.app.sand_surface.map_rgb(self.app.gray)
): ): # when the sand can slide in two directions
if true_false_random(): if true_false_random():
x -= 1 x -= 1
y += 2 y += self.app.slipperiness_left
else: else:
x += 1 x += 1
y += 2 y += self.app.slipperiness_right
elif self.app.matrix[ox - 1, oy + 2] == self.app.sand_surface.map_rgb(self.app.gray): elif self.app.matrix[ox - 1, oy + self.app.slipperiness_left] == self.app.sand_surface.map_rgb(self.app.gray): # can only slide left
x -= 1 x -= 1
y += 2 y += self.app.slipperiness_left
elif self.app.matrix[ox + 1, oy + 2] == self.app.sand_surface.map_rgb(self.app.gray): elif self.app.matrix[ox + 1, oy + self.app.slipperiness_right] == self.app.sand_surface.map_rgb(self.app.gray): # can only slide right
x += 1 x += 1
y += 2 y += self.app.slipperiness_right
else: else:
if self.not_moving == 32: if self.not_moving == 32: # counter that increases when the particle is inactive and if it is 32,
if self.app.mouse_pressed[0]: if self.app.mouse_pressed[0]: # the particle's physics get deactivated for better performance.
self.not_moving = 0 self.not_moving = 0
else: else:
@ -80,7 +82,7 @@ class FallingSandParticle:
return return
if not x > self.app.sand_surface.get_width() - 2:# and not x < 0: if not x > self.app.sand_surface.get_width() - 2: # game would crash when the particle goes outside the border.
self.not_moving = 0 self.not_moving = 0
self.pos = (x, y) self.pos = (x, y)
@ -93,20 +95,23 @@ class FallingSand:
def __init__(self): def __init__(self):
pygame.init() pygame.init()
self.screen = pygame.display.set_mode((1000, 600), pygame.RESIZABLE) self.screen = pygame.display.set_mode((1000, 600), pygame.RESIZABLE) # pygame init default shit
pygame.display.set_caption("Wobbl Sand") pygame.display.set_caption("Wobbl Sand")
self.window = Window.from_display_module() self.window = Window.from_display_module()
self.loading_surface = self.generate_loading_surface() self.loading_surface = self.generate_loading_surface() # just the loading screen
self.screen.blit(self.loading_surface, (0, 0)) self.screen.blit(self.loading_surface, (0, 0))
pygame.display.update() pygame.display.update()
self.settings = load_dataclass_json(Settings, "settings.json") self.settings = load_dataclass_json(Settings, "settings.json") # load settings
setattr(self.settings, "save", lambda: save_dataclass_json(self.settings, "settings.json")) setattr(self.settings, "save", lambda: save_dataclass_json(self.settings, "settings.json"))
self.fps = self.settings.fps self.fps = self.settings.fps
self.slipperiness_left = self.settings.slipperiness_left
self.slipperiness_right = self.settings.slipperiness_right
self.max_height = self.slipperiness_left if self.slipperiness_left > self.slipperiness_right else self.slipperiness_right
self.falling_sand_particles = [] self.falling_sand_particles = []
self.rainbow_data = [[255, 40, 40], 0] self.rainbow_data = [[255, 40, 40], 0] # color calculation stuff
self.rainbow_steps = [(1, True), (0, False), (2, True), (1, False), (0, True), (2, False)] self.rainbow_steps = [(1, True), (0, False), (2, True), (1, False), (0, True), (2, False)]
# colors # colors
@ -142,7 +147,7 @@ class FallingSand:
def loop(self): def loop(self):
self.screen.fill(self.gray) self.screen.fill(self.gray)
self.matrix = pygame.PixelArray(self.sand_surface) self.matrix = pygame.PixelArray(self.sand_surface) # matrix to draw the sand particles on
self.mouse_pressed = self.mouse.get_pressed() self.mouse_pressed = self.mouse.get_pressed()
self.mouse_pos = self.mouse.get_pos() self.mouse_pos = self.mouse.get_pos()
@ -208,7 +213,7 @@ class FallingSand:
bx += ax bx += ax
by += ay by += ay
if not bx > self.sand_surface.get_width() - 2 and self.matrix[bx, by] == self.sand_surface.map_rgb(self.gray) and bx % 2 == 0 and by % 2 == 0: if bx < self.sand_surface.get_width() - 2 and by < self.sand_surface.get_height() -2 and self.matrix[bx, by] == self.sand_surface.map_rgb(self.gray) and bx % 2 == 0 and by % 2 == 0:
self.falling_sand_particles.append(FallingSandParticle(self, (bx, by), color)) self.falling_sand_particles.append(FallingSandParticle(self, (bx, by), color))
def rainbow(self): def rainbow(self):