gamegrid, falling, Tetrominos, collision and deleting rows, GUI Menus and Score, speeding up, hold function and next tetrominos display
This commit is contained in:
+18
-1
@@ -1,6 +1,19 @@
|
|||||||
import pygame
|
import pygame
|
||||||
import settings
|
import settings
|
||||||
|
|
||||||
|
def get_fall_interval(score):
|
||||||
|
"""
|
||||||
|
Calculate the fall interval based on the current score.
|
||||||
|
"""
|
||||||
|
base_interval = 1000 # Base interval in milliseconds
|
||||||
|
decrease_per_1000_points = 50 # Decrease in ms per 1000 points
|
||||||
|
min_interval = 100 # Minimum interval in milliseconds
|
||||||
|
|
||||||
|
# Calculate the fall interval
|
||||||
|
interval = base_interval - (score // 400) * decrease_per_1000_points
|
||||||
|
|
||||||
|
# Ensure the interval doesn't go below the minimum
|
||||||
|
return max(interval, min_interval)
|
||||||
|
|
||||||
#game states
|
#game states
|
||||||
def draw_text(surface, text, size, x, y, color):
|
def draw_text(surface, text, size, x, y, color):
|
||||||
@@ -11,6 +24,7 @@ def draw_text(surface, text, size, x, y, color):
|
|||||||
surface.blit(text_surface, text_rect)
|
surface.blit(text_surface, text_rect)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Grid():
|
class Grid():
|
||||||
def __init__(self, rows, cols, cell_size):
|
def __init__(self, rows, cols, cell_size):
|
||||||
self.cell_size = cell_size
|
self.cell_size = cell_size
|
||||||
@@ -63,4 +77,7 @@ class Grid():
|
|||||||
cleared_rows = len(self.grid) - len(new_grid)
|
cleared_rows = len(self.grid) - len(new_grid)
|
||||||
self.grid = [[0] * self.cols for _ in range(cleared_rows)] + new_grid
|
self.grid = [[0] * self.cols for _ in range(cleared_rows)] + new_grid
|
||||||
|
|
||||||
settings.game_score += 100 * cleared_rows
|
if cleared_rows >= 4:
|
||||||
|
settings.game_score += 100 * cleared_rows + 100
|
||||||
|
else:
|
||||||
|
settings.game_score += 100 * cleared_rows
|
||||||
@@ -1,12 +1,11 @@
|
|||||||
import pygame, sys
|
import pygame, sys
|
||||||
from settings import *
|
from settings import *
|
||||||
from game_manager import Grid, draw_text
|
from game_manager import Grid, draw_text, get_fall_interval
|
||||||
from tetrominos import Tetrominos
|
from tetrominos import Tetrominos
|
||||||
import settings
|
import settings
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def setup():
|
def setup():
|
||||||
pygame.init()
|
pygame.init()
|
||||||
global screen
|
global screen
|
||||||
@@ -65,15 +64,19 @@ def main():
|
|||||||
screen = pygame.display.set_mode((width, height))
|
screen = pygame.display.set_mode((width, height))
|
||||||
|
|
||||||
grid_surface = pygame.Surface((g_width, g_height))
|
grid_surface = pygame.Surface((g_width, g_height))
|
||||||
|
next_surface = pygame.Surface((150, 150))
|
||||||
|
hold_surface = pygame.Surface((150, 150))
|
||||||
|
|
||||||
# Create the grid and tetromino
|
# Create the grid and tetromino
|
||||||
game_grid = Grid(rows, cols, cell_size)
|
game_grid = Grid(rows, cols, cell_size)
|
||||||
tetromino = Tetrominos(cols, cell_size)
|
current_tetromino = Tetrominos(cols, cell_size)
|
||||||
|
next_tetromino = Tetrominos(cols, cell_size)
|
||||||
|
hold_tetromino = None
|
||||||
|
|
||||||
|
hold_used = False # Flag to track if hold has been used for the current tetromino
|
||||||
|
|
||||||
fall_timer = 0
|
fall_timer = 0
|
||||||
|
|
||||||
fall_interval = 500
|
|
||||||
|
|
||||||
move_timer = 0
|
move_timer = 0
|
||||||
move_interval = 100
|
move_interval = 100
|
||||||
|
|
||||||
@@ -83,19 +86,35 @@ def main():
|
|||||||
|
|
||||||
current_time = pygame.time.get_ticks()
|
current_time = pygame.time.get_ticks()
|
||||||
|
|
||||||
|
fall_interval = get_fall_interval(settings.game_score)
|
||||||
|
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
sys.exit()
|
sys.exit()
|
||||||
elif event.type == pygame.KEYDOWN:
|
elif event.type == pygame.KEYDOWN:
|
||||||
if event.key == pygame.K_UP and settings.gamestate == "playing":
|
if event.key == pygame.K_UP and settings.gamestate == "playing":
|
||||||
rotated_shape = [list(row) for row in zip(*tetromino.shape[::-1])]
|
rotated_shape = [list(row) for row in zip(*current_tetromino.shape[::-1])]
|
||||||
if not game_grid.check_collision(rotated_shape, tetromino.x, tetromino.y):
|
if not game_grid.check_collision(rotated_shape, current_tetromino.x, current_tetromino.y):
|
||||||
tetromino.shape = rotated_shape # Apply rotation
|
current_tetromino.shape = rotated_shape # Apply rotation
|
||||||
elif event.key == pygame.K_SPACE and settings.gamestate == "playing":
|
elif event.key == pygame.K_SPACE and settings.gamestate == "playing":
|
||||||
tetromino.instant_drop(game_grid) # Perform instant drop
|
current_tetromino.instant_drop(game_grid) # Perform instant drop
|
||||||
game_grid.add_tetromino(tetromino) # Lock tetromino in place
|
game_grid.add_tetromino(current_tetromino) # Lock tetromino in place
|
||||||
game_grid.check_full_row()
|
game_grid.check_full_row()
|
||||||
tetromino = Tetrominos(cols, cell_size)
|
current_tetromino = next_tetromino
|
||||||
|
next_tetromino = Tetrominos(cols, cell_size)
|
||||||
|
hold_used = False # Reset hold flag when a new tetromino spawns
|
||||||
|
elif event.key == pygame.K_c and settings.gamestate == "playing":
|
||||||
|
if not hold_used:
|
||||||
|
if hold_tetromino is None:
|
||||||
|
hold_tetromino = current_tetromino
|
||||||
|
current_tetromino = next_tetromino
|
||||||
|
next_tetromino = Tetrominos(cols, cell_size)
|
||||||
|
else:
|
||||||
|
hold_tetromino, current_tetromino = current_tetromino, hold_tetromino
|
||||||
|
current_tetromino.x = cols // 2
|
||||||
|
current_tetromino.y = 0
|
||||||
|
hold_used = True
|
||||||
|
|
||||||
elif event.key == pygame.K_ESCAPE: # Pause the game
|
elif event.key == pygame.K_ESCAPE: # Pause the game
|
||||||
if settings.gamestate == "playing":
|
if settings.gamestate == "playing":
|
||||||
settings.gamestate = "paused"
|
settings.gamestate = "paused"
|
||||||
@@ -107,27 +126,29 @@ def main():
|
|||||||
keys = pygame.key.get_pressed()
|
keys = pygame.key.get_pressed()
|
||||||
if keys[pygame.K_RIGHT]:
|
if keys[pygame.K_RIGHT]:
|
||||||
if current_time - move_timer > move_interval: # Ensure delay between movements
|
if current_time - move_timer > move_interval: # Ensure delay between movements
|
||||||
if not game_grid.check_collision(tetromino.shape, tetromino.x + 1, tetromino.y):
|
if not game_grid.check_collision(current_tetromino.shape, current_tetromino.x + 1, current_tetromino.y):
|
||||||
tetromino.move(1, 0) # Move right
|
current_tetromino.move(1, 0) # Move right
|
||||||
move_timer = current_time # Reset timer
|
move_timer = current_time # Reset timer
|
||||||
elif keys[pygame.K_LEFT]:
|
elif keys[pygame.K_LEFT]:
|
||||||
if current_time - move_timer > move_interval: # Ensure delay between movements
|
if current_time - move_timer > move_interval: # Ensure delay between movements
|
||||||
if not game_grid.check_collision(tetromino.shape, tetromino.x - 1, tetromino.y):
|
if not game_grid.check_collision(current_tetromino.shape, current_tetromino.x - 1, current_tetromino.y):
|
||||||
tetromino.move(-1, 0) # Move left
|
current_tetromino.move(-1, 0) # Move left
|
||||||
move_timer = current_time # Reset timer
|
move_timer = current_time # Reset timer
|
||||||
|
|
||||||
# Handle automatic falling based on timer
|
# Handle automatic falling based on timer
|
||||||
if current_time - fall_timer > fall_interval:
|
if current_time - fall_timer > fall_interval:
|
||||||
if not game_grid.check_collision(tetromino.shape, tetromino.x, tetromino.y + 1):
|
if not game_grid.check_collision(current_tetromino.shape, current_tetromino.x, current_tetromino.y + 1):
|
||||||
tetromino.move(0, 1) # Move down
|
current_tetromino.move(0, 1) # Move down
|
||||||
else:
|
else:
|
||||||
# Collision detected, lock the tetromino in place
|
# Collision detected, lock the tetromino in place
|
||||||
game_grid.add_tetromino(tetromino)
|
game_grid.add_tetromino(current_tetromino)
|
||||||
game_grid.check_full_row()
|
game_grid.check_full_row()
|
||||||
tetromino = Tetrominos(cols, cell_size) # Spawn a new tetromino
|
current_tetromino = next_tetromino
|
||||||
|
next_tetromino = Tetrominos(cols, cell_size) # Spawn a new tetromino
|
||||||
|
hold_used = False
|
||||||
|
|
||||||
# Check if game is over
|
# Check if game is over
|
||||||
if game_grid.check_collision(tetromino.shape, tetromino.x, tetromino.y):
|
if game_grid.check_collision(current_tetromino.shape, current_tetromino.x, current_tetromino.y):
|
||||||
game_state = False
|
game_state = False
|
||||||
state_screen(screen, game_state)
|
state_screen(screen, game_state)
|
||||||
|
|
||||||
@@ -136,18 +157,28 @@ def main():
|
|||||||
|
|
||||||
# Draw the grid and tetromino
|
# Draw the grid and tetromino
|
||||||
game_grid.draw_grid(grid_surface)
|
game_grid.draw_grid(grid_surface)
|
||||||
tetromino.draw(grid_surface)
|
current_tetromino.draw(grid_surface)
|
||||||
|
|
||||||
#draw score
|
# Draw the next and hold tetromino previews
|
||||||
draw_text(screen, f'Score: {settings.game_score}', 30, width -75, 100 , (255, 255, 255))
|
next_surface.fill((0, 0, 0)) # Clear the next surface
|
||||||
|
next_tetromino.draw_next_tetromino(next_surface)
|
||||||
|
|
||||||
|
hold_surface.fill((0, 0, 0)) # Clear the hold surface
|
||||||
|
if hold_tetromino is not None:
|
||||||
|
hold_tetromino.draw_next_tetromino(hold_surface)
|
||||||
|
|
||||||
|
# Draw score
|
||||||
|
draw_text(screen, f'Score: {settings.game_score}', 30, width - 75, 100, (255, 255, 255))
|
||||||
|
|
||||||
|
# Render the surfaces
|
||||||
screen.blit(grid_surface)
|
screen.blit(grid_surface)
|
||||||
|
screen.blit(next_surface, (width - 150, 200))
|
||||||
|
screen.blit(hold_surface, (width - 150, 400))
|
||||||
|
|
||||||
pygame.display.update()
|
pygame.display.update()
|
||||||
clock.tick(fps)
|
clock.tick(fps)
|
||||||
|
|
||||||
elif settings.gamestate == "paused":
|
elif settings.gamestate == "paused":
|
||||||
# Render the pause menu
|
|
||||||
pause_menu(screen)
|
pause_menu(screen)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
# Variables
|
# Variables
|
||||||
|
|
||||||
#screen size
|
#screen size
|
||||||
|
|||||||
@@ -60,3 +60,24 @@ class Tetrominos:
|
|||||||
"""Move the tetromino to the lowest possible position."""
|
"""Move the tetromino to the lowest possible position."""
|
||||||
while not grid.check_collision(self.shape, self.x, self.y + 1):
|
while not grid.check_collision(self.shape, self.x, self.y + 1):
|
||||||
self.y += 1
|
self.y += 1
|
||||||
|
|
||||||
|
def draw_next_tetromino(self, surface):
|
||||||
|
"""
|
||||||
|
Draw the next tetromino in the preview window.
|
||||||
|
"""
|
||||||
|
surface.fill((0, 0, 0))
|
||||||
|
|
||||||
|
cell_size = 30 # Smaller cell size for the preview
|
||||||
|
x_offset = (surface.get_width() - len(self.shape[0]) * cell_size) // 2
|
||||||
|
y_offset = (surface.get_height() - len(self.shape) * cell_size) // 2
|
||||||
|
|
||||||
|
for row_idx, row in enumerate(self.shape):
|
||||||
|
for col_idx, cell in enumerate(row):
|
||||||
|
if cell:
|
||||||
|
cell_rect = pygame.Rect(
|
||||||
|
x_offset + col_idx * cell_size,
|
||||||
|
y_offset + row_idx * cell_size,
|
||||||
|
cell_size,
|
||||||
|
cell_size
|
||||||
|
)
|
||||||
|
pygame.draw.rect(surface, self.colors[self.shape_number - 1], cell_rect) # Red color for tetromino
|
||||||
Reference in New Issue
Block a user