-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
129 lines (108 loc) · 4.35 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
import random
import pyxel
import requests
BACKEND = "http://37.27.51.34:20320"
# Game dimensions
CELL_SIZE = 16
BOARD_WIDTH = 10
BOARD_HEIGHT = 20
WINDOW_WIDTH = BOARD_WIDTH * CELL_SIZE
WINDOW_HEIGHT = BOARD_HEIGHT * CELL_SIZE
# Tetris piece definitions
PIECES = [
[(0, 0), (0, 1), (1, 0), (1, 1)], # Square
[(0, 0), (0, 1), (0, 2), (0, 3)], # Line
[(0, 0), (0, 1), (0, 2), (1, 2)], # L-shape
[(0, 0), (0, 1), (0, 2), (1, 0)], # J-shape
[(0, 0), (0, 1), (1, 1), (1, 2)], # S-shape
[(0, 0), (0, 1), (1, 0), (1, 1)], # Z-shape
[(0, 0), (0, 1), (0, 2), (1, 1)], # T-shape
]
class TetrisEngine:
def __init__(self):
pyxel.init(WINDOW_WIDTH, WINDOW_HEIGHT, title="Tetris on Pyxel")
self.board = [[0 for _ in range(BOARD_WIDTH)] for _ in range(BOARD_HEIGHT)]
self.current_piece = random.choice(PIECES)
self.current_x = BOARD_WIDTH // 2 - 2
self.current_y = 0
self.score = 0
self.saving = False
self.tick = 0
self.bestScore = requests.get(f"{BACKEND}/get").json()[0]
print(f"Your best score is {self.bestScore}")
self.is_game_over = False
pyxel.run(self.update, self.draw)
def rotate_piece(self):
self.current_piece = [(y, -x) for x, y in self.current_piece]
def move_piece(self, dx, dy):
self.current_x += dx
self.current_y += dy
if self.check_collision():
self.current_x -= dx
self.current_y -= dy
return False
return True
def check_collision(self):
for x, y in self.current_piece:
new_x = self.current_x + x
new_y = self.current_y + y
if new_x < 0 or new_x >= BOARD_WIDTH or new_y >= BOARD_HEIGHT or self.board[new_y][new_x]:
return True
return False
def check_game_over(self):
for x in range(BOARD_WIDTH):
if self.board[0][x] or self.board[1][x] or self.board[2][x] or self.board[3][x]:
return True
return False
def update(self):
if self.is_game_over:
return
self.tick += 1
if pyxel.btnp(pyxel.KEY_LEFT):
self.move_piece(-1, 0)
elif pyxel.btnp(pyxel.KEY_RIGHT):
self.move_piece(1, 0)
elif pyxel.btnp(pyxel.KEY_DOWN):
self.move_piece(0, 1)
elif pyxel.btnp(pyxel.KEY_UP):
self.rotate_piece()
if self.tick % 15 != 0:
return
if not self.move_piece(0, 1):
for x, y in self.current_piece:
new_x = self.current_x + x
new_y = self.current_y + y
self.board[new_y][new_x] = 1
rows_cleared = 0
for y in range(BOARD_HEIGHT):
if all(self.board[y]):
self.board.pop(y)
self.board.insert(0, [0] * BOARD_WIDTH)
rows_cleared += 1
self.score += rows_cleared
self.current_piece = random.choice(PIECES)
self.current_x = BOARD_WIDTH // 2 - 2
self.current_y = 0
if self.check_game_over():
self.is_game_over = True
def draw(self):
pyxel.cls(0)
for y in range(BOARD_HEIGHT):
for x in range(BOARD_WIDTH):
if self.board[y][x]:
pyxel.rect(x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE, 7)
for x, y in self.current_piece:
pyxel.rect((self.current_x + x) * CELL_SIZE, (self.current_y + y) * CELL_SIZE, CELL_SIZE, CELL_SIZE, 11)
pyxel.text(4, 4, f"Score: {self.score}", 10)
pyxel.text(100, 4, f"Best Score: {self.bestScore}", 10)
if self.is_game_over:
pyxel.text(BOARD_WIDTH * CELL_SIZE // 2 -20, BOARD_HEIGHT * CELL_SIZE // 2 - 10, "GAME OVER", 8)
if self.score > self.bestScore:
pyxel.text(BOARD_WIDTH * CELL_SIZE // 2, BOARD_HEIGHT * CELL_SIZE // 2, "NEW BEST SCORE!", 8)
if self.score > self.bestScore and not self.saving:
self.saving = True
requests.get(f"{BACKEND}/set?s={self.score}")
print("Best score saved")
self.tick = 5
if __name__ == "__main__":
TetrisEngine()