2017-02-14 52 views
1

所以有很多问题正在进行中。我试图在python中做一个蛇形游戏,但它只是一团糟,所以现在我正试图使它在最初的正确运动停止后唯一的方向发生,否则它们必须到达食物的坐标它重新开始。如果他们“吃”了食物/方块,那么它应该开始另一个应该开始新游戏的关卡,这样我就可以保持得分,然而,无论我把什么放在if之下,增长定义似乎都不起作用。我使用python tkinter。如何在tkinter上的小部件与另一个小部件重叠时开始新的级别?

import Tkinter 
import random 
import math 
from Tkinter import * 
import time 
# Create root window 
#### 
root = Tkinter.Tk() 

##### 
# Create Model 
###### 
speed_intvar = Tkinter.IntVar() 
speed_intvar.set(1) # Initialize y coordinate 
# radius and x-coordinate of circle 
new_dir = 0 
leng = 40 
var = 1 
x1 = 10 
y1 = 10 
x2 = 50 
y2 = 10 
x3 = x2 
y3 = 10 
direction = 3 
foodx1 = 1 
foodx2 = 8 
foody1 = 1 
foody2 = 8 
food_present = 0 
length = 8 
food = 0# radians of angle in standard position, ccw from positive x axis 

###### 
# Create Controller 
####### 
# Instantiate and place slider 
# Create and place directions for the user 
#canvas = Tkinter.Canvas(root, width=900, height=900, background='#FFFFFF') 
#canvas.grid(row=0, rowspan=2, column=2) 
#text = Tkinter.Button(root, text='Use WASD to move', command = new_game) 
#text.grid(row=0, column =2) 
#text = Tkinter.Label(root, text='Use WASD to move') 
#text.grid(row=0, column =2) 

###### 
# Create View 
####### 
# Create and place a canvas 


def new_game(): 
    c.delete("all") 
    canvas = Tkinter.Canvas(root, width=900, height=900, background='#FFFFFF') 
    canvas.grid(row=0, rowspan=2, column=1) 

# Create a circle on the canvas to match the initial model 
    circle_item = canvas.create_line(x1, y1, x2, y2, x2, y3, fill='black', width = "8") 
#circle_item2 = canvas.create_rectangle(8, 1, 9, 8, 
#        outline='#000000', fill='black') 


#def move(event): 
# if event.char=='w' 






    def food(): 
     global foodx1, foodx2, foody1, foody2, food_present, food, var 
     food_present = 0 
     if food_present==0: 
      foodx1 = random.randint(1, 900) 
      foody1 = random.randint(1,900) 
      foodx2 = foodx1 + 8 
      foody2 = foody1 + 8 
      food = canvas.create_rectangle(foodx1, foody1, foodx2, foody2, fill = 'black') 
      food_present += 1 

    food() 

#def make_longer(): 
# global food_present, food_list, x1, y1, x2, y2, circle_item 
# #x1 = canvas.coords(circle_item) 
# #y1 = canvas.coords(circle_item) 
# #x2 = canvas.coords(circle_item) 
# #y2 = canvas.coords(circle_item) 
# xx1, yy1, xx2, yy2 = canvas.coords(circle_item) 
# xx1 -= 10 
# xx2 -= 50 
# for i in range(len(food_list)): 
#  circle_item2=canvas.create_rectangle(xx1, yy1, xx2, yy2, fill ="black") 
#make_longer()     

    def animate(): 
     global x1, y1, x2, y2, x3, y3, var 
     x1, y1, x2, y2, x3, y3 = canvas.coords(circle_item) 
     #for i in range(food_present): 
     if var == 1: 
      x1 += 1 
      x2 += 1 
      #x3+= 1 
     if var == 4: 
      y1+=1 
      y2+=1 
      y3+=1 
     if var == 5: 
      x1-=1 
      x2-=1 
      x3-=1 
     if var == 6: 
      y1 -= 1 
      y2 -= 1 
      y3 -= 1 
     canvas.coords(circle_item, x1, y1, x2, y2, x2, y3) 
     #canvas.itemconfig(circle_item, x1, y1, x2, y2, x3, y3, fill="blue") 
     canvas.update() 

    #   
    #while 
    # Get the slider data and create x- and y-components of velocity 
    #velocity_x = speed_intvar.get() * math.cos(direction) # adj = hyp*cos() 
    #velocity_y = speed_intvar.get() * math.sin(direction) # opp = hyp*sin() 
    ## Change the canvas item's coordinates 
    #canvas.move(circle_item, velocity_x, velocity_y) 
    #canvas.move(circle_item2, velocity_x, velocity_y) 
    # Get the new coordinates and act accordingly if ball is at an edge 



    #global location 
    ## If crossing left or right of canvas 
    #if x2>canvas.winfo_width(): 
    #  
    #if x1<0: 
    #  canvas.move(circle_item, x+400, y) 
    #global direction 


     canvas.after(1, animate) 
