Skip to content

Commit

Permalink
Reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
chyok committed Jul 19, 2024
1 parent 4947152 commit f3886e7
Show file tree
Hide file tree
Showing 7 changed files with 227 additions and 86 deletions.
76 changes: 58 additions & 18 deletions ten_drops/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"TEXT_FONT_PATH",
"BREAK_SOUND",
"GROW_SOUND",
"HP_SOUND"
"HP_SOUND",
]

pygame.init()
Expand Down Expand Up @@ -70,35 +70,71 @@ def get_drop_images() -> List[Status]:
drop_image_paths = [join(drop_image_path, f"{i}.png") for i in range(134)]

drop_images = [image.load(i).convert_alpha() for i in drop_image_paths]
_ = [i.fill(pygame.Color("blue"), special_flags=pygame.BLEND_ADD) for i in drop_images]
_ = [
i.fill(pygame.Color("blue"), special_flags=pygame.BLEND_ADD)
for i in drop_images
]

max_length = max(max(i.get_size()) for i in drop_images[:130])
radio = PLAYGROUND_LENGTH / GRID_SIZE / max_length

drop_images = [smoothscale(i, (i.get_width() * radio,
i.get_height() * radio)) for i in drop_images]

return [Status(action=drop_images[0:20], change_action=drop_images[25:31], static=drop_images[19]),
Status(action=drop_images[48:66], change_action=drop_images[69:76], static=drop_images[60]),
Status(action=drop_images[76:90], change_action=drop_images[108:115], static=drop_images[85]),
Status(action=drop_images[115:130], change_action=drop_images[130:134], static=drop_images[126])]
drop_images = [
smoothscale(i, (i.get_width() * radio, i.get_height() * radio))
for i in drop_images
]

return [
Status(
action=drop_images[0:20],
change_action=drop_images[25:31],
static=drop_images[19],
),
Status(
action=drop_images[48:66],
change_action=drop_images[69:76],
static=drop_images[60],
),
Status(
action=drop_images[76:90],
change_action=drop_images[108:115],
static=drop_images[85],
),
Status(
action=drop_images[115:130],
change_action=drop_images[130:134],
static=drop_images[126],
),
]


def get_droplet_images() -> List[Status]:
droplet_image_path = join(ImageFolderPath, "droplet")
droplet_image_paths = [join(droplet_image_path, f"{i}.png") for i in range(7)]

droplet_images = [image.load(i).convert_alpha() for i in droplet_image_paths]
_ = [i.fill(pygame.Color("blue"), special_flags=pygame.BLEND_ADD) for i in droplet_images]
_ = [
i.fill(pygame.Color("blue"), special_flags=pygame.BLEND_ADD)
for i in droplet_images
]

max_length = max(max(i.get_size()) for i in droplet_images[:3])

radio = PLAYGROUND_LENGTH / GRID_SIZE / max_length / 3 # droplet is 3 times smaller than a drop
radio = (
PLAYGROUND_LENGTH / GRID_SIZE / max_length / 3
) # droplet is 3 times smaller than a drop

droplet_images = [smoothscale(i, (i.get_width() * radio,
i.get_height() * radio)) for i in droplet_images]
droplet_images = [
smoothscale(i, (i.get_width() * radio, i.get_height() * radio))
for i in droplet_images
]

return [Status(action=droplet_images[:3], change_action=droplet_images[3:], static=droplet_images[1])]
return [
Status(
action=droplet_images[:3],
change_action=droplet_images[3:],
static=droplet_images[1],
)
]


SCREEN = pygame.display.set_mode((800, 600))
Expand All @@ -107,11 +143,15 @@ def get_droplet_images() -> List[Status]:
FONT_PATH = join(Path, "asset", "font", "kust.ttf")
TEXT_FONT_PATH = join(Path, "asset", "font", "FiraCode-Regular.ttf")

BACKGROUND = smoothscale(image.load(join(ImageFolderPath, "background.png")),
(SCREEN.get_width(), SCREEN.get_height()))
BACKGROUND = smoothscale(
image.load(join(ImageFolderPath, "background.png")),
(SCREEN.get_width(), SCREEN.get_height()),
)

NOTIFICATION = smoothscale(image.load(join(ImageFolderPath, "notification.png")),
(SCREEN.get_width() / 1.5, SCREEN.get_height() / 1.5))
NOTIFICATION = smoothscale(
image.load(join(ImageFolderPath, "notification.png")),
(SCREEN.get_width() / 1.5, SCREEN.get_height() / 1.5),
)

PLAYGROUND = BACKGROUND.copy()

Expand Down
12 changes: 9 additions & 3 deletions ten_drops/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@ def _update_image(self):
cover_surface.fill((255, 255, 255, 30))

title_image = self.TitleFont.render("ten drops", True, (51, 255, 255))
cover_surface.blit(title_image, ((width - title_image.get_width()) / 2, height / 10))
cover_surface.blit(
title_image, ((width - title_image.get_width()) / 2, height / 10)
)

text_image = self.TextFont.render("A ten drops game written in pygame.", True, (32, 32, 32))
text_image = self.TextFont.render(
"A ten drops game written in pygame.", True, (32, 32, 32)
)

cover_surface.blit(text_image, ((width - text_image.get_width()) / 2, height / 3))
cover_surface.blit(
text_image, ((width - text_image.get_width()) / 2, height / 3)
)

self.image = cover_surface
self.rect = self.image.get_rect()
Expand Down
53 changes: 41 additions & 12 deletions ten_drops/drop.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import pygame
from pygame.sprite import Sprite

