In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how python realizes the simple Tetris game. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
What are the advantages of Python: 1. Easy to use. Compared with traditional languages such as Java, Java and C #, Python has less strict requirements on code format. 2. Python is open source and everyone can see the source code and can be ported to many platforms. 3. Python is object-oriented and can support both process-oriented programming and object-oriented programming. 4. Python is an explanatory language, the program written by Python does not need to be compiled into binary code, and can be run directly from the source code; 5. Python is powerful and has many modules, which can basically achieve all the common functions.
Import pygame,sys,random,timeall_block = [0rect 0], [0recast 1], [0rect 2], [[0rect 0], [0recory 1], [1recory 1], [1recory 0], [0recast 0], [0mmel 1], [- 1je 0], [- 1je 1], [- 1je 1]], [- 1je 0], [- 1je 1], [- 1je 0], [[0je 0], [0je 1], [1je 0], [0] -1]], [[0rect 0], [1je 0], [- 1je 0], [1je Rue 1]], [[0je 0], [1je 0], [- 1je 0] ] background = [[0 for column in range (0je 10)] for row in range (0je 22)] background [0] = [1 for column in range (0J 10)] select_block = list (random.choice (all_block)) block_initial_position = [21 J 5] times = 0score = [0] gameover = [] press = Falsepygame.init () screen = pygame.display.set_mode ((250500)) title = pygame.display.set_caption ("Tetris") # whereabouts, position, array detection, score, score Screen information def block_move_down (): y_drop=block_initial_position [0] x_move=block_initial_position [1] y_drop-=1 for row Column in select_block: row+=y_drop column+=x_move if background [row] [column] = = 1: break else: block_initial_position.clear () block_initial_position.extend ([yawdropMagazine xroommove]) return yawdropMagazine xportable moveblock initializedposition for row Column in select_block: background [y _ drop+row] [x_move+column] = 1 complete_row= [] for row in range (1Magne21): if 0 not in background [row]: complete_row.append (row) complete_row.sort (reverse=True) for row in complete_row: background.pop (row) background.append ([0 for column in range (0mem10)]) score [0] + = len (complete_row) pygame.display.set_caption (str (score [0]) + 'min') select_block.clear () select_block.extend (list (random.choice (all_block)) block_initial_position.clear () block_initial_position.extend ([20L5]) y_drop X_move=block_initial_position for row,column in select_block: row+=y_drop column+=x_move if background [row] [column]: gameover.append (1) # Box setting, change, background change def new_draw (): ykindrop for row,column in select_block: row+=y_drop column+=x_move pygame.draw.rect (screen,) (column*25500-row*25,23,23)) for row in range (0jue 20): for column in range (0je 10): bottom_block= background [row] [column] if bottom_block: pygame.draw.rect (screen, (0meme 0255), (column*25500-row*25,23,23)) # the movement of squares Prevent going out of bounds Collision def move_left_right (n): collision def move_left_right (n): yroomdropMagic XeroveBlockblock initializedposition x_move+=n for row,column in select_block: row+=y_drop column+=x_move if column9 or background [row] [column]: break else: block_initial_position.clear () block_initial_position.extend ([yawdropMagazine xroommove]) # rotate The positions are all changed def rotate (): y-column,row _ drop _ Column in rotating_position: row+=y_drop column+=x_move if column9 or background [row] [column]: break else: select_block.clear () select_block.extend (rotating_position) while True: screen.fill (255255255) for event in pygame.event.get (): if event.type==pygame.QUIT: sys.exit () Elif event.type==pygame.KEYDOWN and event.key==pygame.K_LEFT: move_left_right (- 1) elif event.type==pygame.KEYDOWN and event.key==pygame.K_RIGHT: move_left_right (1) elif event.type==pygame.KEYDOWN and event.key==pygame.K_UP: rotate () elif event.type==pygame.KEYDOWN and event.key==pygame.K_DOWN: press=True Elif event.type==pygame.KEYUP and event.key==pygame.K_DOWN: press=False if press: times+=10 if times > = 50: block_move_down () times=0 else: times+=1 if gameover: sys.exit () new_draw () pygame.time.Clock (). Tick
Effect:
The above is how python implements the simple Tetris game. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to 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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
This article mainly introduces the relevant knowledge of "how to use WeChat Mini Programs selector". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to use WeChat Mini Programs selector" can help you solve the problem. Page structure
Wechat
About us
Contact us
Product review
car news
thenatureplanet
More Form oMedia:
AutoTimes.
Bestcoffee.
SL News.
Jarebook.
Coffee Hunters.
Sundaily.
Modezone.
NNB.
Coffee.
Game News.
FrontStreet.
GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.