├── README.md └── Snake_by_DCM.py /README.md: -------------------------------------------------------------------------------- 1 | # Snake-Python 2 | Simple snake game using turtle module. 3 | 4 | ![alt tag](http://i.imgur.com/KIxtK1M.png) 5 | -------------------------------------------------------------------------------- /Snake_by_DCM.py: -------------------------------------------------------------------------------- 1 | # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 2 | # Dinis Marques # 3 | # Snake Python # 4 | # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 5 | import turtle 6 | import random 7 | 8 | #head orientation 9 | h = [0] 10 | 11 | #score 12 | a = [0] 13 | b = [0] 14 | 15 | #food coord 16 | fcoord = [0,0,0] 17 | 18 | #position 19 | pos = [] 20 | 21 | 22 | def home(x,y): 23 | x = 0 24 | y = 0 25 | a[0] = 0 26 | b[0] = 0 27 | h[0] = 0 28 | fcoord[2] = 0 29 | pos[:] = [] 30 | turtle.hideturtle() 31 | turtle.clear() 32 | turtle.pu() 33 | turtle.color("black") 34 | turtle.goto(0,0) 35 | turtle.write("Play") 36 | turtle.title("Snake") 37 | turtle.onscreenclick(start) 38 | turtle.mainloop() 39 | 40 | def level_1(): 41 | turtle.clear() 42 | turtle.pu() 43 | turtle.speed(0) 44 | turtle.pensize(20) 45 | turtle.color("grey") 46 | turtle.goto(-220,220) 47 | turtle.pd() 48 | turtle.goto(220,220) 49 | turtle.goto(220,-220) 50 | turtle.goto(-220,-220) 51 | turtle.goto(-220,220) 52 | turtle.pu() 53 | turtle.goto(0,0) 54 | 55 | def start(x,y): 56 | turtle.onscreenclick(None) 57 | 58 | level_1() 59 | 60 | tfood = turtle.Turtle() 61 | tfood.hideturtle() 62 | tfood.pu() 63 | tfood.speed(0) 64 | tfood.shape("square") 65 | tfood.color("red") 66 | 67 | tscore = turtle.Turtle() 68 | tscore.hideturtle() 69 | tscore.pu() 70 | tscore.speed(0) 71 | tscore.goto(100,-250) 72 | tscore.write("Score:" + str(a[0]), align="center",font=(10)) 73 | 74 | while x > -210 and x < 210 and y > -210 and y <210: 75 | if fcoord[2] == 0: 76 | food(tfood) 77 | fcoord[2] = 1 78 | turtle.onkey(u,"Up") 79 | turtle.onkey(l,"Left") 80 | turtle.onkey(r,"Right") 81 | turtle.onkey(d,"Down") 82 | turtle.listen() 83 | move() 84 | x = turtle.xcor() 85 | y = turtle.ycor() 86 | if x > fcoord[0]*20-5 and x < fcoord[0]*20+5 and y > fcoord[1]*20-5 and y < fcoord[1]*20+5: 87 | fcoord[2] = 0 88 | tfood.clear() 89 | a[0] += 1 90 | tscore.clear() 91 | tscore.write("Score:" + str(a[0]), align="center",font=(10)) 92 | 93 | if len(pos) > 1: 94 | for i in range(1,len(pos)): 95 | if x < pos[i][0]+5 and x > pos[i][0]-5 and y < pos[i][1]+5 and y > pos[i][1]-5: 96 | tscore.clear() 97 | tfood.clear() 98 | gameover() 99 | tscore.clear() 100 | tfood.clear() 101 | gameover() 102 | 103 | 104 | #Food 105 | def food(tfood): 106 | x = random.randrange(-8,8,1) 107 | y = random.randrange(-8,8,1) 108 | fcoord[0] = x 109 | fcoord[1] = y 110 | tfood.hideturtle() 111 | tfood.pu() 112 | tfood.shape("square") 113 | tfood.color("red") 114 | tfood.goto(x*20,y*20) 115 | tfood.stamp() 116 | 117 | #Up 118 | def u(): 119 | if h[0] == 270: 120 | pass 121 | else: 122 | h[0] = 90 123 | #Down 124 | def d(): 125 | if h[0] == 90: 126 | pass 127 | else: 128 | h[0] = 270 129 | #Left 130 | def l(): 131 | if h[0] == 0: 132 | pass 133 | else: 134 | h[0] = 180 135 | #Right 136 | def r(): 137 | if h[0] == 180: 138 | pass 139 | else: 140 | h[0] = 0 141 | 142 | def move(): 143 | turtle.pensize(1) 144 | turtle.color("black") 145 | turtle.pu() 146 | turtle.speed(3) 147 | turtle.setheading(h[0]) 148 | turtle.shape("square") 149 | turtle.stamp() 150 | turtle.fd(20) 151 | x = turtle.xcor() 152 | y = turtle.ycor() 153 | if b[0] > a[0]: 154 | turtle.clearstamps(1) 155 | pos.insert(0,[round(x),round(y)]) 156 | pos.pop(-1) 157 | else: 158 | pos.insert(0,[round(x),round(y)]) 159 | b[0] += 1 160 | 161 | def gameover(): 162 | turtle.onscreenclick(None) 163 | turtle.speed(0) 164 | turtle.pu() 165 | turtle.goto(0,150) 166 | turtle.color("red") 167 | turtle.write("Game Over",align="center", font=(10)) 168 | turtle.goto(0,50) 169 | turtle.write("Score:" + str(a[0]),align="center",font=(10)) 170 | turtle.goto(200,-200) 171 | turtle.write("(Click anywhere to return to the main menu)",align="right",font=(0.0000001)) 172 | turtle.onscreenclick(home) 173 | turtle.mainloop() 174 | 175 | 176 | # # # # # # # # # # # # # # # # # # # # # # 177 | # Main # 178 | # # # # # # # # # # # # # # # # # # # # # # 179 | if __name__ == '__main__': 180 | home(0,0) --------------------------------------------------------------------------------