您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)人如何用python實(shí)現(xiàn)一個讓人戒不掉的百變款消消樂,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
每日游戲更新系列——今天帶大家解鎖消消樂小游戲!
一天深夜,我像往常一樣快樂地遨游在互聯(lián)網(wǎng)的海洋里,猝不及防地接到了一個電話。
屏幕上一個觸目驚心的大字:媽。
我一邊動作遲緩地按下通話鍵,一邊迅速反省是不是剛才發(fā)朋友圈忘記屏蔽,被她發(fā)現(xiàn)我又在熬夜了。
電話那頭果然傳來了她火急火燎的聲音,但我猜中了開頭,卻猜不到這結(jié)局。
我媽說:“還沒睡覺吧,給我解鎖一下消消樂的新關(guān)卡。”
我還在判斷她的操作是有意試探還是虛晃一槍時,她已經(jīng)開始著急了:“快點(diǎn),就差最后一個人了,我在等著你??!”
對于不想睡覺的人來說,每個輾轉(zhuǎn)難眠的夜晚都是相似的,但同樣清醒如雞的我和我媽卻有著各自不同的快樂源泉!
其實(shí)消消樂的話主題的素材可以隨意一點(diǎn)兒, 可以是月餅中秋版本的,動漫版本的等等等.......萬物皆可。
這邊準(zhǔn)備了2組消消樂的主題:1.表情包消消樂;2.十二生肖。
設(shè)置拼圖游戲精靈類:
class gemSprite(pygame.sprite.Sprite): def __init__(self, img_path, size, position, downlen, **kwargs): pygame.sprite.Sprite.__init__(self) self.image = pygame.image.load(img_path) self.image = pygame.transform.smoothscale(self.image, size) self.rect = self.image.get_rect() self.rect.left, self.rect.top = position self.downlen = downlen self.target_x = position[0] self.target_y = position[1] + downlen self.type = img_path.split('/')[-1].split('.')[0] self.fixed = False self.speed_x = 10 self.speed_y = 10 self.direction = 'down'
拼圖上下左右移動:
def move(self): if self.direction == 'down': self.rect.top = min(self.target_y, self.rect.top+self.speed_y) if self.target_y == self.rect.top: self.fixed = True elif self.direction == 'up': self.rect.top = max(self.target_y, self.rect.top-self.speed_y) if self.target_y == self.rect.top: self.fixed = True elif self.direction == 'left': self.rect.left = max(self.target_x, self.rect.left-self.speed_x) if self.target_x == self.rect.left: self.fixed = True elif self.direction == 'right': self.rect.left = min(self.target_x, self.rect.left+self.speed_x) if self.target_x == self.rect.left: self.fixed = True
定義游戲類開始游戲主循環(huán):
class gemGame(): def __init__(self, screen, sounds, font, gem_imgs, cfg, **kwargs): self.info = 'Gemgem' self.screen = screen self.sounds = sounds self.font = font self.gem_imgs = gem_imgs self.cfg = cfg self.reset() def start(self): clock = pygame.time.Clock() # 遍歷整個游戲界面更新位置 overall_moving = True # 指定某些對象個體更新位置 individual_moving = False # 定義一些必要的變量 gem_selected_xy = None gem_selected_xy2 = None swap_again = False add_score = 0 add_score_showtimes = 10 time_pre = int(time.time()) while True: for event in pygame.event.get(): if event.type == pygame.QUIT or (event.type == pygame.KEYUP and event.key == pygame.K_ESCAPE): pygame.quit() sys.exit() elif event.type == pygame.MOUSEBUTTONUP: if (not overall_moving) and (not individual_moving) and (not add_score): position = pygame.mouse.get_pos() if gem_selected_xy is None: gem_selected_xy = self.checkSelected(position) else: gem_selected_xy2 = self.checkSelected(position) if gem_selected_xy2: if self.swapGem(gem_selected_xy, gem_selected_xy2): individual_moving = True swap_again = False else: gem_selected_xy = None if overall_moving: overall_moving = not self.dropGems(0, 0) # 移動一次可能可以拼出多個3連塊 if not overall_moving: res_match = self.isMatch() add_score = self.removeMatched(res_match) if add_score > 0: overall_moving = True if individual_moving: gem1 = self.getGemByPos(*gem_selected_xy) gem2 = self.getGemByPos(*gem_selected_xy2) gem1.move() gem2.move() if gem1.fixed and gem2.fixed: res_match = self.isMatch() if res_match[0] == 0 and not swap_again: swap_again = True self.swapGem(gem_selected_xy, gem_selected_xy2) self.sounds['mismatch'].play() else: add_score = self.removeMatched(res_match) overall_moving = True individual_moving = False gem_selected_xy = None gem_selected_xy2 = None self.screen.fill((135, 206, 235)) self.drawGrids() self.gems_group.draw(self.screen) if gem_selected_xy: self.drawBlock(self.getGemByPos(*gem_selected_xy).rect) if add_score: if add_score_showtimes == 10: random.choice(self.sounds['match']).play() self.drawAddScore(add_score) add_score_showtimes -= 1 if add_score_showtimes < 1: add_score_showtimes = 10 add_score = 0 self.remaining_time -= (int(time.time()) - time_pre) time_pre = int(time.time()) self.showRemainingTime() self.drawScore() if self.remaining_time <= 0: return self.score pygame.display.update() clock.tick(self.cfg.FPS)
既然是游戲拿肯定是有加分設(shè)置的、玩游戲要設(shè)置一個時間限制更有趣。
def showRemainingTime(self): remaining_time_render = self.font.render('CountDown: %ss' % str(self.remaining_time), 1, (85, 65, 0)) rect = remaining_time_render.get_rect() rect.left, rect.top = (self.cfg.SCREENSIZE[0]-201, 6) self.screen.blit(remaining_time_render, rect) def drawScore(self): score_render = self.font.render('SCORE:'+str(self.score), 1, (85, 65, 0)) rect = score_render.get_rect() rect.left, rect.top = (10, 6) self.screen.blit(score_render, rect) def drawAddScore(self, add_score): score_render = self.font.render('+'+str(add_score), 1, (255, 100, 100)) rect = score_render.get_rect() rect.left, rect.top = (250, 250) self.screen.blit(score_render, rect)
三個圖片相同即可消除:
def removeMatched(self, res_match): if res_match[0] > 0: self.generateNewGems(res_match) self.score += self.reward return self.reward return 0
定義下來的圖片特效:
def dropGems(self, x, y): if not self.getGemByPos(x, y).fixed: self.getGemByPos(x, y).move() if x < self.cfg.NUMGRID - 1: x += 1 return self.dropGems(x, y) elif y < self.cfg.NUMGRID - 1: x = 0 y += 1 return self.dropGems(x, y) else: return self.isFull()
拼圖的位置交換:
def swapGem(self, gem1_pos, gem2_pos): margin = gem1_pos[0] - gem2_pos[0] + gem1_pos[1] - gem2_pos[1] if abs(margin) != 1: return False gem1 = self.getGemByPos(*gem1_pos) gem2 = self.getGemByPos(*gem2_pos) if gem1_pos[0] - gem2_pos[0] == 1: gem1.direction = 'left' gem2.direction = 'right' elif gem1_pos[0] - gem2_pos[0] == -1: gem2.direction = 'left' gem1.direction = 'right' elif gem1_pos[1] - gem2_pos[1] == 1: gem1.direction = 'up' gem2.direction = 'down' elif gem1_pos[1] - gem2_pos[1] == -1: gem2.direction = 'up' gem1.direction = 'down' gem1.target_x = gem2.rect.left gem1.target_y = gem2.rect.top gem1.fixed = False gem2.target_x = gem1.rect.left gem2.target_y = gem1.rect.top gem2.fixed = False self.all_gems[gem2_pos[0]][gem2_pos[1]] = gem1 self.all_gems[gem1_pos[0]][gem1_pos[1]] = gem2 return True
附游戲主程序界面:
import os import sys import cfg import pygame from modules import * def main(): pygame.init() screen = pygame.display.set_mode(cfg.SCREENSIZE) pygame.display.set_caption('消消樂小游戲') # 加載背景音樂 pygame.mixer.init() pygame.mixer.music.load(os.path.join(cfg.ROOTDIR, "resources/audios/bg.mp3")) pygame.mixer.music.set_volume(0.6) pygame.mixer.music.play(-1) # 加載音效 sounds = {} sounds['mismatch'] = pygame.mixer.Sound(os.path.join(cfg.ROOTDIR, 'resources/audios/badswap.wav')) sounds['match'] = [] for i in range(6): sounds['match'].append(pygame.mixer.Sound(os.path.join(cfg.ROOTDIR, 'resources/audios/match%s.wav' % i))) # 加載字體 font = pygame.font.Font(os.path.join(cfg.ROOTDIR, 'resources/font/font.TTF'), 25) # 圖片加載 gem_imgs = [] for i in range(1, 8): gem_imgs.append(os.path.join(cfg.ROOTDIR, 'resources/images/gem%s.png' % i)) # 主循環(huán) game = gemGame(screen, sounds, font, gem_imgs, cfg) while True: score = game.start() flag = False # 一輪游戲結(jié)束后玩家選擇重玩或者退出 while True: for event in pygame.event.get(): if event.type == pygame.QUIT or (event.type == pygame.KEYUP and event.key == pygame.K_ESCAPE): pygame.quit() sys.exit() elif event.type == pygame.KEYUP and event.key == pygame.K_r: flag = True if flag: break screen.fill((135, 206, 235)) text0 = 'Final score: %s' % score text1 = 'Press <R> to restart the game.' text2 = 'Press <Esc> to quit the game.' y = 150 for idx, text in enumerate([text0, text1, text2]): text_render = font.render(text, 1, (85, 65, 0)) rect = text_render.get_rect() if idx == 0: rect.left, rect.top = (212, y) elif idx == 1: rect.left, rect.top = (122.5, y) else: rect.left, rect.top = (126.5, y) y += 100 screen.blit(text_render, rect) pygame.display.update() game.reset() '''run''' if __name__ == '__main__': main()
效果圖:
說實(shí)話這個表情包版本的找的好哇!哈哈哈 很多相似的找都找不出來~
這個是真卡哇伊哈哈哈 適合家里的小朋友玩~
以上就是人如何用python實(shí)現(xiàn)一個讓人戒不掉的百變款消消樂,小編相信有部分知識點(diǎn)可能是我們?nèi)粘9ぷ鲿姷交蛴玫降摹OM隳芡ㄟ^這篇文章學(xué)到更多知識。更多詳情敬請關(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)容。