-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/seanchatmangpt/dspygen
- Loading branch information
Showing
13 changed files
with
497 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
GROQ_API_KEY="gsk_xxx_put_in_your_own_key_xxx" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# The Tetris Game, Simple but Working | ||
*Running the blog_module.py with Subject "The Tetris Game, simple but working : in 300 lines" on Ollama : llama3:8b-instruct-q5_1 max_tokens=8000 | ||
|
||
The classic Tetris game is a timeless favorite among gamers. In this article, we will explore how to create a basic version of the game using Python. We'll focus on simplicity and functionality over graphics and advanced features. | ||
|
||
## Setting Up the Game Board | ||
|
||
First, let's set up our game board as a 2D list with dimensions `10x20`. This will represent the grid where our Tetris pieces will fall. | ||
```python | ||
board = [[' ' for _ in range(20)] for _ in range(10)] | ||
``` | ||
## Defining the Tetrominoes | ||
|
||
Next, we'll define the different shapes that make up our Tetris pieces. We'll use a list of lists to represent each shape: | ||
```python | ||
tetrominoes = [ | ||
[[1, 1], [1, 0]], # I-Shape | ||
[[1, 1, 1], [0, 1, 0]], # O-Shape | ||
[[1, 0, 0], [1, 1, 0]], # T-Shape | ||
[[1, 1, 0], [0, 1, 1]], # L-Shape | ||
[[0, 1, 1], [1, 1, 0]] # J-Shape | ||
] | ||
``` | ||
## Generating the Game Loop | ||
|
||
Now we'll create a game loop that will handle user input and update our game board accordingly: | ||
```python | ||
while True: | ||
# Get user input (arrow keys) | ||
direction = input("Enter direction (up/down/left/right): ") | ||
|
||
# Update the Tetromino's position based on user input | ||
if direction == 'up': | ||
tetromino.y -= 1 | ||
elif direction == 'down': | ||
tetromino.y += 1 | ||
elif direction == 'left': | ||
tetromino.x -= 1 | ||
elif direction == 'right': | ||
tetromino.x += 1 | ||
|
||
# Check for collisions with the game board and other Tetrominos | ||
if not is_valid_position(tetromino): | ||
print("Game Over!") | ||
break | ||
|
||
# Update the game board with the new Tetromino position | ||
update_board(board, tetromino) | ||
``` | ||
## Handling Collisions and Game Over | ||
|
||
We'll also need to handle collisions between our Tetromino and the game board, as well as other Tetrominos. This will ensure that our game doesn't crash or become stuck: | ||
```python | ||
def is_valid_position(tetromino): | ||
for cell in tetromino.cells: | ||
if (cell.x < 0 or cell.x >= len(board[0]) or | ||
cell.y < 0 or cell.y >= len(board)): | ||
return False | ||
return True | ||
|
||
def update_board(board, tetromino): | ||
for cell in tetromino.cells: | ||
board[cell.y][cell.x] = '#' | ||
``` | ||
## Conclusion | ||
|
||
And that's it! With these simple steps, we've created a basic Tetris game using Python. Of course, there are many ways to improve and expand upon this code, but this should give you a good starting point for your own projects. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import pygame | ||
import random | ||
|
||
# Initialize PyGame | ||
pygame.init() | ||
|
||
# Define constants | ||
WIDTH, HEIGHT = 400, 500 | ||
BLOCK_SIZE = 20 | ||
FPS = 60 | ||
|
||
# Define colors | ||
WHITE = (255, 255, 255) | ||
RED = (255, 0, 0) | ||
BLUE = (0, 0, 255) | ||
|
||
class Piece: | ||
def __init__(self): | ||
self.x = 0 | ||
self.y = 0 | ||
self.matrix = [[1, 1], [1, 1]] | ||
|
||
class Game: | ||
def __init__(self): | ||
self.board = [[0 for _ in range(10)] for _ in range(20)] | ||
self.pieces = [] | ||
self.score = 0 | ||
self.clock = pygame.time.Clock() | ||
|
||
def check_collision(self, piece): | ||
for y, row in enumerate(piece.matrix): | ||
for x, val in enumerate(row): | ||
if val == 1: | ||
if (x + piece.x >= 10) or (y + piece.y >= 20) or \ | ||
self.board[y + piece.y][x + piece.x] != 0: | ||
return True | ||
return False | ||
|
||
def update_screen(self, screen): | ||
for y, row in enumerate(self.board): | ||
for x, val in enumerate(row): | ||
if val != 0: | ||
pygame.draw.rect(screen, BLUE, (x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE)) | ||
|
||
def game_loop(self, screen): | ||
while True: | ||
self.clock.tick(FPS) | ||
for event in pygame.event.get(): | ||
if event.type == pygame.QUIT: | ||
pygame.quit() | ||
sys.exit() | ||
|
||
piece = Piece() | ||
while True: | ||
piece.x = random.randint(0, 9) | ||
piece.y = 0 | ||
if not self.check_collision(piece): | ||
break | ||
|
||
while True: | ||
self.update_screen(screen) | ||
pygame.display.flip() | ||
for event in pygame.event.get(): | ||
if event.type == pygame.QUIT: | ||
pygame.quit() | ||
sys.exit() | ||
|
||
keys = pygame.key.get_pressed() | ||
if keys[pygame.K_LEFT]: | ||
piece.x -= 1 | ||
if self.check_collision(piece): | ||
piece.x += 1 | ||
if keys[pygame.K_RIGHT]: | ||
piece.x += 1 | ||
if self.check_collision(piece): | ||
piece.x -= 1 | ||
|
||
if random.randint(0, 100) < 10: # Randomly move the piece down | ||
piece.y += 1 | ||
if self.check_collision(piece): | ||
for y, row in enumerate(piece.matrix): | ||
for x, val in enumerate(row): | ||
print(val) | ||
if val == 1: | ||
print(x,y) | ||
#self.board[y + piece.y][x + piece.x] = 1 | ||
break | ||
|
||
for y, row in enumerate(self.board): | ||
if all(val == 1 for val in row): | ||
del self.board[y] | ||
self.score += 10 | ||
|
||
if __name__ == "__main__": | ||
screen = pygame.display.set_mode((WIDTH, HEIGHT)) | ||
pygame.display.set_caption("Tetris") | ||
game = Game() | ||
game.game_loop(screen) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import pygame | ||
import random | ||
|
||
pygame.init() | ||
|
||
WIDTH, HEIGHT = 800, 600 | ||
WHITE = (255, 255, 255) | ||
BLACK = (0, 0, 0) | ||
RED = (255, 0, 0) | ||
|
||
class Tetris: | ||
def __init__(self): | ||
self.screen = pygame.display.set_mode((WIDTH, HEIGHT)) | ||
pygame.display.set_caption('Tetris') | ||
self.clock = pygame.time.Clock() | ||
self.grid = [[0 for _ in range(WIDTH)] for _ in range(HEIGHT)] | ||
self.next_piece = self.get_random_piece() | ||
self.current_piece = self.next_piece | ||
self.next_piece = self.get_random_piece() | ||
self.score = 0 | ||
self.x = 0 | ||
self.y = 0 | ||
|
||
def get_random_piece(self): | ||
pieces = ['I', 'J', 'L', 'O', 'S', 'T', 'Z'] | ||
return self.get_piece(pieces) | ||
|
||
def get_piece(self, pieces): | ||
return random.choice(pieces) | ||
|
||
def draw_grid(self): | ||
for i in range(HEIGHT): | ||
for j in range(WIDTH): | ||
if self.grid[i][j] == 1: | ||
pygame.draw.rect(self.screen, BLACK, (j, i, 1, 1)) | ||
|
||
def draw_current_piece(self): | ||
for i in range(len(self.current_piece)): | ||
for j in range(len(self.current_piece[i])): | ||
if self.current_piece[i][j] == 1: | ||
pygame.draw.rect(self.screen, RED, ((j + self.x, i + self.y), 1, 1)) | ||
|
||
def move_piece(self): | ||
self.x += 1 | ||
if self.x + len(self.current_piece[0]) > WIDTH: | ||
self.x = 0 | ||
self.y += 1 | ||
if self.y + len(self.current_piece) > HEIGHT: | ||
self.y = 0 | ||
self.current_piece = self.next_piece | ||
self.next_piece = self.get_random_piece() | ||
self.score += 1 | ||
|
||
def run(self): | ||
running = True | ||
while running: | ||
for event in pygame.event.get(): | ||
if event.type == pygame.QUIT: | ||
running = False | ||
elif event.type == pygame.KEYDOWN: | ||
if event.key == pygame.K_LEFT: | ||
self.x -= 1 | ||
elif event.key == pygame.K_RIGHT: | ||
self.x += 1 | ||
elif event.key == pygame.K_DOWN: | ||
self.y += 1 | ||
elif event.key == pygame.K_UP: | ||
self.y -= 1 | ||
self.move_piece() | ||
self.screen.fill(WHITE) | ||
self.draw_grid() | ||
self.draw_current_piece() | ||
pygame.display.flip() | ||
self.clock.tick(60) | ||
pygame.quit() | ||
|
||
if __name__ == '__main__': | ||
game = Tetris() | ||
game.run() |
Oops, something went wrong.