-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
83 lines (73 loc) · 2.57 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import sys, pygame
from gameboard import GameBoard
from shape import Shape
from ui import UI
BLOCK_SIZE = 30
PLAYAREA_BLOCK_WIDTH = 10
PLAYAREA_BLOCK_HEIGHT = 20
playarea_starting_point = (30,30)
grid_thickness = 2
move_speed = 1
clock = pygame.time.Clock()
def setup_pygame():
pygame.init()
pygame.key.set_repeat(50)
return pygame.display.set_mode((500, 800))
def setup_gameboard(screen):
return GameBoard(screen, PLAYAREA_BLOCK_HEIGHT, PLAYAREA_BLOCK_WIDTH, playarea_starting_point, BLOCK_SIZE, grid_thickness)
def setup_UI(screen):
return UI(screen)
def main():
screen = setup_pygame()
game_board = setup_gameboard(screen)
ui = setup_UI(screen)
game_loop = True
down_speed = 30
fallspeed_counter = 0
tetrimino_is_done = False
score = 0
level = 1
LEVEL_GAP = 1000 * level
BASE_POINT = 100
tetrimino = Shape('T')
tetrimino.place((5,0),game_board)
while game_loop:
if tetrimino_is_done:
lines = game_board.get_finished_lines() #Returns a list of finished line numbers or empty if none
if lines:
game_board.color_lines(lines)
score += level * BASE_POINT * len(lines)
game_board.remove_lines(lines)
if score >= LEVEL_GAP:
level += 1
if down_speed > 0:
down_speed -= down_speed/2.0
tetrimino = Shape() #Spawn a new shape at the top
tetrimino.place((4,0), game_board)
tetrimino_is_done = False
if fallspeed_counter == down_speed and not tetrimino_is_done:
tetrimino_is_done = not tetrimino.move_down(game_board)
fallspeed_counter = 0
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
# if event.key == pygame.K_SPACE:
# tetrimino.dunk(game_board)
if event.key == pygame.K_UP:
tetrimino.rotate(game_board)
if event.key == pygame.K_LEFT:
tetrimino.move_left(game_board)
if event.key == pygame.K_RIGHT:
tetrimino.move_right(game_board)
if event.key == pygame.K_DOWN:
tetrimino_is_done = not tetrimino.move_down(game_board)
game_board.draw()
ui.draw_score(score)
ui.draw_level(level)
pygame.display.flip()
clock.tick(60)
fallspeed_counter += 1
if __name__ == "__main__":
main()