How to Draw a Graph of Snake in Python


In this example we will show how to draw a snake using Python.

Source Code

#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# drawSnake.py
# use python turtle library to draw a snake

import turtle
import time

def drawSnake(rad,angle,len,neckrad):
	snakeColor=["black","red","red","blue","yellow"]
	yColor=["yellow","green","yellow","red","red"]

	for i in range(len):
		turtle.pencolor(snakeColor[i])		# set color
		turtle.circle(rad, angle)			# draw a circle
		turtle.pencolor(yColor[i])
		turtle.circle(-rad, angle)

	turtle.pencolor("green") 	    
	turtle.circle(rad, angle / 2)	
	turtle.pencolor("yellow")
	turtle.fd(rad)							
	turtle.pencolor("red")
	turtle.circle(neckrad + 1, 180)
	turtle.pencolor("green")
	turtle.fd(rad * 2 / 3)

def main():
	turtle.setup(1300, 800, 0, 0)
	pythonsize = 30
	turtle.pensize(pythonsize)
	turtle.seth(-40)
	
	drawSnake(40, 80, 5, pythonsize / 2)
	time.sleep(10)

if __name__ == '__main__':
    main()

After running the codes above, we can get the following result:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments