好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > Python實現簡單的2048小游戲

Python實現簡單的2048小游戲

熱門標簽:舉辦過冬奧會的城市地圖標注 400電話申請資格 正安縣地圖標注app 阿里電話機器人對話 地圖地圖標注有嘆號 qt百度地圖標注 電銷機器人系統廠家鄭州 螳螂科技外呼系統怎么用 遼寧智能外呼系統需要多少錢

本文實例為大家分享了Python實現簡單的2048小游戲的具體代碼,供大家參考,具體內容如下

運行效果:

1.項目結構

2.代碼

configs.py

import argparse

def parse_args():
 parser = argparse.ArgumentParser(description='Game 2048')

 # Form
 """
 screen_width: Width of the form
 screen_height: Height of the form
 """
 parser.add_argument('--screen_width', default=400)
 parser.add_argument('--screen_height', default=500)

 # Block
 """
 block_gap: Gap between two blocks
 block_size: Size of a block
 block_arc: Arc of a block
 """
 parser.add_argument('--block_gap', default=10)
 parser.add_argument('--block_size', default=86)
 parser.add_argument('--block_arc', default=10)

 return parser.parse_args()

main.py

import configs
from Game2048 import Game2048


def main(args):
 """
 screen_width: Width of the form
 screen_height: Height of the form
 block_gap: Gap between two blocks
 block_size: Size of a block
 """
 screen_width = args.screen_width
 screen_height = args.screen_height
 block_gap = args.block_gap
 block_size = args.block_size
 block_arc = args.block_arc

 game = Game2048(screen_width, screen_height, block_gap, block_size, block_arc)
 game.Form()


if __name__ == '__main__':
 args = configs.parse_args()
 main(args)

Game2048.py

import os
import sys
import numpy
import random
import pygame

"""
Form(): 窗口的設置
Action(): 用戶行為: 按鍵/鼠標
InitGame(): 游戲初始化
CreatNum(): 隨機在一個位置生成一個數
GetEmpty(): 獲取空白方格
MoveUp(): 向上移動
MoveDown(): 向下移動
MoveLeft(): 向左移動
MoveRight(): 向右移動
JudgeGameOver(): 判斷游戲是否結束
JudgeGameSuccess(): 判斷游戲是否成功
Paint(): 繪制表格
"""


