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
- KEY_UP,KEY_DOWN,KEY_LEFT,KEY_RIGHT="Up","Down","Left","Right"
- foods=[]
- food_x=[]
- food_y=[]
- food_q=[]
- foodNumber=0
- g_direction=None
- foodMove=0
- caught=False
- pausing=False
- contacting=0
- timeBegin=0
- timeNow=0
- moveTrace=[(0,0)]
- moveTime=0
- screen=Screen()
- head=Turtle()
- text=Turtle()
- monster=Turtle()
- def setWrite():
- screen.setup(width=500, height=500, startx=None, starty=None)
- turtle.title("Fenny's Game")
- text.up()
- text.setpos(-245,225)
- text.write("Welcom to Fenny's head 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 setFood():
- global foods
- global food_q
- global food_x
- global food_y
- food1=Turtle()
- food2=Turtle()
- food3=Turtle()
- food4=Turtle()
- food5=Turtle()
- food6=Turtle()
- food7=Turtle()
- food8=Turtle()
- food9=Turtle()
- food10=Turtle()
- foods.append(food1)
- foods.append(food2)
- foods.append(food3)
- foods.append(food4)
- foods.append(food5)
- foods.append(food6)
- foods.append(food7)
- foods.append(food8)
- foods.append(food9)
- foods.append(food10)
- for i in range(10):
- foods[i].ht()
- food_x.append(0)
- food_y.append(0)
- food_q.append(5)
- 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
- foods[i].up()
- foods[i].ht()
- foods[i].setpos(x,y)
- foods[i].write(i)
- food_x.append(x)
- food_y.append(y)
- food_q.append(i)
- def moveUp():
- global g_direction
- if g_direction!=270:
- g_direction=90
- def moveDown():
- global g_direction
- if g_direction!=90:
- g_direction=270
- def moveLeft():
- global g_direction
- if g_direction!=0:
- g_direction=180
- def moveRight():
- global g_direction
- if g_direction!=180:
- g_direction=0
- def pause():
- global pausing
- pausing=not pausing
- def screenControl():
- screen.onkey(moveUp,KEY_UP)
- screen.onkey(moveDown,KEY_DOWN)
- screen.onkey(moveLeft,KEY_LEFT)
- screen.onkey(moveRight,KEY_RIGHT)
- turtle.onkey(pause, "space")
- def remove():
- global foodMove
- foods[foodMove].clear()
- food_x.remove(food_x[foodMove])
- food_y.remove(food_y[foodMove])
- food_q.remove(food_q[foodMove])#先remove 前面那个5==出现错误
- foods.remove(foods[foodMove])
- def headMove(x,y):
- global g_direction
- if g_direction==90 and y>=230:#head的运动
- return 0
- if g_direction==180 and x<=-230:
- return 0
- if g_direction==270 and y<=-230:
- return 0
- if g_direction==0 and x>=230:
- return 0
- else:
- head.setheading(g_direction)
- head.forward(20)
- def appear(x,y):
- head.color("blue","black")
- head.stamp()
- head.color("red")
- headMove(x,y)
- def moveForward(x,y):
- if (g_direction!=90 or y<=230) and (g_direction!=180 or x>=-230) and \
- (g_direction!=270 or y>=-230) and (g_direction!=0 or x<=230):
- head.color("blue","black")
- head.stamp()
- head.color("red")
- headMove(x,y)
- head.clearstamp(head.stampItems[0])
- def snakeMove():
- global caught
- global foodMove
- global food_x
- global food_y
- global food_q
- global foodNumber
- global pausing
- if g_direction!=None and pausing!=True:
- x,y=head.pos()
- i=0
- while i<len(foods):
- if abs(x-food_x[i])<20 and abs(y-food_y[i])<20:
- foodMove=i
- foodNumber=food_q[i]+foodNumber
- remove()
- i=i+1
- if foodNumber>0:
- appear(x,y)
- foodNumber-=1
- else:
- moveForward(x,y)
- if caught==True:
- return
- if len(head.stampItems)==50:
- text.up()
- text.ht()
- text.setpos(x,y)
- text.color("red")
- text.write("Winner!!!",font=("Arial", 20, "bold"))
- return
- elif pausing==True:
- head.forward(0)
- screen.ontimer(snakeMove,500)
- def monsterMove():
- global g_direction
- global caught
- global pausing
- if g_direction!=None and pausing!=True:
- x,y=head.pos()
- a,b=monster.pos()
- delta1 = 2/math.sqrt(abs(x-a)*abs(x-a)+abs(y-b)*abs(y-b))
- delta2 = random.random() + 1
- if (abs(a-x)>20) or (abs(b-y)>20):
- monster.setx(a + (x - a)*delta1*delta2) # new pos
- monster.sety(b + (y- b)*delta1*delta2)
- else:
- caught=True
- x,y=head.pos()
- text.up()
- text.ht()
- text.setpos(x,y)
- text.color("yellow")
- text.write("Game Over!!!",font=("Arial", 20, "bold"))
- return
- if len(head.stampItems)==50:
- return
- elif pausing==True:
- monster.forward(0)
- screen.ontimer(monsterMove,100)
- def contact():
- global g_direction
- global moveTrace
- global contacting
- global pausing
- if g_direction!=None and pausing!=True:
- length=len(head.stampItems)
- x,y=head.pos()
- a,b=monster.pos()
- if (x,y)!=moveTrace[-1]:
- moveTrace.append((x,y))
- for i in range(1,length+1):
- m,n=moveTrace[-i]
- if (abs(a-m)<20) and (abs(b-n)<20):
- contacting+=1
- screen.ontimer(contact,500)#speeed=snake
- def timing():
- global g_direction
- global timeBegin
- global timeNow
- global moveTime
- if g_direction!=None:
- timeNow = time.time()
- moveTime=timeNow-timeBegin
- screen.ontimer(timing,500)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement