<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
咱們Python 集中營有一個專題就是分享一些有意思的東西,今天大概看了一下pygame的這個非標準庫就想著使用它來做個小遊戲-拼圖。
通過加入自己定義的圖片,對這個圖片完成一定數量的拆分後再將拆分後的小圖片進行隨機打亂,這也算是實現一個拼圖小遊戲的基本思路吧。
為了將其做成一個桌面應用,這裡使用的是pygame這一個非標準庫。python環境中還沒有pygame的話可以選擇使用pip的方式安裝一下。
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/
下面將開發中使用到的python庫都列舉一下,除了pygame處理遊戲之外,還有sys、random分別用來做系統操作和亂數的處理。
# Importing the pygame, sys, and random modules. import pygame, sys, random # Importing all the constants from the pygame.locals module. from pygame.locals import *
建立一個python類GameMain,將所有的遊戲處理相關的業務全部寫到這個類中進行處理,包括一些初始化操作等等,最後我們通過main函數呼叫啟動整個拼圖遊戲。
class GameMain(): def __init__(self, window_width, window_height, background_color, fps, colums, max_round_time, game_image): self.init_static_data(window_width=window_width, window_height=window_height, background_color=background_color, fps=fps, colums=colums, max_round_time=max_round_time) self.init_game_data(game_image='./wd.jpeg') self.init_game_main()
通過GameMain類的初始化函數init來做類的初始化並且分別呼叫init_static_data、init_game_data、init_game_main三個主要的實現函數。
init_static_data函數主要用來將一些全域性的靜態引數進行初始化賦值,為了避免沒有值時候的報錯預設分別給這些引數都賦值,包括背景顏色、拼圖遊戲分割成小圖片的列數等等。
def init_static_data(self, window_width=500, window_height=500, background_color=(255, 255, 255), fps=40, colums=3, max_round_time=100): """ The function initializes the game data and the game main :param window_width: The width of the game window :param window_height: The height of the game window :param background_color: The background color of the game window :param fps: The number of frames per second that the game will run at :param colums: The number of columns in the game :param max_round_time: The maximum time for each round :param game_image: The image that will be used for the game """ self.window_width = window_width self.window_height = window_height self.background_color = background_color self.black_color = (0, 0, 0) self.fps = fps self.colums = colums self.cell_nums = self.colums * self.colums self.max_round_time = max_round_time
init_game_data函數,主要將遊戲執行過程中的一些引數進行計算或者計算之後的資料值進行儲存,因為在後面的遊戲主迴圈中肯定是需要用到的,包括遊戲載入的一整張圖片的路徑以及遊戲視窗的標題等等。
def init_game_data(self, game_image='./wd.jpeg'): """ > This function initializes the game data by reading the game image and extracting the game data from it :param game_image: The image of the game you want to play, defaults to ./wd.jpeg (optional) """ pygame.init() self.main_clock = pygame.time.Clock() self.game_image = pygame.image.load(game_image) self.game_rect = self.game_image.get_rect() self.window_surface = pygame.display.set_mode((self.game_rect.width, self.game_rect.height)) pygame.display.set_caption('拼圖遊戲') self.cell_width = int(self.game_rect.width / self.colums) self.cell_height = int(self.game_rect.height / self.colums) self.finished = False self.game_board, self.black_cell = self.generate_game_borad()
init_game_main函數中加入了死迴圈的方式讓遊戲一直處於執行中的狀態,除非是已經完成了遊戲或是直接退出遊戲了才會停止。主要實現的是遊戲步驟以及鍵盤的監聽或滑鼠的點選事件維持整個遊戲狀態的執行。
def init_game_main(self): """ > This function initializes the game data by reading the game image and extracting the game data from it :param game_image: The image of the game you want to play, defaults to ./wd.jpeg (optional), defaults to ./wd.jpeg (optional) """ while True: for event in pygame.event.get(): if event.type == QUIT: self.game_exit() if self.finished: continue if event.type == KEYDOWN: if event.key == K_LEFT or event.key == ord('a'): self.black_cell = self.move_left(self.game_board, self.black_cell) if event.key == K_RIGHT or event.key == ord('d'): self.black_cell = self.move_right(self.game_board, self.black_cell) if event.key == K_UP or event.key == ord('w'): self.black_cell = self.move_up(self.game_board, self.black_cell) if event.key == K_DOWN or event.key == ord('s'): self.black_cell = self.move_down(self.game_board, self.black_cell) if event.type == MOUSEBUTTONDOWN and event.button == 1: x, y = pygame.mouse.get_pos() col = int(x / self.cell_width) row = int(y / self.cell_height) index = col + row * self.colums if ( index == self.black_cell - 1 or index == self.black_cell + 1 or index == self.black_cell - self.colums or index == self.black_cell + self.colums): self.game_board[self.black_cell], self.game_board[index] = self.game_board[index], self.game_board[self.black_cell] self.black_cell = index if (self.is_finished(self.game_board)): self.game_board[self.black_cell] = self.cell_nums - 1 self.finished = True self.window_surface.fill(self.background_color) for i in range(self.cell_nums): row_dst = int(i / self.colums) col_dst = int(i % self.colums) rect_dst = pygame.Rect(col_dst * self.cell_width, row_dst * self.cell_height, self.cell_width, self.cell_height) if self.game_board[i] == -1: continue row_area = int(self.game_board[i] / self.colums) col_area = int(self.game_board[i] % self.colums) rect_area = pygame.Rect(col_area * self.cell_width, row_area * self.cell_height, self.cell_width, self.cell_height) self.window_surface.blit(self.game_image, rect_dst, rect_area) for i in range(self.colums + 1): pygame.draw.line(self.window_surface, self.black_color, (i * self.cell_height, 0), (i * self.cell_width, self.game_rect.height)) for i in range(self.colums + 1): pygame.draw.line(self.window_surface, self.black_color, (0, i * self.cell_height), (self.game_rect.width, i * self.cell_height)) pygame.display.update() self.main_clock.tick(self.fps)
game_exit函數,執行遊戲退出操作。
def game_exit(self): """ It exits the game. """ pygame.quit() sys.exit()
generate_game_borad函數,生成遊戲執行的主佈局。
def generate_game_borad(self): """ It generates the game board. """ board = [] for i in range(self.cell_nums): board.append(i) black_cell = self.cell_nums - 1 board[black_cell] = -1 for i in range(self.max_round_time): direction = random.randint(0, 3) if (direction == 0): black_cell = self.move_left(board, black_cell) elif (direction == 1): black_cell = self.move_right(board, black_cell) elif (direction == 2): black_cell = self.move_up(board, black_cell) elif (direction == 3): black_cell = self.move_down(board, black_cell) return board, black_cell
move_right函數,執行向右移動的操作。
def move_right(self, board, black_cell): """ > The function `move_right` takes in a board and a black cell and returns the board after the black cell has moved right :param board: the board that the game is being played on :param black_cell: the cell that is currently black """ if black_cell % self.colums == 0: return black_cell board[black_cell - 1], board[black_cell] = board[black_cell], board[black_cell - 1] return black_cell - 1
move_left函數,執行向左移動的操作。
def move_left(self, board, black_cell): """ It moves the black cell to the left. :param board: the board that the game is being played on :param black_cell: the cell that is currently black """ if black_cell % self.colums == self.colums - 1: return black_cell board[black_cell + 1], board[black_cell] = board[black_cell], board[black_cell + 1] return black_cell + 1
move_down函數,執行向下移動的操作。
def move_left(self, board, black_cell): """ It moves the black cell to the left. :param board: the board that the game is being played on :param black_cell: the cell that is currently black """ if black_cell % self.colums == self.colums - 1: return black_cell board[black_cell + 1], board[black_cell] = board[black_cell], board[black_cell + 1] return black_cell + 1 def move_down(self, board, black_cell): """ It moves the black cell down. :param board: the board that the game is being played on :param black_cell: the cell that the player is currently on """ if black_cell < self.colums: return black_cell board[black_cell - self.colums], board[black_cell] = board[black_cell], board[black_cell - self.colums] return black_cell - self.colums
move_up函數,執行向下移動的操作。
def move_up(self, board, black_cell): """ It moves the black cell up one space. :param board: the board that the game is being played on :param black_cell: the cell that the player is currently on """ if black_cell >= self.cell_nums - self.colums: return black_cell board[black_cell + self.colums], board[black_cell] = board[black_cell], board[black_cell + self.colums] return black_cell + self.colums
is_finished函數,校驗拼圖是否已經完成的操作。
def is_finished(self, board): """ If the board is full, the game is over :param board: a 2D array representing the current state of the board. The board is composed of 3 characters: 'X', 'O', and ' '. 'X' and 'O' represent the two players. ' ' represents an empty space """ for i in range(self.cell_nums - 1): if board[i] != i: return False return True
最後,只需要通過main的主函數將這個遊戲應用呼叫就可以直接開啟拼圖遊戲了,並且可以設定相關的圖片引數想對哪個圖片拼圖就設定哪個,一般建議設定1024畫素以內的圖片效果會比較好。
# This is a special variable in Python that is set when the program is run. If the program is being run directly (as # opposed to being imported), then `__name__` will be set to `'__main__'`. if __name__ == '__main__': """ It moves the black cell up one space :param board: the board that the game is being played on :param black_cell: the cell that the player is currently on :return: The new position of the black cell. """ GameMain(window_width=500, window_height=500, background_color=(255, 255, 255), fps=40, colums=3, max_round_time=100, game_image='./wd.jpeg')
以上就是基於Python實現自制拼圖小遊戲的詳細內容,更多關於Python拼圖遊戲的資料請關注it145.com其它相關文章!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45