Home > Back-end >  How to make that when the cube touches the circle, the circle moves to a random place on the field?
How to make that when the cube touches the circle, the circle moves to a random place on the field?

Time:12-13

How to make that when touching (when the coordinates of two objects coincide) the cube to the circle, the circle moves to a random place on the field? Without using classes.

import random
from tkinter import *

root = Tk()
root.title('Snake')
root["width"] = 400
root["height"] = 400

field = Canvas(root)
oval = field.create_oval(10, 20, 30, 40)
field.grid(row=0, column=0)

rectangle = field.create_rectangle(10, 20, 30, 40)
field.grid(row=0, column=0)

x = 0
y = 0

food_x = random.randrange(0, 300, 20)
food_y = random.randrange(0, 300, 20)
field.move(oval, food_x, food_y)

def food():
    global food_x
    global food_y
    if x == food_x and y == food_y:
        food_x = random.randrange(0, 300, 20)
        food_y = random.randrange(0, 300, 20)
        field.move(oval, food_x, food_y)
        root.after(250, food)
        food()

def position(event):
    global x, y
    if event.char in 'wasd':
        x = 0
        y = 0
        if event.char == 'w':
            y = -20
        elif event.char == 'a':
            x = -20
        elif event.char == 's':
            y = 20
        elif event.char == 'd':
            x = 20


def snake():
    field.move(rectangle, x, y)
    root.after(250, snake)

root.bind("<Key>", position)
snake()
food()
root.mainloop()

CodePudding user response:

I think this shows how to do what you want. I've changed the name of your functions and variables to better reflect what they do or represent to make the code clearer. To detect whether the cube and circle are touching is accomplished by first retrieving the coordinates of their corresponding bounding-boxes via the coords() method common to all Canvas objects, before comparing them to determine whether any overlap exists.

I also changed how the food circle is move to a new positions because what you had was capable of moving it very far away from its current location, frequently completely off the field. The replacement code only moves it by the difference between where it currently is and a new position that's within the boundaries of the field.

import random
from tkinter import *

root = Tk()
root.title('Snake')
root["width"] = 400
root["height"] = 400

DELAY = 250

field = Canvas(root)
field.grid(row=0, column=0)

food_obj = field.create_oval(10, 20, 30, 40)
snake_obj = field.create_rectangle(10, 20, 30, 40)
x, y = 0, 0


def move_food():
    """Move food to random location."""

    food_coords = field.coords(food_obj)
    x1 = random.randrange(0, 300, 20)
    y1 = random.randrange(0, 300, 20)
    field.move(food_obj, x1-food_coords[0], y1-food_coords[1])


def check_collision():
    """If bounding box of snake and food overlap, move food to new position."""

    a_x1, a_y1, a_x2, a_y2 = food_coords = field.coords(food_obj)
    b_x1, b_y1, b_x2, b_y2 = snake_coords = field.coords(snake_obj)
    # Determine of bounding boxes overlap.
    if a_x1 <= b_x2 and b_x1 <= a_x2 and a_y1 <= b_y2 and b_y1 <= a_y2:
        move_food()

    root.after(DELAY, check_collision)


def on_keypress(event):
    global x, y
    if event.char in 'wasd ':
        x, y = 0, 0
        if event.char == 'w':
            y = -20
        elif event.char == 'a':
            x = -20
        elif event.char == 's':
            y = 20
        elif event.char == 'd':
            x = 20

def slither():
    field.move(snake_obj, x, y)
    root.after(DELAY, slither)


root.bind("<Key>", on_keypress)
move_food()
slither()
check_collision()
root.mainloop()
  • Related