鱼C论坛

 找回密码
 立即注册
查看: 1399|回复: 2

[作品展示] 2048 乞丐版

[复制链接]
发表于 2021-7-12 23:25:32 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
继飞机大战之后的第二款全部由自己开发设计的小游戏,记录一下^_^
过程中遇到一些问题,很开心自己都能较好的解决 ,不过运行起来感觉有点慢XD,希望下次能写出更好的作品
# 运行速度较慢 不知道是为啥
# 空格是回到上一步
# 写入了检测是否死局的功能
# 最大的收获应该是对矩阵有了初步的了解 很高兴能用到数学书上的知识
  1. import pygame
  2. import random as r
  3. import sys
  4. import traceback
  5. from pygame.locals import *

  6. ### py(3.9.5)
  7. pygame.init()
  8. screen_size = width, height = 400, 400
  9. screen = pygame.display.set_mode(screen_size)
  10. clock = pygame.time.Clock()
  11. fps = 30

  12. # 列表中是否有零
  13. def not_zero_num(lista):
  14.     for i in range(len(lista)):
  15.         if lista[i] != 0:
  16.             return lista[i], i

  17. # 清除列表前n个0 orient = False <---, orient = True --->
  18. def clear_zero(listb, orient = False):
  19.     # 先清除中间部分
  20.     target_list = listb.copy()
  21.     for i in range(len(target_list) - 1):
  22.         if target_list[i] == 0:
  23.             target_list.pop(i)
  24.             target_list.insert(len(target_list), 0)
  25.     if not orient :
  26.         for i in range(len(target_list)):
  27.             if target_list[0] == 0:
  28.                 target_list.pop(0)
  29.                 target_list.append(0)
  30.     elif orient:
  31.         length = len(target_list)
  32.         for i in range(length):
  33.             if target_list[length - 1] == 0:
  34.                 target_list.pop(length - 1)
  35.                 target_list.insert(0, 0)
  36.     return target_list

  37. # 主函数 移动矩阵
  38. def move_box(listc, orient = False, high_dim = False): # <--- 移动方向 orient 默认方向
  39.     calc_list = listc[:] # 好像列表复制不好用
  40.     calc_list = clear_zero(calc_list, orient)
  41.     if orient:
  42.         for i in range(len(calc_list) - 1):
  43.             if calc_list[len(calc_list) - 1 - i] == calc_list[len(calc_list) - 2 - i]:
  44.                 calc_list[len(calc_list) - 1 - i] = 2 * calc_list[len(calc_list) - 1 - i]
  45.                 calc_list.pop(len(calc_list) - 2 - i)
  46.                 calc_list.insert(0, 0)
  47.     elif not orient:
  48.         for i in range(len(calc_list) - 1):
  49.             if calc_list[i] == calc_list[i + 1]:
  50.                 calc_list[i] = calc_list[i] * 2
  51.                 calc_list.pop(i + 1)
  52.                 calc_list.insert(len(calc_list), 0)
  53.     return calc_list

  54. # 转置矩阵
  55. def reverse_matrix(matrix):
  56.     result = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
  57.     for y in range(len(matrix)):
  58.         for x in range(len(matrix[0])):
  59.             result[y][x] = matrix[x][y]
  60.     return result

  61. # 随机空白位置生成新数字
  62. def new_num(matrix):
  63.     blank = []
  64.     for y in range(len(matrix)):
  65.         for x in range(len(matrix[0])):
  66.             if not matrix[y][x]:
  67.                 blank.append((y, x))
  68.     if not blank:
  69.         return False
  70.     else:
  71.         select = r.choice(blank)
  72.         matrix[select[0]][select[1]] = r.choice([2,4])
  73.         return True

  74. # 色库
  75. BLACK = 0, 0, 0
  76. GREEN = 0, 255, 0

  77. # 字体
  78. font = pygame.font.Font(None, 20)

  79. # 初始化数据
  80. main_list = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
  81. font_list = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
  82. font_pos =  [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
  83. check =     [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]] # 检验矩阵
  84. memorize =  [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]] # 记忆矩阵
  85. running = True
  86. row, col = 4, 4
  87. rect_size = x, y = 96, 96
  88. check_active = False
  89. new_num(main_list) # 初始化游戏

  90. def move_left(row, matrix):
  91.     for i in range(row):
  92.         matrix[i] = move_box(matrix[i], False)
  93.     return matrix

  94. def move_right(row, matrix):
  95.     for i in range(row):
  96.         matrix[i] = move_box(matrix[i], True)
  97.     return matrix

  98. def move_up(col, matrix):
  99.     matrix = reverse_matrix(matrix)
  100.     move_left(col, matrix)         
  101.     matrix = reverse_matrix(matrix)
  102.     return matrix

  103. def move_down(col, matrix):
  104.     matrix = reverse_matrix(matrix)
  105.     move_right(col, matrix)
  106.     matrix = reverse_matrix(matrix)
  107.     return matrix

  108. # 记录文字位置初始数据
  109. for i in range(screen_size[0] // x): # 行
  110.     for j in range(screen_size[1] // y): # 列
  111.         font_pos[i][j] = (pygame.draw.rect(screen, BLACK, [i * 100 + 2, j * 100 + 2, x, y]).center)

  112. while running:
  113.     # 检测事件
  114.    
  115.     screen.fill(GREEN)
  116.     for event in pygame.event.get():
  117.         if event.type == QUIT:
  118.             pygame.quit()
  119.             sys.exit()
  120.         # 用户操作
  121.         key_pressed = pygame.key.get_pressed()
  122.         if key_pressed[K_LEFT] or key_pressed[K_RIGHT] or key_pressed[K_DOWN] or key_pressed[K_UP]:
  123.             memorize = main_list[:]
  124.             check_active = True
  125.             if key_pressed[K_LEFT]:
  126.                 main_list = move_left(row, main_list)
  127.             if key_pressed[K_RIGHT]:
  128.                 main_list = move_right(row, main_list)
  129.             if key_pressed[K_DOWN]:
  130.                 main_list = move_down(col, main_list)
  131.             if key_pressed[K_UP]:
  132.                 main_list = move_up(col, main_list)

  133.             check = main_list[:]
  134.             check = move_left(row, check)
  135.             if check == main_list:
  136.                 check = move_right(row, check)
  137.                 if check == main_list:
  138.                     check = move_down(col, check)
  139.                     if  check == main_list:
  140.                         move_up(col, check)
  141.                         if  check == main_list:
  142.                             print("game over")
  143.                             # 检验是否真的无法移动
  144.                             ENDDING = True
  145.                             # game over
  146.                             while ENDDING:
  147.                                 for event in pygame.event.get():
  148.                                     if event.type == QUIT:
  149.                                         pygame.quit()
  150.                                         sys.exit()
  151.                                     if event.type == KEYDOWN:
  152.                                         if event.key == K_RETURN:
  153.                                             ENDDING = False
  154.                                             # reset
  155.                                             main_list = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
  156.                                             font_list = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
  157.                                             check =  [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
  158.                                             new_num(main_list)
  159.                                             running = True
  160.                                             row, col = 4, 4
  161.                                             rect_size = x, y = 96, 96
  162.                                             check_active = False
  163.                                 screen.fill(BLACK)
  164.                                 font_rect = font.render("Game Over. Press [Enter] to continue", True, (255,255,255)).get_rect()
  165.                                 font_rect.center = screen_size[0] // 2, screen_size[1] // 2
  166.                                 screen.blit(font.render("Game Over. Press [Enter] to continue", True, (255,255,255)),font_rect)
  167.                                 pygame.display.flip()
  168.             new_num(main_list)

  169.         # 恢复上一步
  170.         if event.type == KEYDOWN:
  171.             if event.key == K_SPACE:
  172.                 if check_active:
  173.                     main_list = memorize.copy()
  174.                     check_active = False

  175.     # 绘制背景 及 数据
  176.     for i in range(screen_size[0] // x): # 列
  177.         for j in range(screen_size[1] // y): # 行
  178.             font_list[i][j] = font.render(str(main_list[j][i]), True, GREEN)
  179.             pygame.draw.rect(screen, BLACK, [i * 100 + 2, j * 100 + 2, x, y])
  180.             if main_list[j][i] != 0:
  181.                 screen.blit(font_list[i][j], (font_pos[i][j][0] - font_list[i][j].get_rect().width // 2,\
  182.                                               font_pos[i][j][1] - font_list[i][j].get_rect().height // 2))

  183.     pygame.display.flip()
  184.     clock.tick(fps)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-7-12 23:26:36 | 显示全部楼层
呀,调试之后忘了把壳子套上了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-12 23:49:34 | 显示全部楼层
思路很棒
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-7 15:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表