class Game2048(object):
 # 初始化函數
 def __init__(self, screen_width, screen_height, block_gap, block_size, block_arc):
 """
 :param screen_width: Width of the form
 :param screen_height: Height of the form
 :param block_gap: Gap between two blocks
 :param block_size: Size of a block
 :param size: Dimension of matrix
 :param martix: Zero matrix
 :param is_over: Sign of the end of the game
 :param is_success: Sign of the success of the game
 :param form: The form
 :param score: score
 :param title_font: Title type and size of form
 :param score_font: Scores type and size
 :param tips_font: Tips type and type
 :param font: The numberes
 :param isadd: Add number or not
 """
 """ 窗口 """
 self.screen_width = screen_width # 窗口的寬 400
 self.screen_height = screen_height # 窗口的高 500
 self.block_gap = block_gap # 方塊間隙 10
 self.block_size = block_size # 方塊大小 86
 self.block_arc = block_arc # 方塊的弧度
 self.size = 4 # 矩陣 4 * 4
 self.martix = [] # 初始化矩陣 4 * 4 的 0 矩陣
 self.form = ''

 """ 其他 """
 self.is_over = False # 游戲是否結束
 self.is_success = False # 游戲是否成功
 self.score = 0 # 分數
 self.isadd = True # 是否添加數字
 self.block_color = { # 方塊顏色
  0: (205, 193, 180),
  2: (238, 228, 218),
  4: (237, 224, 200),
  8: (242, 177, 121),
  16: (245, 149, 99),
  32: (246, 124, 95),
  64: (246, 94, 59),
  128: (237, 207, 114),
  256: (237, 204, 97),
  512: (237, 200, 80),
  1024: (237, 197, 63),
  2048: (237, 194, 46)
 }
 self.nums_color = {
  # 0: (0, 0, 0),
  0: (205, 193, 180),
  2: (0, 0, 0),
  4: (0, 0, 0),
  8: (255, 255, 255),
  16: (255, 255, 255),
  32: (255, 255, 255),
  64: (255, 255, 255),
  128: (255, 255, 255),
  256: (255, 255, 255),
  512: (255, 255, 255),
  1024: (255, 255, 255),
  2048: (255, 255, 255)
 }

 """ 字體 """
 self.title_font = '' # 窗口標題字體類型及大小: 2048
 self.score_font = '' # 分數字體類型及大小
 self.tips_font = '' # 說明字體類型及大小
 self.font = '' # 數字字體

 # 窗口的設置
 def Form(self):
 """
 init(): 初始化所有導入的 pygame 模塊
 display.set_caption(title): 設置窗口的標題
 display.set_mode(): 初始化一個準備顯示的窗口或屏幕
 display.update(): 使繪制的顯示到窗口上
 """
 pygame.init() # 初始化所有導入的 pygame 模塊
 pygame.display.set_caption("Game2048") # 窗口標題
 os.environ['SDL_VIDEO_CENTERED'] = '1' # 窗口居中顯示
 self.form = pygame.display.set_mode([self.screen_width, self.screen_height], 0, 0) # 窗口大小
 self.InitGame() # 矩陣的初始化

 while True:
  self.Action() # 用戶行為: 按鍵/鼠標
  self.Paint() # 表格繪制
  pygame.display.update() # 使繪制的顯示到窗口上

 # 用戶行為: 按鍵/鼠標
 def Action(self):
 for event in pygame.event.get(): # pygame.event.get(): 獲取所有消息并將其從隊列中刪除
  if event.type == pygame.QUIT: # pygame.QUIT: 窗口右上角的紅 ×
  sys.exit() # sys.exit()函數是通過拋出異常的方式來終止進程的
  elif event.type == pygame.KEYDOWN:
  """
  pygame.KEYDOWN 按下鍵盤時
  pygame.KEYUP 釋放鍵盤時
  """
  """ 
  K_ESCAPE: ESC
  K_UP: ↑
  K_DOWN: ↓
  K_LEFT: ←
  K_RIGHT: →
  """

  """ 重新開始游戲 """
  if event.key == pygame.K_ESCAPE:
   # print('ESC')
   self.InitGame() # 游戲初始化

  """ ↑ """
  if event.key == pygame.K_UP and self.is_over == False:
   # print('UP')
   self.MoveUp()
   # self.CreatNum()

  """ ↓ """
  if event.key == pygame.K_DOWN and self.is_over == False:
   # print('DOWN')
   self.MoveDown()
   # self.CreatNum()

  """ ← """
  if event.key == pygame.K_LEFT and self.is_over == False:
   # print('LEFT')
   self.MoveLeft()
   # self.CreatNum()

  """ → """
  if event.key == pygame.K_RIGHT and self.is_over == False:
   # print('RIGHT')
   self.MoveRight()
   # self.CreatNum()

 # 游戲初始化
 def InitGame(self):
 self.score = 0
 self.is_over = False
 self.is_success = False
 self.martix = numpy.zeros([self.size, self.size])

 # 隨機生成兩個數
 for i in range(2):
  self.isadd = True
  self.CreatNum()

 # 隨機在一個位置生成一個數
 def CreatNum(self):

 list = self.GetEmpty() # 獲取空白方格下標
 if list and self.isadd:
  """ 隨機生成的數字 """
  # 2, 4出現概率3:1
  # random.randint(m, n): 隨機生成[m, n]
  value = 4 if random.randint(0, 3) % 3 == 0 else 2

  """ 獲取隨機位置下標 """
  x, y = random.sample(list, 1)[0]

  """ 在隨機位置上生成隨機數字 """
  self.martix[x][y] = value

  self.isadd = False

  # print('CreatNum: {}'.format(value), (x, y))
  # print(self.martix)

 # 獲取空白方格
 def GetEmpty(self):
 list = []
 for i in range(4):
  for j in range(4):
  if self.martix[i][j] == 0:
   list.append([i, j])
 return list

 # 向上移動
 def MoveUp(self):
 # print('up')
 """ Move Up """
 """
 向上移動,只需考慮第二行到第四行
 共分為兩種情況:
 1、當前數字上邊無空格,即上邊值不為 0
  a. 當前數字與上邊數字相等,合并
  b. 當前數字與上邊數字不相等,continue
 2、當前數字上邊有空格,即上邊值為 0, 上移
 """
 for j in range(4):
  index = 0
  for i in range(1, 4):
  if self.martix[i][j] > 0:
   if self.martix[i][j] == self.martix[index][j]:
   # 當前數字 == 上邊數字
   """ 分數: 當前數字 + 上邊數字
    數值: 上邊數字 = 上邊數字 + 當前數字, 當前數字 = 0 """
   self.score += self.martix[i][j] + self.martix[index][j]
   self.martix[index][j] = self.martix[i][j] + self.martix[index][j]
   self.martix[i][j] = 0
   index += 1
   self.isadd = True
   # 當前數字與上邊數字不相等,continue 可以省略不寫
   elif self.martix[index][j] == 0:
   # 當前數字上邊有0
   """ 分數: 不變
    數值: 上邊數字 = 當前數字, 當前數字 = 0 """
   self.martix[index][j] = self.martix[i][j]
   self.martix[i][j] = 0
   self.isadd = True
   else:
   index += 1
   if self.martix[index][j] == 0:
    # index相當于慢指針,j相當于快指針
    # 也就是說快指針和慢指針中間可能存在一個以上的空格,或者index和j并未相鄰
    # 上邊數字 = 0
    """ 分數: 不變
    數值: 上邊數字 = 當前數字, 當前數字 = 0 """
    self.martix[index][j] = self.martix[i][j]
    self.martix[i][j] = 0
    self.isadd = True
 # print('up')
 # print(self.martix)

 # 向下移動
 def MoveDown(self):
 # print('down')
 """ Move Down """
 """
 向下移動,只需考慮第一列到第三列
 共分為兩種情況:
 1、當前數字下邊無空格,即下邊值不為 0
  a. 當前數字與下邊數字相等,合并
  b. 當前數字與下邊數字不相等,continue
 2、當前數字下邊有空格,即下邊值為 0, 下移
 """
 for j in range(4):
  index = 3
  for i in range(2, -1, -1):
  if self.martix[i][j] > 0:
   if self.martix[i][j] == self.martix[index][j]:
   # 當前數字 == 下邊數字
   """ 分數: 當前數字 + 下邊數字
    數值: 下邊數字 = 下邊數字 + 當前數字, 當前數字 = 0 """
   self.score += self.martix[i][j] + self.martix[index][j]
   self.martix[index][j] = self.martix[i][j] + self.martix[index][j]
   self.martix[i][j] = 0
   index -= 1
   self.isadd = True
   # 當前數字與下邊數字不相等,continue 可以省略不寫
   elif self.martix[index][j] == 0:
   # 當前數字下邊有0
   """ 分數: 不變
    數值: 下邊數字 = 當前數字, 當前數字 = 0 """
   self.martix[index][j] = self.martix[i][j]
   self.martix[i][j] = 0
   self.isadd = True
   else:
   index -= 1
   if self.martix[index][j] == 0:
    # index相當于慢指針,j相當于快指針
    # 也就是說快指針和慢指針中間可能存在一個以上的空格,或者index和j并未相鄰
    # 下邊數字 = 0
    """ 分數: 不變
    數值: 下邊數字 = 當前數字, 當前數字 = 0 """
    self.martix[index][j] = self.martix[i][j]
    self.martix[i][j] = 0
    self.isadd = True

 # print('down')
 # print(self.martix)

 # 向左移動
 def MoveLeft(self):
 # print('left')
 """
 Move Left
 """
 """
 向左移動,只需考慮第二列到第四列
 共分為兩種情況:
 1、當前數字左邊無空格,即左邊值不為 0
  a. 當前數字與左邊數字相等,合并
  b. 當前數字與左邊數字不相等,continue
 2、當前數字左邊有空格,即左邊值為 0, 左移
 """
 for i in range(4):
  index = 0
  for j in range(1, 4):
  if self.martix[i][j] > 0:
   if self.martix[i][j] == self.martix[i][index]:
   # 當前數字 == 左邊數字
   """ 分數: 當前數字 + 左邊數字
    數值: 左邊數字 = 左邊數字 + 當前數字, 當前數字 = 0 """
   self.score += self.martix[i][j] == self.martix[i][index]
   self.martix[i][index] = self.martix[i][j] + self.martix[i][index]
   self.martix[i][j] = 0
   index += 1
   self.isadd = True
   # 當前數字與左邊數字不相等,continue 可以省略不寫
   elif self.martix[i][index] == 0:
   # 當前數字左邊有0
   """ 分數: 不變
    數值: 左邊數字 = 當前數字, 當前數字 = 0 """
   self.martix[i][index] = self.martix[i][j]
   self.martix[i][j] = 0
   self.isadd = True
   else:
   index += 1
   if self.martix[i][index] == 0:
    # index相當于慢指針,j相當于快指針
    # 也就是說快指針和慢指針中間可能存在一個以上的空格,或者index和j并未相鄰
    # 左邊數字 = 0
    """ 分數: 不變
    數值: 左邊數字 = 當前數字, 當前數字 = 0 """
    self.martix[i][index] = self.martix[i][j]
    self.martix[i][j] = 0
    self.isadd = True
 # print('left')
 # print(self.martix)

 # 向右移動
 def MoveRight(self):
 # print('right')
 """
 Move Right
 """
 """
 向右移動,只需考慮第一列到第三列
 共分為兩種情況:
 1、當前數字右邊無空格,即右邊值不為 0
  a. 當前數字與右邊數字相等,合并
  b. 當前數字與右邊數字不相等,continue
 2、當前數字右邊有空格,即右邊值為 0, 右移
 """
 for i in range(4):
  index = 3
  for j in range(2, -1, -1):
  if self.martix[i][j] > 0:
   if self.martix[i][j] == self.martix[i][index]:
   # 當前數字 == 右邊數字
   """ 分數: 當前數字 + 右邊數字
    數值: 右邊數字 = 右邊數字 + 當前數字, 當前數字 = 0 """
   self.score += self.martix[i][j] + self.martix[i][index]
   self.martix[i][index] = self.martix[i][j] + self.martix[i][index]
   self.martix[i][j] = 0
   index -= 1
   self.isadd = True
   # 當前數字與左邊數字不相等,continue 可以省略不寫
   elif self.martix[i][index] == 0:
   # 當前數字右邊有0
   """ 分數: 不變
    數值: 右邊數字 = 當前數字, 當前數字 = 0 """
   self.martix[i][index] = self.martix[i][j]
   self.martix[i][j] = 0
   self.isadd = True
   else:
   index -= 1
   if self.martix[i][index] == 0:
    # index相當于慢指針,j相當于快指針
    # 也就是說快指針和慢指針中間可能存在一個以上的空格,或者index和j并未相鄰
    # 右邊數字 = 0
    """ 分數: 不變
    數值: 右邊數字 = 當前數字, 當前數字 = 0 """
    self.martix[i][index] = self.martix[i][j]
    self.martix[i][j] = 0
    self.isadd = True
 # print('right')
 # print(self.martix)

 # 判斷游戲是否結束
 def JudgeGameOver(self):
 # 當空白空格不為空時,即游戲未結束
 zerolist = self.GetEmpty()
 if zerolist:
  return False

 # 當空白方格為空時,判斷是否存在可合并的方格
 for i in range(3):
  for j in range(3):
  if self.martix[i][j] == self.martix[i][j + 1]:
   return False
  if self.martix[i][j] == self.martix[i + 1][j]:
   return False

 # 若不滿足以上兩種情況,則游戲結束
 return True

 # 判斷游戲是否成功
 def JudgeGameSuccess(self):
 # 檢查是否有2048
 if self.martix.max() == 2048:
  return True
 return False

 # 繪制表格
 def Paint(self):
 """ 游戲背景 """
 # fill(color): 填充某一種顏色
 self.form.fill((220, 220, 220))

 """ 字體設置 """
 # 初始化字體
 pygame.font.init()
 # 添加標題
 # f = pygame.font.get_fonts() #: 獲取字體樣式
 # pygame.font.Font.render(): 在一個新 Surface 對象上繪制文本
 self.title_font = pygame.font.SysFont('幼圓', 50, True)
 title_text = self.title_font.render('2048', True, (0, 0, 0))
 self.form.blit(title_text, (50, 10))

 # 添加分數: 得分: 0
 pygame.draw.rect(self.form, (128, 128, 128), (250, 0, 120, 60))
 self.score_font = pygame.font.SysFont('幼圓', 28, True)
 score_text = self.score_font.render('得 分', True, (0, 0, 0))
 self.form.blit(score_text, (275, 0))

 digtial_score = self.score_font.render(str(int(self.score)), True, (255, 250, 250))
 self.form.blit(digtial_score, (280, 30))

 # 添加游戲說明
 self.tips_font = pygame.font.SysFont('simsunnsimsun', 20)
 tips_text = self.tips_font.render('操作: ↑ ↓ ← →, 按esc鍵重新開始', True, (0, 0, 0))
 self.form.blit(tips_text, (25, 70))

 """ 繪制方格 """
 for i in range(4):
  for j in range(4):
  # (x, y) 方塊的初始位置
  x = j * self.block_size + (j + 1) * self.block_gap
  y = i * self.block_size + (i + 1) * self.block_gap
  # 繪制方塊
  value = int(self.martix[i][j])
  # print(value)
  pygame.draw.rect(self.form, self.block_color[value], (x + 5, y + 100, self.block_size, self.block_size),
     border_radius=self.block_arc)

  # 數字字體即大小
  if value  10:
   self.font = pygame.font.SysFont('simsunnsimsun', 46, True) # 數字2、4、8
   value_text = self.font.render(str(value), True, self.nums_color[value])
   self.form.blit(value_text, (x + 35, y + 120))
  elif value  100:
   self.font = pygame.font.SysFont('simsunnsimsun', 40, True) # 數字16, 32, 64
   value_text = self.font.render(str(value), True, self.nums_color[value])
   self.form.blit(value_text, (x + 25, y + 120))
  elif value  1000:
   self.font = pygame.font.SysFont('simsunnsimsun', 34, True) # 數字128, 256, 512
   value_text = self.font.render(str(value), True, self.nums_color[value])
   self.form.blit(value_text, (x + 15, y + 120))
  else:
   self.font = pygame.font.SysFont('simsunnsimsun', 28, True) # 數字1024, 2048
   value_text = self.font.render(str(value), True, self.nums_color[value])
   self.form.blit(value_text, (x + 5, y + 120))

 # 新增數字
 self.CreatNum()

 """ 如果游戲結束 """
 self.is_over = self.JudgeGameOver()
 if self.is_over:
  over_font = pygame.font.SysFont("simsunnsimsun", 60, True)
  str_text = over_font.render('Game Over!', True, (255, 255, 255))
  self.form.blit(str_text, (30, 220))

 """ 如果游戲成功 """
 self.is_success = self.JudgeGameSuccess()
 if self.is_success:
  success_font = pygame.font.SysFont("simsunnsimsun", 60, True)
  str_text = success_font.render('Successful!', True, (178, 34, 34))
  self.form.blit(str_text, (10, 220))

