Add text generation example and game of life output

This commit is contained in:
moshferatu 2023-11-09 09:07:08 -08:00
parent 03a6b82254
commit ebddce4c6c
3 changed files with 163 additions and 0 deletions

View File

@ -0,0 +1,84 @@
Sure, I can guide you through creating a simple implementation of Conway's Game of Life using Pygame in Python. The Game of Life is a cellular automaton devised by the British mathematician John Horton Conway. It's a zero-player game, meaning its evolution is determined by its initial state, requiring no further input.
Make sure you have the `pygame` library installed. If not, you can install it using pip:
```bash
pip install pygame
```
Here is a sample code for the Game of Life using Pygame:
```python
import pygame
import numpy as np
# Initialize pygame
pygame.init()
# Define constants
WIDTH, HEIGHT = 800, 600
CELL_SIZE = 10
GRID_WIDTH, GRID_HEIGHT = WIDTH // CELL_SIZE, HEIGHT // CELL_SIZE
# Set up the display
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Conway's Game of Life")
# Initialize the grid
def empty_grid(width, height):
return np.zeros((width, height), dtype=int)
def random_grid(width, height):
return np.random.randint(2, size=(width, height), dtype=int)
grid = random_grid(GRID_WIDTH, GRID_HEIGHT)
# Game of Life logic
def update_grid(grid):
new_grid = grid.copy()
for i in range(grid.shape[0]):
for j in range(grid.shape[1]):
# Count living neighbors
live_neighbors = np.sum(grid[i-1:i+2, j-1:j+2]) - grid[i, j]
if grid[i, j] == 1 and (live_neighbors < 2 or live_neighbors > 3):
new_grid[i, j] = 0
elif grid[i, j] == 0 and live_neighbors == 3:
new_grid[i, j] = 1
return new_grid
# Game Loop
running = True
clock = pygame.time.Clock()
while running:
screen.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Update the grid and draw the new state
grid = update_grid(grid)
for i in range(GRID_WIDTH):
for j in range(GRID_HEIGHT):
color = (255, 255, 255) if grid[i, j] == 1 else (0, 0, 0)
rect = (i * CELL_SIZE, j * CELL_SIZE, CELL_SIZE, CELL_SIZE)
pygame.draw.rect(screen, color, rect)
pygame.display.flip()
clock.tick(10) # control the simulation speed
# Quit Pygame
pygame.quit()
```
This code sets up a Pygame window with a grid of cells, each of which can be alive (white) or dead (black). The grid updates according to the rules of Conway's Game of Life:
1. Any live cell with fewer than two live neighbors dies, as if by underpopulation.
2. Any live cell with two or three live neighbors lives on to the next generation.
3. Any live cell with more than three live neighbors dies, as if by overpopulation.
4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
The grid initially starts off with a random state. It updates with each tick of the game's clock and is displayed in the Pygame window. The simulation speed can be adjusted by changing the `clock.tick()` parameter.

View File

@ -0,0 +1,63 @@
import pygame
import numpy as np
# Initialize pygame
pygame.init()
# Define constants
WIDTH, HEIGHT = 800, 600
CELL_SIZE = 10
GRID_WIDTH, GRID_HEIGHT = WIDTH // CELL_SIZE, HEIGHT // CELL_SIZE
# Set up the display
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Conway's Game of Life")
# Initialize the grid
def empty_grid(width, height):
return np.zeros((width, height), dtype=int)
def random_grid(width, height):
return np.random.randint(2, size=(width, height), dtype=int)
grid = random_grid(GRID_WIDTH, GRID_HEIGHT)
# Game of Life logic
def update_grid(grid):
new_grid = grid.copy()
for i in range(grid.shape[0]):
for j in range(grid.shape[1]):
# Count living neighbors
live_neighbors = np.sum(grid[i-1:i+2, j-1:j+2]) - grid[i, j]
if grid[i, j] == 1 and (live_neighbors < 2 or live_neighbors > 3):
new_grid[i, j] = 0
elif grid[i, j] == 0 and live_neighbors == 3:
new_grid[i, j] = 1
return new_grid
# Game Loop
running = True
clock = pygame.time.Clock()
while running:
screen.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Update the grid and draw the new state
grid = update_grid(grid)
for i in range(GRID_WIDTH):
for j in range(GRID_HEIGHT):
color = (255, 255, 255) if grid[i, j] == 1 else (0, 0, 0)
rect = (i * CELL_SIZE, j * CELL_SIZE, CELL_SIZE, CELL_SIZE)
pygame.draw.rect(screen, color, rect)
pygame.display.flip()
clock.tick(10) # control the simulation speed
# Quit Pygame
pygame.quit()

16
text_generation.py Normal file
View File

@ -0,0 +1,16 @@
from dotenv import load_dotenv
from openai import OpenAI
load_dotenv()
client = OpenAI()
response = client.chat.completions.create(
model = 'gpt-4-1106-preview',
messages = [
{'role': 'system', 'content': 'You are a helpful Python coding assistant.'},
{'role': 'user', 'content': 'Please code the game of life using pygame.'}
]
)
print(response.choices[0].message.content)