In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
How to use python to make Tetris, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
Introduction
Why is there such a simple game? Is this game so popular?
Is it just because it appeared in an era of extreme scarcity in the gaming industry that it became the memory of a generation? I'm afraid not.
Anyone who has played Tetris knows that it feels like eating melon seeds, and once it starts, it will be as difficult as an addiction to stop, racking their brains to fill the gaps.
Ha ha ha! The editor will basically sort out some game code every week!
This article will bring you to develop a Mini Game Tetris!
Text
Rules of the game: plates of different shapes composed of small squares fall from the top of the screen one after another, and players adjust the position and direction of the plates to make them spell out a complete one or several at the bottom of the screen.
These complete stripes will then disappear, making room for the newly fallen plate, while players will be rewarded with points. Squares that have not been eliminated continue to pile up, and once they reach the top of the screen, the player loses and the game is over.
(1) definition of the game, different types of Tetris:
Class tetrisShape (): def _ _ init__ (self Shape=0): # empty block self.shape_empty = 0 # word block self.shape_I = 1 # L block self.shape_L = 2 # left L block self.shape_J = 3 # T block self.shape_T = 4 # field block self. Shape_O = 5 # reverse Z block self.shape_S = 6 # Z block self.shape_Z = 7
(2) obtain the relative coordinate distribution of the four small squares in the current rotation state of the shape:
Def getRotatedRelativeCoords (self, direction): # initial distribution if direction = = 0 or self.shape = = self.shape_O: return self.relative_coords # rotate 90 degrees counterclockwise if direction = = 1: return [[- y, x] for x Y in self.relative_coords] # rotate 180 degrees counterclockwise if direction = = 2: if self.shape in [self.shape_I, self.shape_Z, self.shape_S]: return self.relative_coords else: return [[- x,-y] for x Y in self.relative_coords] # rotate 270degrees counterclockwise if direction = = 3: if self.shape in [self.shape_I, self.shape_Z, self.shape_S]: return [[- y, x] for x, y in self.relative_coords] else: return [[y,-x] for x, y in self.relative_coords]
(3) the box of the game can be moved in different directions:
'' move right''def moveRight (self): if self.ableMove ([self.current_coord [0] + 1, self.current_coord [1]): self.current_coord [0] + = 1' 'move left' 'def moveLeft (self): if self.ableMove ([self.current_coord [0]-1) Self.current_coord [1]]): self.current_coord [0]-= 1''turn clockwise' 'def rotateClockwise (self): if self.ableMove (self.current_coord) (self.current_direction-1)% 4): self.current_direction = (self.current_direction-1)% 4''turn counterclockwise' 'def rotateAnticlockwise (self): if self.ableMove (self.current_coord) (self.current_direction+1)% 4): self.current_direction = (self.current_direction+1)% 4''move Down' 'def moveDown (self): removed_lines = 0 if self.ableMove ([self.current_coord [0], self.current_coord [1] + 1]): self.current_coord [1] + = 1 else: x_min, x_max Y_min, y_max = self.current_tetris.getRelativeBoundary (self.current_direction) # for simplicity, if self.current_coord [1] + y_min determines the end of the game if it exceeds the screen.
< 0: self.is_gameover = True return removed_lines self.mergeTetris() removed_lines = self.removeFullLines() self.createNewTetris() return removed_lines '''坠落''' def dropDown(self): removed_lines = 0 while self.ableMove([self.current_coord[0], self.current_coord[1] + 1]): self.current_coord[1] += 1 x_min, x_max, y_min, y_max = self.current_tetris.getRelativeBoundary(self.current_direction) # 简单起见, 有超出屏幕就判定游戏结束 if self.current_coord[1] + y_min < 0: self.is_gameover = True return removed_lines self.mergeTetris() removed_lines = self.removeFullLines() self.createNewTetris() return removed_lines (4)合并俄罗斯方块(最下面定型不能再动的那些): def mergeTetris(self): for x, y in self.current_tetris.getAbsoluteCoords(self.current_direction, self.current_coord[0], self.current_coord[1]): self.board_data[x + y * self.width] = self.current_tetris.shape self.current_coord = [-1, -1] self.current_direction = 0 self.current_tetris = tetrisShape() (5)当每行铺满之后会得分,相应的消失一行: '''移出整行都有小方块的''' def removeFullLines(self): new_board_data = [0] * self.width * self.height new_y = self.height - 1 removed_lines = 0 for y in range(self.height - 1, -1, -1): cell_count = sum([1 if self.board_data[x + y * self.width] >0 else 0 for x in range (self.width)]) if cell_count < self.width: for x in range (self.width): new_board_ data [x + new_y * self.width] = self.board_ data [x + y * self.width] new_y-= 1 else: removed_lines + = 1 self.board_data = new_board_data return removed_lines effect:
Ha ha ha! All right! Press and hold the arrow key can also deform ha! Try it quickly.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.