注意這里需要導入兩個包(numpy,pygame),然后運行main文件即可。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • Python實現簡單2048小游戲
  • python反編譯教程之2048小游戲實例
  • 一步步教你用Python實現2048小游戲
  • 用Python寫一個無界面的2048小游戲
  • Python新手實現2048小游戲
  • python實現2048小游戲
  • 用Python手把手教你實現2048小游戲

標簽:阜新 淘寶好評回訪 信陽 隨州 興安盟 合肥 昭通 濟源

巨人網絡通訊聲明:本文標題《Python實現簡單的2048小游戲》,本文關鍵詞  Python,實現,簡單,的,2048,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Python實現簡單的2048小游戲》相關的同類信息!
  • 本頁收集關于Python實現簡單的2048小游戲的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 亚洲人成网址| 永久免费AV无码国产网站18禁 | 国产va无码人在线观看| 香艳按摩绝品按摩师| 男男高h情趣用品play小说| 人妻~夫の上司犯感との| 午夜精品久久久久久久99国产| 欧美日韩一区视频| 被cao哭嗯啊屁股撅起来调教| 日日麻批免费视频播放高清| 日韩视频毛片18禁| 91麻豆精品一二三区在线| 乱码一乱码二乱码三新功能介绍| 嫩草亚洲国产精品| 天天射| 娇妻跪趴高撅肥臀出白浆| 美女张开腿给男人桶爽| 国产91精品探花一区二区| 国产专区日韩精品欧美色| 91精品国产自产老师啪| 日本japanesexxxx中文字幕| 天干天干天干天无人区视频| 国产91??丝袜在线播放九色| 午夜伦伦电影理论片A片大苹果| 亚洲性图第一页| 韩国漫画在线免费看| 男男啪啪网站| 特黄特色大片免费播放器图片 | 性爱动图| 牛牛影视在线| 久久嫩草精品久久久久精品抖音| 久久夜色精品亚洲AV图片红桃| 国产日韩精品无码专区| 中文字幕久久久久久精品直播牛 | 奥特曼丽娜的三级AV在线观看| 91精品国产综合久久青草| 爽死你个放荡粗暴小淫货漫画图| 99精品一级欧美片免费播放| 诱他上瘾| 亚洲AV成人片无码网站2023| 亚洲综合色一区二区三区另类|