Не обрабатываются вообще никакие, хотя событие выход из игры - вполне
Вот код полностью:
import pygame as pg
from func import *
from const import *
pg.init()
pg.font.init()
screen = pg.display.set_mode((WIDTH, HEIGHT))
font = pg.font.SysFont(pg.font.get_default_font(), 70)
text_menu = text = font.render('играть' , True , BLACK)
clock = pg.time.Clock()
generation_happen = 1
time = None
game_over = True
running = True
board = [
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
]
generation_2_or_4(board)
generation_2_or_4(board)
while running:
clock.tick(60)
mouse = pg.mouse.get_pos()
for event in pg.event.get():
if event.type == pg.QUIT:
running = False
if game_over:
if event.type == pg.MOUSEBUTTONDOWN:
if WIDTH / 3 <= mouse[0] <= WIDTH / 3 + WIDTH_BUTT0N and HEIGHT / 2.5 <= mouse[1] <= HEIGHT / 2.5 + HEIGHT_BUTTON:
game_over = False
if not game_over:
if time is not None:
time += 1
for event in pg.event.get():
if event.type == pg.KEYDOWN and generation_happen == 1:
if event.key == pg.K_LEFT:
board = move_left(board)
if event.key == pg.K_UP:
board = move_up(board)
if event.key == pg.K_DOWN:
board = move_down(board)
if event.key == pg.K_RIGHT:
board = move_right(board)
time = 0
generation_happen = 0
# if is_winning(board):
# win = True
# screen.fill((51, 25, 0))
if is_terminal(board):
game_over = True
if time is not None and time >= 10:
generation_2_or_4(board)
time = None
generation_happen = 1
screen.fill(BACK)
for row in range(4):
for column in range(4):
COLOR = colors[board[row][column]]
x_play_cell = column * size_of_cell + (column + 1) * margin
y_play_cell = row * size_of_cell + (row + 1) * margin
pg.draw.rect(screen, COLOR, (x_play_cell, y_play_cell, size_of_cell, size_of_cell))
if board[row][column] != 0:
text = font.render(str(board[row][column]), True, BLACK)
screen.blit(text, (35 + x_play_cell, y_play_cell + 25))
else:
screen.fill(BACK)
pg.draw.rect(screen, WHITE, [WIDTH / 3, HEIGHT / 2.5, WIDTH_BUTT0N, HEIGHT_BUTTON])
screen.blit(text, (WIDTH / 3, HEIGHT / 2.5))
pg.display.flip()
pg.quit()
файл с функциями:
from random import randint
def is_board_full(board):
number_of_full = 0
for elements in board:
for element in elements:
if element != 0:
number_of_full += 1
if number_of_full == 16:
return True
return False
def is_terminal(board):
if is_board_full(board):
for row in range(3):
if board[row][3] == board[row + 1][3]:
return False
for column in range(3):
if board[row][column] == board[row + 1][column] or board[row][column] == board[row][column + 1]:
return False
if board[3][column] == board[3][column + 1]:
return False
return True
def is_winning(board):
for row in range(4):
for column in range(4):
if board[row][column] == 2048:
return True
return False
def is_cell_is_empty(board, row_of_cell, column_of_cell):
if board[row_of_cell][column_of_cell] == 0:
return True
else:
return False
def generation_2_or_4(board):
random_num = randint(0,1)
row_of_cell = randint(0, 3)
column_of_cell = randint(0,3)
while not is_cell_is_empty(board, row_of_cell, column_of_cell):
row_of_cell = randint(0, 3)
column_of_cell = randint(0, 3)
figure = 2
if random_num > 0.9:
figure = 4
board[row_of_cell][column_of_cell] = figure
return board[row_of_cell][column_of_cell]
def move_left(board):
new_board = [
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
]
for row in range(4):
pos = 0
for column in range(4):
if board[row][column] != 0:
new_board[row][pos] = board[row][column]
if pos > 0 and new_board[row][pos] == new_board[row][pos - 1]:
new_board[row][pos - 1] *= 2
new_board[row][pos] = 0
else:
pos += 1
return new_board
def move_right(board):
new_board = [
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
]
for row in range(4):
pos = 3
for column in range(3, -1 ,-1):
if board[row][column] != 0:
new_board[row][pos] = board[row][column]
if pos < 3 and new_board[row][pos] == new_board[row][pos + 1]:
new_board[row][pos + 1] *= 2
new_board[row][pos] = 0
else:
pos -= 1
return new_board
def move_up(board):
new_board = [
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
]
for column in range(4):
pos = 0
for row in range(4):
if board[row][column] != 0:
new_board[pos][column] = board[row][column]
if pos > 0 and new_board[pos - 1][column] == new_board[pos][column]:
new_board[pos - 1][column] *= 2
new_board[pos][column] = 0
else:
pos += 1
# if pos < 3:
# pos += 1
return new_board
def move_down(board):
new_board = [
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
]
for column in range(4):
pos = 3
for row in range(3, -1, -1):
if board[row][column] != 0:
new_board[pos][column] = board[row][column]
if pos < 3 and new_board[pos + 1][column] == new_board[pos][column]:
new_board[pos + 1][column] *= 2
new_board[pos][column] = 0
else:
pos -= 1
return new_board
с константами:
HEIGHT = 450
WIDTH = 450
HEIGHT_BUTTON = 60
WIDTH_BUTT0N = 160
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BACK = (51, 25, 0)
YELLOW = (230, 231, 215)
colors = {
0: (230, 231, 215),
2: (220, 221, 177),
4: (220, 221, 177),
8: (220, 150, 100),
16: (220, 221, 177),
32: (220, 221, 177),
64: (220, 221, 177),
128: (220, 221, 177),
256: (220, 221, 177),
512: (220, 221, 177),
1024: (220, 221, 177),
2048: (220, 221, 177)
}
x_play_cell, y_play_cell = 0, 0
size_of_cell = 100
margin = 10
generation_happen = 1
time = None