-
Notifications
You must be signed in to change notification settings - Fork 7
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,98 @@ | ||
""" | ||
Pygame of life module. Contains the short engine | ||
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 | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could remove these commented lines :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok sir There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -15,22 +106,20 @@ | |
|
||
|
||
def get_neighbours(grid: Grid, x: int, y: int) -> Neighbours: | ||
""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the extra spaces after the last char There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
|
@@ -45,21 +134,18 @@ def update_grid(grid: Grid) -> Grid: | |
new_cells.add((pos[0], pos[1])) | ||
|
||
return Grid(grid.dim, new_cells) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 😄 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
@@ -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) | ||
|
||
|
||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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