Thisissand in Python

Hier werden alle anderen GUI-Toolkits sowie Spezial-Toolkits wie Spiele-Engines behandelt.
Antworten
robisalonta
User
Beiträge: 1
Registriert: Sonntag 19. April 2026, 12:13

Da mich das Spiel Thisissand, das ich online spiele, sehr entspannt, habe ich beschlossen, es in Python nachzubauen. Ich habe auch versucht, einige Verbesserungen am Originalspiel vorzunehmen, das eigentlich ein altes Flash-Spiel ist, und ich bin gespannt, was ihr davon haltet.

Ich lasse meine Arbeit unten da.

import pygame
import random

# Setări
WIDTH, HEIGHT = 600, 400
CELL_SIZE = 4

COLS = WIDTH // CELL_SIZE
ROWS = HEIGHT // CELL_SIZE

pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()

# Grid: fiecare celulă = None sau (r, g, b)
grid = [[None for _ in range(COLS)] for _ in range(ROWS)]

def random_sand_color(y):
# gradient + variație
base = 200 - int((y / ROWS) * 80) # mai închis jos
variation = random.randint(-15, 15)

r = max(0, min(255, base + variation))
g = max(0, min(255, base - 20 + variation))
b = max(0, min(255, base - 60 + variation))

return (r, g, b)

def update_grid():
for y in range(ROWS - 2, -1, -1):
for x in range(COLS):
if grid[y][x] is not None:
if grid[y + 1][x] is None:
grid[y + 1][x] = grid[y][x]
grid[y][x] = None
else:
dirs = [-1, 1]
random.shuffle(dirs)
for d in dirs:
nx = x + d
if 0 <= nx < COLS and grid[y + 1][nx] is None:
grid[y + 1][nx] = grid[y][x]
grid[y][x] = None
break

def draw():
screen.fill((10, 10, 10))

for y in range(ROWS):
for x in range(COLS):
if grid[y][x]:
pygame.draw.rect(
screen,
grid[y][x],
(x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE)
)

def add_sand(mx, my):
gx = mx // CELL_SIZE
gy = my // CELL_SIZE

# brush mic (cerc)
for dy in range(-2, 3):
for dx in range(-2, 3):
nx, ny = gx + dx, gy + dy
if 0 <= nx < COLS and 0 <= ny < ROWS:
if random.random() < 0.7:
grid[ny][nx] = random_sand_color(ny)

running = True
while running:
clock.tick(60)

for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False

if pygame.mouse.get_pressed()[0]:
mx, my = pygame.mouse.get_pos()
add_sand(mx, my)

update_grid()
draw()
pygame.display.flip()

pygame.quit()
Antworten