-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2048 human opperational to use.py
More file actions
169 lines (140 loc) · 5.64 KB
/
2048 human opperational to use.py
File metadata and controls
169 lines (140 loc) · 5.64 KB
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
164
165
166
167
168
169
# DO NOT TOUCH THIS CODE. THIS IS THE ONLY VERSION THAT WORKS PROPERLY
import pygame
import random
import sys
# Initializing pygame
pygame.init()
#constants
SIZE = 4
CELL_SIZE = 100
GRID_SIZE = SIZE * CELL_SIZE
WINDOW_SIZE = GRID_SIZE + 2 * CELL_SIZE
BACKGROUND_COLOR = (187 , 173 , 160)
CELL_COLOR = (205 , 193 , 180)
CELL_EMPTY_COLOR = (205 , 193 , 180)
FONT_COLOR = (119 , 110 , 101)
FONT = pygame.font.SysFont("arial", 40)
# Color mapping for numbers
COLOR_MAPPING = {
2: (238, 228, 218),
4: (237, 224, 200),
8: (242, 117, 121),
16: (245, 149, 99),
32: (246, 124, 95),
64: (246, 124, 95),
128: (237, 207, 114),
256: (237, 207, 114),
512: (237, 200, 80),
1024: (237, 197, 63),
2048: (237, 194, 46),
}
#initializing game board
def initialize_board():
board = [[0] * SIZE for _ in range(SIZE)]
add_new_tile(board)
add_new_tile(board)
return board
#adding new tile to the board
def add_new_tile(board):
empty_cells = [(i , j) for i in range(SIZE) for j in range(SIZE) if board[i][j] == 0]
if empty_cells:
i , j = random.choice(empty_cells)
board[i][j] = 2 if random.random() < 0.9 else 4
#slide and combine logic
def slide_and_combine(row):
new_row = [i for i in row if i != 0]
for i in range(len(new_row) - 1):
if new_row[i] == new_row[i + 1]:
new_row[i] *= 2
new_row[i + 1 ] = 0
new_row = [i for i in new_row if i != 0]
return new_row + [0] * (SIZE - len(new_row))
#Apply move to the board
def apply_move(board , direction):
if direction == 'w':
for j in range(SIZE):
column = slide_and_combine([board[i][j] for i in range(SIZE)])
for i in range(SIZE):
board[i][j] = column[i]
elif direction == 's':
for j in range(SIZE):
column = slide_and_combine([board[i][j] for i in range(SIZE -1, -1, -1)])
for i in range(SIZE):
board[SIZE - 1 - i][j] = column[i]
elif direction == 'a':
for i in range(SIZE):
board[i] = slide_and_combine(board[i])
elif direction == 'd':
for i in range(SIZE):
board[i] = slide_and_combine(board[i][::-1])[::-1]
# Game over check
def is_game_over(board):
for i in range(SIZE):
for j in range(SIZE):
if board[i][j] == 0 or \
(i < SIZE - 1 and board[i][j] == board[i + 1][j] ) or \
(j < SIZE - 1 and board[i][j] == board[i][j + 1]):
return False
return True
# The board drawn with animation
def draw_board(board, screen, animations):
screen.fill(BACKGROUND_COLOR)
# Drawing grid lines
for i in range(SIZE + 1):
pygame.draw.line(screen, (0, 0, 0), (CELL_SIZE, i * CELL_SIZE + CELL_SIZE), (GRID_SIZE + CELL_SIZE, i * CELL_SIZE + CELL_SIZE), 6)
pygame.draw.line(screen, (0, 0, 0), (i * CELL_SIZE + CELL_SIZE, CELL_SIZE), (i * CELL_SIZE + CELL_SIZE, GRID_SIZE + CELL_SIZE), 6)
for i in range(SIZE):
for j in range(SIZE):
value = board[i][j]
rect = pygame.Rect(j * CELL_SIZE + CELL_SIZE + 3, i * CELL_SIZE + CELL_SIZE + 3, CELL_SIZE - 6, CELL_SIZE - 6)
cell_color = COLOR_MAPPING.get(value, CELL_EMPTY_COLOR)
pygame.draw.rect(screen, cell_color, rect)
if value:
if(i , j) in animations:
#Animate numbers towards their positions
current_x, current_y = animations[(i , j)]
target_x, target_y = j * CELL_SIZE + CELL_SIZE + 3, i * CELL_SIZE + CELL_SIZE + 3
delta_x = (target_x - current_x) // 5
delta_y = (target_y - current_y) // 5
current_x += delta_x
current_y += delta_y
animations[(i, j)] = (current_x, current_y)
text = FONT.render(str(value),True,FONT_COLOR)
text_rect = text.get_rect(center=(current_x + (CELL_SIZE - 6) // 2, current_y + (CELL_SIZE - 6) // 2))
screen.blit(text , text_rect)
else:
text = FONT.render(str(value), True , FONT_COLOR)
text_rect = text.get_rect(center = rect.center)
screen.blit(text, text_rect)
#Main
def main():
board = initialize_board()
screen = pygame.display.set_mode((WINDOW_SIZE, WINDOW_SIZE))
pygame.display.set_caption("2048 Game")
clock = pygame.time.Clock()
animations = {} # dictionary to store animation states
while True:
draw_board(board, screen, animations)
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key in (pygame.K_w, pygame.K_UP):
apply_move(board, 'w')
elif event.key in (pygame.K_s, pygame.K_DOWN):
apply_move(board, 's')
elif event.key in (pygame.K_a, pygame.K_LEFT):
apply_move(board, 'a')
elif event.key in (pygame.K_d, pygame.K_RIGHT):
apply_move(board, 'd')
add_new_tile(board)
animations.clear() # clears animations after move
if is_game_over(board):
print("Game Over")
pygame.quit()
sys.exit()
clock.tick(10)
if __name__ == "__main__":
main()