gamegrid, falling, Tetrominos
This commit is contained in:
@@ -0,0 +1,21 @@
|
|||||||
|
import pygame
|
||||||
|
|
||||||
|
class Grid():
|
||||||
|
def __init__(self, rows, cols, cell_size):
|
||||||
|
self.cell_size = cell_size
|
||||||
|
self.rows = rows
|
||||||
|
self.cols = cols
|
||||||
|
self.grid = [[0 for j in range(self.cols)] for k in range(self.rows)]
|
||||||
|
|
||||||
|
def draw_grid(self, surface):
|
||||||
|
for row in range(self.rows):
|
||||||
|
for col in range(self.cols):
|
||||||
|
cell_value = self.grid[row][col]
|
||||||
|
color = (0, 0, 0) if cell_value == 0 else (255, 0, 0) # Black for empty, red for filled
|
||||||
|
cell_rect = pygame.Rect(
|
||||||
|
col * self.cell_size + 1,
|
||||||
|
row * self.cell_size + 1,
|
||||||
|
self.cell_size - 2,
|
||||||
|
self.cell_size - 2
|
||||||
|
)
|
||||||
|
pygame.draw.rect(surface, color, cell_rect)
|
||||||
@@ -1,36 +1,50 @@
|
|||||||
"""importing Important Modules"""
|
import pygame, sys
|
||||||
|
from settings import *
|
||||||
from settings import width, height
|
from game_manager import Grid
|
||||||
import sys
|
from tetrominos import Tetrominos
|
||||||
import pygame
|
|
||||||
import random
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
# Initialize Pygame
|
|
||||||
pygame.init()
|
pygame.init()
|
||||||
|
|
||||||
# Set up the display
|
|
||||||
screen = pygame.display.set_mode((width, height))
|
screen = pygame.display.set_mode((width, height))
|
||||||
|
clock = pygame.time.Clock()
|
||||||
pygame.display.set_caption("Tetris")
|
pygame.display.set_caption("Tetris")
|
||||||
|
|
||||||
# Set up the clock for controlling the frame rate
|
# Create the grid and tetromino
|
||||||
clock = pygame.time.Clock()
|
game_grid = Grid(rows, cols, cell_size)
|
||||||
|
tetromino = Tetrominos(cols, cell_size)
|
||||||
|
|
||||||
|
# Timer for controlling the falling speed
|
||||||
|
fall_timer = 0
|
||||||
|
fall_interval = 1000 // falling_speed # Falling interval in milliseconds
|
||||||
|
|
||||||
# Main game loop
|
|
||||||
while True:
|
while True:
|
||||||
|
screen.fill((0, 153, 255)) # Background color
|
||||||
|
current_time = pygame.time.get_ticks()
|
||||||
|
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
pygame.quit()
|
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
elif event.type == pygame.KEYDOWN:
|
||||||
|
if event.key == pygame.K_RIGHT:
|
||||||
|
tetromino.move(1, 0) # Move right
|
||||||
|
elif event.key == pygame.K_LEFT:
|
||||||
|
tetromino.move(-1, 0) # Move left
|
||||||
|
elif event.key == pygame.K_UP:
|
||||||
|
tetromino.rotate() # Rotate shape
|
||||||
|
|
||||||
# Update the display
|
# Handle automatic falling based on timer
|
||||||
pygame.display.flip()
|
if current_time - fall_timer > fall_interval:
|
||||||
|
tetromino.move(0, 1)
|
||||||
|
fall_timer = current_time
|
||||||
|
|
||||||
# Control the frame rate
|
# Draw the grid and tetromino
|
||||||
clock.tick(60)
|
game_grid.draw_grid(screen)
|
||||||
|
tetromino.draw(screen)
|
||||||
|
|
||||||
|
pygame.display.update()
|
||||||
|
clock.tick(60) # Fixed frame rate for smooth movement
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|||||||
+10
-3
@@ -1,3 +1,10 @@
|
|||||||
# Set global width and height variables
|
|
||||||
global width, height
|
# Variables
|
||||||
width, height = 800, 600
|
global height, width
|
||||||
|
height, width = 600, 300
|
||||||
|
|
||||||
|
rows = 20
|
||||||
|
cols = 10
|
||||||
|
cell_size = 30
|
||||||
|
|
||||||
|
falling_speed = 5
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
import random
|
||||||
|
import pygame
|
||||||
|
|
||||||
|
|
||||||
|
class Tetrominos:
|
||||||
|
SHAPES = [
|
||||||
|
[[1, 1, 1, 1]], # I-shape
|
||||||
|
[[1, 1], [1, 1]], # O-shape
|
||||||
|
[[0, 1, 0], [1, 1, 1]], # T-shape
|
||||||
|
[[1, 0, 0], [1, 1, 1]], # L-shape
|
||||||
|
[[0, 0, 1], [1, 1, 1]], # J-shape
|
||||||
|
[[0, 1, 1], [1, 1, 0]], # S-shape
|
||||||
|
[[1, 1, 0], [0, 1, 1]], # Z-shape
|
||||||
|
]
|
||||||
|
|
||||||
|
def __init__(self, cols, cell_size):
|
||||||
|
self.cell_size = cell_size
|
||||||
|
self.shape = random.choice(self.SHAPES) # Randomly pick a shape
|
||||||
|
self.color = (random.randint(50, 255), random.randint(50, 255), random.randint(50, 255)) # Random color
|
||||||
|
self.x = cols // 2 - len(self.shape[0]) // 2 # Center horizontally
|
||||||
|
self.y = 0 # Start at the top
|
||||||
|
|
||||||
|
def draw(self, screen):
|
||||||
|
"""Draw the tetromino on the screen."""
|
||||||
|
for row_idx, row in enumerate(self.shape):
|
||||||
|
for col_idx, cell in enumerate(row):
|
||||||
|
if cell: # Draw only if the cell is part of the tetromino
|
||||||
|
x = (self.x + col_idx) * self.cell_size + 1
|
||||||
|
y = (self.y + row_idx) * self.cell_size + 1
|
||||||
|
pygame.draw.rect(
|
||||||
|
screen,
|
||||||
|
self.color,
|
||||||
|
pygame.Rect(x, y, self.cell_size - 2, self.cell_size - 2)
|
||||||
|
)
|
||||||
|
|
||||||
|
def rotate(self):
|
||||||
|
"""Rotate the tetromino shape clockwise."""
|
||||||
|
self.shape = [list(row) for row in zip(*self.shape[::-1])]
|
||||||
|
|
||||||
|
def move(self, dx, dy):
|
||||||
|
"""Move the tetromino."""
|
||||||
|
self.x += dx
|
||||||
|
self.y += dy
|
||||||
Reference in New Issue
Block a user