-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
163 lines (148 loc) · 5.58 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import pygame
from tetromino import *
from display import *
from board import *
from pcPlayer import *
from direction import *
from rotation import *
#Bools that control game state
isOpen = True
newGame = True
gameOver = False
paused = False
selfPlay = False
locked = False
#Create game window and clock
window = Window()
draw = Draw(window)
draw.createScreen()
clock = pygame.time.Clock()
while isOpen:
#Draw new Frame
pygame.display.update()
#Clear screen
draw.screen.fill("White")
#reset board
if newGame:
board = Board()
pcPlayer = PcPlayer(board)
tetromino = board.generatePiece()
timeCount = 0
draw.drawStartScreen(board)
#newGame screen loop
while newGame:
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
newGame = False
isOpen = False
keyInput = pygame.key.get_pressed()
if keyInput[pygame.K_p]:
newGame = False
selfPlay = False
if keyInput[pygame.K_b]:
selfPlay = True
newGame = False
#Pause / Start screen loop
while paused:
draw.drawPauseScreen()
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
paused = False
isOpen = False
keyInput = pygame.key.get_pressed()
if keyInput[pygame.K_ESCAPE]:
paused = False
if keyInput[pygame.K_n]:
newGame = True
paused = False
gameFlags = [newGame, gameOver, paused, (not isOpen)]
#gamePlay Loop
while (not any(gameFlags)):
#Draw game elements to screen
draw.refreshScreen(board, tetromino)
#pcPlayer code
if (selfPlay):
if (board.isHeldPieceEmpty()):
board.setHeldPiece(tetromino)
tetromino = board.generatePiece()
draw.refreshScreen(board, tetromino)
locked = board.moveOrLockPiece(tetromino, Direction.DOWN)
draw.refreshScreen(board, tetromino)
if (locked):
tetromino = board.newPieceOrGameOver(tetromino)
if tetromino == None:
gameOver = True
break
(swapPiece, position) = pcPlayer.choosePieceAndPosition(board, tetromino)
if (swapPiece):
tetromino = board.swapWithHeldPiece(tetromino)
draw.refreshScreen(board, tetromino)
pcPlayer.makeMove(board, tetromino, position, draw)
tetromino = board.newPieceOrGameOver(tetromino)
draw.refreshScreen(board, tetromino)
if tetromino == None:
gameOver = True
break
#Step game forward
timeCount += clock.get_rawtime()
clock.tick()
if (timeCount >= board.getDropInterval()):
timeCount = 0
locked = board.moveOrLockPiece(tetromino, Direction.DOWN)
if (locked):
tetromino = board.newPieceOrGameOver(tetromino)
if tetromino == None:
gameOver = True
break
draw.refreshScreen(board, tetromino)
#Check for user input
for event in pygame.event.get():
if event.type == pygame.QUIT:
isOpen = False
keyInput = pygame.key.get_pressed()
if keyInput[pygame.K_ESCAPE]:
paused = True
if keyInput[pygame.K_n]:
newGame = True
#Game controls only if not bot
if (not selfPlay):
if keyInput[pygame.K_LCTRL] or keyInput[pygame.K_RCTRL]:
board.rotatePiece(tetromino, Rotation.ANTICLOCKWISE)
if keyInput[pygame.K_UP]:
board.rotatePiece(tetromino, Rotation.CLOCKWISE)
if keyInput[pygame.K_RIGHT]:
board.moveOrLockPiece(tetromino, Direction.RIGHT)
if keyInput[pygame.K_LEFT]:
board.moveOrLockPiece(tetromino, Direction.LEFT)
if keyInput[pygame.K_DOWN]:
locked = board.moveOrLockPiece(tetromino, Direction.DOWN)
if (locked):
tetromino = board.newPieceOrGameOver(tetromino)
if tetromino == None:
gameOver = True
if keyInput[pygame.K_RETURN]:
board.dropAndLockPiece(tetromino)
tetromino = board.newPieceOrGameOver(tetromino)
if tetromino == None:
gameOver = True
if keyInput[pygame.K_LSHIFT] or keyInput[pygame.K_RSHIFT]:
if (board.isHeldPieceEmpty()):
board.setHeldPiece(tetromino)
tetromino = board.generatePiece()
else:
tetromino = board.swapWithHeldPiece(tetromino)
gameFlags = [newGame, gameOver, paused, (not isOpen)]
#Game over screen loop
while gameOver:
draw.drawGameOver(board)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameOver = False
isOpen = False
keyInput = pygame.key.get_pressed()
if keyInput[pygame.K_n] or keyInput[pygame.K_ESCAPE]:
newGame = True
gameOver = False