day 20 - fixed collision issues
This commit is contained in:
17
020/food.py
17
020/food.py
@ -1,22 +1,23 @@
|
||||
from turtle import Turtle
|
||||
from turtle import Turtle, Screen
|
||||
import random
|
||||
|
||||
class Food:
|
||||
class Food(Turtle):
|
||||
def __init__(self, screen_x_size, screen_y_size):
|
||||
self.turtle = Turtle(shape="square")
|
||||
self.turtle.color("cyan")
|
||||
self.turtle.pu()
|
||||
super().__init__()
|
||||
self.shape("square")
|
||||
self.color("cyan")
|
||||
self.pu()
|
||||
|
||||
x_limit = int((screen_x_size-20)/40)
|
||||
y_limit = int((screen_y_size-20)/40)
|
||||
|
||||
x_pos = random.randint(-x_limit, x_limit)*20
|
||||
y_pos = random.randint(-y_limit, y_limit)*20
|
||||
self.turtle.teleport(x=x_pos, y=y_pos)
|
||||
self.teleport(x=x_pos, y=y_pos)
|
||||
|
||||
|
||||
def get_position(self):
|
||||
return self.turtle.xcor(), self.turtle.ycor()
|
||||
return self.xcor(), self.ycor()
|
||||
|
||||
def hide(self):
|
||||
self.turtle.hideturtle()
|
||||
self.hideturtle()
|
||||
@ -39,11 +39,9 @@ class GameManager:
|
||||
return wall_collision or snake_collision
|
||||
|
||||
def is_food_eaten(self):
|
||||
snake_x, snake_y = self.snake.get_head_position()
|
||||
food_x, food_y = self.food.get_position()
|
||||
snake_head = self.snake.get_head()
|
||||
|
||||
if int(snake_x) == int(food_x) and int(snake_y) == int(food_y):
|
||||
print("Yihaa")
|
||||
if snake_head.distance(self.food) <= 15:
|
||||
self.food.hide()
|
||||
self.snake.extend()
|
||||
self.food_on_screen = False
|
||||
|
||||
@ -51,9 +51,12 @@ class Snake:
|
||||
def get_head_position(self):
|
||||
return self.segments[0].xcor(), self.segments[0].ycor()
|
||||
|
||||
def get_head(self):
|
||||
return self.segments[0]
|
||||
|
||||
def tailbite(self):
|
||||
for i in range(len(self.segments) - 1, 0, -1):
|
||||
if self.segments[0].position() == self.segments[i].position():
|
||||
if self.segments[0].distance(self.segments[i]) <= 15:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
Reference in New Issue
Block a user