클래스 상속 예시
super().init() 조합으로 서로 다른 클래스를 상속할 수 있다.
아래 코드와같이 Fish의 nemo는 메소드 swim()뿐만 아니라 breathe() 메소드도 사용 할 수있는것을 확인할 수 있다.
또한 메소드안에서도 super()를 통해 메소드를 수정할 수 있다. ex) Animal의 breathe와 Fish의 breathe
class Animal:
def __init__(self):
self.num_eyes = 2
def breathe(self):
print("들숨, 날숨.")
class Fish(Animal) :
def __init__(self):
super().__init__()
def breathe(self):
super().breathe()
print("물속에서")
def swim(self):
print("물안에서 움직인다.")
nemo = Fish()
nemo.swim()
nemo.breathe()
# 결과
물안에서 움직인다.
들숨, 날숨.
물속에서
메인 파일
import turtle
import time
from turtle import Screen
from snake import Snake
from food import Food
from scoreboard import ScoreBoard
screen = Screen()
screen.setup(width=600, height=600)
screen.bgcolor("black")
screen.title("My Snake Game")
screen.tracer(0)
turtle.hideturtle()
turtle.speed(1)
snake = Snake()
food = Food()
scoreboard = ScoreBoard()
screen.listen()
screen.onkey(fun=snake.move_up, key="Up")
screen.onkey(fun=snake.move_down, key="Down")
screen.onkey(fun=snake.move_left, key="Left")
screen.onkey(fun=snake.move_right, key="Right")
game_is_on = True
while game_is_on :
screen.update()
time.sleep(0.1)
scoreboard.update_scoreboard()
snake.move_snake()
# 먹이와 충돌
if snake.head.distance(food) < 15 :
food.refresh()
scoreboard.update_scoreboard()
scoreboard.increase_score()
snake.extend()
# 벽과 충돌
if snake.head.xcor() > 280 or snake.head.xcor() < -280 or snake.head.ycor() > 280 or snake.head.ycor() < -280 :
game_is_on = False
scoreboard.game_over()
# 꼬리와 충돌
for segment in snake.all_segment[1:] :
if snake.head.distance(segment) < 10 :
game_is_on = False
scoreboard.game_over()
screen.exitonclick()
from turtle import Turtle
STARTING_POINT = [(0, 0), (-20, 0), (-40, 0)]
MOVE_DISTANCE = 20
UP = 90
DOWN = 270
LEFT = 180
RIGHT = 0
class Snake() :
def __init__(self):
self.all_segment=[]
self.create_snake()
self.head = self.all_segment[0]
def create_snake(self):
for segment in STARTING_POINT:
self.add_segment(position=segment)
def add_segment(self, position):
new_segment = Turtle("square")
new_segment.color("white")
new_segment.penup()
new_segment.goto(position)
self.all_segment.append(new_segment)
def extend(self):
self.add_segment(self.all_segment[-1].position())
def move_snake(self):
for seg_num in range(len(self.all_segment) - 1, 0, -1):
new_x = self.all_segment[seg_num - 1].xcor()
new_y = self.all_segment[seg_num - 1].ycor()
self.all_segment[seg_num].goto(new_x, new_y)
self.head.forward(MOVE_DISTANCE)
def move_up(self):
if self.head.heading() != DOWN :
self.head.setheading(UP)
def move_down(self):
if self.head.heading() != UP :
self.head.setheading(DOWN)
def move_left(self):
if self.head.heading() != RIGHT :
self.head.setheading(LEFT)
def move_right(self):
if self.head.heading() != LEFT :
self.head.setheading(RIGHT)
from turtle import Turtle
STARTING_POINT = [(0, 0), (-20, 0), (-40, 0)]
MOVE_DISTANCE = 20
UP = 90
DOWN = 270
LEFT = 180
RIGHT = 0
class Snake() :
def __init__(self):
self.all_segment=[]
self.create_snake()
self.head = self.all_segment[0]
def create_snake(self):
for segment in STARTING_POINT:
self.add_segment(position=segment)
def add_segment(self, position):
new_segment = Turtle("square")
new_segment.color("white")
new_segment.penup()
new_segment.goto(position)
self.all_segment.append(new_segment)
def extend(self):
self.add_segment(self.all_segment[-1].position())
def move_snake(self):
for seg_num in range(len(self.all_segment) - 1, 0, -1):
new_x = self.all_segment[seg_num - 1].xcor()
new_y = self.all_segment[seg_num - 1].ycor()
self.all_segment[seg_num].goto(new_x, new_y)
self.head.forward(MOVE_DISTANCE)
def move_up(self):
if self.head.heading() != DOWN :
self.head.setheading(UP)
def move_down(self):
if self.head.heading() != UP :
self.head.setheading(DOWN)
def move_left(self):
if self.head.heading() != RIGHT :
self.head.setheading(LEFT)
def move_right(self):
if self.head.heading() != LEFT :
self.head.setheading(RIGHT)
from turtle import Turtle
import random
class Food(Turtle):
def __init__(self):
super().__init__()
self.shape("circle")
self.penup()
self.shapesize(stretch_len=0.5,stretch_wid=0.5)
self.color("blue")
self.speed("fastest")
self.refresh()
def refresh(self):
random_x = random.randint(-280, 280)
random_y = random.randint(-280, 280)
self.goto(random_x, random_y)