import sys import random import pygame pygame.init() # 初始化pygame() size = 75 # 每个游戏方格的大小 queue = 4 # 初始设置为4X4阵列 game_lis = [] # 存放 background_color = (255, 239, 213) # 背景颜色 Dividingline_color = (255, 222, 173) # 分割线颜色 Dividingline_width = 15 # 分割线宽度 score_height = 75 # 得分区的高度 score_width = 75 # 得分区的宽度 score_color = (205, 193, 180) # 得分字体颜色 font1 = pygame.font.SysFont('SimHei', 50) # 得分区域字体显示黑体24 font_pos_x = Dividingline_width # 得分区域字体位置的X坐标 font_pos_y = int(font1.size('得分')[1]) # 得分区域字体位置的Y坐标 score = 0 # 得分初始值为0 font3 = pygame.font.Font(None, 50) # 数字字体 black = (0, 0, 0) # 数字颜色 screen_height = (((size + Dividingline_width) * queue) + score_height + Dividingline_width * 2) # 屏幕高度 screen_width = (((size + Dividingline_width) * queue) + Dividingline_width) # 屏幕宽度 colors = {0: (205, 193, 180), # 各种方块对应的颜色 2: (238, 228, 218), 4: (237, 224, 200), 8: (242, 177, 121), 16: (245, 149, 99), 32: (246, 124, 95), 64: (246, 94, 59), 128: (237, 207, 114), 256: (237, 204, 98), 512: (237, 200, 80), 1024: (237, 197, 63), 2048: (225, 187, 0)} # 绘制背景 def _draw_background(screen): screen.fill(background_color)#屏幕颜色 Dividingline_width_half = int(Dividingline_width / 2) Difference = score_height + Dividingline_width + int(Dividingline_width / 2) for i in range(queue + 1): # 绘制横线 pygame.draw.line(screen, Dividingline_color, (0, i * (size + Dividingline_width) + Difference), (screen_height, i * (size + Dividingline_width) + Difference), Dividingline_width) for j in range(queue + 1): # 绘制竖线 pygame.draw.line(screen, Dividingline_color, (Dividingline_width_half + j * (size + Dividingline_width), Difference), (Dividingline_width_half + j * (size + Dividingline_width), screen_height), Dividingline_width) # 绘制得分区域 def _draw_score(screen, font, pos_x, pos_y): global score print_text(screen, font, pos_x, 10, f'得分') print_text(screen, font, pos_x, pos_y + 6, f'{score}') # 得分区域 def print_text(screen, font, x, y, text): imgText = font.render(text, True, score_color) screen.blit(imgText, (x, y)) # 初始化游戏列表,存放2048数字 def _game_list(): for i in range(queue): # 初始设置全为0 lis = [] for j in range(queue): lis.append(0) game_lis.append(lis) # 显示游戏区域:游戏方块和数字 def _show_game(screen): for i in range(queue): for j in range(queue): rect_color = colors[game_lis[i][j]] # 取出字典colors对应的值 rect_position = [(j + 1) * Dividingline_width + j * size, # 色块的位置 Dividingline_width * (i + 2) + size * i + score_height] pygame.draw.rect(screen, rect_color, (rect_position, (size, size)), 0) # 绘制色块 if game_lis[i][j] != 0: # 只有数列里不为0的时候才会输出 textSurfaceObj = font3.render(str(game_lis[i][j]), True, black) # get_rect()方法返回rect对象 textRectObj = textSurfaceObj.get_rect() rect_position = [(j + 1) * Dividingline_width + (j + 0.5) * size, # 数字的位置 Dividingline_width * (i + 2) + size * (i + 0.5) + score_height] textRectObj.center = tuple(rect_position) screen.blit(textSurfaceObj, textRectObj) # 在随机位置产生随机数 def _random(): random_num = random.randint(1, 2) # 随机数 num = pow(2, random_num) random_pos_x = random.randint(0, queue - 1) # 随机X坐标 random_pos_y = random.randint(0, queue - 1) # 随机Y坐标 if game_lis[random_pos_x][random_pos_y] == 0: game_lis[random_pos_x][random_pos_y] = num else: _random() # 捕获键盘LEFT操作 def _LEFT(): global score for i in range(4): for j in range(3): for k in range(j+1, 4): if game_lis[i][k] > 0: if game_lis[i][j] == 0: game_lis[i][j] = game_lis[i][k] game_lis[i][k] = 0 elif game_lis[i][j] == game_lis[i][k]: game_lis[i][j] *= 2 score+=game_lis[i][j] game_lis[i][k] = 0 break # 捕获键盘RIGHT操作 def _RIGHT(): global score for i in range(4): for j in range(3, 0, -1): for k in range(j - 1, -1, -1): if game_lis[i][k] > 0: if game_lis[i][j] == 0: game_lis[i][j] = game_lis[i][k] game_lis[i][k] = 0 elif game_lis[i][j] == game_lis[i][k]: game_lis[i][j] *= 2 score+=game_lis[i][j] game_lis[i][k] = 0 break # 获取键盘UP操作 def _UP(): global score for i in range(4): for j in range(3): for k in range(j + 1, 4): if game_lis[k][i] > 0: if game_lis[j][i] == 0: game_lis[j][i] = game_lis[k][i] game_lis[k][i] = 0 elif game_lis[k][i] == game_lis[j][i]: game_lis[j][i] *= 2 score+=game_lis[j][i] game_lis[k][i] = 0 break # 获取键盘DOWN操作 def _DOWN(): global score for i in range(4): for j in range(3, 0, -1): for k in range(j - 1, -1, -1): if game_lis[k][i] > 0: if game_lis[j][i] == 0: game_lis[j][i] = game_lis[k][i] game_lis[k][i] = 0 elif game_lis[j][i] == game_lis[k][i]: game_lis[j][i] *= 2 score+=game_lis[j][i] game_lis[k][i] = 0 break # 主函数 def main(): screen = pygame.display.set_mode((screen_width, screen_height)) # 建立游戏窗口 pygame.display.set_caption('2048') # 设置窗口标题 _draw_background(screen) # 绘制背景 _game_list() # 初始化游戏列表 _show_game(screen) # 显示游戏方块和数字 _draw_score(screen, font1, font_pos_x, font_pos_y) # 绘制得分区 # 开始游戏 _random() # 产生随机数 pygame.display.flip() # 更新游戏 _show_game(screen) # 显示游戏方块和数字 _draw_score(screen, font1, font_pos_x, font_pos_y) # 绘制得分区 while True: for event in pygame.event.get(): # get()获取事件的返回值 if event.type == pygame.QUIT: # 判断事件是否是退出事件,是则退出 pygame.quit() # 先退出pygame窗口 sys.exit() # 再退出pygame程序 elif event.type == pygame.KEYDOWN: # 捕获按键操作 if event.key == pygame.K_LEFT: # 按下左按键 _LEFT() print("left") elif event.key == pygame.K_RIGHT: # 按下右按键 _RIGHT() print("right") elif event.key == pygame.K_UP: # 按下上按键 _UP() print("up") elif event.key == pygame.K_DOWN: # 按下下按键 _DOWN() print("down") else: print("False") pygame.display.flip() # 更新游戏 _draw_background(screen) # 绘制背景 _random() # 产生随机数 _show_game(screen) # 显示游戏方块和数字 _draw_score(screen, font1, font_pos_x, font_pos_y) # 绘制得分区 if __name__ == '__main__': main()