def start_game():
# Play music
pygame.mixer.music.play(-1)
# The global variable that defines the storage fraction
global score
score = 0
# Defines a variable that stores the direction of motion entered by the player's keyboard , Start right
run_direction = "right"
# A variable that defines the direction of motion of the greedy snake , Initially type the direction for the player
run = run_direction
# Instantiate snake and food objects
head = Snake(randint(0, 30) * 20, randint(0, 20) * 20)
# The length of the instantiated snake is 2 A unit of
snake_body = [Snake(head.x, head.y + 20), Snake(head.x, head.y + 40)]
# Instantiated food list , The list should be shortened as the food is eaten
food_list = [Food(randint(0, 45) * 20, randint(0, 28) * 20, (randint(10, 255), randint(10, 255), randint(10, 255)))]
for i in range(1,24):
food_list.append(Food(randint(0, 45) * 20, randint(0, 28) * 20, (randint(10, 255), randint(10, 255), randint(10, 255))))
# Instantiate a single food , Facilitate the generation of a single new food in the cycle
food = Food(randint(0, 45) * 20, randint(0, 28) * 20, (randint(10, 255), randint(10, 255), randint(10, 255)))
while True:
window.blit(background, (0,0))
# Monitor the movement direction value entered by the player's keyboard ,[ Energy futures ](https://www.gendan5.com/cf/ef.html) And convert to up、down、right or left, Call in convenient procedure
# pygame.event.get() Return a list , Store this game A series of events encountered during the execution of a program ( Store in chronological order )
for event in pygame.event.get():
# pygame.QUIT Event refers to the event that the user clicks on the top right corner of the window "×"
if event.type == pygame.QUIT:
# Display the result interface
show_end()
# If the event type is press the keyboard , branch ↑ ↓ ← → Four cases are discussed
elif event.type == pygame.KEYDOWN:
# If the event type is press the keyboard ↑
# key Is the key value , What is the key value to press
if event.key == ord('w'):
run_direction = "up"
# If the event type is press the keyboard ↓
if event.key == ord('s'):
run_direction = "down"
# If the event type is press the keyboard ←
if event.key == ord('a'):
run_direction = "left"
# If the event type is press the keyboard →
if event.key == ord('d'):
run_direction = "right"
# Draw initialized 25 Food image (24+1=25)
# As the food in the list is eaten , The list should be continuous pop To remove what has been eaten
for item in food_list:
draw_food(item)
# Draw a new food image after being eaten by a greedy snake
draw_food(food)
# Draw snake head image
draw_snake(black, head)
# Draw snake body image
for item in snake_body:
draw_snake(blue, item)
# Judging the direction of Snake Movement (run) The direction of motion entered with the player's keyboard (run_direction) Does it violate normal movement
if run == "up" and not run_direction == "down":
run = run_direction
elif run == "down" and not run_direction == "up":
run = run_direction
elif run == "left" and not run_direction == "right":
run = run_direction
elif run == "right" and not run_direction == "left":
run = run_direction
# Insert snake head position into snake body list
snake_body.insert(0, Snake(head.x, head.y))
# Snake head according to the direction entered by the player xy Update
if run == "up":
head.y -= 20
elif run == "down":
head.y += 20
elif run == "left":
head.x -= 20
elif run == "right":
head.x += 20
# To judge whether or not to die
die_flag = False
# Traverse the list of snake poses , From 1 Start ,( The first 0 A snake head )
for body in snake_body[1:]:
# If the snake head xy And snake body xy equal , Then the collision is determined , Set up flag by ture
if head.x == body.x and head.y == body.y:
die_flag = True
# If snake head xy Outside the display form , or flag by true, The end interface is displayed , And quit the game
if die_flag == True or head.x < 0 or head.x > 960 or head.y < 0 or head.y > 600:
# Stop playing music
pygame.mixer.music.stop()
show_end()
# die_snake(head, snake_body)
# Determine the coordinates of snake head and food , If equal , Then add points , And produce new food
# Define flag , Indicates whether something equal to the snake head has been found
global flag
flag = 0
# If the snake head coincides with the food
for item in food_list:
if head.x == item.x and head.y == item.y or head.x == food.x and head.y == food.y:
flag = 1
score += 1
# Pop up the eaten food
food_list.pop(food_list.index(item))
# Produce another food
food = new_food(head)
# Insert new food into food_list, The drawing will be updated in the next cycle
food_list.append(food)
break
if flag == 0:
snake_body.pop()
font = pygame.font.SysFont("simHei", 25)
mode_title = font.render(' Normal mode ', False, grey)
socre_title = font.render(' score : %s' % score, False, grey)
window.blit(mode_title, (50, 30))
window.blit(socre_title, (50, 65))
# Update the data of snake head, body and food
pygame.display.update()
# Set snake speed through frame rate
clock.tick(8)