import pygame as pg
class ENGINE:
def __init__(self, _title : str, **_SETTINGS : dict) -> object:
self.SETTINGS = _SETTINGS
self.title = pg.display.set_caption(_title)
self.clock = pg.time.Clock()
self.SCREEN_SIZE = self.SETTINGS['WIDTH'] * self.SETTINGS['TILE'], self.SETTINGS['HEIGHT'] * self.SETTINGS['TILE']
self.window = pg.display.set_mode(self.SCREEN_SIZE)
self.colors = {
'black' : pg.Color(0, 0, 0),
'white' : pg.Color(255, 255, 255),
'sky' : pg.Color(87, 174, 209),
'sand' : pg.Color(255, 234, 0),
'water' : pg.Color(15,94,156)
}
###########################
self.brick = [pg.Rect(self.SETTINGS['TILE'] * y, self.SETTINGS['TILE'] * x, self.SETTINGS['TILE'], self.SETTINGS['TILE']) for x in range(self.SETTINGS['WIDTH']) for y in range(self.SETTINGS['HEIGHT'])]
self.brick_colors = [self.colors['sky'] for _ in range(self.SETTINGS['WIDTH'] * self.SETTINGS['HEIGHT'])]
self.default = 1
def Create_window(self):
pg.init()
def IfIn(self, y : int, x : int):
return (x < 0 or y < 0 or x >= self.SETTINGS['WIDTH'] or y >= self.SETTINGS['HEIGHT'])
def MoveSand(self, y : int, x : int):
for n in [0, -1, 1]:
if not self.IfIn(y + 1, x+n):
if (self.brick_colors[(y+1) * self.SETTINGS['WIDTH'] + (x+n)] == self.colors['sky'] or
self.brick_colors[(y+1) * self.SETTINGS['WIDTH'] + (x+n)] == self.colors['water']):
self.brick_colors[y * self.SETTINGS['WIDTH'] + x] = self.brick_colors[(y+1) * self.SETTINGS['WIDTH'] + (x+n)]
self.brick_colors[(y+1) * self.SETTINGS['WIDTH'] + (x+n)] = self.colors['sand']
return
def MoveWater(self, y : int, x : int):
if not self.IfIn(y + 1, x):
if self.brick_colors[(y+1) * self.SETTINGS['WIDTH'] + x] == self.colors['sky']:
self.brick_colors[y * self.SETTINGS['WIDTH'] + x] = self.brick_colors[(y+1) * self.SETTINGS['WIDTH'] + x]
self.brick_colors[(y+1) * self.SETTINGS['WIDTH'] + x] = self.colors['water']
return
for up in [1, 0]:
for down in [1, -1]:
if not self.IfIn(y + up, x+down):
if self.brick_colors[(y+up) * self.SETTINGS['WIDTH'] + (x+down)] == self.colors['sky']:
self.brick_colors[y * self.SETTINGS['WIDTH'] + x] = self.brick_colors[(y+up) * self.SETTINGS['WIDTH'] + (x+down)]
self.brick_colors[(y+up) * self.SETTINGS['WIDTH'] + (x+down)] = self.colors['water']
return
def MoveSubstance(self):
for y in range(self.SETTINGS['HEIGHT']-1, 0-1, -1):
for x in range(0, self.SETTINGS['WIDTH'], 1):
if self.brick_colors[y * self.SETTINGS['WIDTH'] + x] == self.colors['sand']:
self.MoveSand(y, x)
if self.brick_colors[y * self.SETTINGS['WIDTH'] + x] == self.colors['water']:
self.MoveWater(y, x)
def Logic(self):
self.MoveSubstance()
def Draw(self):
for y in range(self.SETTINGS['HEIGHT']):
for x in range(self.SETTINGS['WIDTH']):
if pg.mouse.get_pressed()[0] and self.brick[y * self.SETTINGS['WIDTH'] + x].collidepoint(pg.mouse.get_pos()):
if self.default == 1:
self.brick_colors[y * self.SETTINGS['WIDTH'] + x] = self.colors['sand']
elif self.default == 2:
self.brick_colors[y * self.SETTINGS['WIDTH'] + x] = self.colors['water']
elif pg.mouse.get_pressed()[2] and self.brick[y * self.SETTINGS['WIDTH'] + x].collidepoint(pg.mouse.get_pos()):
self.brick_colors[y * self.SETTINGS['WIDTH'] + x] = self.colors['sky']
pg.draw.rect(self.window, self.brick_colors[y * self.SETTINGS['WIDTH'] + x], self.brick[y * self.SETTINGS['WIDTH'] + x])
def Inner(self):
self.window.fill(self.colors['black'])
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
exit()
elif event.type == pg.KEYDOWN:
if event.key == pg.K_1:
self.default = 1
elif event.key == pg.K_2:
self.default = 2
self.Logic()
self.Draw()
def InTheEndFor_Createwindow(self):
pg.display.update()
self.clock.tick(self.SETTINGS['FPS'])
engine = ENGINE('...', HEIGHT = 60, WIDTH = 60, TILE = 10, FPS = 60)
def Loop():
engine.Create_window()
while True:
engine.Inner()
engine.InTheEndFor_Createwindow()
if __name__ == '__main__':
Loop()
import pygame as pg
class ENGINE:
def __init__(self):
self.settings = self.SETTINGS
self.logic = self.LOGIC
self.window = self.WINDOW
class SETTINGS:
def __init__(self, **config):
self.TITLE = config['TITLE']
self.FPS = config['FPS']
self.HEIGHT = config['HEIGHT']
self.WIDTH = config['WIDTH']
self.TILE = config['TILE']
self.SCREEN_SIZE = self.WIDTH * self.TILE, self.HEIGHT * self.TILE
######
self.colors = {
'black': (0, 0, 0),
'white': (255, 255, 255),
'sky': (87, 174, 209),
'sand': (255, 234, 0),
'water': (15, 94, 156)
}
######
self.brick = [pg.Rect(self.TILE * y, self.TILE * x, self.TILE, self.TILE)
for x in range(self.WIDTH) for y in range(self.HEIGHT)]
self.brick_colors = [self.colors['sky']
for _ in range(self.WIDTH * self.HEIGHT)]
class LOGIC:
def __init__(self, **_c_copy):
self.c_copy = _c_copy
def IfIn(self, y: int, x: int):
return (x < 0 or y < 0 or x >= self.c_copy['WIDTH'] or y >= self.c_copy['HEIGHT'])
def MoveSand(self, y: int, x: int):
for n in [0, -1, 1]:
if not self.IfIn(y + 1, x+n):
if (self.c_copy['brick_colors'][(y+1) * self.c_copy['WIDTH'] + (x+n)] == self.c_copy['colors']['sky']
or self.c_copy['brick_colors'][(y+1) * self.c_copy['WIDTH'] + (x+n)] == self.c_copy['colors']['water']):
self.c_copy['brick_colors'][y * self.c_copy['WIDTH']
+ x] = self.c_copy['brick_colors'][(y+1) * self.c_copy['WIDTH'] + (x+n)]
self.c_copy['brick_colors'][(
y+1) * self.c_copy['WIDTH'] + (x+n)] = self.c_copy['colors']['sand']
return
def MoveSubstance(self):
for y in range(self.c_copy['HEIGHT']-1, 0-1, -1):
for x in range(0, self.c_copy['WIDTH'], 1):
if self.c_copy['brick_colors'][y * self.c_copy['WIDTH'] + x] == self.c_copy['colors']['sand']:
self.MoveSand(y, x)
class WINDOW(LOGIC):
def __init__(self, **_c_copy):
self.c_copy = _c_copy
pg.display.set_caption(self.c_copy['TITLE'])
self.clock = pg.time.Clock()
self.window = pg.display.set_mode(self.c_copy['SCREEN_SIZE'])
pg.init()
def Events(self):
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
exit()
self.MoveSubstance()
for y in range(self.c_copy['HEIGHT']):
for x in range(self.c_copy['WIDTH']):
if pg.mouse.get_pressed()[0] and self.c_copy['brick'][y * self.c_copy['WIDTH'] + x].collidepoint(pg.mouse.get_pos()):
self.c_copy['brick_colors'][y * self.c_copy['WIDTH']
+ x] = self.c_copy['colors']['sand']
pg.draw.rect(
self.window, self.c_copy['brick_colors'][y * self.c_copy['WIDTH'] + x], self.c_copy['brick'][y * self.c_copy['WIDTH'] + x])
def Inner(self):
self.window.fill(self.c_copy['colors']['black'])
self.Events()
pg.display.update()
self.clock.tick(self.c_copy['FPS'])
def Loop(self):
while True:
self.Inner()
if __name__ == '__main__':
engine = ENGINE()
settings_engine = engine.SETTINGS(
HEIGHT=60, WIDTH=60, TILE=10, FPS=60, TITLE='...')
logic_engine = engine.LOGIC(**settings_engine.__dict__)
window_engine = engine.WINDOW(**settings_engine.__dict__)
# print(logic_engine.__dict__)
window_engine.Loop()