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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
| import turtle import time import random
global pre_direction,snake,snake_hide,monster,head,food,pen,pen1,num_array,food_num,wn,contact,delay,num_str,count,pre_x,pre_y,starttime ,endtime, last_length,time_eated,time_win
monster = turtle.Turtle() monster.shape("square") monster.color("purple") monster.penup() monster.goto(200, 200) monster.direction = "Stop" pre_direction = "Stop" monster.speed(1)
pen = turtle.Turtle() pen.speed(0) pen.shape("square") pen.color("black") pen.penup() pen.hideturtle() pen.goto(0, 210) pen.write("Score : 0 High Score : 0", align="center", font=("candara", 24, "bold"))
pen1 = turtle.Turtle() pen1.speed(0) pen1.shape("square") pen1.color("black") pen1.penup() pen1.hideturtle() pen1.goto(-250, 210) pen1.pendown() pen1.forward(500) pen1.right(90) pen1.forward(500) pen1.right(90) pen1.forward(500) pen1.right(90) pen1.forward(580) pen1.right(90) pen1.forward(500) pen1.right(90) pen1.forward(580)
starttime = time.time() endtime = time.time() count = 3 stop_flag = 0 contact = 0 delay = 0.1 high_contact = 0
wn = turtle.Screen() wn = turtle.Screen() wn.title("Snake Game") wn.bgcolor("white")
wn.setup(width=580, height=660) wn.tracer(0)
def group(): if head.direction != "down": head.direction = "up"
def godown(): global monster, head, food, pen, pen1, num_array, food_num, wn, contact, delay, num_str, count if head.direction != "up": head.direction = "down"
def goleft(): global monster, head, food, pen, pen1, num_array, food_num, wn, contact, delay, num_str, count if head.direction != "right": head.direction = "left"
def goright(): global monster, head, food, pen, pen1, num_array, food_num, wn, contact, delay, num_str, count if head.direction != "left": head.direction = "right"
def stop(): global monster, head, food, pen, pen1, num_array, food_num, wn, contact, delay, num_str, count,pre_direction
if head.direction != "stop": pre_direction = head.direction head.direction = "stop" return if head.direction == "stop": head.direction = pre_direction print("之前动作",pre_direction) print("尝试恢复",head.direction) return
def del_num(): global monster, head, food, pen, pen1, num_array, food_num, wn, contact, delay, num_str, count if len(num_array) > 0: for i in reversed(range(len(num_array))): if num_array[i] == num_str: del num_array[i] def move(): global monster, head, food, pen, pen1, num_array, food_num, wn, contact, delay, num_str, count if head.direction == "up": y = head.ycor() head.sety(y + 20) if head.direction == "down": y = head.ycor() head.sety(y - 20) if head.direction == "left": x = head.xcor() head.setx(x - 20) if head.direction == "right": x = head.xcor() head.setx(x + 20)
wn.listen() wn.onkeypress(group, "w") wn.onkeypress(stop," ") wn.onkeypress(godown, "s") wn.onkeypress(goleft, "a") wn.onkeypress(goright, "d")
def staus_upgrade(): global monster, head, food, pen, pen1, num_array, food_num, wn, contact, delay, num_str, count,starttime ,endtime pen.clear() endtime = time.time() time_c = int(endtime - starttime) pen.write("Contact: {} Time: {} Motion {} ".format( contact, time_c, head.direction), align="center", font=("candara", 18, "bold"))
def food_born():
global food_num,foods,num_array,num_str,foods,num_array1 food_x_array = [] for i in range(0,food_num,1): food_x_array.append(random.randrange(-200,200)) food_y_array = [] for i in range(0,food_num,1): food_y_array.append(random.randrange(-270,200)) foods= [] print("food_num",food_num,"num_array",num_array) for i in reversed(range(0,food_num,1)): food = turtle.Turtle() food.speed(0) food.shape("square") food.color("black") food.penup() food.hideturtle() food.goto(food_x_array[i], food_y_array[i]) num_str =str(num_array[i]) up_or_down = random.choice([0,1]) if up_or_down == 0: food.write(num_str, align="center", font=("candara", 24, "bold")) else: pass foods.append(food)
def If_Over(): global foods global collision_flag,time_eated, time_win, monster, head, food, pen, pen1, num_array, food_num, wn, contact, delay, num_str, count, starttime, endtime,last_length if food_num == 0: food.clear() print("here") time_win = time.time() print(int(time_win - time_eated)) if int(time_win - time_eated) > 2: for index in range(0,len(foods),1): foods[index].clear() foods= [] time.sleep(1) head.goto(1000, 1000) head.direction = "stop" head.clear() for segment in segments: segment.goto(1000, 1000) segments.clear() contact = 0 delay = 0.1 pen.write("Contact: {} Time: {} Motion {} ".format(0, 0, 0), align="center", font=("Arial", 24, "bold")) init() if collision_flag or head.xcor() > 250 or head.xcor() < -250 or head.ycor() > 210 or head.ycor() < -290: time.sleep(1) head.goto(1000, 1000) head.direction = "stop" head.clear() monster.goto(200,200) for segment in segments: segment.goto(1000, 1000) segments.clear() contact = 0 delay = 0.1 pen.write("Contact: {} Time: {} Motion {} ".format(0, 0, 0), align="center",font=("Arial", 24, "bold")) init()
def food_eated(): global snake_hide,monster, head, food, pen, pen1, num_array, food_num, wn, contact, delay, num_str, count, last_length,time_eated global foods,food_x_array,food_y_array,num_array1 for index in reversed(range(0,len(foods))): print("index--------------------------",index) if head.distance(foods[index]) < 20: if food_num > 0: food_num -= 1 print("index......",index,"num_aray1",num_array1) foods[index].clear del foods[index] print("clear", num_array1[index]) contact += int(num_array1[index]) time_eated = time.time() if int(num_array1[index]) > 0: snake_hide += int(num_array1[index]) for i in range(int(num_array1[index])): new_segment = turtle.Turtle() new_segment.speed(0) new_segment.shape("square") new_segment.color(random.choice(["green", "red", "blue", "black", "yellow", "pink"])) new_segment.penup() new_segment.setx(1000) new_segment.sety(1000) segments.append(new_segment) print("len seg", len(segments)) if food_num > 0: food_born() print("food_num ", food_num) delay -= 0.001 staus_upgrade()
def seg_move(): global monster, head, food, pen, pen1, num_array, food_num, wn, contact, delay, num_str, count,pre_x,pre_y if head.direction != "stop": if len(segments) > 0: pre_x = segments[0].xcor() pre_y = segments[0].ycor() x = head.xcor() y = head.ycor() segments[0].goto(x, y) for index in range(len(segments) - 1, 0, -1): if index == 1: segments[1].goto(pre_x, pre_y) return x = segments[index - 1].xcor() y = segments[index - 1].ycor() segments[index].goto(x, y)
def chase(): global monster, head, food, pen, pen1, num_array, food_num, wn, contact, delay, num_str, count x = head.xcor() y = head.ycor() x1 = monster.xcor() y1 = monster.ycor() if abs(y1 - y) > abs(x1 - x): if y1 < y: monster.seth(90) monster.forward(20) else: monster.seth(270) monster.forward(20) else: if x1 < x: monster.seth(0) monster.forward(20) else: monster.seth(180) monster.forward(20)
def if_chased(): global snake,collision_flag,segments,head,monster for index in range(0,len(snake),1): if snake[index].distance(monster) <20: time.sleep(1) head.goto(0, 0) head.direction = "stop" print("here..........qqqq.....................") colors = random.choice(['red', 'blue', 'green']) shapes = random.choice(['square', 'circle']) for segment in segments: segment.goto(1000, 1000) segment.clear() collision_flag = 1
def if_head_runto_body(): global collision_flag,segments for segment in segments: if segment.distance(head) < 20 and snake_hide == 0: time.sleep(1) head.goto(0, 0) head.direction = "stop" print("here...............................") colors = random.choice(['red', 'blue', 'green']) shapes = random.choice(['square', 'circle']) for segment in segments: segment.goto(1000, 1000) segment.clear() collision_flag = 1 contact = 0 delay = 0.1
def init(): global monster, head, food, pen, pen1, num_array, food_num, wn, contact, delay, num_str, count, starttime , endtime global segments,snake_hide,collision_flag,food_x_array ,food_y_array ,foods,num_array1 collision_flag = 0 starttime = time.time() endtime = time.time() segments = []
head = turtle.Turtle() head.shape("square") head.color("red") head.penup() head.goto(0, 0) head.direction = "stop" pre_direction = "stop" segments = []
num_array = ['1', '2', '3', '4', '5'] num_array1 = ['1', '2', '3', '4', '5'] food_num = 5 food_born() snake_hide = 5 for i in range(5): new_segment = turtle.Turtle() new_segment.speed(0) new_segment.shape("square") new_segment.color(random.choice(["green", "red", "blue", "black", "yellow", "pink"])) new_segment.penup() new_segment.setx(1000) new_segment.sety(1000) segments.append(new_segment)
def Game_Start(): global snake,snake_hide,monster,head,food,pen,pen1,num_array,food_num,wn,contact,delay,num_str,count,last_length,segments wn.update() count -= 1 if count == 0: count = random.choice([ 2, 3, 4]) chase() food_eated() snake = [] snake.append(head) for i in range(0,len(segments),1): snake.append(segments[i]) If_Over() seg_move()
move() if snake_hide > 0 and head.direction != "stop": snake_hide -= 1 print("hide'''''",snake_hide) if_head_runto_body() if_chased() staus_upgrade() wn.ontimer(Game_Start, 100)
init() seg_move() Game_Start() wn.mainloop()
|