tkinter - Need help for a Python Snake game -


i'm creating snake game on python using tkinter library. right now, i've implemented movements, food system, , score system. still need on how can make snake grow when eats food.

here's code (it's working , can test out!):

from tkinter import * import os random import randint  #coded nociph/alteus  #---setting variables--- gamewindow = tk() gamewindow.title("atsnake")  playerx1 = 100 playery1 = 100 playermov = 0 alreadymove = 0  foodx1 = 10 foodx2 = 20 foody1 = 10 foody2 = 20 foodpresent = 0  score = 0   def key(event):     global playermov     global alreadymove      if event.char == 'z' , playermov != 3:         playermov = 1         print("input:",event.char,"playerx1: ",playerx1," - playery1",playery1,"up")     if event.char == 'q' , playermov != 4:         playermov = 2         print("input:",event.char,"playerx1: ",playerx1," - playery1",playery1,"left")     if event.char == 's' , playermov != 1:         playermov = 3         print("input:",event.char,"playerx1: ",playerx1," - playery1",playery1,"down")     if event.char == 'd' , playermov != 2:         playermov = 4         print("input:",event.char,"playerx1: ",playerx1," - playery1",playery1,"right")      if alreadymove == 0:         game.after(500,moving)         alreadymove = 1  def moving():     game.after_cancel(moving)     global playermov     global playerx1     global playery1      if playermov == 1:         playery1 = playery1 - 10     if playermov == 2:         playerx1 = playerx1 - 10     if playermov == 3:         playery1 = playery1 + 10     if playermov == 4:         playerx1 = playerx1 + 10     game.after(100,moving)     game.coords(player, playerx1, playery1)     checkcoll()  def checkcoll():     global playerx1     global playery1     global alreadymove     global foodpresent      #---crossing edges---     #--- x ---     if playerx1 > 200:         playerx1 = 0     else:         if playerx1 < 0:             playerx1 = 200      #--- y ---     if playery1 > 200:         playery1 = 0     else:         if playery1 < 0:             playery1 = 200      #--check fruit collision     print("foodpresent :",foodpresent)     if playerx1 >= foodx1 , playerx1 <= foodx2 , playery1 >= foody1 , playery1 <= foody2:         foodcom(1)     else:         foodcom(0)     printscore()   def callback(event):     game.focus_set()     print ("clicked at", event.x, event.y)   def foodcom(command):          #---0: create  1: delete     global foodx1, foody1, foodx2, foody2, foodpresent, foodini, food, score     print("callingfoodfonction")      if command == 1:         game.delete(food)         foodpresent = 0     else:         if foodpresent == 0:             foodx1  = randint(1,19) * 10             foody1  = randint(1,19) * 10             foodx2 = foodx1 + 10             foody2 = foody1 + 10             food = game.create_rectangle(foodx1, foody1, foodx2, foody2, fill="red")              score = score + 1             foodpresent = 1             foodini = 0   def printscore():     global score, scoretxt     game.delete(scoretxt)     scoretxt = game.create_text(190, 190, text=score, font="arial 10 bold", fill="grey")  #---object definition--- game = canvas(gamewindow, width=200, height=200, background='white') snaketitle = game.create_text(100, 100, text="snake", font="arial 30 bold", fill="#ebeaee") pak = game.create_text(100, 120, text="press key start", font="arial 15", fill="#ebeaee") ctrls = game.create_text(100, 140, text="use zqsd move", font="arial 15", fill="#ebeaee") player = game.create_text(100, 100, text="x", font="arial 15", fill="blue") scoretxt = game.create_text(190, 190, text=score, font="arial 10 bold", fill="grey")   #---binding--- game.bind("<key>", key) game.bind("<button-1>", callback) game.pack()  foodcom(0) gamewindow.mainloop() 

i not see associated code displays current position of snake on screen , remove after movement, can change size if make length of snake variable , have drawn , removed in iterate fashion. when food eaten, can increase size of snake length variable , pause erasing of snake movement proceeds along vector until desired growth has occurred, @ time removal can proceed @ new length rate. please clarify part of code renders snakes position.


Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -