您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)怎么在python中使用pygame實(shí)現(xiàn)一個2048游戲,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
代碼
import random import sys import pygame from pygame.locals import * PIXEL = 150 SCORE_PIXEL = 100 SIZE = 4 # 地圖的類 class Map: def __init__(self, size): self.size = size self.score = 0 self.map = [[0 for i in range(size)] for i in range(size)] self.add() self.add() # 新增2或4,有1/4概率產(chǎn)生4 def add(self): while True: p = random.randint(0, self.size * self.size - 1) if self.map[p / self.size][p % self.size] == 0: x = random.randint(0, 3) > 0 and 2 or 4 self.map[p / self.size][p % self.size] = x self.score += x break # 地圖向左靠攏,其他方向的靠攏可以通過適當(dāng)旋轉(zhuǎn)實(shí)現(xiàn),返回地圖是否更新 def adjust(self): changed = False for a in self.map: b = [] last = 0 for v in a: if v != 0: if v == last: b.append(b.pop() << 1) last = 0 else: b.append(v) last = v b += [0] * (self.size - len(b)) for i in range(self.size): if a[i] != b[i]: changed = True a[ : ] = b return changed # 逆時針旋轉(zhuǎn)地圖90度 def rotate90(self): self.map = [[self.map[c][r] for c in range(self.size)] for r in reversed(range(self.size))] # 判斷游戲結(jié)束 def over(self): for r in range(self.size): for c in range(self.size): if self.map[r][c] == 0: return False for r in range(self.size): for c in range(self.size - 1): if self.map[r][c] == self.map[r][c + 1]: return False for r in range(self.size - 1): for c in range(self.size): if self.map[r][c] == self.map[r + 1][c]: return False return True def moveUp(self): self.rotate90() if self.adjust(): self.add() self.rotate90() self.rotate90() self.rotate90() def moveRight(self): self.rotate90() self.rotate90() if self.adjust(): self.add() self.rotate90() self.rotate90() def moveDown(self): self.rotate90() self.rotate90() self.rotate90() if self.adjust(): self.add() self.rotate90() def moveLeft(self): if self.adjust(): self.add() # 更新屏幕 def show(map): for i in range(SIZE): for j in range(SIZE): # 背景顏色塊 screen.blit(map.map[i][j] == 0 and block[(i + j) % 2] or block[2 + (i + j) % 2], (PIXEL * j, PIXEL * i)) # 數(shù)值顯示 if map.map[i][j] != 0: map_text = map_font.render(str(map.map[i][j]), True, (106, 90, 205)) text_rect = map_text.get_rect() text_rect.center = (PIXEL * j + PIXEL / 2, PIXEL * i + PIXEL / 2) screen.blit(map_text, text_rect) # 分?jǐn)?shù)顯示 screen.blit(score_block, (0, PIXEL * SIZE)) score_text = score_font.render((map.over() and "Game over with score " or "Score: ") + str(map.score), True, (106, 90, 205)) score_rect = score_text.get_rect() score_rect.center = (PIXEL * SIZE / 2, PIXEL * SIZE + SCORE_PIXEL / 2) screen.blit(score_text, score_rect) pygame.display.update() map = Map(SIZE) pygame.init() screen = pygame.display.set_mode((PIXEL * SIZE, PIXEL * SIZE + SCORE_PIXEL)) pygame.display.set_caption("2048") block = [pygame.Surface((PIXEL, PIXEL)) for i in range(4)] # 設(shè)置顏色 block[0].fill((152, 251, 152)) block[1].fill((240, 255, 255)) block[2].fill((0, 255, 127)) block[3].fill((225, 255, 255)) score_block = pygame.Surface((PIXEL * SIZE, SCORE_PIXEL)) score_block.fill((245, 245, 245)) # 設(shè)置字體 map_font = pygame.font.Font(None, PIXEL * 2 / 3) score_font = pygame.font.Font(None, SCORE_PIXEL * 2 / 3) clock = pygame.time.Clock() show(map) while not map.over(): # 12為實(shí)驗(yàn)參數(shù) clock.tick(12) for event in pygame.event.get(): if event.type == QUIT: sys.exit() # 接收玩家操作 pressed_keys = pygame.key.get_pressed() if pressed_keys[K_w] or pressed_keys[K_UP]: map.moveUp() elif pressed_keys[K_s] or pressed_keys[K_DOWN]: map.moveDown() elif pressed_keys[K_a] or pressed_keys[K_LEFT]: map.moveLeft() elif pressed_keys[K_d] or pressed_keys[K_RIGHT]: map.moveRight() show(map) # 游戲結(jié)束 pygame.time.delay(3000)
上述就是小編為大家分享的怎么在python中使用pygame實(shí)現(xiàn)一個2048游戲了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。