In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces you how to make gluttonous snake game by Python. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.
Foreword:
The article uses Python pygame to make a gluttonous Mini Game and explains clearly what each piece of code is for.
It is said that the Snake game is a classic arcade game launched by Gremlin in 1976. Well, the gluttonous Snake Mini Game we make with Python today is a pixel version. Although it is simple and crude, it can still be played.
What we need to do is as follows:
Create a game window
Draw gluttonous snakes and food
Snakes eat food.
The chessboard model of the gluttonous snake:
Let's start our code now, first of all, import the module:
Import pygameimport randomimport copy1. Create the game window 1.1Game initialization pygame.init () clock = pygame.time.Clock () # set the game clock pygame.display.set_caption ("Snake-answers, source code, related information can be trusted me") # initialize title screen = pygame.display.set_mode The size of the initialization window form is 5001.2. Initialize the position of the snake. The length of the snake is 10 10, that is, the snake's X Y coordinate snake_list = [10, 10].
First set a running direction of the snake, and then judge that the keyboard event is determining the running direction of the snake.
The snake can run, so the next thing is to eat food to increase its length and not to eat food in different locations.
Initial direction of the snake:
Move_up = Falsemove_down = Falsemove_left = Falsemove_right = True1.3 initialize the food x = random.randint (10,490) y = random.randint (10,490) food_point = [x, y] 1.4 start the game cycle running = Truewhile running: # Game clock refresh frequency clock.tick (20) 1.5 fill background white screen.fill ([255,255,255]) 1.6 draw background for x in range (0501) 10): pygame.draw.line (screen, (195,197,199), (x, 0), (x, 500), 1) pygame.draw.line (screen, (195,197,199), (0,x), (500,x), 1) food_rect = pygame.draw.circle (screen, [255,0,0], food_point, 15,0)
Snake_rect = [] for pos in snake_list: # 1.7.1 draw the snake's body snake_rect.append (pygame.draw.circle (screen, [255,0,0], pos, 5,0))
two。 Draw gluttonous snake and food 2.1 to get the length of the snake, move the snake's body pos = len (snake_list)-1while pos > 0: snake_ list [pos] = copy.deepcopy (snake_ list [pos-1]) pos-= 12.2 change the snakehead position if move_up: snake_ list [pos] [1]-= 10 if snake_ list [pos] [1]
< 0: snake_list[pos][1] = 500if move_down: snake_list[pos][1] += 10 if snake_list[pos][1] >Snake_ list [pos] [1] = 0if move_left: snake_ list [pos] [0]-= 10 if snake_ list [pos] [0]
< 0: snake_list[pos][0] = 500if move_right: snake_list[pos][0] += 10 if snake_list[pos][0] >500: snake_ list [pos] [0] = 02.3Keyboard control mobile position for event in pygame.event.get (): # print (event) # judge the key pressed if event.type = = pygame.KEYDOWN: # up key if event.key = = pygame.K_UP: move_up = True move_down = False move_left = False move_right = False # Down key if event.key = = pygame.K_DOWN: move_up = False move_down = True move_left = False move_right = False # left key if event.key = = pygame.K_LEFT: move_up = False move_down = False Move_left = True move_right = False # right if event.key = = pygame.K_RIGHT: move_up = False move_down = False move_left = False move_right = True2.4 get the length of the snake Moving snake body pos = len (snake_list)-1while pos > 0: snake_ list [pos] = copy.deepcopy (snake_ list [pos-1]) pos-= 1
3. Snake eating Food 3.1 collision Detection if Snake eats Food if food_rect.collidepoint (pos): # Snake eats Food snake_list.append (food_point) # reset Food position food_point = [random.randint (10,490), random.randint (10,490)] food_rect = pygame.draw.circle (screen, [255,0,0], food_point, 15 0) break3.2 if the snake eats itself head_rect = snake_rect [0] count = len (snake_rect) while count > 1: if head_rect.colliderect (snake_ count [count-1]): running = False count-= 1pygame.display.update ()
About Python how to make gluttonous snake game to share here, hope that the above content can have some help to everyone, 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.
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.