Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pygame之2048.

Similar presentations


Presentation on theme: "Pygame之2048."— Presentation transcript:

1 Pygame之2048

2 一、方块对象 class Block(): def __init__(self): #随机数值 self.number = random.choice([2,2,2,4]) #根据数值加载对应的图片 self.image = pygame.image.load("Block_%d.png" % self.number) def changeNumber(self, number): #改变数字的同时改变图片 self.number = number self.image = pygame.image.load("Block_%d.png" % number)

3 二、方格盘 theBoard = [ [0,0,0,0] ] 用x表示横坐标,y表示纵坐标 theBoard[y][x]来表示一个格 子

4 二、方格盘 生成空方格盘函数: def GenerateNewBoard(): newBoard = [] #将棋盘初始化为元素都为0的4x4的二维列 表 for i in range(4): newBoard.append([0, 0, 0, 0]) return newBoard theBoard = GenerateNewBoard()

5 三、移动棋盘 def SildeBoard(board, direction): #删除所有的0 y = 0 zeroNumber = [0, 0, 0, 0] while y < len(board): x = 0 while x < len(board[y]): if board[y][x] == 0: board[y].pop(x) zeroNumber[y] += 1 x -= 1 x += 1 y += 1 …… [2] [2,4] [] []

6 三、移动棋盘 def SildeBoard(board, direction): …… #向左滑动 if direction == "LEFT": for y in range(4): for num in range(zeroNumber[y]): board[y].append(0) #向右滑动 elif direction == "RIGHT": board[y].insert(0, 0) [2,0,0,0] [0,0,0,2] [2,4,0,0] [0,0,2,4] [0,0,0, 0] [0,0,0, 0]

7 三、移动方块 def SlideBoard(board, direction): #先逆时针旋转方格盘 if direction ==“UP”or direction ==“DOWN”: board = RotateBoard(board, “COUNTERCLOSEWISE”) …… elif direction == “UP”: #向上移动 for y in range(4): for num in zeroNumber[y]: board[y].append(0) board = RotateBoard(board, “CLOSEWISE”) elif direction == “DOWN”: #向下移动 board[y].insert(0, 0) return board 原上方 原下方

8 四、旋转方格盘 [0,0,0,2] [0,0,2,4] [0,0,0,0] [2,4,0,0] [0,2,0,0] [0,0,0,0] def RotateBoard(board, direction): newBoard = [] #逆时针旋转 if direction == “COUNTERCLOCKWISE”: for x in range(-1, -5, -1): newLine = [] for y in range(4): newline.append(board[y][x]) newBoard.append(newline) #顺时针旋转 elif direction == “CLOCKWISE”: for x in range(4): for y in range(-1, -5, -1): return newBoard 原上方 原下方

9 五、合并方格 def MergeBlock(board, direction): #向左合并 for y in range(4): for x in range(3): if board[y][x] == board[y][x+1]: board[y][x].changeNumber(board[y][x].number*2) board[y][x+1] = 0 #向右合并 for x in range(-1, -4, -1): if board[y][x] == board[y][x-1]: board[y][x-1] = 0

10 五、合并方格 def MergeBlock(board, direction): #向上合并 if direction == “UP”: board = RotateBoard(board, “COUNTERCLOCKWISE”) for y in range(4): for x in range(3): if board[y][x] == board[y][x+1]: board[y][x].changeNumber(board[y][x].number*2) board[y][x+1] = 0 board = RotateBoard(board, “CLOCKWISE”)

11 五、合并方格 def MergeBlock(board, direction): #向下合并 if direction == “DOWN”: board = RotateBoard(board, “COUNTERCLOCKWISE”) for y in range(4): for x in range(-1, -4, -1): if board[y][x] == board[y][x-1]: board[y][x].changeNumber(board[y][x].number*2) board[y][x-1] = 0 board = RotateBoard(board, “CLOCKWISE”)

12 五、合并方格 def MergeBlock(board, direction): #向下合并 if direction == “LEFT”: …… board = SlideBoard(board, “LEFT”) elif direction == “UP”: board = RotateBoard(board, “CLOCKWISE”) return board

13 六、绘制棋盘 def DrawBoard(board, Screen): #载入背景和边框图片 background = pygame.image.load('background.png') border = pygame.image.load('border.png') #绘制背景 Screen.blit(background, [0,0]) #绘制所有方块 for y in range(4): for x in range(4): if board[y][x] != 0: Screen.blit(board[y][x].image, [x*BLOCK_WIDTH, y*BLOCK_WIDTH]) #绘制边框 Screen.blit(border, [0,0]) #返回绘制后的界面 return Screen

14 七、生成新方块 def GenerateNewBlock(board): #空位置,用于从中随机挑选出一个位置作为新方块的位置 emptyPos=[] for y in range(4): for x in range(4): #如果位置是空的,加入到空位置列表当中 if board[y][x] == 0 : emptyPos.append([y,x]) #从空位当中随机挑一个作为新方块的位置 newPos = random.choice(emptyPos) board[newPos[0]][newPos[1]] = Block() #将生成新方块后的棋盘当做结果返回 return board

15 八、初始化游戏 theBoard = GenerateNewBoard()
#新建棋盘 theBoard = GenerateNewBoard() #生成初始的两个方块 theBoard = GenerateNewBlock(theBoard) #绘制界面 screen = DrawBoard(theBoard, screen) #刷新界面 pygame.display.flip()

16 九、消息循环 running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN: direction = "" pressed = False if event.key == pygame.K_UP: direction = "UP" pressed = True elif event.key == pygame.K_DOWN: direction = "DOWN" elif event.key == pygame.K_LEFT: direction = "LEFT" elif event.key == pygame.K_RIGHT: direction = "RIGHT" if pressed: theBoard = SildeBoard(theBoard, direction) theBoard = MergeBlock(theBoard, direction) theBoard = GenerateNewBlock(theBoard) screen = DrawBoard(theBoard, screen) pygame.display.flip()


Download ppt "Pygame之2048."

Similar presentations


Ads by Google