正文
导语
Python版的消除类的游戏还是很多的,木木子之前也是推过不少~
源码基地——私信小编06即可啦~
比如:百变的消消乐,还记得嘛?今天就出一个消除类——泡泡龙小游戏,希望你们喜欢哈~!
《泡泡乐》是一款适合全年龄玩家的游戏,采用非常经典的“泡泡龙”式的消除泡泡的玩法,
游戏没有太多创新玩法,容易上手。当我们一个人独处而无人聊天时可以用它来打发时间。
来来来,跟着木木子一起开始玩泡泡龙游戏吧~
正文一、准备中
1)游戏规则:
游戏玩法是玩家从下方中央的弹珠发射台射出彩珠,等于3个同色珠相连则会消失。直到完全消除界面上的同款泡泡机
可胜利,还可以跟小小伙伴儿比拼, 看谁用的彩球越少。
2)环境安装
本文用到的环境:Python3、Pycharm、Pygame以及自带的。
二、开始敲代码
1)导入模块
import math, pygame, sys, os, copy, time, randomimport pygame.gfxdrawfrom pygame.locals import *2)主程序
FPS = 120WINDOWWIDTH = 640WINDOWHEIGHT = 480TEXTHEIGHT = 20BUBBLERADIUS = 20BUBBLEWIDTH = BUBBLERADIUS * 2BUBBLELAYERS = 5BUBBLEYADJUST = 5STARTX = WINDOWWIDTH / 2STARTY = WINDOWHEIGHT - 27ARRAYWIDTH = 16ARRAYHEIGHT = 14RIGHT = 'right'LEFT = 'left'BLANK = '.'## COLORS ### R G BGRAY = (100, 100, 100)NAVYBLUE = ( 60, 60, 100)WHITE = (255, 255, 255)RED = (255, 0, 0)GREEN = ( 0, 255, 0)BLUE = ( 0, 0, 255)YELLOW = (255, 255, 0)ORANGE = (255, 128, 0)PURPLE = (255, 0, 255)CYAN = ( 0, 255, 255)BLACK = ( 0, 0, 0)COMBLUE = (233, 232, 255)BGCOLOR = WHITECOLORLIST = [RED, GREEN, BLUE, YELLOW, ORANGE, PURPLE, CYAN] class Bubble(pygame.sprite.Sprite): def __init__(self, color, row=0, column=0): pygame.sprite.Sprite.__init__(self) self.rect = pygame.Rect(0, 0, 30, 30) self.rect.centerx = STARTX self.rect.centery = STARTY self.speed = 10 self.color = color self.radius = BUBBLERADIUS self.angle = 0 self.row = row self.column = column def update(self): if self.angle == 90: xmove = 0 ymove = self.speed * -1 elif self.angle 90: xmove = self.xcalculate(self.angle) ymove = self.ycalculate(self.angle) elif self.angle 90: xmove = self.xcalculate(180 - self.angle) * -1 ymove = self.ycalculate(180 - self.angle) self.rect.x += xmove self.rect.y += ymove def draw(self): pygame.gfxdraw.filled_circle(DISPLAYSURF, self.rect.centerx, self.rect.centery, self.radius, self.color) pygame.gfxdraw.aacircle(DISPLAYSURF, self.rect.centerx, self.rect.centery, self.radius, GRAY) def xcalculate(self, angle): radians = math.radians(angle) xmove = math.cos(radians)*(self.speed) return xmove def ycalculate(self, angle): radians = math.radians(angle) ymove = math.sin(radians)*(self.speed) * -1 return ymoveclass Arrow(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self) self.angle = 90 arrowImage = pygame.image.load('Arrow.png') arrowImage.convert_alpha() arrowRect = arrowImage.get_rect() self.image = arrowImage self.transformImage = self.image self.rect = arrowRect self.rect.centerx = STARTX self.rect.centery = STARTY def update(self, direction): if direction == LEFT and self.angle 180: self.angle += 2 elif direction == RIGHT and self.angle 0: self.angle -= 2 self.transformImage = pygame.transform.rotate(self.image, self.angle) self.rect = self.transformImage.get_rect() self.rect.centerx = STARTX self.rect.centery = STARTY def draw(self): DISPLAYSURF.blit(self.transformImage, self.rect)class Score(object): def __init__(self): self.total = 0 self.font = pygame.font.SysFont('Helvetica', 15) self.render = self.font.render('Score: ' + str(self.total), True, BLACK, WHITE) self.rect = self.render.get_rect() self.rect.left = 5 self.rect.bottom = WINDOWHEIGHT - 5 def update(self, deleteList): self.total += ((len(deleteList)) * 10) self.render = self.font.render('Score: ' + str(self.total), True, BLACK, WHITE) def draw(self): DISPLAYSURF.blit(self.render, self.rect)def main(): global FPSCLOCK, DISPLAYSURF, DISPLAYRECT, MAINFONT pygame.init() FPSCLOCK = pygame.time.Clock() pygame.display.set_caption('泡泡龙小游戏') MAINFONT = pygame.font.SysFont('Helvetica', TEXTHEIGHT) DISPLAYSURF, DISPLAYRECT = makeDisplay() while True: score, winorlose = runGame() endScreen(score, winorlose)def runGame(): musicList =['bgmusic.ogg', 'Utopian_Theme.ogg', 'Goofy_Theme.ogg'] pygame.mixer.music.load(musicList[0]) pygame.mixer.music.play() track = 0 gameColorList = copy.deepcopy(COLORLIST) direction = None launchBubble = False newBubble = None arrow = Arrow() bubbleArray = makeBlankBoard() setBubbles(bubbleArray, gameColorList) nextBubble = Bubble(gameColorList[0]) nextBubble.rect.right = WINDOWWIDTH - 5 nextBubble.rect.bottom = WINDOWHEIGHT - 5 score = Score() while True: DISPLAYSURF.fill(BGCOLOR) for event in pygame.event.get(): if event.type == QUIT: terminate() elif event.type == KEYDOWN: if (event.key == K_LEFT): direction = LEFT elif (event.key == K_RIGHT): direction = RIGHT elif event.type == KEYUP: direction = None if event.key == K_SPACE: launchBubble = True elif event.key == K_ESCAPE: terminate() if launchBubble == True: if newBubble == None: newBubble = Bubble(nextBubble.color) newBubble.angle = arrow.angle newBubble.update() newBubble.draw() if newBubble.rect.right = WINDOWWIDTH - 5: newBubble.angle = 180 - newBubble.angle elif newBubble.rect.left = 5: newBubble.angle = 180 - newBubble.angle launchBubble, newBubble, score = stopBubble(bubbleArray, newBubble, launchBubble, score) finalBubbleList = [] for row in range(len(bubbleArray)): for column in range(len(bubbleArray[0])): if bubbleArray[row][column] != BLANK: finalBubbleList.append(bubbleArray[row][column]) if bubbleArray[row][column].rect.bottom (WINDOWHEIGHT - arrow.rect.height - 10): return score.total, 'lose' if len(finalBubbleList) 1: return score.total, 'win' gameColorList = updateColorList(bubbleArray) random.shuffle(gameColorList) if launchBubble == False: nextBubble = Bubble(gameColorList[0]) nextBubble.rect.right = WINDOWWIDTH - 5 nextBubble.rect.bottom = WINDOWHEIGHT - 5 nextBubble.draw() if launchBubble == True: coverNextBubble() arrow.update(direction) arrow.draw() setArrayPos(bubbleArray) drawBubbleArray(bubbleArray) score.draw() if pygame.mixer.music.get_busy() == False: if track == len(musicList) - 1: track = 0 else: track += 1 pygame.mixer.music.load(musicList[track]) pygame.mixer.music.play() pygame.display.update() FPSCLOCK.tick(FPS)def makeBlankBoard(): array = [] for row in range(ARRAYHEIGHT): column = [] for i in range(ARRAYWIDTH): column.append(BLANK) array.append(column) return arraydef setBubbles(array, gameColorList): for row in range(BUBBLELAYERS): for column in range(len(array[row])): random.shuffle(gameColorList) newBubble = Bubble(gameColorList[0], row, column) array[row][column] = newBubble setArrayPos(array)def setArrayPos(array): for row in range(ARRAYHEIGHT): for column in range(len(array[row])): if array[row][column] != BLANK: array[row][column].rect.x = (BUBBLEWIDTH * column) + 5 array[row][column].rect.y = (BUBBLEWIDTH * row) + 5 for row in range(1, ARRAYHEIGHT, 2): for column in range(len(array[row])): if array[row][column] != BLANK: array[row][column].rect.x += BUBBLERADIUS for row in range(1, ARRAYHEIGHT): for column in range(len(array[row])): if array[row][column] != BLANK: array[row][column].rect.y -= (BUBBLEYADJUST * row) deleteExtraBubbles(array)def deleteExtraBubbles(array): for row in range(ARRAYHEIGHT): for column in range(len(array[row])): if array[row][column] != BLANK: if array[row][column].rect.right WINDOWWIDTH: array[row][column] = BLANKdef updateColorList(bubbleArray): newColorList = [] for row in range(len(bubbleArray)): for column in range(len(bubbleArray[0])): if bubbleArray[row][column] != BLANK: newColorList.append(bubbleArray[row][column].color) colorSet = set(newColorList) if len(colorSet) 1: colorList = [] colorList.append(WHITE) return colorList else: return list(colorSet) def checkForFloaters(bubbleArray): bubbleList = [column for column in range(len(bubbleArray[0])) if bubbleArray[0][column] != BLANK] newBubbleList = [] for i in range(len(bubbleList)): if i == 0: newBubbleList.append(bubbleList[i]) elif bubbleList[i] bubbleList[i - 1] + 1: newBubbleList.append(bubbleList[i]) copyOfBoard = copy.deepcopy(bubbleArray) for row in range(len(bubbleArray)): for column in range(len(bubbleArray[0])): bubbleArray[row][column] = BLANK for column in newBubbleList: popFloaters(bubbleArray, copyOfBoard, column)def popFloaters(bubbleArray, copyOfBoard, column, row=0): if (row 0 or row (len(bubbleArray)-1) or column 0 or column (len(bubbleArray[0])-1)): return elif copyOfBoard[row][column] == BLANK: return elif bubbleArray[row][column] == copyOfBoard[row][column]: return bubbleArray[row][column] = copyOfBoard[row][column] if row == 0: popFloaters(bubbleArray, copyOfBoard, column + 1, row ) popFloaters(bubbleArray, copyOfBoard, column - 1, row ) popFloaters(bubbleArray, copyOfBoard, column, row + 1) popFloaters(bubbleArray, copyOfBoard, column - 1, row + 1) elif row % 2 == 0: popFloaters(bubbleArray, copyOfBoard, column + 1, row ) popFloaters(bubbleArray, copyOfBoard, column - 1, row ) popFloaters(bubbleArray, copyOfBoard, column, row + 1) popFloaters(bubbleArray, copyOfBoard, column - 1, row + 1) popFloaters(bubbleArray, copyOfBoard, column, row - 1) popFloaters(bubbleArray, copyOfBoard, column - 1, row - 1) else: popFloaters(bubbleArray, copyOfBoard, column + 1, row ) popFloaters(bubbleArray, copyOfBoard, column - 1, row ) popFloaters(bubbleArray, copyOfBoard, column, row + 1) popFloaters(bubbleArray, copyOfBoard, column + 1, row + 1) popFloaters(bubbleArray, copyOfBoard, column, row - 1) popFloaters(bubbleArray, copyOfBoard, column + 1, row - 1) def stopBubble(bubbleArray, newBubble, launchBubble, score): deleteList = [] popSound = pygame.mixer.Sound('popcork.ogg') for row in range(len(bubbleArray)): for column in range(len(bubbleArray[row])): if (bubbleArray[row][column] != BLANK and newBubble != None): if (pygame.sprite.collide_rect(newBubble, bubbleArray[row][column])) or newBubble.rect.top 0: if newBubble.rect.top 0: newRow, newColumn = addBubbleToTop(bubbleArray, newBubble) elif newBubble.rect.centery = bubbleArray[row][column].rect.centery: if newBubble.rect.centerx = bubbleArray[row][column].rect.centerx: if row == 0 or (row) % 2 == 0: newRow = row + 1 newColumn = column if bubbleArray[newRow][newColumn] != BLANK: newRow = newRow - 1 bubbleArray[newRow][newColumn] = copy.copy(newBubble) bubbleArray[newRow][newColumn].row = newRow bubbleArray[newRow][newColumn].column = newColumn else: newRow = row + 1 newColumn = column + 1 if bubbleArray[newRow][newColumn] != BLANK: newRow = newRow - 1 bubbleArray[newRow][newColumn] = copy.copy(newBubble) bubbleArray[newRow][newColumn].row = newRow bubbleArray[newRow][newColumn].column = newColumn elif newBubble.rect.centerx bubbleArray[row][column].rect.centerx: if row == 0 or row % 2 == 0: newRow = row + 1 newColumn = column - 1 if newColumn 0: newColumn = 0 if bubbleArray[newRow][newColumn] != BLANK: newRow = newRow - 1 bubbleArray[newRow][newColumn] = copy.copy(newBubble) bubbleArray[newRow][newColumn].row = newRow bubbleArray[newRow][newColumn].column = newColumn else: newRow = row + 1 newColumn = column if bubbleArray[newRow][newColumn] != BLANK: newRow = newRow - 1 bubbleArray[newRow][newColumn] = copy.copy(newBubble) bubbleArray[newRow][newColumn].row = newRow bubbleArray[newRow][newColumn].column = newColumn elif newBubble.rect.centery bubbleArray[row][column].rect.centery: if newBubble.rect.centerx = bubbleArray[row][column].rect.centerx: if row == 0 or row % 2 == 0: newRow = row - 1 newColumn = column if bubbleArray[newRow][newColumn] != BLANK: newRow = newRow + 1 bubbleArray[newRow][newColumn] = copy.copy(newBubble) bubbleArray[newRow][newColumn].row = newRow bubbleArray[newRow][newColumn].column = newColumn else: newRow = row - 1 newColumn = column + 1 if bubbleArray[newRow][newColumn] != BLANK: newRow = newRow + 1 bubbleArray[newRow][newColumn] = copy.copy(newBubble) bubbleArray[newRow][newColumn].row = newRow bubbleArray[newRow][newColumn].column = newColumn elif newBubble.rect.centerx = bubbleArray[row][column].rect.centerx: if row == 0 or row % 2 == 0: newRow = row - 1 newColumn = column - 1 if bubbleArray[newRow][newColumn] != BLANK: newRow = newRow + 1 bubbleArray[newRow][newColumn] = copy.copy(newBubble) bubbleArray[newRow][newColumn].row = newRow bubbleArray[newRow][newColumn].column = newColumn else: newRow = row - 1 newColumn = column if bubbleArray[newRow][newColumn] != BLANK: newRow = newRow + 1 bubbleArray[newRow][newColumn] = copy.copy(newBubble) bubbleArray[newRow][newColumn].row = newRow bubbleArray[newRow][newColumn].column = newColumn popBubbles(bubbleArray, newRow, newColumn, newBubble.color, deleteList) if len(deleteList) = 3: for pos in deleteList: popSound.play() row = pos[0] column = pos[1] bubbleArray[row][column] = BLANK checkForFloaters(bubbleArray) score.update(deleteList) launchBubble = False newBubble = None return launchBubble, newBubble, score def addBubbleToTop(bubbleArray, bubble): posx = bubble.rect.centerx leftSidex = posx - BUBBLERADIUS columnDivision = math.modf(float(leftSidex) / float(BUBBLEWIDTH)) column = int(columnDivision[1]) if columnDivision[0] 0.5: bubbleArray[0][column] = copy.copy(bubble) else: column += 1 bubbleArray[0][column] = copy.copy(bubble) row = 0 return row, column def popBubbles(bubbleArray, row, column, color, deleteList): if row 0 or column 0 or row (len(bubbleArray)-1) or column (len(bubbleArray[0])-1): return elif bubbleArray[row][column] == BLANK: return elif bubbleArray[row][column].color != color: return for bubble in deleteList: if bubbleArray[bubble[0]][bubble[1]] == bubbleArray[row][column]: return deleteList.append((row, column)) if row == 0: popBubbles(bubbleArray, row, column - 1, color, deleteList) popBubbles(bubbleArray, row, column + 1, color, deleteList) popBubbles(bubbleArray, row + 1, column, color, deleteList) popBubbles(bubbleArray, row + 1, column - 1, color, deleteList) elif row % 2 == 0: popBubbles(bubbleArray, row + 1, column, color, deleteList) popBubbles(bubbleArray, row + 1, column - 1, color, deleteList) popBubbles(bubbleArray, row - 1, column, color, deleteList) popBubbles(bubbleArray, row - 1, column - 1, color, deleteList) popBubbles(bubbleArray, row, column + 1, color, deleteList) popBubbles(bubbleArray, row, column - 1, color, deleteList) else: popBubbles(bubbleArray, row - 1, column, color, deleteList) popBubbles(bubbleArray, row - 1, column + 1, color, deleteList) popBubbles(bubbleArray, row + 1, column, color, deleteList) popBubbles(bubbleArray, row + 1, column + 1, color, deleteList) popBubbles(bubbleArray, row, column + 1, color, deleteList) popBubbles(bubbleArray, row, column - 1, color, deleteList) def drawBubbleArray(array): for row in range(ARRAYHEIGHT): for column in range(len(array[row])): if array[row][column] != BLANK: array[row][column].draw() def makeDisplay(): DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT)) DISPLAYRECT = DISPLAYSURF.get_rect() DISPLAYSURF.fill(BGCOLOR) DISPLAYSURF.convert() pygame.display.update() return DISPLAYSURF, DISPLAYRECT def terminate(): pygame.quit() sys.exit()def coverNextBubble(): whiteRect = pygame.Rect(0, 0, BUBBLEWIDTH, BUBBLEWIDTH) whiteRect.bottom = WINDOWHEIGHT whiteRect.right = WINDOWWIDTH pygame.draw.rect(DISPLAYSURF, BGCOLOR, whiteRect)def endScreen(score, winorlose): endFont = pygame.font.SysFont('Helvetica', 20) endMessage1 = endFont.render('You ' + winorlose + '! Your Score is ' + str(score) + '. Press Enter to Play Again.', True, BLACK, BGCOLOR) endMessage1Rect = endMessage1.get_rect() endMessage1Rect.center = DISPLAYRECT.center DISPLAYSURF.fill(BGCOLOR) DISPLAYSURF.blit(endMessage1, endMessage1Rect) pygame.display.update() while True: for event in pygame.event.get(): if event.type == QUIT: terminate() elif event.type == KEYUP: if event.key == K_RETURN: return elif event.key == K_ESCAPE: terminate() if __name__ == '__main__': main()三、效果展示
空格键是发球、方向键左右是遥控箭头的。
1)运行界面
2)同色三个可消除
3)结束页面
一颗球是10个成绩点,界面的球被我消了总的591个才结束这个游戏!2333,有点难
总结嘿!小游戏写到这结束了,自己动手玩一玩吖~哈哈哈。源码都是免费拿滴~
于茫茫人海相遇——感谢你的阅读!相遇即是缘分,如有帮助到你,记得三连哦~
我是木木子,一个不止能编程的女码农,还能教你玩游戏、制作节日惊喜、甚至撩小姐姐、小哥哥的表白小程序哦......
写在最后——往期也有很多精彩内容,欢迎阅读!关注我,每日更新
本文TAG:泡泡龙小游戏下载
- 上一篇: 冒险岛战神小常识 分享玩转战神在此-冒险岛发布站程序
- 下一篇: 包含神泣单机版的词条
猜你喜欢
- 2023-08-30赤炎单职业传奇嬉戏里做事押镖任务中有什么方法?讨论赤炎单职业传奇每日押镖任务帮扶你极快升级
- 2023-08-30龙吟单职业传奇中帮你如何搭配夫妻传送功能(龙吟单职业传奇攻略详解)
- 2023-08-30首次接触合击传奇卡怪游戏玩家的绝对晓得的最好怪物,,跟合击传奇卡怪中最先起步的高好机会打宝地图无保留分享传授
- 2023-08-30【双通单职业传奇攻略】双通单职业传奇背包东西扔不出去是怎么回事
- 2022-02-22新开传奇手游使用拍卖行用于交易时最好提前沟通好
- 2022-02-22有哪些方法可以让我们弄到元宝
- 最近发表
- 标签列表
-
- 传奇手游私服 (7)
- 新开传奇手游网站 (7)
- 传奇单职业 (2)
- 传奇手游变态版 (1)
- 传奇发布网 (7)
- 传奇sf手游版 (1)
- 新开传奇 (1)
- 传奇私服 (1)
- 拳皇7k7k小游戏 (0)
- nitrome小游戏 (0)
- 最新传奇发布网 (0)
- 每日新开传奇网 (0)
- 找176复古传奇 (0)
- 超级变态热血传奇 (0)
- nba2k11最新名单 (0)
- 王者荣耀更新 (0)
- 王者荣耀体验服申请 (0)
- 英雄联盟出装顺序 (0)
- 三国志单机游戏 (0)
- 王者荣耀宫本武藏符文 (0)
- 梦幻西游109神器任务攻略 (0)
- 下载英雄联盟 (0)
- 机械公敌兰博出装 (0)
- 王者荣耀西施 (0)
- 王者荣耀无限火力 (0)
- 不知火舞王者荣耀 (0)
- 300英雄三笠出装 (0)
- 冒险岛2职业 (0)
- 龙将2官网 (0)
- 热血传奇怀旧版好玩吗 (0)
- 传奇超级 (0)
- 传奇师服 (0)
- 迷失传奇发布网 (0)
- 韩版传奇私服发布网 (0)
- 传世新开 (0)
- 传奇2外挂 (0)
- 传奇复古 (0)
- 单职业变态 (0)
- 传奇sf180 (0)
- 传世sf吧 (0)
- 传奇私服gm命令大全 (0)
- 传奇世界私服刷元宝 (0)
- dnf私服发布网 (0)
- 网页三国游戏排行榜 (0)
- 傲视千雄私服 (0)
- 热血传奇176客户端下载 (0)
- 最新机战私服 (0)
- 传奇吉吉免费版 (0)
- 蜘蛛纸牌游戏 (0)
- 最热门的网游排行榜 (0)
- 2d网游 (0)
- 网游私服排行榜 (0)
- moba网游 (0)
- 439小游戏 (0)
- 飞车小游戏 (0)
- 奥奇传说小游戏 (0)
- 龙斗士小游戏 (0)
- 下载手机游戏 (0)
- 维京神域之战 (0)
- 星际2单机 (0)
- 防守类单机游戏 (0)
- 灰烬攻略 (0)
- 战神3pc版 (0)
- 腐尸之屋 (0)
- 新神奇传说3秘籍 (0)
- 生化危机5怎么存档 (0)
- 三国群英传1单机版下载 (0)
- 捕鱼达人手机版 (0)
- 仙剑奇侠传游戏1 (0)
- 暴力摩托单机版下载 (0)
- fifa12下载 (0)
- 色单机游戏 (0)
- 三国志13修改器 (0)
- 梦幻西游单机版神剑情天3 (0)
- 对打单机游戏 (0)
- 三国群英传7补丁 (0)
- 最好单机游戏下载 (0)
- 流星蝴蝶剑秘籍大全 (0)
- 孢子 (0)
- 梦幻西游赚钱 (0)
- 梦幻西游答题 (0)
- 王者荣耀防沉迷 (0)
- 梦幻西游挖图技巧 (0)
- 梦幻岛游戏 (0)
- 轩辕剑网游官网 (0)
- 梦幻西游手游论坛 (0)
- 极道阴阳师 (0)
- 仙剑奇侠传3下载 (0)
- 七龙珠人物换装2 (0)
- 赛尔号手机版下载 (0)
- lol战队名字 (0)
- 黑湾海盗中文版 (0)
- 超级街霸4街机版 (0)
- 侵略行为 (0)
- 孤岛惊魂3结局 (0)
- 龙珠单机游戏大全 (0)
- 仙剑三游戏下载 (0)
- 策略单机游戏 (0)
- 家园2简体中文版 (0)
- 现代战争2 (0)