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 the Brick Game in Python Pygame

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article Xiaobian for you to introduce in detail "Python Pygame how to achieve the brick game", the content is detailed, the steps are clear, the details are handled properly, I hope that this "Python Pygame how to achieve the brick game" article can help you solve your doubts, following the editor's ideas slowly in-depth, together to learn new knowledge.

Development tools

Python version: 3.6.4

Related modules:

Pygame module

And some modules that come with Python.

Environment building

Install Python and add it to the environment variable, and pip installs the relevant modules you need.

Effect display

Just run the Game18.py file in the cmd window.

The effect is as follows:

Video link

Brief introduction of principle

Rules of the game (excerpt from Wikipedia):

Beating bricks is a kind of video game. There are several layers of bricks at the top of the screen, and a ball bounces between the brick at the top of the screen and the wall, the mobile board at the bottom of the screen and the walls on both sides. When the ball hits the brick, the ball will bounce and the brick will disappear. Players want to control the board at the bottom of the screen, so that the "ball" through the impact to eliminate all the "bricks", the ball hits the bottom edge of the screen will disappear, all the balls disappear and the game fails. You can break through by removing all the bricks.

Board operation: press "→" to the right and "←" to the left.

Step by step:

The implementation of the game is actually very simple. First of all, we define three game genie classes according to the rules of the game, which are:

Board class

Ball type

Bricks.

The advantage of defining the game wizard first is to facilitate the collision detection between the subsequent game elves and the operation and management of the game elves. Specifically, for the board class, it should have functions such as moving according to the player's operation, and its code implementation is as follows:

Class Paddle (pygame.sprite.Sprite): def _ init__ (self, x, y, width, height, SCREENWIDTH, SCREENHEIGHT, * * kwargs): pygame.sprite.Sprite.__init__ (self) self.init_state = [x, y, width, height] self.rect = pygame.Rect (x, y, width) Height) self.base_speed = 10 self.SCREENWIDTH = SCREENWIDTH self.SCREENHEIGHT = SCREENHEIGHT''move the board' 'def move (self, direction): if direction =' left': self.rect.left = max (0, self.rect.left-self.base_speed) elif direction = 'right': self.rect.right = min (self.SCREENWIDTH) Self.rect.right+self.base_speed) else: raise ValueError ('Paddle.move.direction unsupport...'% direction) return True 'bind to the screen' 'def draw (self, screen, color): pygame.draw.rect (screen, color) Self.rect) return True''reset' 'def reset (self): self.rect = pygame.Rect (self.init_state [0], self.init_state [1], self.init_state [2], self.init_state [3]) return True

For balls, the way they move is controlled by the computer (such as automatically changing directions when hitting a wall, etc.). The code is implemented as follows:

Class Ball (pygame.sprite.Sprite): def _ init__ (self, x, y, radius, SCREENWIDTH, SCREENHEIGHT, * * kwargs): pygame.sprite.Sprite.__init__ (self) self.init_state = [x, y, radius*2, radius*2] self.rect = pygame.Rect (x, y, radius*2, radius*2) self.base_speed = [5 5] self.direction = [random.choice ([1,-1]) -1] self.radius = radius self.SCREENWIDTH = SCREENWIDTH self.SCREENHEIGHT = SCREENHEIGHT''moving Ball' 'def move (self): self.rect.left + = self.direction [0] * self.base_speed [0] self.rect.top + = self.direction [1] * self.base_speed [1] if self.rect.left = self.SCREENWIDTH: Self.rect.right = self.SCREENWIDTH self.direction [0] =-self.direction [0] if self.rect.top = self.SCREENHEIGHT: return False return True 'change the speed and direction (when colliding with the beat)' 'def change (self): self.base_speed = [random.choice ([4]) 5, 6]), random.choice ([4, 5, 6])] self.direction = [random.choice ([1,-1]),-1] return True''bind to the screen' 'def draw (self, screen, color): pygame.draw.circle (screen, color, (self.rect.left+self.radius, self.rect.top+self.radius) Self.radius) return True''reset' 'def reset (self): self.rect = pygame.Rect (self.init_state [0], self.init_state [1], self.init_state [2], self.init_state [3]) return True

For the brick class, it is relatively simple, and the code implementation is as follows:

Class Brick (pygame.sprite.Sprite): def _ init__ (self, x, y, width, height, * * kwargs): pygame.sprite.Sprite.__init__ (self) self.init_state = [x, y, width, height] self.rect = pygame.Rect (x, y, width, height)''bind to the screen def draw (self, screen) Color): pygame.draw.rect (screen, color, self.rect) return True''reset' 'def reset (self): self.rect = pygame.Rect (self.init_state [0], self.init_state [1], self.init_state [2], self.init_state [3]) return True

Then, as before, get a few more levels, and each level map is defined with a .level file, such as this:

Where B represents the location of the brick.

OK, next you can consider implementing the main cycle of the game. The basic logic is:

That is to say, at the end of each clearance, you will judge whether you have passed the customs or GG, and if you have passed the customs, you will enter the next level, otherwise you will directly enter the end interface. Of course, the last level is an exception, because after it is over, we must enter the end interface. Specifically, the main logic code is implemented as follows:

Def run (self): while True: self.__startInterface () for idx Levelpath in enumerate (self.cfg.LEVELPATHS): state = self.__runLevel (levelpath) if idx = = len (self.cfg.LEVELPATHS)-1: break if state = = 'win': self.__nextLevel () else: break if state = =' fail': self.__ EndInterface (False) else: self.__endInterface (True) reads here This article "how to play the Brick Game with Python Pygame" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about the article, please follow the industry information channel.

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