Working on rotation. /JL
This commit is contained in:
33
bricks.py
33
bricks.py
@@ -1,21 +1,9 @@
|
|||||||
import pygame
|
import pygame
|
||||||
|
from globals import BRICKS, TILE_SIZE, GRID_WIDTH, GRID_HEIGHT, grid
|
||||||
import enums
|
import enums
|
||||||
from random import choice
|
from random import choice
|
||||||
|
|
||||||
BRICKS = [
|
|
||||||
" X " + "\n" + "XXX" + "\n" + " X ",
|
|
||||||
" X" + "\n" + "XXX",
|
|
||||||
"X " + "\n" + "XXX",
|
|
||||||
"X",
|
|
||||||
"XXXX",
|
|
||||||
"XX" + "\n" + "XX",
|
|
||||||
" XX" + "\n" + "XX ",
|
|
||||||
"XX " + "\n" + " XX",
|
|
||||||
"X X" + "\n" + "XXX",
|
|
||||||
"XXX" + "\n" + " X "
|
|
||||||
]
|
|
||||||
|
|
||||||
TILE_SIZE = 48
|
|
||||||
|
|
||||||
class Brick:
|
class Brick:
|
||||||
def __init__(self, brick, state = enums.BrickState.Next):
|
def __init__(self, brick, state = enums.BrickState.Next):
|
||||||
@@ -64,8 +52,25 @@ class Brick:
|
|||||||
self.y += 1
|
self.y += 1
|
||||||
self.location = (self.x, self.y)
|
self.location = (self.x, self.y)
|
||||||
|
|
||||||
|
def rotate_clockwise(self, shape):
|
||||||
|
return [list(row)[::-1] for row in zip(*shape)]
|
||||||
|
|
||||||
def rotate(self):
|
def rotate(self):
|
||||||
print("Rotating")
|
new_shape = self.rotate_clockwise(self.layout)
|
||||||
|
if not self.collision(new_shape, self.x, self.y):
|
||||||
|
self.layout = new_shape
|
||||||
|
|
||||||
|
def collision(self, shape, x, y):
|
||||||
|
for row_idx, row in enumerate(shape):
|
||||||
|
for col_idx, cell in enumerate(row):
|
||||||
|
if cell:
|
||||||
|
new_x = x +col_idx
|
||||||
|
new_y = y + row_idx
|
||||||
|
if new_x <= 0 or new_x >= GRID_WIDTH or new_y >= GRID_HEIGHT:
|
||||||
|
return True
|
||||||
|
if new_y >= 0 and grid[new_y][new_x]:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
def set_state(self, state):
|
def set_state(self, state):
|
||||||
self.state = state
|
self.state = state
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
import pygame
|
import pygame
|
||||||
|
from globals import TILE_SIZE
|
||||||
from enums import BrickColor
|
from enums import BrickColor
|
||||||
|
|
||||||
class DropNext():
|
class DropNext():
|
||||||
@@ -8,8 +9,8 @@ class DropNext():
|
|||||||
self.height = height
|
self.height = height
|
||||||
self.dropnext.fill((0, 0, 0)) # Fill with black
|
self.dropnext.fill((0, 0, 0)) # Fill with black
|
||||||
|
|
||||||
def draw(self, screen, tile_size):
|
def draw(self, screen):
|
||||||
screen.blit(self.dropnext, (tile_size * 15, tile_size * 2))
|
screen.blit(self.dropnext, (TILE_SIZE * 15, TILE_SIZE * 2))
|
||||||
|
|
||||||
def draw_block(self, brick):
|
def draw_block(self, brick):
|
||||||
self.dropnext.fill(BrickColor.Black.value)
|
self.dropnext.fill(BrickColor.Black.value)
|
||||||
|
12
dropzone.py
12
dropzone.py
@@ -1,18 +1,18 @@
|
|||||||
import pygame
|
import pygame
|
||||||
|
from globals import TILE_SIZE, grid
|
||||||
from enums import BrickColor
|
from enums import BrickColor
|
||||||
|
|
||||||
class DropZone():
|
class DropZone():
|
||||||
def __init__(self, tile_size, width, height):
|
def __init__(self, width, height):
|
||||||
self.dropzone = pygame.Surface((width * tile_size, height * tile_size))
|
self.dropzone = pygame.Surface((width * TILE_SIZE, height * TILE_SIZE))
|
||||||
self.width = width
|
self.width = width
|
||||||
self.height = height
|
self.height = height
|
||||||
self.tile_size = tile_size
|
|
||||||
self.grid_row = [" "] * self.width
|
self.grid_row = [" "] * self.width
|
||||||
self.grid = [self.grid_row] * self.height
|
grid = [self.grid_row] * self.height
|
||||||
|
|
||||||
def draw(self, screen):
|
def draw(self, screen):
|
||||||
screen.blit(self.dropzone, (self.tile_size * 4, self.tile_size * 1))
|
screen.blit(self.dropzone, (TILE_SIZE * 4, TILE_SIZE * 1))
|
||||||
|
|
||||||
def draw_brick(self, brick, location):
|
def draw_brick(self, brick, location):
|
||||||
self.dropzone.fill(BrickColor.Black.value)
|
self.dropzone.fill(BrickColor.Black.value)
|
||||||
self.dropzone.blit(brick, (location[0] * self.tile_size, location[1] * self.tile_size))
|
self.dropzone.blit(brick, (location[0] * TILE_SIZE, location[1] * TILE_SIZE))
|
||||||
|
22
globals.py
Normal file
22
globals.py
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
def init():
|
||||||
|
global BRICKS
|
||||||
|
BRICKS = [
|
||||||
|
" X " + "\n" + "XXX" + "\n" + " X ",
|
||||||
|
" X" + "\n" + "XXX",
|
||||||
|
"X " + "\n" + "XXX",
|
||||||
|
"X",
|
||||||
|
"XXXX",
|
||||||
|
"XX" + "\n" + "XX",
|
||||||
|
" XX" + "\n" + "XX ",
|
||||||
|
"XX " + "\n" + " XX",
|
||||||
|
"X X" + "\n" + "XXX",
|
||||||
|
"XXX" + "\n" + " X "
|
||||||
|
]
|
||||||
|
global TILE_SIZE
|
||||||
|
TILE_SIZE = 48
|
||||||
|
global GRID_WIDTH
|
||||||
|
GRID_WIDTH = 10
|
||||||
|
global GRID_HEIGHT
|
||||||
|
GRID_HEIGHT = 18
|
||||||
|
global grid
|
||||||
|
grid = None
|
22
hud.py
22
hud.py
@@ -1,9 +1,10 @@
|
|||||||
import pygame
|
import pygame
|
||||||
import os
|
import os
|
||||||
|
from globals import TILE_SIZE
|
||||||
from enums import BrickColor
|
from enums import BrickColor
|
||||||
|
|
||||||
class Hud:
|
class Hud:
|
||||||
def __init__(self, screen_width, screen_height, tile_size, font_size=36, highscore_file="highscore.txt"):
|
def __init__(self, screen_width, screen_height, font_size=36, highscore_file="highscore.txt"):
|
||||||
self.highscore_file = highscore_file
|
self.highscore_file = highscore_file
|
||||||
self.highscore = self.load_highscore()
|
self.highscore = self.load_highscore()
|
||||||
|
|
||||||
@@ -11,7 +12,6 @@ class Hud:
|
|||||||
self.color = BrickColor.Red.value
|
self.color = BrickColor.Red.value
|
||||||
self.screen_width = screen_width
|
self.screen_width = screen_width
|
||||||
self.screen_height = screen_height
|
self.screen_height = screen_height
|
||||||
self.tile_size = tile_size
|
|
||||||
|
|
||||||
self.reset()
|
self.reset()
|
||||||
|
|
||||||
@@ -56,24 +56,24 @@ class Hud:
|
|||||||
# Score (top-left)
|
# Score (top-left)
|
||||||
score_text = self.font.render(f"Score:", True, self.color)
|
score_text = self.font.render(f"Score:", True, self.color)
|
||||||
score = self.font.render(f"{self.score}", True, self.color)
|
score = self.font.render(f"{self.score}", True, self.color)
|
||||||
screen.blit(score_text, (self.tile_size / 2, self.tile_size))
|
screen.blit(score_text, (TILE_SIZE / 2, TILE_SIZE))
|
||||||
screen.blit(score,(self.tile_size / 2, self.tile_size + 24))
|
screen.blit(score,(TILE_SIZE / 2, TILE_SIZE + 24))
|
||||||
|
|
||||||
lines_text = self.font.render(f"Lines:", True, self.color)
|
lines_text = self.font.render(f"Lines:", True, self.color)
|
||||||
lines = self.font.render(f"{self.lines}", True, self.color)
|
lines = self.font.render(f"{self.lines}", True, self.color)
|
||||||
screen.blit(lines_text, (self.tile_size / 2, self.tile_size * 2 + 24))
|
screen.blit(lines_text, (TILE_SIZE / 2, TILE_SIZE * 2 + 24))
|
||||||
screen.blit(lines, (self.tile_size / 2, self.tile_size * 3))
|
screen.blit(lines, (TILE_SIZE / 2, TILE_SIZE * 3))
|
||||||
|
|
||||||
level_text = self.font.render(f"Level:", True, self.color)
|
level_text = self.font.render(f"Level:", True, self.color)
|
||||||
level = self.font.render(f"{self.level}", True, self.color)
|
level = self.font.render(f"{self.level}", True, self.color)
|
||||||
screen.blit(level_text, (self.tile_size / 2, self.tile_size * 4))
|
screen.blit(level_text, (TILE_SIZE / 2, TILE_SIZE * 4))
|
||||||
screen.blit(level, (self.tile_size / 2, self.tile_size * 4 + 24))
|
screen.blit(level, (TILE_SIZE / 2, TILE_SIZE * 4 + 24))
|
||||||
|
|
||||||
# Highscore (top-center)
|
# Highscore (top-center)
|
||||||
highscore_text = self.font.render(f"High Score:", True, self.color)
|
highscore_text = self.font.render(f"High Score:", True, self.color)
|
||||||
highscore = self.font.render(f"{self.highscore}", True, self.color)
|
highscore = self.font.render(f"{self.highscore}", True, self.color)
|
||||||
screen.blit(highscore_text, (self.tile_size / 2, self.tile_size * 5 + 24))
|
screen.blit(highscore_text, (TILE_SIZE / 2, TILE_SIZE * 5 + 24))
|
||||||
screen.blit(highscore, (self.tile_size / 2, self.tile_size * 6))
|
screen.blit(highscore, (TILE_SIZE / 2, TILE_SIZE * 6))
|
||||||
|
|
||||||
next_text = self.font.render("Next:", True, self.color)
|
next_text = self.font.render("Next:", True, self.color)
|
||||||
screen.blit(next_text, (self.tile_size * 15, self.tile_size))
|
screen.blit(next_text, (TILE_SIZE * 15, TILE_SIZE))
|
13
tetris.py
13
tetris.py
@@ -1,13 +1,15 @@
|
|||||||
import pygame
|
import pygame
|
||||||
import pygameControls as PC
|
import pygameControls as PC
|
||||||
|
from globals import init
|
||||||
|
from globals import GRID_WIDTH, GRID_HEIGHT, TILE_SIZE, BRICKS
|
||||||
import enums
|
import enums
|
||||||
from bricks import Brick, BRICKS, TILE_SIZE
|
from bricks import Brick
|
||||||
from random import randrange
|
from random import randrange
|
||||||
from dropzone import DropZone
|
from dropzone import DropZone
|
||||||
from dropnext import DropNext
|
from dropnext import DropNext
|
||||||
from hud import Hud
|
from hud import Hud
|
||||||
|
|
||||||
__version__ = "0.0.4"
|
__version__ = "0.0.5"
|
||||||
|
|
||||||
# Constants
|
# Constants
|
||||||
HAT_REPEAT_DELAY = 0 # milliseconds before first repeat
|
HAT_REPEAT_DELAY = 0 # milliseconds before first repeat
|
||||||
@@ -45,8 +47,8 @@ class Tetris:
|
|||||||
print(self.next.layout)
|
print(self.next.layout)
|
||||||
print(self.next.color)
|
print(self.next.color)
|
||||||
|
|
||||||
self.hud = Hud(self.screen_width, self.screen_height, TILE_SIZE)
|
self.hud = Hud(self.screen_width, self.screen_height)
|
||||||
self.dropzone = DropZone(TILE_SIZE, width = 10, height = 18)
|
self.dropzone = DropZone(TILE_SIZE, GRID_WIDTH, GRID_HEIGHT)
|
||||||
self.dropnext = DropNext(width = TILE_SIZE * 4, height = TILE_SIZE * 4)
|
self.dropnext = DropNext(width = TILE_SIZE * 4, height = TILE_SIZE * 4)
|
||||||
|
|
||||||
self.clock = pygame.time.Clock()
|
self.clock = pygame.time.Clock()
|
||||||
@@ -68,7 +70,7 @@ class Tetris:
|
|||||||
self.hud.draw(self.screen)
|
self.hud.draw(self.screen)
|
||||||
self.dropzone.draw(self.screen)
|
self.dropzone.draw(self.screen)
|
||||||
self.dropzone.draw_brick(self.current.brick, self.current.location)
|
self.dropzone.draw_brick(self.current.brick, self.current.location)
|
||||||
self.dropnext.draw(self.screen, TILE_SIZE)
|
self.dropnext.draw(self.screen)
|
||||||
self.dropnext.draw_block(self.next.brick)
|
self.dropnext.draw_block(self.next.brick)
|
||||||
|
|
||||||
self.handle_input()
|
self.handle_input()
|
||||||
@@ -190,4 +192,5 @@ class Tetris:
|
|||||||
self.running = False
|
self.running = False
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
globals.init()
|
||||||
game = Tetris()
|
game = Tetris()
|
Reference in New Issue
Block a user