2048游戏的算法参考:2048 Game in Python - GeeksforGeeks
以下是2048游戏的简化版, 在python 3.9运行没有问题。 没有用精美的图像, 主要是原理的实现
import random
import pygame
import numpy as np
WINDOW_WIDTH, WINDOW_HEIGHT = 600, 601
CELL_SIZE = 60
#ref:https://www.geeksforgeeks.org/2048-game-in-python/
class mat2048():
def __init__(self,screen):
self.reset_mat()
self.screen=screen
self.bg_cell=None
self.font = pygame.font.SysFont('comicsans', 40)
def reset_mat(self):
self.mat=[[0 for c in range(4)] for r in range(4)]
self.add2()
def add2(self):
r = random.randint(0, 3)
c = random.randint(0, 3)
while (self.mat[r][c]!=0):
r = random.randint(0, 3)
c = random.randint(0, 3)
self.mat[r][c]=2
def compress(self):
new_mat=[[0 for c in range(4)] for r in range(4)]
changed = False
for i in range(4):
pos = 0
for j in range(4):
if(self.mat[i][j] != 0):
new_mat[i][pos] = self.mat[i][j]
if(j != pos):
changed = True
pos += 1
self.mat=new_mat
return changed
def merge(self):
changed = False
for i in range(4):
for j in range(3):
if(self.mat[i][j] == self.mat[i][j + 1] and self.mat[i][j] != 0):
self.mat[i][j] = self.mat[i][j] * 2
self.mat[i][j + 1] = 0
changed = True
return changed
def move(self,key):
action = { pygame.K_DOWN: self.move_down,
pygame.K_LEFT: self.move_left,
pygame.K_RIGHT: self.move_right,
pygame.K_UP: self.move_up }
changed=action[key]()
if changed: self.add2()
def move_left(self):
changed1= self.compress()
changed2 =self. merge()
changed = changed1 or changed2
temp = self.compress()
return changed
def move_right(self):
self.reverse()
changed=self.move_left()
self.reverse()
return changed
def move_up(self):
self.transpose()
changed=self.move_left()
self.transpose()
return changed
def move_down(self):
self.transpose()
changed=self.move_right()
self.transpose()
return changed
def reverse(self):
new_mat=[]
for i in self.mat:
new_mat.append(i[::-1])
self.mat=new_mat
def transpose(self):
new_mat = []
for i in range(4):
new_mat.append([])
for j in range(4):
new_mat[i].append(self.mat[j][i])
self.mat= new_mat
def draw_cell(self,x,y):
self.bg_cell=pygame.Surface((CELL_SIZE,CELL_SIZE))
self.bg_cell.fill((255,255,255))
rect=self.bg_cell.get_rect(topleft=(x,y))
pygame.draw.rect(self.bg_cell,'blue',(0, 0, CELL_SIZE, CELL_SIZE),1)
self.screen.blit(self.bg_cell, rect)
def draw_text(self,text, x, y, color):
text_surface = self.font.render(text, True, color)
text_rect = text_surface.get_rect()
text_rect.topleft = (x+10, y+10)
self.screen.blit(text_surface, text_rect)
def mat_update(self):
x0,y0=100,100
for i in range(4):
for j in range(4):
x=x0+i*CELL_SIZE
y=y0+j*CELL_SIZE
self.draw_cell(x,y)
d=self.mat[j][i]
c=(d*50%255,255-d*10%150,(d+100)%100)
if d==0:
txt=''
else:
txt=str(d)
self.draw_text(txt,x,y,c)
def checkEvents(mat):
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
if event.type==pygame.KEYDOWN:
if event.key in [pygame.K_LEFT, pygame.K_UP,pygame.K_RIGHT, pygame.K_DOWN]:
mat.move(event.key)
pygame.init()
screen=pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
A=mat2048(screen)
run=True
while run:
checkEvents(A)
A.mat_update()
pygame.display.update()