├── .gitattributes └── snake.py /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /snake.py: -------------------------------------------------------------------------------- 1 | #imports 2 | import turtle 3 | import time 4 | import random 5 | 6 | delay = 0.1 7 | 8 | #scores 9 | score = 0 10 | high_score = 0 11 | 12 | #set up screen 13 | wn = turtle.Screen() 14 | wn.title("Snake Game") 15 | wn.bgcolor('yellow') 16 | wn.setup(width=600, height=600) 17 | wn.tracer(0) 18 | 19 | #snake head 20 | head = turtle.Turtle() 21 | head.speed(0) 22 | head.shape("square") 23 | head.color("black") 24 | head.penup() 25 | head.goto(0,0) 26 | head.direction = "stop" 27 | 28 | #snake food 29 | food= turtle.Turtle() 30 | food.speed(0) 31 | food.shape("square") 32 | food.color("red") 33 | food.penup() 34 | food.goto(0,100) 35 | 36 | segments = [] 37 | 38 | #scoreboards 39 | sc = turtle.Turtle() 40 | sc.speed(0) 41 | sc.shape("square") 42 | sc.color("black") 43 | sc.penup() 44 | sc.hideturtle() 45 | sc.goto(0,260) 46 | sc.write("score: 0 High score: 0", align = "center", font=("ds-digital", 24, "normal")) 47 | 48 | #Functions 49 | def go_up(): 50 | if head.direction != "down": 51 | head.direction = "up" 52 | def go_down(): 53 | if head.direction != "up": 54 | head.direction = "down" 55 | def go_left(): 56 | if head.direction != "right": 57 | head.direction = "left" 58 | def go_right(): 59 | if head.direction != "left": 60 | head.direction = "right" 61 | def move(): 62 | if head.direction == "up": 63 | y = head.ycor() 64 | head.sety(y+20) 65 | if head.direction == "down": 66 | y = head.ycor() 67 | head.sety(y-20) 68 | if head.direction == "left": 69 | x = head.xcor() 70 | head.setx(x-20) 71 | if head.direction == "right": 72 | x = head.xcor() 73 | head.setx(x+20) 74 | 75 | #keyboard bindings 76 | wn.listen() 77 | wn.onkeypress(go_up, "w") 78 | wn.onkeypress(go_down, "s") 79 | wn.onkeypress(go_left, "a") 80 | wn.onkeypress(go_right, "d") 81 | 82 | #MainLoop 83 | while True: 84 | wn.update() 85 | 86 | #check collision with border area 87 | if head.xcor()>290 or head.xcor()<-290 or head.ycor()>290 or head.ycor()<-290: 88 | time.sleep(1) 89 | head.goto(0,0) 90 | head.direction = "stop" 91 | 92 | #hide the segments of body 93 | for segment in segments: 94 | segment.goto(1000,1000) #out of range 95 | #clear the segments 96 | segments.clear() 97 | 98 | #reset score 99 | score = 0 100 | 101 | #reset delay 102 | delay = 0.1 103 | 104 | sc.clear() 105 | sc.write("score: {} High score: {}".format(score, high_score), align="center", font=("ds-digital", 24, "normal")) 106 | 107 | #check collision with food 108 | if head.distance(food) <20: 109 | # move the food to random place 110 | x = random.randint(-290,290) 111 | y = random.randint(-290,290) 112 | food.goto(x,y) 113 | 114 | #add a new segment to the head 115 | new_segment = turtle.Turtle() 116 | new_segment.speed(0) 117 | new_segment.shape("square") 118 | new_segment.color("black") 119 | new_segment.penup() 120 | segments.append(new_segment) 121 | 122 | #shorten the delay 123 | delay -= 0.001 124 | #increase the score 125 | score += 10 126 | 127 | if score > high_score: 128 | high_score = score 129 | sc.clear() 130 | sc.write("score: {} High score: {}".format(score,high_score), align="center", font=("ds-digital", 24, "normal")) 131 | 132 | #move the segments in reverse order 133 | for index in range(len(segments)-1,0,-1): 134 | x = segments[index-1].xcor() 135 | y = segments[index-1].ycor() 136 | segments[index].goto(x,y) 137 | #move segment 0 to head 138 | if len(segments)>0: 139 | x = head.xcor() 140 | y = head.ycor() 141 | segments[0].goto(x,y) 142 | 143 | move() 144 | 145 | #check for collision with body 146 | for segment in segments: 147 | if segment.distance(head)<20: 148 | time.sleep(1) 149 | head.goto(0,0) 150 | head.direction = "stop" 151 | 152 | #hide segments 153 | for segment in segments: 154 | segment.goto(1000,1000) 155 | segments.clear() 156 | score = 0 157 | delay = 0.1 158 | 159 | #update the score 160 | sc.clear() 161 | sc.write("score: {} High score: {}".format(score,high_score), align="center", font=("ds-digital", 24, "normal")) 162 | time.sleep(delay) 163 | wn.mainloop() --------------------------------------------------------------------------------