gamegrid, falling, Tetrominos, collision and deleting rows, GUI Menus and Score
This commit is contained in:
@@ -1,4 +1,15 @@
|
|||||||
import pygame
|
import pygame
|
||||||
|
import settings
|
||||||
|
|
||||||
|
|
||||||
|
#game states
|
||||||
|
def draw_text(surface, text, size, x, y, color):
|
||||||
|
"""Utility function to render text onto a surface."""
|
||||||
|
font = pygame.font.Font(None, size) # Use default font
|
||||||
|
text_surface = font.render(text, True, color)
|
||||||
|
text_rect = text_surface.get_rect(center=(x, y))
|
||||||
|
surface.blit(text_surface, text_rect)
|
||||||
|
|
||||||
|
|
||||||
class Grid():
|
class Grid():
|
||||||
def __init__(self, rows, cols, cell_size):
|
def __init__(self, rows, cols, cell_size):
|
||||||
@@ -51,3 +62,5 @@ class Grid():
|
|||||||
new_grid = [row for row in self.grid if not all(cell == 1 for cell in row)]
|
new_grid = [row for row in self.grid if not all(cell == 1 for cell in row)]
|
||||||
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
|
||||||
|
|||||||
@@ -1,37 +1,108 @@
|
|||||||
import pygame, sys
|
import pygame, sys
|
||||||
from settings import *
|
from settings import *
|
||||||
from game_manager import Grid
|
from game_manager import Grid, draw_text
|
||||||
from tetrominos import Tetrominos
|
from tetrominos import Tetrominos
|
||||||
|
import settings
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def setup():
|
||||||
|
pygame.init()
|
||||||
|
global screen
|
||||||
|
screen = pygame.display.set_mode((width, height))
|
||||||
|
global clock
|
||||||
|
clock = pygame.time.Clock()
|
||||||
|
pygame.display.set_caption("Tetris")
|
||||||
|
icon_img = pygame.image.load('assets/icon.png')
|
||||||
|
pygame.display.set_icon(icon_img)
|
||||||
|
state_screen(screen, game_state)
|
||||||
|
|
||||||
|
def state_screen(screen, game_state):
|
||||||
|
"""Display the start screen."""
|
||||||
|
width = 300
|
||||||
|
screen = pygame.display.set_mode((width, height))
|
||||||
|
|
||||||
|
surface = pygame.Surface((WIDTH, HEIGHT))
|
||||||
|
surface.fill((0, 0, 0)) # Black background
|
||||||
|
|
||||||
|
if game_state:
|
||||||
|
# Draw text
|
||||||
|
draw_text(surface, "TETRIS", 50, WIDTH // 2, HEIGHT // 2 - 50, (255, 255, 255))
|
||||||
|
draw_text(surface, "Press ENTER to Start", 30, WIDTH // 2, HEIGHT // 2 + 50, (200, 200, 200))
|
||||||
|
|
||||||
|
else:
|
||||||
|
draw_text(surface, "Game Over", 50, WIDTH // 2, HEIGHT // 2 - 50, (255, 0, 0))
|
||||||
|
draw_text(surface, "Press Enter to Restart", 30, WIDTH // 2, HEIGHT // 2 + 50, (200, 200, 200))
|
||||||
|
|
||||||
|
# Display the surface
|
||||||
|
screen.blit(surface, (0, 0))
|
||||||
|
pygame.display.update()
|
||||||
|
|
||||||
|
# Wait for the player to press ENTER
|
||||||
|
while True:
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
pygame.quit()
|
||||||
|
sys.exit()
|
||||||
|
elif event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN:
|
||||||
|
game_state = True
|
||||||
|
settings.game_score = 0
|
||||||
|
main()
|
||||||
|
return # Exit the start screen
|
||||||
|
|
||||||
|
def pause_menu(screen):
|
||||||
|
"""Render the pause menu."""
|
||||||
|
screen.fill((0, 0, 0)) # Black background
|
||||||
|
draw_text(screen, "Game Paused", 50, 450 // 2, HEIGHT // 2 - 50, (255, 255, 255))
|
||||||
|
draw_text(screen, "Press ESC to Resume", 30, 450 // 2, HEIGHT // 2 + 10, (200, 200, 200))
|
||||||
|
pygame.display.update()
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
pygame.init()
|
|
||||||
|
width = 450
|
||||||
screen = pygame.display.set_mode((width, height))
|
screen = pygame.display.set_mode((width, height))
|
||||||
clock = pygame.time.Clock()
|
|
||||||
pygame.display.set_caption("Tetris")
|
grid_surface = pygame.Surface((g_width, g_height))
|
||||||
|
|
||||||
# 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)
|
tetromino = Tetrominos(cols, cell_size)
|
||||||
|
|
||||||
fall_timer = 0
|
fall_timer = 0
|
||||||
fall_interval = 1000 // falling_speed # Falling interval in milliseconds
|
|
||||||
|
fall_interval = 500
|
||||||
|
|
||||||
move_timer = 0
|
move_timer = 0
|
||||||
move_interval = 100
|
move_interval = 100
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
screen.fill((0, 153, 255)) # Background color
|
screen.fill((138, 138, 138))
|
||||||
|
grid_surface.fill((87, 87, 87)) # Background color
|
||||||
|
|
||||||
current_time = pygame.time.get_ticks()
|
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:
|
||||||
sys.exit()
|
sys.exit()
|
||||||
elif event.type == pygame.KEYDOWN:
|
elif event.type == pygame.KEYDOWN:
|
||||||
if event.key == pygame.K_UP:
|
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(*tetromino.shape[::-1])]
|
||||||
if not game_grid.check_collision(rotated_shape, tetromino.x, tetromino.y):
|
if not game_grid.check_collision(rotated_shape, tetromino.x, tetromino.y):
|
||||||
tetromino.shape = rotated_shape # Apply rotation
|
tetromino.shape = rotated_shape # Apply rotation
|
||||||
|
elif event.key == pygame.K_SPACE and settings.gamestate == "playing":
|
||||||
|
tetromino.instant_drop(game_grid) # Perform instant drop
|
||||||
|
game_grid.add_tetromino(tetromino) # Lock tetromino in place
|
||||||
|
game_grid.check_full_row()
|
||||||
|
tetromino = Tetrominos(cols, cell_size)
|
||||||
|
elif event.key == pygame.K_ESCAPE: # Pause the game
|
||||||
|
if settings.gamestate == "playing":
|
||||||
|
settings.gamestate = "paused"
|
||||||
|
elif settings.gamestate == "paused":
|
||||||
|
settings.gamestate = "playing"
|
||||||
|
|
||||||
|
if settings.gamestate == "playing":
|
||||||
|
|
||||||
keys = pygame.key.get_pressed()
|
keys = pygame.key.get_pressed()
|
||||||
if keys[pygame.K_RIGHT]:
|
if keys[pygame.K_RIGHT]:
|
||||||
@@ -57,18 +128,28 @@ def main():
|
|||||||
|
|
||||||
# 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(tetromino.shape, tetromino.x, tetromino.y):
|
||||||
print("Game Over")
|
game_state = False
|
||||||
sys.exit()
|
state_screen(screen, game_state)
|
||||||
|
|
||||||
fall_timer = current_time
|
fall_timer = current_time
|
||||||
|
|
||||||
|
|
||||||
# Draw the grid and tetromino
|
# Draw the grid and tetromino
|
||||||
game_grid.draw_grid(screen)
|
game_grid.draw_grid(grid_surface)
|
||||||
tetromino.draw(screen)
|
tetromino.draw(grid_surface)
|
||||||
|
|
||||||
|
#draw score
|
||||||
|
draw_text(screen, f'Score: {settings.game_score}', 30, width -75, 100 , (255, 255, 255))
|
||||||
|
|
||||||
|
screen.blit(grid_surface)
|
||||||
|
|
||||||
pygame.display.update()
|
pygame.display.update()
|
||||||
clock.tick(60) # Fixed frame rate for smooth movement
|
clock.tick(fps)
|
||||||
|
|
||||||
|
elif settings.gamestate == "paused":
|
||||||
|
# Render the pause menu
|
||||||
|
pause_menu(screen)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
setup()
|
||||||
|
|||||||
+18
-1
@@ -1,10 +1,27 @@
|
|||||||
|
|
||||||
# Variables
|
# Variables
|
||||||
|
|
||||||
|
#screen size
|
||||||
global height, width
|
global height, width
|
||||||
height, width = 600, 300
|
height, width = 600, 300
|
||||||
|
|
||||||
|
#grid surface
|
||||||
|
g_height, g_width = 600, 300
|
||||||
|
|
||||||
|
#start surface size
|
||||||
|
HEIGHT, WIDTH = 600, 300
|
||||||
|
|
||||||
|
#state of the game
|
||||||
|
game_state = True
|
||||||
|
|
||||||
|
#score
|
||||||
|
game_score = 0
|
||||||
|
|
||||||
rows = 20
|
rows = 20
|
||||||
cols = 10
|
cols = 10
|
||||||
cell_size = 30
|
cell_size = 30
|
||||||
|
|
||||||
falling_speed = 5
|
fps = 60
|
||||||
|
|
||||||
|
global gamestate # Add game state variable
|
||||||
|
gamestate = "playing"
|
||||||
@@ -55,3 +55,8 @@ class Tetrominos:
|
|||||||
"""Move the tetromino."""
|
"""Move the tetromino."""
|
||||||
self.x += dx
|
self.x += dx
|
||||||
self.y += dy
|
self.y += dy
|
||||||
|
|
||||||
|
def instant_drop(self, grid):
|
||||||
|
"""Move the tetromino to the lowest possible position."""
|
||||||
|
while not grid.check_collision(self.shape, self.x, self.y + 1):
|
||||||
|
self.y += 1
|
||||||
|
|||||||
Reference in New Issue
Block a user