Home > database >  How to make the square change direction and move in the right direction when you press the "w,
How to make the square change direction and move in the right direction when you press the "w,

Time:12-08

How to make the square change direction and move in the right direction when you press the "w, a, s, d" buttons (So that he moves himself, after a single press of the button that sets the direction) Without using classes

from tkinter import *

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

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

def w(y):
    field.move(rectangle, 0, y)
    y  = 1
    root.after(5)

def s(y):
    field.move(rectangle, 0, y)
    y -= 1
    root.after(5)


def a(x):
    field.move(rectangle, x, 0)
    x -= 1
    root.after(5)

def d(x):
    field.move(rectangle, x, 0)
    x  = 10
    root.after(5)

x=0
y=0

def snake(event):
    if event.char == 'w':
        w(y)
    elif event.char == 'a':
        a(x)
    elif event.char == 's':
        s(y)
    elif event.char == 'd':
        d(x)
    field.move(rectangle, x, y)

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

CodePudding user response:

You need to declare two global variables for the x and y directions, for example dx and dy.

Then if you want the rectangle keep moving, you need to use .after() loop.

from tkinter import *

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

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

def move_snake():
    field.move(rectangle, dx, dy)
    # change the delay value 30 (ms) to other value to suit your case
    root.after(30, move_snake)

# initial directions for x and y
dx = 0
dy = 0

def change_direction(event):
    global dx, dy
    if event.char in 'wasd':
        dx = dy = 0
    if event.char == 'w':
        dy = -1    # move up
    elif event.char == 'a':
        dx = -1    # move left
    elif event.char == 's':
        dy = 1     # move down
    elif event.char == 'd':
        dx = 1     # move right

root.bind("<Key>", change_direction)
move_snake()  # start the moving loop
root.mainloop()

CodePudding user response:

Instead of incrementing and decrementing the x,y, have a default value, which you change as per the direction -

from tkinter import *

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

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

x = 0 # Default
y = 0
speed = 10 # Set your speed. The lesser the value, the more fast 

def pos(event):
    global x, y
    x = 0
    y = 0
    if event.char == 'w': 
        y = -1   # Changes the default values
    elif event.char == 'a':
        x = -1   
    elif event.char == 's':
        y = 1   
    elif event.char == 'd':
        x = 1

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

root.bind("<Key>", pos)
snake()
root.mainloop()
  • Related