自己实现的2048

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

# _*_utf-8_*_
SCREEN_SIZE = (640,480)
import pygame
from pygame.locals import *
from gameobjects.vector2 import Vector2
from sys import exit
import random

background_filename = 'C:\\python32\\sushiplate.jpg'
image_file_name = '2048-1.jpg'



class Cell(object):
    '''
    this is for one small cell,
    can hold the file path, 
    '''
    def __init__(self, num, pos_x, pos_y):
        self.num = num
        self.file_name = '2048-' + str(num) + '.jpg'
        self.position = Vector2(pos_x, pos_y)
        self.image = pygame.image.load(self.file_name).convert()
    def get(self):
        return self.num
    def set(self,num):
        self.num = num
        self.file_name = '2048-' + str(num) + '.jpg'
        self.image = pygame.image.load(self.file_name).convert()
    def add(self, cell):
        self.num += cell.num
        self.set(self.num)  
    def isEqual(self, instanse):
        if self.num == instanse.num:
            return True
        else:
            return False         
    def draw(self):
        screen.blit(self.image, self.position)
    def clear(self):
        self.num = 0
        self.set(self.num)
        
def randomPlace(cells):
    ran_num = [2,4]
    place = random.choice(range(16))
    for cell in cells:
        if cell.num == 0:
            break
    else:
        return
    
    if cells[place].num == 0:
        cells[place].set(random.choice(ran_num))
        return
    else:
        randomPlace(cells)

def gameInit(cells):
    
    Start = 0
    if len(cells) == 0:
        for i in range(16):
            c = i/4
            y = i % 4
            cells.append(Cell(Start,y*60 + 100 , c * 60 + 100))
    '''
    else:
        for i in range(16):
            cells[i].set(Start)
    cells[8].clear()
    cells[10].clear()
    cells[1].clear()
    cells[15].clear()
    '''
   
    randomPlace(cells)
    randomPlace(cells)
    
def display(screen, background, cells):
    screen.blit(background,(0,0))
    
    for i in range(16):
        cells[i].draw()
    
def move_x(cells, flag):
    if not flag:# to left move
        for k in range(4): #move by row
            i = k *4
            while i < k*4 + 4:
                if cells[i].num != 0:
                    i += 1
                    continue
                j = i + 1
                while j < k*4 +4:
                    if cells[j].num == 0:
                        j += 1
                        continue
                    else:
                        cells[i].set(cells[j].num)
                        cells[j].clear()
                        j += 1
                        i += 1
                        break
                else:
                    i += 1
    else:
        for k in range(4):
            i = k*4+3
            while  i >=k*4:
                if cells[i].num != 0:
                    i -= 1
                    continue
                j = i-1
                while j >=k*4:
                    if cells[j].num == 0:
                        j -= 1
                        continue
                    else:
                        cells[i].set(cells[j].num)
                        cells[j].clear()
                        j -= 1
                        i -= 1
                        break
                else: # this is for while
                    i -= 1
                    
                    
def move_y(cells, flag):
    if not flag:
        for k in range(4):
            
            for i in range(3):
                if cells[i*4 + k].num != 0:
                    i += 1
                    continue
                j = i +1
                while j < 4:
                    if cells[j*4 + k].num == 0:
                        j += 1
                        continue
                    else:
                        cells[i*4 + k].set(cells[j*4 + k].num)
                        cells[j*4 + k].clear()
                        i += 1
                        j += 1
                else:# this is for while
                    i += 1
    elif flag:
        for k in range(4):
            
            i = 3
            while i >= 0:
                if cells[i*4 + k].num != 0:
                    i -= 1
                    continue
                j = i - 1
                while j >= 0:
                    if cells[j*4 + k].num == 0:
                        j -= 1
                        continue
                    else:
                        cells[i*4 + k].set(cells[j*4 + k].num)
                        cells[j*4 + k].clear()
                        i -= 1
                        j -= 1
                else:
                    i -= 1
                
if __name__ == '__main__':
  
    pygame.init()
    screen = pygame.display.set_mode(SCREEN_SIZE, 0, 32)
    
    background = pygame.image.load(background_filename).convert()
    
    cells = []
   
    gameInit(cells)
    
    deal_flag = True
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                exit()
            if event.type == KEYDOWN and event.key == K_ESCAPE:
                pygame.quit()
                exit()
            if event.type == KEYUP:
                deal_flag = True
            
        #to store all the pressed keys
        pressed_keys = pygame.key.get_pressed()
        # the space key to reset the game
        if pressed_keys[K_SPACE]:
            gameInit(cells)
        # Left button is to move left
        if pressed_keys[K_LEFT] and deal_flag:
            deal_flag = False
            move_x(cells, 0)  
            for k in range(4):              
                i = k*4
                while i < k*4+3:
                    if cells[i].isEqual(cells[i+1]):
                        cells[i].add(cells[i+1])
                        cells[i+1].clear()  
                    i += 1
            move_x(cells, 0)
            randomPlace(cells)
        # right button is to move right
        if pressed_keys[K_RIGHT] and deal_flag:
            deal_flag = False
            move_x(cells, 1)
            for k  in range(4):
                i = k*4+3
                while i > k*4:
                    if cells[i].isEqual(cells[i-1]):
                        cells[i].add(cells[i-1])
                        cells[i-1].clear()
                    i -= 1
            move_x(cells, 1)
            randomPlace(cells)
        # up button is to move up
        if pressed_keys[K_UP] and deal_flag:
            deal_flag = False
            move_y(cells, 0)
            for k in range(4):
                for i in range(3):
                    if cells[i*4 + k].isEqual(cells[(i+1)*4 + k]):
                        cells[i*4 + k].add(cells[(i+1)*4 + k])
                        cells[(i+1)*4 + k].clear()
            move_y(cells, 0)
            randomPlace(cells)
        # down button is to move down
        if pressed_keys[K_DOWN] and deal_flag:
            deal_flag = False
            move_y(cells, 1)
            for k in range(4):
                
                for i in range(3, -1, -1):
                    if cells[i*4 + k].isEqual(cells[(i-1)*4 + k]):
                        cells[i*4 + k].add(cells[(i-1)*4 + k])
                        cells[(i-1)*4 + k].clear()
            move_y(cells, 1)
            randomPlace(cells)
    
    
        display(screen, background, cells)
        
        pygame.display.update()