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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
| def start_game_double(): pygame.mixer.music.play(-1) global score1 global score2 score1 = score2 = 0 run_direction1 = "right" run_direction2 = "up" run1 = run_direction1 run2 = run_direction2 head1 = Snake(randint(0, 30) * 20, randint(0, 20) * 20) head2 = Snake(randint(0, 30) * 20, randint(0, 20) * 20) snake_body1 = [Snake(head1.x, head1.y + 20), Snake(head1.x, head1.y + 40)] snake_body2 = [Snake(head2.x, head2.y + 20), Snake(head2.x, head2.y + 40)] 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() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: run_direction2 = "up" if event.key == pygame.K_DOWN: run_direction2 = "down" if event.key == pygame.K_LEFT: run_direction2 = "left" if event.key == pygame.K_RIGHT: run_direction2 = "right" if event.key == ord('w'): run_direction1 = "up" if event.key == ord('s'): run_direction1 = "down" if event.key == ord('a'): run_direction1 = "left" if event.key == ord('d'): run_direction1 = "right" for item in food_list: draw_food(item) draw_food(food) if len(snake_body1) != 0: draw_snake(black, head1) if len(snake_body2) != 0: draw_snake(black, head2) for item in snake_body1: draw_snake(blue, item) for item in snake_body2: draw_snake(green, item) if len(snake_body1) != 0: snake_body1.insert(0, Snake(head1.x, head1.y)) if len(snake_body2) != 0: snake_body2.insert(0, Snake(head2.x, head2.y)) if run1 == "up" and not run_direction1 == "down": run1 = run_direction1 if run1 == "down" and not run_direction1 == "up": run1 = run_direction1 if run1 == "left" and not run_direction1 == "right": run1 = run_direction1 if run1 == "right" and not run_direction1 == "left": run1 = run_direction1 if run2 == "up" and not run_direction2 == "down": run2 = run_direction2 if run2 == "down" and not run_direction2 == "up": run2 = run_direction2 if run2 == "left" and not run_direction2 == "right": run2 = run_direction2 if run2 == "right" and not run_direction2 == "left": run2 = run_direction2 if run1 == "up": head1.y -= 20 if run1 == "down": head1.y += 20 if run1 == "left": head1.x -= 20 if run1 == "right": head1.x += 20 if run2 == "up": head2.y -= 20 if run2 == "down": head2.y += 20 if run2 == "left": head2.x -= 20 if run2 == "right": head2.x += 20 die_flag1 = die_flag2 = False for body in snake_body1[1:]+snake_body2[1:]: if head1.x == body.x and head1.y == body.y: die_flag1 = True if head2.x == body.x and head2.y == body.y: die_flag2 = True if die_flag1 == True or head1.x < 0 or head1.x > 960 or head1.y < 0 or head1.y > 600: snake_body1.clear() if die_flag2 == True or head2.x < 0 or head2.x > 960 or head2.y < 0 or head2.y > 600: die_flag2 = True snake_body2.clear() if len(snake_body1) == 0 and len(snake_body2) == 0: show_end() global flag1 global flag2 flag1 = flag2 = 0 for item in food_list: if len(snake_body1) != 0 and (head1.x == item.x and head1.y == item.y or head1.x == food.x and head1.y == food.y): flag1 = 1 score1 += 1 food_list.pop(food_list.index(item)) food = new_food(head1) food_list.append(food) break elif len(snake_body2) != 0 and head2.x == item.x and head2.y == item.y or head2.x == food.x and head2.y == food.y: flag2 = 1 score2 += 1 food_list.pop(food_list.index(item)) food = new_food(head2) food_list.append(food) break if len(snake_body1) != 0 and flag1 == 0: snake_body1.pop() if len(snake_body2) != 0 and flag2 == 0: snake_body2.pop () font = pygame.font.SysFont("simHei", 25) mode_title1 = mode_title2 = font.render('正常模式', False, grey) socre_title1 = font.render('得分: %s' % score1, False, grey) socre_title2 = font.render('得分: %s' % score2, False, grey) window.blit(mode_title1, (50, 30)) window.blit(socre_title1, (50, 65)) window.blit(mode_title2, (800, 30)) window.blit(socre_title2, (800, 65)) pygame.display.update() clock.tick(8)
|