Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Bobble Dragon Mini Game by Pygame

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces how to achieve Bobble Dragon Mini Game by Pygame. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

Introduction

There are still many elimination games in the Python version, and Muzi has pushed a lot before.

For example: the ever-changing Xiaolu, remember? Today, there is an elimination class-Bobble Mini Game, I hope you like it!

"Bubble Music" is a game suitable for all-age players, using the very classic "Bubble Dragon" style to eliminate bubbles. There are not many innovative ways to play the game.

Easy to use. We can use it to pass the time when we are alone and no one is talking. Come on, follow Muzi and start playing Bubble Bobble.

Text 1. Preparation

1) rules of the game:

The game is played by players shooting colored beads from the marble launcher in the center below, which equals to three beads of the same color that will disappear when connected. Until the same bubble on the interface is completely eliminated, that is,

But to win, you can also compete with your little friends to see who uses fewer colored balls.

2) Environmental installation

The environment used in this article: Python3, Pycharm, Pygame and built-in.

Start tapping the code

1) Import module

Import math, pygame, sys, os, copy, time, randomimport pygame.gfxdrawfrom pygame.locals import *

2) main program

FPS = 120WINDOWWIDTH = 640WINDOWHEIGHT = 480TEXTHEIGHT = 20BUBBLERADIUS = 20BUBBLEWIDTH = BUBBLERADIUS * 2BUBBLELAYERS = 5BUBBLEYADJUST = 5STARTX = WINDOWWIDTH / 2STARTY = WINDOWHEIGHT-27ARRAYWIDTH = 16ARRAYHEIGHT = 14 RIGHT = 'right'LEFT =' left'BLANK ='.'# # COLORS # # R G BGRAY = (100,100,100) NAVYBLUE = (60,100,100) 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 ('Mini Game') 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 (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 (bubbleArrow] [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 = 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 () 3. Effect display

The space bar is the serve, the direction key is the remote control arrow.

1) running interface

2) three of the same color can be eliminated

3) end page

A ball is 10 points, the interface of the ball was eliminated by me a total of 591 to end the game! 2333, it's a little difficult.

On how to achieve Pygame Bobble Mini Game to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report