المشكله في لعبه الثعبان

المشكله عندي في الثعبان غير سلس في الرسومات وهذه الصوره

وهذا هو الكود




3 إعجابات

في ال snake لازم الشكل يكون"square"
كمان في اكتر من كلمة self كاتبتها salf

3 إعجابات

مش ضروري يخلى الشكل مربع، انا عملته دائرة وكان احلي :

3 إعجابات

هل تعمل على الهاتف
كيف اطلع ازرار التحكم

إعجابَين (2)

عملت الازرار من onscreenclick، وهذا هو الكود :

from turtle import Turtle, Screen 
from random import choice 
from time import sleep 

class Snake ():

  def __init__(self):
    self.snake_body = []
    self.eyes = []
    self.position = [(0,0),(-40,0),(-80,0)]
    self.positions = [(0, -400), (0, -500), (-150, -450), (150, -450)]
    self.turn = [90,270,180,0]
    self.positions = [(0, -400), (0, -500), (-150, -450), (150, -450)]
    self.turn = [90,270,180,0]
    self.create_arrow ()
    self.create_snake ()
    self.create_eyes ()

  def create_snake (self):
    for x in range(len(self.position)):
        snake = Turtle ('circle')
        snake.shapesize (stretch_wid=1, stretch_len=1, outline=5)
        snake.color ('white','light green')
        snake.penup ()
        snake.turtlesize (2)
        snake.goto (self.position [x])
        self.snake_body.append (snake)
    self.head=self.snake_body[-1]

  def add_piece (self):
    snake = Turtle ('circle')
    snake.shapesize (stretch_wid=1, stretch_len=1, outline=5)
    snake.color ('white','light green')
    snake.penup ()
    snake.turtlesize (2)
    snake.goto (self.snake_body [0].pos())
    self.snake_body.insert(0,snake)
    self.eyes[0].hideturtle ()
    self.eyes[1].hideturtle ()
    self.eyes = []
    self.create_eyes ()

  def create_eyes (self):
    for x in range(2):
      eye = Turtle ('circle')
      eye .penup ()
      eye .color ('white' , 'black')
      eye .shapesize (stretch_wid=0.1, stretch_len=0.1, outline=4)
      eye .turtlesize (0.5)
      eye .setheading (self.head.heading())
      eye.goto(self.head.pos())
      eye .left (self.turn[x])
      eye.forward (7)
      eye .setheading (self.head.heading())
      self.eyes.append (eye)

  def create_arrow (self):
    for x in range (4):
      arrow = Turtle ('arrow')
      arrow.hideturtle ()
      arrow .color ("white",'dark green')
      arrow.turtlesize (7)
      arrow.penup ()
      arrow.shapesize(stretch_wid=7, stretch_len=10, outline=10)
      arrow.goto (self.positions [x-1] )
      arrow.setheading (self.turn [x-1] )
      arrow.showturtle()

  def move (self):
    for x in range (len(self.snake_body)-1):
      self.snake_body [x].goto (self.snake_body [x+1].pos())
    self.head.forward (40)
    for eye in self.eyes :
      eye.forward (40)

  def up (self):
    self.head.setheading (90)
    self.eyes[0].goto (self.head.xcor()-7,self.head.ycor())
    self.eyes[1].goto (self.head.xcor()+7,self.head.ycor())
    for eye in self.eyes :
      eye.setheading (90)

  def dawn (self):
    self.head.setheading (270)
    self.eyes[0].goto (self.head.xcor()+7,self.head.ycor())
    self.eyes[1].goto (self.head.xcor()-7,self.head.ycor())
    for eye in self.eyes :
      eye.setheading (270)

  def left (self):
    self.head.setheading (180)
    self.eyes[0].goto (self.head.xcor(),self.head.ycor()-7)
    self.eyes[1].goto (self.head.xcor(),self.head.ycor()+7)
    for eye in self.eyes :
      eye.setheading (180)

  def right (self):
    self.head.setheading (0)
    self.eyes[0].goto (self.head.xcor(),self.head.ycor()+7)
    self.eyes[1].goto (self.head.xcor(),self.head.ycor()-7)
    for eye in self.eyes :
      eye.setheading (0)

  def turns (self, x, y):

    self.heading = self.head.heading()

    if -70 < x < 70 and -420 < y < -320: # up
      if self.heading == 0 or self.heading == 180:
        self.up ()

    elif -70 < x < 70 and -570 < y < -470: # dawn 
      if self.heading == 0 or self.heading == 180:
        self.dawn ()

    elif -220 < x < -100 and -500 < y < -350: # left 
      if self.heading == 90 or self.heading == 270:
        self.left ()

    elif 100 < x < 220 and -500 < y < -350: # right 
      if self.heading == 90 or self.heading == 270:
        self.right ()

class Food (Turtle):
  def __init__ (self):
    super(). __init__()
    self.shape('circle')
    self.color('red')
    self.penup ()
    self.food_place ()

  def food_place (self):
    x = choice (range (-320,320,40))
    y = choice (range(-400,400,40))
    self.goto (x,y)

class Score (Turtle):

  def __init__ (self):
    super().__init__()
    self.score = 0
    try :
      with open ('high_score.txt') as high :
        self.high_score = int (high.read())
    except :
      self.high_score = 0
    self.penup ()
    self.color ('white')
    self.hideturtle ()
    self.print_score ()

  def print_score (self):
    self.goto (0,450)
    self.write (f'score : {self.score}',align='center',font=('Aerial',10,'normal'))

  def add_score (self):
    self.score += 1
    if self.score > self.high_score :
      with open ('high_score.txt','w') as high :
        high.write (str(self.score))
    self.clear ()
    self.print_score ()

  def game_over (self):
    self.screen.bgcolor ('dark red')
    with open ('high_score.txt') as high :
      self.high_score = high.read ()
    self.goto (0,200)
    self.write ('game over !!',align ='center',font = ('Aerial',15,'normal'))
    self.goto (0,-100)
    self.write (f'===== the score =====\nyour score :  {self.score}\nhigh score : {self.high_score}',align ='center',font = ('Aerial',15,'normal'))

window = Screen ()
window .bgcolor ('green')
window.setup (700,1200)
window.tracer (0)

snake=Snake ()
food=Food ()
score=Score ()
game_on = True 
snake .move ()
window.update ()

while game_on :

  window.onscreenclick (snake.turns)
  snake .move ()
  window .update ()
  sleep (0.15)
  window.onscreenclick (snake.turns)

  if snake.head.distance (food) < 20:
    food.food_place()
    snake.add_piece ()
    score.add_score ()

  if snake.head.xcor() > 330 or snake.head.xcor() < -330 or snake.head.ycor() > 580 or snake.head.ycor() < -580 :
    score.game_over ()
    game_on = False 

  for part in snake.snake_body [:-1]:
    if snake.head.distance (part) < 10:
      score.game_over ()
      game_on = False 

window.exitonclick ()
إعجابَين (2)