基于pygame的2048小游戏实验代码

时间: 2023-09-03 07:16:36 浏览: 44
以下是基于pygame的2048小游戏实验的代码,供参考: ``` python import pygame import random # 定义颜色 BLACK = (0, 0, 0) WHITE = (255, 255, 255) GRAY = (128, 128, 128) BLUE = (0, 0, 255) GREEN = (0, 255, 0) RED = (255, 0, 0) # 初始化pygame pygame.init() # 设置窗口尺寸 size = (400, 500) screen = pygame.display.set_mode(size) # 设置窗口标题 pygame.display.set_caption("2048") # 字体设置 FONT = pygame.font.SysFont('Arial', 50) # 加载音效 MOVE_SOUND = pygame.mixer.Sound('move_sound.wav') MERGE_SOUND = pygame.mixer.Sound('merge_sound.wav') # 加载背景图片及数字图片 BACKGROUND_IMAGE = pygame.image.load('background.png').convert() NUMBERS_IMAGE = [] for i in range(1, 12): NUMBERS_IMAGE.append(pygame.image.load(str(i) + '.png').convert_alpha()) # 定义游戏矩阵及状态 matrix = [[0 for i in range(4)] for j in range(4)] state = 'playing' # 随机生成一个新的数字 def add_new_number(): while True: x = random.randint(0, 3) y = random.randint(0, 3) if matrix[x][y] == 0: matrix[x][y] = random.choice([2, 4]) break # 绘制游戏界面 def draw_game(): screen.blit(BACKGROUND_IMAGE, (0, 0)) for i in range(4): for j in range(4): if matrix[i][j] != 0: num_index = int(pow(2, (matrix[i][j] - 1))) screen.blit(NUMBERS_IMAGE[num_index - 1], (j * 100 + 10, i * 100 + 110)) pygame.display.update() # 判断游戏是否胜利 def check_win(): for i in range(4): for j in range(4): if matrix[i][j] == 11: return True return False # 判断游戏是否失败 def check_lose(): for i in range(4): for j in range(4): if matrix[i][j] == 0: return False if j < 3 and matrix[i][j] == matrix[i][j + 1]: return False if i < 3 and matrix[i][j] == matrix[i + 1][j]: return False return True # 移动方块 def move(direction): global state global matrix moved = False if direction == 'left': for i in range(4): for j in range(1, 4): if matrix[i][j] != 0: k = j - 1 while k >= 0 and matrix[i][k] == 0: k -= 1 if k >= 0 and matrix[i][k] == matrix[i][j]: matrix[i][k] += 1 matrix[i][j] = 0 MERGE_SOUND.play() moved = True elif k + 1 != j: matrix[i][k + 1] = matrix[i][j] matrix[i][j] = 0 MOVE_SOUND.play() moved = True elif direction == 'right': for i in range(4): for j in range(2, -1, -1): if matrix[i][j] != 0: k = j + 1 while k <= 3 and matrix[i][k] == 0: k += 1 if k <= 3 and matrix[i][k] == matrix[i][j]: matrix[i][k] += 1 matrix[i][j] = 0 MERGE_SOUND.play() moved = True elif k - 1 != j: matrix[i][k - 1] = matrix[i][j] matrix[i][j] = 0 MOVE_SOUND.play() moved = True elif direction == 'up': for j in range(4): for i in range(1, 4): if matrix[i][j] != 0: k = i - 1 while k >= 0 and matrix[k][j] == 0: k -= 1 if k >= 0 and matrix[k][j] == matrix[i][j]: matrix[k][j] += 1 matrix[i][j] = 0 MERGE_SOUND.play() moved = True elif k + 1 != i: matrix[k + 1][j] = matrix[i][j] matrix[i][j] = 0 MOVE_SOUND.play() moved = True elif direction == 'down': for j in range(4): for i in range(2, -1, -1): if matrix[i][j] != 0: k = i + 1 while k <= 3 and matrix[k][j] == 0: k += 1 if k <= 3 and matrix[k][j] == matrix[i][j]: matrix[k][j] += 1 matrix[i][j] = 0 MERGE_SOUND.play() moved = True elif k - 1 != i: matrix[k - 1][j] = matrix[i][j] matrix[i][j] = 0 MOVE_SOUND.play() moved = True if moved: add_new_number() draw_game() if check_win(): state = 'win' elif check_lose(): state = 'lose' # 初始化游戏矩阵 for i in range(2): add_new_number() # 游戏循环 done = False clock = pygame.time.Clock() while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True elif event.type == pygame.KEYDOWN: if state == 'playing': if event.key == pygame.K_LEFT: move('left') elif event.key == pygame.K_RIGHT: move('right') elif event.key == pygame.K_UP: move('up') elif event.key == pygame.K_DOWN: move('down') elif state == 'win' or state == 'lose': if event.key == pygame.K_SPACE: matrix = [[0 for i in range(4)] for j in range(4)] for i in range(2): add_new_number() state = 'playing' # 绘制游戏界面 draw_game() # 绘制游戏状态 if state == 'win': text = FONT.render('YOU WIN!', True, GREEN) screen.blit(text, (50, 20)) elif state == 'lose': text = FONT.render('GAME OVER', True, RED) screen.blit(text, (10, 20)) # 刷新显示 pygame.display.update() # 设置帧率 clock.tick(60) # 退出pygame pygame.quit() ```

相关推荐

最新推荐

recommend-type

使用Python第三方库pygame写个贪吃蛇小游戏

今天看到几个关于pygame模块的博客和视频,感觉非常有趣,这里照猫画虎写了一个贪吃蛇小游戏,目前还有待完善,但是基本游戏功能已经实现,下面是代码: # 导入模块 import pygame import random # 初始化 pygame....
recommend-type

pygame游戏之旅 调用按钮实现游戏开始功能

主要为大家详细介绍了pygame游戏之旅的第12篇,教大家调用按钮实现游戏开始功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

python pygame实现五子棋小游戏

主要为大家详细介绍了python pygame实现五子棋小游戏,代码注释很详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

grpcio-1.47.0-cp310-cp310-linux_armv7l.whl

Python库是一组预先编写的代码模块,旨在帮助开发者实现特定的编程任务,无需从零开始编写代码。这些库可以包括各种功能,如数学运算、文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
recommend-type

小程序项目源码-美容预约小程序.zip

小程序项目源码-美容预约小程序小程序项目源码-美容预约小程序小程序项目源码-美容预约小程序小程序项目源码-美容预约小程序小程序项目源码-美容预约小程序小程序项目源码-美容预约小程序小程序项目源码-美容预约小程序小程序项目源码-美容预约小程序v
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【实战演练】MATLAB用遗传算法改进粒子群GA-PSO算法

![MATLAB智能算法合集](https://static.fuxi.netease.com/fuxi-official/web/20221101/83f465753fd49c41536a5640367d4340.jpg) # 2.1 遗传算法的原理和实现 遗传算法(GA)是一种受生物进化过程启发的优化算法。它通过模拟自然选择和遗传机制来搜索最优解。 **2.1.1 遗传算法的编码和解码** 编码是将问题空间中的解表示为二进制字符串或其他数据结构的过程。解码是将编码的解转换为问题空间中的实际解的过程。常见的编码方法包括二进制编码、实数编码和树形编码。 **2.1.2 遗传算法的交叉和
recommend-type

openstack的20种接口有哪些

以下是OpenStack的20种API接口: 1. Identity (Keystone) API 2. Compute (Nova) API 3. Networking (Neutron) API 4. Block Storage (Cinder) API 5. Object Storage (Swift) API 6. Image (Glance) API 7. Telemetry (Ceilometer) API 8. Orchestration (Heat) API 9. Database (Trove) API 10. Bare Metal (Ironic) API 11. DNS
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。