from ten_drops import DROP_IMAGES, GRID_SIZE, PLAYGROUND_OFFSET, PLAYGROUND_LENGTH, GROW_SOUND
from ten_drops import (
DROP_IMAGES,
GRID_SIZE,
PLAYGROUND_OFFSET,
PLAYGROUND_LENGTH,
GROW_SOUND,
)


class ActionType:
Expand All @@ -24,10 +30,18 @@ def __init__(self, row, col, state=0, *groups):
def _update_image(self, image):
self.image = image
rect = self.image.get_rect()
rect.x = self.col * (PLAYGROUND_LENGTH // GRID_SIZE) + (
PLAYGROUND_LENGTH // GRID_SIZE // 2) - rect.width // 2 + PLAYGROUND_OFFSET
rect.y = self.row * (PLAYGROUND_LENGTH // GRID_SIZE) + (
PLAYGROUND_LENGTH // GRID_SIZE // 2) - rect.height // 2 + PLAYGROUND_OFFSET
rect.x = (
self.col * (PLAYGROUND_LENGTH // GRID_SIZE)
+ (PLAYGROUND_LENGTH // GRID_SIZE // 2)
- rect.width // 2
+ PLAYGROUND_OFFSET
)
rect.y = (
self.row * (PLAYGROUND_LENGTH // GRID_SIZE)
+ (PLAYGROUND_LENGTH // GRID_SIZE // 2)
- rect.height // 2
+ PLAYGROUND_OFFSET
)
self.rect = rect

def click(self):
Expand Down Expand Up @@ -74,7 +88,9 @@ def update(self):
elif self.action_type == ActionType.change:
all_count = len(DROP_IMAGES[self.state - 1].change_action)
if self.action_count < all_count:
self._update_image(DROP_IMAGES[self.state - 1].change_action[self.action_count])
self._update_image(
DROP_IMAGES[self.state - 1].change_action[self.action_count]
)
self.action_count += 1
else:
self.action_count = 0
Expand All @@ -92,11 +108,24 @@ def __init__(self, row, col, *groups):
super().__init__(*groups)
self.row = row
self.col = col
self.image = pygame.Surface(((PLAYGROUND_LENGTH // GRID_SIZE // 2), (PLAYGROUND_LENGTH // GRID_SIZE // 2)),
pygame.SRCALPHA)
self.image = pygame.Surface(
(
(PLAYGROUND_LENGTH // GRID_SIZE // 2),
(PLAYGROUND_LENGTH // GRID_SIZE // 2),
),
pygame.SRCALPHA,
)
rect = self.image.get_rect()
rect.x = self.col * (PLAYGROUND_LENGTH // GRID_SIZE) + (
PLAYGROUND_LENGTH // GRID_SIZE // 2) - rect.width // 2 + PLAYGROUND_OFFSET
rect.y = self.row * (PLAYGROUND_LENGTH // GRID_SIZE) + (
PLAYGROUND_LENGTH // GRID_SIZE // 2) - rect.height // 2 + PLAYGROUND_OFFSET
rect.x = (
self.col * (PLAYGROUND_LENGTH // GRID_SIZE)
+ (PLAYGROUND_LENGTH // GRID_SIZE // 2)
- rect.width // 2
+ PLAYGROUND_OFFSET
)
rect.y = (
self.row * (PLAYGROUND_LENGTH // GRID_SIZE)
+ (PLAYGROUND_LENGTH // GRID_SIZE // 2)
- rect.height // 2
+ PLAYGROUND_OFFSET
)
self.rect = rect
31 changes: 24 additions & 7 deletions ten_drops/droplet.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,32 @@ def _update_image(self, image):

rect = image.get_rect()

rect.x = self.col * (PLAYGROUND_LENGTH // GRID_SIZE) + (
PLAYGROUND_LENGTH // GRID_SIZE // 2) - rect.width // 2 + PLAYGROUND_OFFSET
rect.y = self.row * (PLAYGROUND_LENGTH // GRID_SIZE) + (
PLAYGROUND_LENGTH // GRID_SIZE // 2) - rect.height // 2 + PLAYGROUND_OFFSET
rect.x = (
self.col * (PLAYGROUND_LENGTH // GRID_SIZE)
+ (PLAYGROUND_LENGTH // GRID_SIZE // 2)
- rect.width // 2
+ PLAYGROUND_OFFSET
)
rect.y = (
self.row * (PLAYGROUND_LENGTH // GRID_SIZE)
+ (PLAYGROUND_LENGTH // GRID_SIZE // 2)
- rect.height // 2
+ PLAYGROUND_OFFSET
)

self.rect = rect

def update(self):
self.row += self.direction[0] * self.speed
self.col += self.direction[1] * self.speed
if any([self.row < -0.5,
if any(
[
self.row < -0.5,
self.col < -0.5,
self.row > GRID_SIZE - 0.5,
self.col > GRID_SIZE - 0.5]):
self.col > GRID_SIZE - 0.5,
]
):
# The actual animation is offset by half a grid cell
self.kill()
return
Expand All @@ -58,6 +70,11 @@ def update(self):
def diffusion(cls, row, col, *group) -> List["Droplet"]:
BREAK_SOUND.play()
droplets = []
for direction in [Direction.Down, Direction.Up, Direction.Left, Direction.Right]:
for direction in [
Direction.Down,
Direction.Up,
Direction.Left,
Direction.Right,
]:
droplets.append(Droplet(row, col, direction, *group))
return droplets
Loading

0 comments on commit f3886e7

Please sign in to comment.