Update README.md

main
hnu202309010107 9 months ago
parent 584615abe6
commit e3f2da32d8

@ -1,240 +1,260 @@
# 2048 # 2048
import turtle, random import sys
# 定义一个类,用来画除了数字方块之外的图形 import random
class BackGround(turtle.Turtle): import pygame
def __init__(self):
super().__init__() pygame.init() # 初始化pygame()
self.penup() size = 75 # 每个游戏方格的大小
self.ht() queue = 4 # 初始设置为4X4阵列
def draw_block(self): game_lis = [] # 存放
self.shape('bg.gif') # 画出背景方块 background_color = (255, 239, 213) # 背景颜色
for i in allpos: Dividingline_color = (255, 222, 173) # 分割线颜色
self.goto(i) Dividingline_width = 15 # 分割线宽度
self.stamp()
self.color('white', 'white') # 画出其他背景 score_height = 75 # 得分区的高度
self.goto(-215, 120) score_width = 75 # 得分区的宽度
self.begin_fill() score_color = (205, 193, 180) # 得分字体颜色
self.goto(215, 120) font1 = pygame.font.SysFont('SimHei', 50) # 得分区域字体显示黑体24
self.goto(215, 110) font_pos_x = Dividingline_width # 得分区域字体位置的X坐标
self.goto(-215, 110) font_pos_y = int(font1.size('得分')[1]) # 得分区域字体位置的Y坐标
self.end_fill() score = 0 # 得分初始值为0
self.shape('title.gif')
self.goto(-125, 210) font3 = pygame.font.Font(None, 50) # 数字字体
self.stamp() black = (0, 0, 0) # 数字颜色
self.shape('score.gif')
self.goto(125, 245) screen_height = (((size + Dividingline_width) * queue) + score_height + Dividingline_width * 2) # 屏幕高度
self.stamp() screen_width = (((size + Dividingline_width) * queue) + Dividingline_width) # 屏幕宽度
self.shape('top_score.gif')
self.goto(125, 170) colors = {0: (205, 193, 180), # 各种方块对应的颜色
self.stamp() 2: (238, 228, 218),
4: (237, 224, 200),
# 游戏失败及达成2048的提示文字 8: (242, 177, 121),
def judge(self): 16: (245, 149, 99),
global flag_win, flag_win_lose_text 32: (246, 124, 95),
self.color('blue') 64: (246, 94, 59),
judge = 0 # 判断是否还有位置可以移动 128: (237, 207, 114),
for i in block_dic.values(): 256: (237, 204, 98),
for j in block_dic.values(): 512: (237, 200, 80),
if i.num == 0 or i.num == j.num and i.distance(j) == 100: 1024: (237, 197, 63),
judge += 1 2048: (225, 187, 0)}
if judge == 0: # 无位置可移动,游戏失败
self.write(' GAME OVER\n重新开始请按空格键', align='center', font=('黑体', 30, 'bold'))
flag_win_lose_text = False # 绘制背景
if flag_win is True: # 此条件让2048达成的判断只能进行一次 def _draw_background(screen):
for k in block_dic.values(): screen.fill(background_color)
if k.num == 2048: # 游戏达成 Dividingline_width_half = int(Dividingline_width / 2)
flag_win = False Difference = score_height + Dividingline_width + int(Dividingline_width / 2)
self.write(' 达成2048\n继续游戏请按回车键', align='center', font=('黑体', 30, 'bold'))
flag_win_lose_text = False for i in range(queue + 1): # 绘制横线
pygame.draw.line(screen, Dividingline_color,
def win_lose_clear(self): (0, i * (size + Dividingline_width) + Difference),
global flag_win_lose_text (screen_height, i * (size + Dividingline_width) + Difference),
self.clear() Dividingline_width)
flag_win_lose_text = True
for j in range(queue + 1): # 绘制横线
def show_score(self): # 分值的显示 pygame.draw.line(screen, Dividingline_color,
global score, top_score (Dividingline_width_half + j * (size + Dividingline_width), Difference),
if score > top_score: (Dividingline_width_half + j * (size + Dividingline_width), screen_height),
top_score = score Dividingline_width)
with open('.\\score.txt', 'w') as f:
f.write(f'{top_score}')
self.color('white') # 绘制得分区域
self.goto(125, 210) def _draw_score(screen, font, pos_x, pos_y):
self.clear() global score
self.write(f'{score}', align='center', font=('Arial', 20, 'bold')) print_text(screen, font, pos_x, 10, f'得分')
self.goto(125, 135) print_text(screen, font, pos_x, pos_y + 6, f'{score}')
self.write(f'{top_score}', align='center', font=('Arial', 20, 'bold'))
# 数字方块类 # 得分区域
class Block(turtle.Turtle): def print_text(screen, font, x, y, text):
def __init__(self): imgText = font.render(text, True, score_color)
super().__init__() screen.blit(imgText, (x, y))
self.ht()
self.penup()
self.num = 0 # 初始化游戏列表存放2048数字
def _game_list():
def draw(self): for i in range(queue): # 初始设置全为0
self.clear() lis = []
dic_draw = {2: '#eee6db', 4: '#efe0cd', 8: '#f5af7b', for j in range(queue):
16: '#fb9660', 32: '#f57d5a', 64: '#f95c3d', lis.append(0)
128: '#eccc75', 256: '#eece61', 512: '#efc853', game_lis.append(lis)
1024: '#ebc53c', 2048: '#eec430', 4096: '#aeb879',
8192: '#aab767', 16384: '#a6b74f'}
if self.num > 0: # 数字大于0画出方块 # 显示游戏区域:游戏方块和数字
self.color(f'{dic_draw[self.num]}') # 选择颜色 def _show_game(screen):
self.begin_fill() for i in range(queue):
self.goto(self.xcor()+48, self.ycor()+48) for j in range(queue):
self.goto(self.xcor()-96, self.ycor()) rect_color = colors[game_lis[i][j]] # 取出字典colors对应的值
self.goto(self.xcor(), self.ycor()-96) rect_position = [(j + 1) * Dividingline_width + j * size, # 色块的位置
self.goto(self.xcor()+96, self.ycor()) Dividingline_width * (i + 2) + size * i + score_height]
self.goto(self.xcor(), self.ycor()+96) pygame.draw.rect(screen, rect_color, (rect_position, (size, size)), 0) # 绘制色块
self.end_fill()
self.goto(self.xcor()-48, self.ycor()-68) if game_lis[i][j] != 0: # 只有数列里不为0的时候才会输出
if self.num > 4: # 按照数字选择数字的颜色 textSurfaceObj = font3.render(str(game_lis[i][j]), True, black) # get_rect()方法返回rect对象
self.color('white') textRectObj = textSurfaceObj.get_rect()
else: rect_position = [(j + 1) * Dividingline_width + (j + 0.5) * size, # 数字的位置
self.color('#6d6058') Dividingline_width * (i + 2) + size * (i + 0.5) + score_height]
self.write(f'{self.num}', align='center', font=('Arial', 27, 'bold')) textRectObj.center = tuple(rect_position)
self.goto(self.xcor(), self.ycor()+20) screen.blit(textSurfaceObj, textRectObj)
class Game(): # 在随机位置产生随机数
def init(self): def _random():
back = BackGround() # 实例画出游戏的背景 random_num = random.randint(1, 2) # 随机数
back.draw_block() num = pow(2, random_num)
for i in allpos: # 画出16个海龟对应16个数字块 random_pos_x = random.randint(0, queue - 1) # 随机X坐标
block = Block() random_pos_y = random.randint(0, queue - 1) # 随机Y坐标
block.goto(i) if game_lis[random_pos_x][random_pos_y] == 0:
block_dic[i] = block game_lis[random_pos_x][random_pos_y] = num
game.grow() else:
_random()
def restart(self): # 重开游戏的方法
global score, flag_win_lose_text
score = 0 # 捕获键盘LEFT操作
for i in block_dic.values(): def _LEFT():
i.num = 0 global score
i.clear() for i in range(queue):
win_lose_text.clear() while 0 in game_lis[i]:
game.grow() game_lis[i].remove(0)
flag_win_lose_text = True # 此flag为游戏达成或失败出现提示语后的判断要提示语被clear后才能继续move for j in range(len(game_lis[i]) - 1):
if game_lis[i][j] == game_lis[i][j + 1]:
def grow(self): # 随机出现一个2或4的数字块 game_lis[i][j] = game_lis[i][j] + game_lis[i][j + 1]
block_list = [] score = score + game_lis[i][j]
for i in allpos: game_lis[i][j + 1] = 0
if block_dic[i].num == 0: while 0 in game_lis[i]:
block_list.append(block_dic[i]) # 挑出空白方块的海龟 game_lis[i].remove(0)
turtle_choice = random.choice(block_list) # 随机选中其中一个海龟 lis = []
turtle_choice.num = random.choice([2, 2, 2, 2, 4]) # 赋属性num=2/4 for j in range(queue - len(game_lis[i])):
turtle_choice.draw() lis.append(0)
win_lose_text.judge() game_lis[i] = game_lis[i] + lis
show_score_text.show_score()
ms.update()
# 捕获键盘RIGHT操作
def move_up(self): def _RIGHT():
allpos1 = allpos[::4] # 切片为四列 global score
allpos2 = allpos[1::4] for i in range(queue):
allpos3 = allpos[2::4] while 0 in game_lis[i]:
allpos4 = allpos[3::4] game_lis[i].remove(0)
self.move_move(allpos1, allpos2, allpos3, allpos4) for j in range(len(game_lis[i]) - 1, 0, -1):
if game_lis[i][j] == game_lis[i][j - 1]:
def move_down(self): game_lis[i][j] = game_lis[i][j] + game_lis[i][j - 1]
allpos1 = allpos[-4::-4] score = score + game_lis[i][j]
allpos2 = allpos[-3::-4] game_lis[i][j - 1] = 0
allpos3 = allpos[-2::-4] while 0 in game_lis[i]:
allpos4 = allpos[-1::-4] game_lis[i].remove(0)
self.move_move(allpos1, allpos2, allpos3, allpos4) lis = []
for j in range(queue - len(game_lis[i])):
def move_left(self): lis.append(0)
allpos1 = allpos[:4] game_lis[i] = lis + game_lis[i]
allpos2 = allpos[4:8]
allpos3 = allpos[8:12]
allpos4 = allpos[12:16] # 获取键盘UP操作
self.move_move(allpos1, allpos2, allpos3, allpos4) def _UP():
global score
def move_right(self): lis = [[0, 0, 0, 0],
allpos1 = allpos[-1:-5:-1] [0, 0, 0, 0],
allpos2 = allpos[-5:-9:-1] [0, 0, 0, 0],
allpos3 = allpos[-9:-13:-1] [0, 0, 0, 0]]
allpos4 = allpos[-13:-17:-1] for i in range(queue): # 整个列表顺时针旋转90度
self.move_move(allpos1, allpos2, allpos3, allpos4) for j in range(queue):
lis[i][j] = game_lis[queue - 1 - j][i]
def move_move(self, allpos1, allpos2, allpos3, allpos4):
if flag_win_lose_text is True: for i in range(queue):
count1 = self.move(allpos1) # 四列或四行依次移动 while 0 in lis[i]:
count2 = self.move(allpos2) lis[i].remove(0)
count3 = self.move(allpos3) for j in range(len(lis[i]) - 1, 0, -1):
count4 = self.move(allpos4) if lis[i][j] == lis[i][j - 1]:
if count1 or count2 or count3 or count4: # 判断是否有方块移动,有才能继续出现新的数字块 lis[i][j] = lis[i][j] + lis[i][j - 1]
self.grow() score = score + lis[i][j]
lis[i][j - 1] = 0
def move(self, pos_list): while 0 in lis[i]:
num_list = [] # 为某一列或行的数字块海龟的坐标 lis[i].remove(0)
for i in pos_list: list1 = []
num_list.append(block_dic[i].num) # 把这些海龟的NUM形成list for j in range(queue - len(lis[i])):
new_num_list, count = self.list_oper(num_list) # 只是list_oper的方法形成新的list list1.append(0)
for j in range(len(new_num_list)): # 把新的list依次赋值给对应的海龟.num属性并调用draw()方法 lis[i] = list1 + lis[i]
block_dic[pos_list[j]].num = new_num_list[j]
block_dic[pos_list[j]].draw() for i in range(queue): # 整个列表逆时针旋转90度
return count for j in range(queue):
game_lis[i][j] = lis[j][queue - 1 - i]
def list_oper(self, num_list): # num_list的操作假设其为【2,0,2,2】
global score
count = True # 获取键盘DOWN操作
temp = [] def _DOWN():
new_temp = [] global score
for j in num_list: lis = [[0, 0, 0, 0],
if j != 0: [0, 0, 0, 0],
temp.append(j) # temp=[2,2,2] [0, 0, 0, 0],
flag = True [0, 0, 0, 0]]
for k in range(len(temp)):
if flag: for i in range(queue): # 整个列表顺时针旋转90度
if k < len(temp)-1 and temp[k] == temp[k+1]: for j in range(queue):
new_temp.append(temp[k]*2) lis[i][j] = game_lis[queue - 1 - j][i]
flag = False
score += temp[k] for i in range(queue):
while 0 in lis[i]:
lis[i].remove(0)
for j in range(len(lis[i]) - 1):
if lis[i][j] == lis[i][j + 1]:
lis[i][j] = lis[i][j] + lis[i][j + 1]
score = score + lis[i][j]
lis[i][j + 1] = 0
while 0 in lis[i]:
lis[i].remove(0)
list1 = []
for j in range(queue - len(lis[i])):
list1.append(0)
lis[i] = lis[i] + list1
for i in range(queue): # 整个列表逆时针旋转90度
for j in range(queue):
game_lis[i][j] = lis[j][queue - 1 - i]
# 主函数
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: else:
new_temp.append(temp[k]) # new_temp=[4,2] print("False")
else: pygame.display.flip() # 更新游戏
flag = True _draw_background(screen) # 绘制背景
for m in range(len(num_list)-len(new_temp)): _random() # 产生随机数
new_temp.append(0) # new_temp=[4,2,0,0] _show_game(screen) # 显示游戏方块和数字
if new_temp == num_list: _draw_score(screen, font1, font_pos_x, font_pos_y) # 绘制得分区
count = False # 此变量判断num_list没有变化数字块无移动
return(new_temp, count)
if __name__ == '__main__': if __name__ == '__main__':
ms = turtle.Screen() # 主窗口的设置 main()
ms.setup(430, 630, 400, 50)
ms.bgcolor('gray')
ms.title('2048')
ms.tracer(0)
ms.register_shape('bg.gif')
ms.register_shape('title.gif')
ms.register_shape('score.gif')
ms.register_shape('top_score.gif')
block_dic = {} # 放数字方块海龟的字典位置坐标为key,对应海龟为value
allpos = [(-150, 50), (-50, 50), (50, 50), (150, 50),
(-150, -50), (-50, -50), (50, -50), (150, -50),
(-150, -150), (-50, -150), (50, -150), (150, -150),
(-150, -250), (-50, -250), (50, -250), (150, -250)]
flag_win = True # 达成2048的判断让达成的文字仅出现一次
flag_win_lose_text = True # 用来判断失败或成功的提示文字是否有被清除,不清除不能继续移动方块
score = 0
with open('.\\score.txt', 'r') as f:
top_score = int(f.read()) # 读取score中的数据
show_score_text = BackGround()
win_lose_text = BackGround()
game = Game()
game.init()
ms.listen()
ms.onkey(game.move_up, 'Up')
ms.onkey(game.move_down, 'Down')
ms.onkey(game.move_left, 'Left')
ms.onkey(game.move_right, 'Right')
ms.onkey(win_lose_text.win_lose_clear, 'Return')
ms.onkey(game.restart, 'space')
ms.mainloop()

Loading…
Cancel
Save