# Call function directly to start the recursion 
    animate() 
    # 
    def move(event): 
     global direction, var, x1, x2, x3, y1, y2, y3, new_dir 
     direction = 3 
     if event.char=='s' and direction == 3: 
      if direction == 1: 
       return 
      else: 
       new_dir = 1 
       var = 3 
       for i in range(10): 
        x1, y1, x2, y2, x3, y3 = canvas.coords(circle_item) 
     #for i in range(food_present): 
        if var == 3: 
         x1 += 1 
         x2 += 0 
         x3 +=0 
         y1 +=0 
         y2= y1 
       #x2 += 1 
       #x3 += 1 

         y3 += 1 
         canvas.coords(circle_item, x1, y1, x2, y2, x3, y3) 
         canvas.update() 
      if x1 == x3: 
       var = 4 
       direction = 1 
      canvas.update() 

    # if event.char=='s' and direction == 2: 
    #  if direction == 1: 
    #   return 
    #  else: 
    #   new_dir = 1 
    #   var = 3 
    #   for i in range(10): 
    #    x1, y1, x2, y2, x3, y3 = canvas.coords(circle_item) 
    ##for i in range(food_present): 
    #    if var == 3: 
    #     x1 -= 0 
    #     x2 -= 1 
    #     x3 -=1 
    #     y1 -=0 
    #     y2 -=1 
    #   #x2 += 1 
    #   #x3 += 1 
    #    
    #     y3 -= 1 
    #    canvas.coords(circle_item, x1, y1, x2, y2, x3, y3) 
    #    canvas.update() 
    #   if x1 == x3: 
    #    var = 4 
    #    direction = 1 
    #   canvas.update() 

    root.bind("<Key>", move) 

    def x(): 
     i = Tkinter.Canvas(root, width=900, height=900, background='#FFFFFF') 
     x = Tkinter.Button(root, text='Use S to move down. Try and eat the food.', command = new_game) 
     x.grid(row=0, column =1) 

    def grow(): 
     global x1, y1, x2, y2, x3, y3, var, food_present, foodx1, foody1, foodx2, foody2 
     x1, y1, x2, y2, x3, y3 = canvas.coords(circle_item) 
     if x2>canvas.winfo_width() or x1<0: 
      x() 
     if y2>canvas.winfo_height() or y3>canvas.winfo_height(): 
      food_present = 0 
      x() 
     canvas.update() 
    grow() 




c = Tkinter.Canvas(root, width=900, height=900, background='#FFFFFF') 
c.grid(row=0, rowspan=2, column=1) 
text = Tkinter.Button(root, text='Use S to move down. Try and eat the food.', command = new_game) 
text.grid(row=0, column =1) 


root.mainloop() 

回答

0

如果要检查两个物体在画布Tkinter的重叠,可以通过对象画布.coords(objectname)的方法得到他们的位置。您使用.coords方法检查绘制的对象。

例子:

circle = canvas.create_oval(0, 0, 50, 50) 
canvas.move(circle, 5, 0) # moves the circle by 5 pixels to the right 
coords = canvas.coords(circle) # Returns an array (5, 0, 55, 0). 

什么你不这样做是指定的坐标返回是什么对象就可以了(我认为你正在做的)。