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 use pygame to realize Snake Game in python

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

Share

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

In this article Xiaobian for you to introduce in detail "how to use pygame to achieve gluttonous snake game in python", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to use pygame to achieve gluttonous snake game in python" can help you solve your doubts.

I. Preface

I tried to write tic-tac-toe chess with pygame before, but this time I played the Snake series.

The personal feeling module may be larger, so I chose to divide the function and the main logic code into two files.

Fuc is a function module, which stores event sensing and refresh interface.

The main module is the core of the game.

Second, build the interface

I won't repeat it here. You can take a look at this blog first.

There are all the basic elements of the interface.

Main.py

Import pygamefrom fuc import * # basic attributes lattice_wh = 20 # length and width _ color = (84,255,159) food_color = (255,64,64) # drawing interface pygame.init () screen = pygame.display.set_mode (25 eggs latticewings whoring 25 grains latticeWH)) pygame.display.set_caption ('gluttonous snake') # Game state game_stats = 1while game_stats: update (screen,lattice_wh) check_events ()

This time, I'm going to draw an interface of 25 squares, each of which is 20 in length and width.

This time the drawing is not the same as before. I'm going to use the pygame.draw.line () function to draw straight lines.

Pass in parameters:

Interface (screen)

Rbg color (here is (105,105,105), is light gray, right)

Start point coordinates, end point coordinates (coordinate system is available on the above blog)

Code section: (put it in the update function, because the grid needs to be redrawn with each refresh)

Def update (screen,lattice_wh): "" screen refresh "# background color screen.fill ((255255255)) # draw grid for i in range (25): pygame.draw.line (screen, (105,105,105), (0latticeLatitude whorei), (500THI latticeLatitude whorei) for i in range (25): pygame.draw.line (screen, (105,105,105) (lattice_wh*i,0), (lattice_wh*i,500)) pygame.display.flip ()

Fuc module:

Import pygameimport sys "" stores related functions "def update (screen,lattice_wh):" screen refresh "" # background color screen.fill ((255255255)) # drawing grid for i in range (25): pygame.draw.line (screen, (105,105,105), (0Query latticewaterWhitei), (500) Lattice_wh*i)) for i in range (25): pygame.draw.line (screen, (105,105,105), (lattice_wh*i,0), (lattice_wh*i,500)) pygame.display.flip () def check_events (): for event in pygame.event.get (): if event.type = = pygame.QUIT: sys.exit () here This article "how to use pygame to achieve gluttonous Snake Game in python" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it yourself. If you want to know more about the article, please pay attention to 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