2017-02-14 1 views
1

, поэтому есть много проблем, связанных с этим. Я попытался сделать змеиную игру на питоне, но это был просто беспорядок, поэтому теперь я пытаюсь сделать это там, где единственное направление, которое они могут предпринять после того, как начальное правое движение опущено, и они должны добраться до координат пищи, иначе это начинается. если они «съедят» пищу/квадрат, тогда он сделает еще один уровень с началом новой игры, чтобы я мог вести счет, однако определение роста, похоже, не работает вообще, независимо от того, что я ставлю под if. Im использует 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, вы можете получить свои позиции по методу объекта Canvas .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). 

То, что вы не сделать, это указать координату вернуть то, что объект находится на нем (то, что я думаю, что вы делаете).

Смежные вопросы