1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
| def start_kgame_single(): pygame.mixer.music.play(-1) global score score = 0 run_direction = "right" run = run_direction head = Snake(160, 160) snake_body = [Snake(head.x, head.y + 20), Snake(head.x, head.y + 40), Snake(head.x, head.y + 60)] 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)))) 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)) for event in pygame.event.get(): if event.type == pygame.QUIT: show_end_single() elif event.type == pygame.KEYDOWN: if event.key == ord('w'): run_direction = "up" if event.key == ord('s'): run_direction = "down" if event.key == ord('a'): run_direction = "left" if event.key == ord('d'): run_direction = "right" for item in food_list: draw_food(item) draw_food(food) draw_snake(black, head) for item in snake_body: draw_snake(blue, item) 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 snake_body.insert(0, Snake(head.x, head.y)) if run == "up": head.y -= 20 elif run == "down": head.y += 20 elif run == "left": head.x -= 20 elif run == "right": head.x += 20 die_flag = False for body in snake_body[1:]: if head.x == body.x and head.y == body.y: die_flag = True if die_flag: pygame.mixer.music.stop() show_end() else: if head.x < 0: head.x = 960 if head.x > 960: head.x = 0 if head.y < 0: head.y = 600 if head.y > 600: head.y = 0 global flag flag = 0 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 food_list.pop(food_list.index(item)) food = new_food(head) food_list.append(food) break if flag == 0: snake_body.pop() font = pygame.font.SysFont("simHei", 25) mode_title = font.render('穿墙模式', False, grey) socre_title = font.render('得分: %s' % score, False, grey) window.blit(mode_title, (50, 30)) window.blit(socre_title, (50, 65)) pygame.display.update() clock.tick(8)
|