2016-05-12 59 views
1

我和朋友正在使用海龟进行Python游戏。我们的问题在它定义乌龟的部分。我们试图加速每只乌龟,但是当我们这样做时,默认速度就会运行。为什么会发生这个问题?Python Turtle Speed

import turtle 


turtle.setup(1000,1000) 
wn=turtle.Screen() 
wn.title("Snake Game!") 

#defines first turtle 

t1=turtle.Turtle() 
t1.pensize(2) 
t1.speed(3) 
t1.penup() 
t1.goto(0,100) 
t1.color("blue", "blue") 
t1.pendown() 

#defines second turtle 

t2=turtle.Turtle() 
t2.pensize(2) 
t2.speed(3) 
t2.penup() 
t2.goto(0,-100) 
t2.color("red", "red") 
t2.pendown() 

#defines outline 

ol=turtle.Turtle() 
ol.pensize(2) 
ol.speed(7) 
ol.penup() 
ol.goto(450,0) 
ol.color("black", "black") 
ol.pendown() 

ol.left(90) 
ol.forward(300) 
ol.left(90) 
ol.forward(900) 
ol.left(90) 
ol.forward(600) 
ol.left(90) 
ol.forward(900) 
ol.left(90) 
ol.forward(300) 
ol.hideturtle() 

#defines score 

score1=int(2) 
score2=int(2) 

def motion(): 
    global move, score1, score2 
    move = True 
    path1 = [] 
    path2 = [] 

#prints score 


    print("Player 1's score is", str(score1)+"!") 
    print("Player 2's score is", str(score2)+"!") 

#defines motion 

    while move == True: 
     global pos1x, pos2x 

     t1.forward(1) 
     t2.forward(1) 

     pos1x = int(t1.xcor()) 
     pos1y = int(t1.ycor()) 
     t1xy = (pos1x, pos1y) 

     pos2x=int(t2.xcor()) 
     pos2y=int(t2.ycor()) 
     t2xy=(pos2x,pos2y) 


     path1.append(t1xy) 
     path2.append(t2xy) 

#calculates score1 

     if t1xy in path2: 
      score1=int(score1-1) 
      print("") 
      print("Player 1's score is", str(score1)+"!") 
      print("Player 2's score is", str(score2)+"!") 
      t1.clear() 
      path1 = [] 
      t2.clear() 
      path2 = [] 
      t1.penup() 
      t1.goto(0,100) 
      t1.pendown() 
      t2.penup() 
      t2.goto(0,-100) 
      t2.pendown() 
      move = False 



     if score1==0: 
      print("Player 2 wins!") 
      exit() 
     else: 
      move==True 



#calculates score2 


     if t2xy in path1: 
      score2=int(score2-1) 
      print("") 
      print("Player 1's score is", str(score1)+"!") 
      print("Player 2's score is", str(score2)+"!") 
      t2.clear() 
      path2 = [] 
      t1.clear() 
      path1 = [] 
      t2.penup() 
      t2.goto(0,-100) 
      t2.pendown() 
      t1.penup() 
      t1.goto(0,100) 
      t1.pendown() 
      move = False 

     if score2==0: 
      print("Player 1 wins!") 
      exit() 
     else: 
      move==True 


#borders 

     if pos1x > 450: 
      t1.left(135) 

     if pos2x > 450: 
      t2.left(135) 


     if pos1x < -450: 
      t1.left(135) 

     if pos2x < -450: 
      t2.left(135) 


     if pos1y > 300: 
      t1.left(135) 

     if pos2y > 300: 
      t2.left(135) 

     if pos1y < -300: 
      t1.left(135) 

     if pos2y < -300: 
      t2.left(135) 

#defines controls 

def left(): 
    t1.speed(500) 
    t1.left(45) 
    t1.speed(3)  

def right(): 
    t1.speed(500) 
    t1.right(45) 
    t1.speed(3) 

def backwards(): 
    t1.left(180) 

def stop(): 
    global move 
    move = False 
    t1.forward(0) 
    t2.forward(0) 


def left2(): 
    t2.speed(500) 
    t2.left(45) 
    t2.speed(3) 

def right2(): 
    t2.speed(500) 
    t2.right(45) 
    t2.speed(3) 

def backwards2(): 
    t2.left(180) 

def motion2(): 

    move = True 
    path1 = [] 
    path2 = [] 


#onkeys 

wn.onkey(left2, "Left") 
wn.onkey(right2, "Right") 
wn.onkey(backwards2, "Down") 

wn.onkey(left, "a") 
wn.onkey(right, "d") 
wn.onkey(backwards, "s") 

wn.onkey(motion, "t") 
wn.onkey(stop, "y") 
wn.onkey(motion2, "p") 



wn.listen() 
wn.mainloop() 
+3

在大多数情况下,最好发布一个仅显示主要问题的缩小示例,而不是完整的代码/项目。 – linusg

回答

3

你想在这里发生什么?

def left(): 
    t1.speed(500) 
    t1.left(45) 
    t1.speed(3)  

将速度设置为10(快)以上将其设置为0(最快)。一旦操作完成,你就将它设置为3(慢)。

就我所知,你暂时加快了龟头的操作速度, left(),但对于应该从中受益的操作而言,将龟放在缓慢的速度。 motion()

我建议你拿出你所有speed()通话和重新思考它们,最好使用字符串参数“最慢”,“慢”,“正常”,“快” &“最快”的帮助文档你”重做并避免超出范围。

0

如果它是为改变乌龟的速度使用简单:

turtle.speed(number) 

乌龟是龟名称,如。 TestTurtle

该编号可以从110并且必须是整数。你不能有例如4.7作为数字。

将其设置为10将导致速度不起作用。