Created
May 9, 2021 06:52
-
-
Save pcchou/5f17f7fd563c240e5adc196cc79d7b55 to your computer and use it in GitHub Desktop.
一階大作業參考用 Sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import config | |
def getCellsAbsolutePosition(piece): | |
'''取得方塊當前所有方格的座標''' | |
return [(y + piece.y, x + piece.x) for y, x in piece.getCells()] | |
def fixPiece(shot, piece): | |
'''固定已落地的方塊,並且在main中自動切到下一個方塊''' | |
piece.is_fixed = True | |
for y, x in getCellsAbsolutePosition(piece): | |
shot.status[y][x] = 2 | |
shot.color[y][x] = piece.color | |
### Your homework below. Enjoy :) ### | |
def moveLeft(shot, piece): | |
for y, x in getCellsAbsolutePosition(piece): | |
if y >= 0 and (x == 0 or shot.status[y][x - 1] == 2): | |
return | |
piece.x -= 1 | |
def moveRight(shot, piece): | |
for y, x in getCellsAbsolutePosition(piece): | |
if y >= 0 and (x + 1 == config.columns or shot.status[y][x + 1] == 2): | |
return | |
piece.x += 1 | |
def drop(shot, piece): | |
for y, x in getCellsAbsolutePosition(piece): | |
if y+1 == config.rows or y >= 0 and shot.status[y+1][x] == 2: | |
fixPiece(shot, piece) | |
return | |
piece.y += 1 | |
def instantDrop(shot, piece): | |
for _ in range(config.rows): | |
drop(shot, piece) | |
def rotate(shot, piece): | |
shape_list = config.shapes[piece.shape] | |
new_shape = shape_list[(piece.rotation + 1) % len(shape_list)] | |
# 檢查是否可以轉動 | |
for y, x in new_shape: | |
new_y = y + piece.y | |
new_x = x + piece.x | |
if not (0 <= new_y < config.rows and 0 <= new_x < config.columns) \ | |
or shot.status[new_y][new_x] == 2: | |
return | |
piece.rotation += 1 | |
# 另一種寫法 | |
def rotate_alternative(shot, piece): | |
piece.rotation += 1 | |
for y, x in getCellsAbsolutePosition(piece): | |
if not (0 <= y < config.rows and 0 <= x < config.columns) \ | |
or shot.status[y][x] == 2: | |
piece.rotation -= 1 | |
return | |
def isDefeat(shot, piece): | |
for y, x in getCellsAbsolutePosition(piece): | |
if y >= 0 and shot.status[y][x] == 2: | |
return True | |
return False | |
def eliminateFilledRows(shot, piece): | |
eliminated_row_counter = 0 | |
for y in range(config.rows): | |
is_row_filled = True | |
for x in range(config.columns): | |
if shot.status[y][x] != 2: | |
is_row_filled = False | |
if is_row_filled: | |
eliminated_row_counter += 1 | |
for u in range(config.columns): | |
for v in reversed(range(y)): | |
shot.status[v + 1][u] = shot.status[v][u] | |
shot.color[v + 1][u] = shot.color[v][u] | |
shot.status[0][u] = 0 | |
shot.color[0][u] = (0, 0, 0) | |
if eliminated_row_counter > 0: | |
shot.line_count += eliminated_row_counter | |
shot.score += config.score_count[eliminated_row_counter] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment