Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from turtle import Turtle, Screen, mainloop
- import turtle
- import random
- import math
- import time
- head=Turtle()
- screen=Screen()
- tails=Turtle()
- monster=Turtle()
- text=Turtle()
- food=Turtle()
- KEY_UP,KEY_DOWN,KEY_LEFT,KEY_RIGHT="Up","Down","Left","Right"
- #global variable:
- pausing=True
- gameReady=False
- eating=False
- caught=False
- food_x=[]
- food_y=[]
- food_q=[]
- def setWrite():
- text.up()
- text.setpos(-245,225)
- text.write("Welcom to Fenny's snake game",move=False, align="left", font=("Arial", 10, "normal"))
- text.setpos(-245,205)
- text.write("You are going to use the four arrow keys to move the head around the the screen",\
- move=False, align="left", font=("Arial", 10, "normal"))
- text.setpos(-245,185)
- text.write("trying to consume all the food items before the monster catches you... ",\
- move=False, align="left", font=("Arial", 10, "normal"))
- text.setpos(-245,165)
- text.write("Click anywhere on the screen to start the game,have fun!",\
- move=False, align="left", font=("Arial", 10, "normal"))
- text.ht()#hide the turtle
- def setSnake():
- head.up()
- head.setpos(0,0)
- head.shape("square")
- head.fillcolor("red")
- def setMonster():
- monster.up()
- monster.shape("square")
- monster.fillcolor("purple")
- x1=random.randint(-200,-100)
- x2=random.randint(100,200)
- x=random.random()
- if x<1/2:
- x=x1
- else:
- x=x2
- y=random.randint(-200,-100)
- monster.setpos(x,y)
- def gameBegin(m,n):
- -250<=m<=250
- -250<=n<=250
- global gameReady
- global pausing
- if (gameReady):
- pausing=False
- screen.onscreenclick(None)
- def setFood():
- global food_q
- global food_x
- global food_y
- for i in range(1,10):
- x=int(220*random.random())
- c=random.random()
- if c<1/2:
- x=(-1)*x
- y=int(220*random.random())
- d=random.random()
- if d<1/2:
- y=(-1)*y
- food.up()
- food.ht()
- food.setpos(x,y)
- food.write(i)
- food_x.append(x)
- food_y.append(y)
- food_q.append(i)
- #main
- #set Screen
- screen.setup(width=500, height=500, startx=None, starty=None)
- turtle.title("Fenny's Game")
- setWrite()
- setSnake()
- setMonster()
- gameReady=True #前面默认设置完成
- screen.onscreenclick(gameBegin)
- while True:#停在第一个界面直到click(限制在这个循环中)
- if (pausing):
- pass
- else:
- break
- text.clear()
- setFood()
- turtle.title("Snake")
- screen.listen()
- screen.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement