Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change color automatic #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 108 additions & 20 deletions pygame_life.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,98 @@
"""
Pygame of life module. Contains the short engine
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leave the docstring here unchanged

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just worked w/ that file

to simluate the grid of life.
"""
# """
# Pygame of life module. Contains the short engine
# to simluate the grid of life.
# """

# import sys
# import time
# from collections import defaultdict
# from copy import deepcopy

# import pygame

# from example_grids import GOSPER_GLIDER
# from grid_defs import Grid, Neighbours


# def get_neighbours(grid: Grid, x: int, y: int) -> Neighbours:
# """
# Gets the neighbour states for a particular cell in
# (x, y) on the grid.
# """
# offsets = [(-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1)]
# possible_neighbours = {(x + x_add, y + y_add) for x_add, y_add in offsets}
# alive = {(pos[0], pos[1]) for pos in possible_neighbours if pos in grid.cells}
# return Neighbours(alive, possible_neighbours - alive)


# def update_grid(grid: Grid) -> Grid:
# """
# Given a grid, this function returns the next iteration
# of the game of life.
# """
# new_cells = deepcopy(grid.cells)
# undead = defaultdict(int)

# for x, y in grid.cells:
# alive_neighbours, dead_neighbours = get_neighbours(grid, x, y)
# if len(alive_neighbours) not in [2, 3]:
# new_cells.remove((x, y))

# for pos in dead_neighbours:
# undead[pos] += 1

# for pos, _ in filter(lambda elem: elem[1] == 3, undead.items()):
# new_cells.add((pos[0], pos[1]))

# return Grid(grid.dim, new_cells)


# def draw_grid(screen: pygame.Surface, grid: Grid) -> None:
# """
# This function draws the game of life on the given
# pygame.Surface object.
# """
# cell_width = screen.get_width() / grid.dim.width
# cell_height = screen.get_height() / grid.dim.height
# border_size = 2

# for x, y in grid.cells:
# pygame.draw.rect(
# screen,
# (255, 0, 0),
# (
# x * cell_width + border_size,
# y * cell_height + border_size,
# cell_width - border_size,
# cell_height - border_size,
# ),
# )


# def main():
# """
# Main entry point
# """
# grid = GOSPER_GLIDER

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could remove these commented lines :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok sir

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(: yea

# pygame.init()
# screen = pygame.display.set_mode((600, 400))

# while True:
# if pygame.QUIT in [e.type for e in pygame.event.get()]:
# sys.exit(0)

# screen.fill((0, 0, 0))
# draw_grid(screen, grid)
# grid = update_grid(grid)
# pygame.display.flip()
# time.sleep(0.1)


# if __name__ == "__main__":
# main()



import sys
import time
Expand All @@ -15,22 +106,20 @@


def get_neighbours(grid: Grid, x: int, y: int) -> Neighbours:
"""
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add back the documentation and update where you've changed the functions

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok ok thanku so much for guid

Gets the neighbour states for a particular cell in
(x, y) on the grid.
"""
offsets = [(-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1)]
possible_neighbours = {(x + x_add, y + y_add) for x_add, y_add in offsets}
alive = {(pos[0], pos[1]) for pos in possible_neighbours if pos in grid.cells}
return Neighbours(alive, possible_neighbours - alive)



def update_grid(grid: Grid) -> Grid:
new_cells = deepcopy(grid.cells)
"""
Given a grid, this function returns the next iteration
of the game of life.
of the game of life.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the extra spaces after the last char

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, but unnoticeable.

"""
new_cells = deepcopy(grid.cells)

undead = defaultdict(int)

for x, y in grid.cells:
Expand All @@ -45,21 +134,18 @@ def update_grid(grid: Grid) -> Grid:
new_cells.add((pos[0], pos[1]))

return Grid(grid.dim, new_cells)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe these new lines aren't needed here? Unless you think the previous code was unreadable without them 😄

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@matheusgomes28 Just helps cleanliness.



def draw_grid(screen: pygame.Surface, grid: Grid) -> None:
"""
This function draws the game of life on the given
pygame.Surface object.
"""
def draw_grid(screen: pygame.Surface, grid: Grid, color: tuple) -> None:
cell_width = screen.get_width() / grid.dim.width
cell_height = screen.get_height() / grid.dim.height
border_size = 2

for x, y in grid.cells:
pygame.draw.rect(
screen,
(255, 0, 0),
color,
(
x * cell_width + border_size,
y * cell_height + border_size,
Expand All @@ -70,22 +156,24 @@ def draw_grid(screen: pygame.Surface, grid: Grid) -> None:


def main():
"""
Main entry point
"""
grid = GOSPER_GLIDER

pygame.init()
screen = pygame.display.set_mode((600, 400))

color = (255, 0, 0) # Initial color

while True:
if pygame.QUIT in [e.type for e in pygame.event.get()]:
sys.exit(0)

screen.fill((0, 0, 0))
draw_grid(screen, grid)
draw_grid(screen, grid, color)
grid = update_grid(grid)
pygame.display.flip()

# Change color (you can modify this logic based on your requirements)
color = (color[2], color[0], color[1]) # Simple color rotation
time.sleep(0.1)


Expand Down