Click here to Skip to main content
15,892,797 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
$ python snakegame.py
Exception in Tkinter callback
Traceback (most recent call last):
  File "F:\ALL PROGRAM\Python\lib\tkinter\__init__.py", line 1883, in __call__
    return self.func(*args)
  File "F:\ALL PROGRAM\Python\lib\tkinter\__init__.py", line 804, in callit
    func(*args)
  File "snakegame.py", line 65, in perform_action
    self.check_food_collision()
  File "snakegame.py", line 98, in check_food_collision
    self.coords(self.find_withtag("food"), self.food)
  File "F:\ALL PROGRAM\Python\lib\tkinter\__init__.py", line 2761, in coords
    self.tk.call((self._w, 'coords') + args))]
_tkinter.TclError: wrong # coordinates: expected 2, got 1


What I have tried:

Python
  1  from tkinter import *
  2  from random import randint
  3  from PIL import Image, ImageTk
  4  
  5  Move_Increament = 20
  6  Moves_per_second = 85
  7  Game_speed = 1000
  8  
  9  class Snake(Canvas):
 10      def __init__(self):
 11          super().__init__(width=600, height=620, background='black',highlightthickness=0)
 12  
 13          self.snake_position = [(100, 100), (80, 100), (60, 100)]
 14          self.food_position = self.set_new_food_position()
 15          self.score = 0
 16          self.direction = "Right"
 17          self.bind_all("<Key>", self.on_key_press)
 18  
 19          self.load_assets()
 20          self.create_objects()
 21  
 22          self.after(Moves_per_second, self.perform_action)
 23  
 24      def load_assets(self):
 25          try:
 26              self.snake_body_image = Image.open("assets/snake.png")
 27              self.snake_body = ImageTk.PhotoImage(self.snake_body_image)
 28  
 29              self.food_image = Image.open("assets/food.png")
 30              self.food = ImageTk.PhotoImage(self.food_image)
 31          except IOError as error:
 32              print(error)
 33              root.destroy
 34  
 35      def create_objects(self):
 36          self.create_text(
 37              45, 12, text=f"Score={self.score}", tag="score", fill="#fff", font=("TkDefaultFont",14)
 38          )
 39          for x_position, y_position in self.snake_position:
 40              self.create_image(x_position, y_position,image=self.snake_body, tag="snake")
 41          self.create_image(*self.food_position, image=self.food, tag="food")
 42          self.create_rectangle(7, 27, 593, 613, outline="#525d69")
 43  
 44      def move_snake(self):
 45          head_x_position, head_y_position = self.snake_position[0]
 46  
 47          if self.direction =="Left":
 48              new_head_position = (head_x_position - Move_Increament, head_y_position)
 49          elif self.direction =="Right":
 50              new_head_position = (head_x_position + Move_Increament, head_y_position)
 51          elif self.direction =="Down":
 52              new_head_position = (head_x_position, head_y_position + Move_Increament)
 53          elif self.direction =="Up":
 54              new_head_position = (head_x_position, head_y_position- Move_Increament)
 55  
 56          self.snake_position = [new_head_position] + self.snake_position[:-1]
 57          
 58          for segment, position in zip(self.find_withtag("snake"), self.snake_position):
 59              self.coords(segment,position)
 60      
 61      def perform_action(self):
 62          if self.check_collision():
 63              return
 64  
 65          self.check_food_collision()
 66          self.move_snake()
 67          self.after(Moves_per_second ,self.perform_action)
 68  
 69      def check_collision(self):
 70          head_x_position, head_y_position = self.snake_position[0]
 71          
 72          return (
 73              head_x_position in (0,600)
 74              or head_y_position in (20,620)
 75              or (head_x_position, head_y_position) in self.snake_position[1:]
 76          )
 77  
 78      def on_key_press(self, e):
 79          new_direction = e.keysym
 80          all_direction = ("Up", "Down", "Left","Right")
 81          opposites = ({"Up", "Down"}, {"Left", "Right"})
 82  
 83          if (
 84              new_direction in all_direction
 85              and {new_direction, self.direction} not in opposites
 86          ):
 87            self.direction = new_direction
 88      
 89      def check_food_collision(self):
 90          if self.snake_position[0] == self.food_position:
 91              self.score += 1
 92              self.snake_position.append(self.snake_position[-1])
 93  
 94              self.create_image(
 95                  *self.snake_position[-1], image =self.snake_body, tag="snake"
 96              )
 97              self.food_position = self.set_new_food_position
 98              self.coords(self.find_withtag("food"), self.food)
 99  
100              score = self.find_withtag("score")
101              self.itemconfigure(score, text=f"score:{self.score}", tag="score")
102      
103      def set_new_food_position(self):
104          while True:
105              x_position = randint(1, 29) * Move_Increament
106              y_position = randint(3, 30) * Move_Increament
107              food_position = (x_position, y_position)
108  
109              if food_position not in self.snake_position:
110                  return food_position
111  
112  root = Tk()
113  root.title("Sname")
114  root.resizable(False,False)
115  
116  board = Snake()
117  board.pack()
118  
119  
120  root.mainloop()
Posted
Updated 30-Jan-21 23:07pm
v2
Comments
Richard MacCutchan 31-Jan-21 5:11am    
There is something wrong with the arguments being passed at line 98. Use the debugger to find out why only one is being passed in.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900