自制2048

2048

可以输出彩色

import random
import sys


list = [[0, 0, 0, 0],
		[0, 0, 0, 0],
		[0, 0, 0, 0],
		[0, 0, 0, 0]]

x = 0
y = 0


def add_number():
	'''添加数字'''
	global list
	global isNotOver
	global x
	global y

	isTrue = True
	isZero = False
	number = [2, 2, 2, 2, 2, 2, 2, 2, 2, 4]

	while isTrue:
		# 判断是否有0
		for i in list:
			for j in i:
				if j == 0:
					isZero = True
				else:
					continue

		# 没有0退出循环
		if not isZero:
			isTrue = False

		# 随机坐标
		x = random.randint(1, 4) - 1
		y = random.randint(1, 4) - 1
		_list = list[y]

		# 如果是0就替换
		if _list[x] == 0:
			_list[x] = random.choice(number)
			list[y] = _list
			isTrue = False



def print_list():
	'''打印'''
	global list
	global x
	global y

	green = '\033[1;32m' + 'green' + '\033[0m'

	print('---------')
	for i in range(4):
		_list = list[i]
		_str = ''
		for j in range(4):
			if i == y and j == x:
				green = '\033[1;32m' + str(_list[j]) + '\033[0m'
				_str += (' ' + green + '')

			elif _list[j] == 8 or _list[j] == 16:
				blueA = '\033[1;36m' + str(_list[j]) + '\033[0m'
				_str += (' ' + blueA + '')

			elif _list[j] == 32 or _list[j] == 64:
				blueB = '\033[1;34m' + str(_list[j]) + '\033[0m'
				_str += (' ' + blueB + '')

			elif _list[j] == 128 or _list[j] == 256:
				yellow = '\033[1;33m' + str(_list[j]) + '\033[0m'
				_str += (' ' + yellow + '')

			elif _list[j] >= 512:
				red = '\033[1;31m' + str(_list[j]) + '\033[0m'
				_str += (' ' + red + '')

			else:
				_str += (' ' + str(_list[j]) + '')

		print(_str)


def if_over():
	'''判断游戏是否结束'''
	global list

	_isNotOver = False

	# list = [[1, 2, 3, 4],
	# 		  [5, 6, 7, 8],
	# 	   	  [1, 2, 3, 4],
	# 		  [5, 6, 7, 8]]

	# 先判定横向是否相同
	for i in list:
		for j in range(3):
			if i[j] == i[j + 1]:
				_isNotOver = True

			elif i[j] == 0:
				_isNotOver = True
			
			else:
				continue

	# 再判定纵向是否相同
	for i in range(3):
		l = list[i]
		for j in range(4):
			_l = list[i + 1]
			if l[j] == _l[j]:
				_isNotOver = True
			
			elif l[j] == 0 or _l[j] == 0:
				_isNotOver = True

			else:
				continue

	if not _isNotOver:
		print("---------\nGame Over!")
		sys.exit()
  	

def move_up():
	'''上移动'''
	global list

	# list = [[0, 0, 0, 0],
	# 	[0, 0, 0, 0],
	# 	[0, 0, 0, 0],
	# 	[0, 0, 0, 0]]

	for n in range(3):
		for i in range(3):
			_list = list[i]
			_nextList = list[i + 1]
			for j in range(4):
				if _list[j] == _nextList[j]:
					if _list[j] * 2 == _list[j] + _nextList[j]:
						_list[j] += _list[j]
						_nextList[j] = 0

				elif _list[j] == 0:
					_list[j] = _nextList[j]
					_nextList[j] = 0

				else:
					continue

	print('---------\n{:^9}'.format('Up!'))


def move_left():
	'''左移动'''
	global list

	# list = [[0, 0, 0, 0],
	# 	[0, 0, 0, 0],
	# 	[0, 0, 0, 0],
	# 	[0, 0, 0, 0]]

	for n in range(3):
		for i in range(4):
			_list = list[i]
			for j in range(3):
				if _list[j] == _list[j + 1]:
					if _list[j] * 2 == _list[j] + _list[j + 1]:
						_list[j] += _list[j]
						_list[j + 1] = 0

				if _list[j] == 0:
					_list[j] = _list[j + 1]
					_list[j + 1] = 0

				else:
					continue

	print('---------\n{:^9}'.format('Left!'))


def move_right():
	'''右移动'''
	global list
	
	# list = [[0, 0, 0, 0],
	# 	[0, 0, 0, 0],
	# 	[0, 0, 0, 0],
	# 	[0, 0, 0, 0]]

	for n in range(3):
		for i in range(4):
			_list = list[i]
			for j in range(3):
				if _list[j] == _list[j + 1]:
					if _list[j + 1] * 2 == _list[j] + _list[j + 1]:
						_list[j + 1] += _list[j + 1]
						_list[j] = 0

				if _list[j + 1] == 0:
					_list[j + 1] = _list[j]
					_list[j] = 0

				else:
					continue

	print('---------\n{:^9}'.format('Right!'))


def move_down():
	'''下移动'''
	global list

	# list = [[0, 0, 0, 0],
	# 	[0, 0, 0, 0],
	# 	[0, 0, 0, 0],
	# 	[0, 0, 0, 0]]

	for n in range(3):
		for i in range(3):
			_list = list[i]
			_nextList = list[i + 1]
			for j in range(4):
				if _nextList[j] == _list[j]:
					if _nextList[j] * 2 == _list[j] + _nextList[j]:
						_nextList[j] += _nextList[j]
						_list[j] = 0

				elif _nextList[j] == 0:
					_nextList[j] = _list[j]
					_list[j] = 0

				else:
					continue

	print('---------\n{:^9}'.format('Down!'))


def do_move():
	'''解析返回值并移动'''
	global isNotOver

	while True:
		result = input('---------\n{:^8}'.format('Move > '))

		# 上移动
		if result == 'w':
			move_up()
			break

		# 左移动
		elif result == 'a':
			move_left()
			break

		# 右移动
		elif result == 'd':
			move_right()
			break

		# 下移动
		elif result == 's':
			move_down()
			break

		# 退出
		elif result == 'q':
			print('---------\n{:^9}'.format('Over!'))
			sys.exit()

		else:
			print('---------\n{:^9}'.format('Wrong!'))


while True:
	add_number()
	print_list()
	if_over()
	do_move()
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值