Python实现简单的2048小游戏

这篇文章主要为大家详细介绍了Python实现简单的2048小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Python实现简单的2048小游戏的具体代码,供大家参考,具体内容如下

运行效果:

1.项目结构

2.代码

configs.py

 import argparse def parse_args(): parser = argparse.ArgumentParser(description='Game 2048') # Form """ screen_width: Width of the form screen_height: Height of the form """ parser.add_argument('--screen_width', default=400) parser.add_argument('--screen_height', default=500) # Block """ block_gap: Gap between two blocks block_size: Size of a block block_arc: Arc of a block """ parser.add_argument('--block_gap', default=10) parser.add_argument('--block_size', default=86) parser.add_argument('--block_arc', default=10) return parser.parse_args()

main.py

 import configs from Game2048 import Game2048 def main(args): """ screen_width: Width of the form screen_height: Height of the form block_gap: Gap between two blocks block_size: Size of a block """ screen_width = args.screen_width screen_height = args.screen_height block_gap = args.block_gap block_size = args.block_size block_arc = args.block_arc game = Game2048(screen_width, screen_height, block_gap, block_size, block_arc) game.Form() if __name__ == '__main__': args = configs.parse_args() main(args)

Game2048.py

 import os import sys import numpy import random import pygame """ Form(): 窗口的设置 Action(): 用户行为: 按键/鼠标 InitGame(): 游戏初始化 CreatNum(): 随机在一个位置生成一个数 GetEmpty(): 获取空白方格 MoveUp(): 向上移动 MoveDown(): 向下移动 MoveLeft(): 向左移动 MoveRight(): 向右移动 JudgeGameOver(): 判断游戏是否结束 JudgeGameSuccess(): 判断游戏是否成功 Paint(): 绘制表格 """ class Game2048(object): # 初始化函数 def __init__(self, screen_width, screen_height, block_gap, block_size, block_arc): """ :param screen_width: Width of the form :param screen_height: Height of the form :param block_gap: Gap between two blocks :param block_size: Size of a block :param size: Dimension of matrix :param martix: Zero matrix :param is_over: Sign of the end of the game :param is_success: Sign of the success of the game :param form: The form :param score: score :param title_font: Title type and size of form :param score_font: Scores type and size :param tips_font: Tips type and type :param font: The numberes :param isadd: Add number or not """ """ 窗口 """ self.screen_width = screen_width # 窗口的宽 400 self.screen_height = screen_height # 窗口的高 500 self.block_gap = block_gap # 方块间隙 10 self.block_size = block_size # 方块大小 86 self.block_arc = block_arc # 方块的弧度 self.size = 4 # 矩阵 4 * 4 self.martix = [] # 初始化矩阵 4 * 4 的 0 矩阵 self.form = '' """ 其他 """ self.is_over = False # 游戏是否结束 self.is_success = False # 游戏是否成功 self.score = 0 # 分数 self.isadd = True # 是否添加数字 self.block_color = { # 方块颜色 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, 97), 512: (237, 200, 80), 1024: (237, 197, 63), 2048: (237, 194, 46) } self.nums_color = { # 0: (0, 0, 0), 0: (205, 193, 180), 2: (0, 0, 0), 4: (0, 0, 0), 8: (255, 255, 255), 16: (255, 255, 255), 32: (255, 255, 255), 64: (255, 255, 255), 128: (255, 255, 255), 256: (255, 255, 255), 512: (255, 255, 255), 1024: (255, 255, 255), 2048: (255, 255, 255) } """ 字体 """ self.title_font = '' # 窗口标题字体类型及大小: 2048 self.score_font = '' # 分数字体类型及大小 self.tips_font = '' # 说明字体类型及大小 self.font = '' # 数字字体 # 窗口的设置 def Form(self): """ init(): 初始化所有导入的 pygame 模块 display.set_caption(title): 设置窗口的标题 display.set_mode(): 初始化一个准备显示的窗口或屏幕 display.update(): 使绘制的显示到窗口上 """ pygame.init() # 初始化所有导入的 pygame 模块 pygame.display.set_caption("Game2048") # 窗口标题 os.environ['SDL_VIDEO_CENTERED'] = '1' # 窗口居中显示 self.form = pygame.display.set_mode([self.screen_width, self.screen_height], 0, 0) # 窗口大小 self.InitGame() # 矩阵的初始化 while True: self.Action() # 用户行为: 按键/鼠标 self.Paint() # 表格绘制 pygame.display.update() # 使绘制的显示到窗口上 # 用户行为: 按键/鼠标 def Action(self): for event in pygame.event.get(): # pygame.event.get(): 获取所有消息并将其从队列中删除 if event.type == pygame.QUIT: # pygame.QUIT: 窗口右上角的红 × sys.exit() # sys.exit()函数是通过抛出异常的方式来终止进程的 elif event.type == pygame.KEYDOWN: """ pygame.KEYDOWN 按下键盘时 pygame.KEYUP 释放键盘时 """ """ K_ESCAPE: ESC K_UP: ↑ K_DOWN: ↓ K_LEFT: ← K_RIGHT: → """ """ 重新开始游戏 """ if event.key == pygame.K_ESCAPE: # print('ESC') self.InitGame() # 游戏初始化 """ ↑ """ if event.key == pygame.K_UP and self.is_over == False: # print('UP') self.MoveUp() # self.CreatNum() """ ↓ """ if event.key == pygame.K_DOWN and self.is_over == False: # print('DOWN') self.MoveDown() # self.CreatNum() """ ← """ if event.key == pygame.K_LEFT and self.is_over == False: # print('LEFT') self.MoveLeft() # self.CreatNum()

以上就是Python实现简单的2048小游戏的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » python