Test/Python(20220101~)
Days24. 파일과 디렉토리, 경로 - 뱀게임 최고점을 파일에 기록,사용
kiostory
2022. 3. 6. 12:23
*main.py
from turtle import Screen
from snake import Snake
from food import Food
from scoreboard import Scoreboard
import time
screen = Screen()
screen.setup(width=600, height=600)
screen.bgcolor("black")
screen.tracer(0)
screen.title("kio's snake game - ver.20220223(the first one)")
snake = Snake()
food = Food()
scoreboard = Scoreboard()
screen.listen()
screen.onkey(snake.up, "Up")
screen.onkey(snake.down, "Down")
screen.onkey(snake.left, "Left")
screen.onkey(snake.right, "Right")
game_is_on = True
while game_is_on:
screen.update()
time.sleep(0.2)
snake.move()
# Detect collision with food.
if snake.head.distance(food) < 20:
food.refresh()
snake.extend()
scoreboard.add_score()
scoreboard.score()
# Detect collision with wall.
if snake.head.xcor() < -290 or snake.head.xcor() > 290 or snake.head.ycor() < -290 or snake.head.ycor() > 290:
scoreboard.reset()
snake.reset()
for tut in snake.turtles[1:]: # slicing
if snake.head.distance(tut) < 10:
scoreboard.reset()
snake.reset()
screen.exitonclick()
*scoreboard.py
from turtle import Turtle
ALIGNMENT = "center"
FONT = ("Arial", 24, "normal")
class Scoreboard(Turtle):
def __init__(self):
super().__init__()
with open("data.txt") as file: #같은 디렉토리의 data.txt가 최고점수 기록파일임
self.highscore = int(file.read()) #read하면 string값. 대입을 위해 int화
self.yumyum = 0
self.goto(0, 265)
self.color("white")
self.pu()
self.hideturtle()
self.score()
def reset(self):
if self.yumyum >= self.highscore:
with open("data.txt", mode="w") as file: #쓰기 모드로 다시 열고
file.write(f"{self.yumyum}") #최고점을 data.txt에 쓴다
self.highscore = self.yumyum
self.yumyum = 0
self.score()
def add_score(self):
self.yumyum += 1
def score(self):
self.clear()
self.write(f"Score: {self.yumyum} High Score: {self.highscore}", align=ALIGNMENT, font=FONT)
*food.py
from turtle import Turtle
import random
class Food(Turtle):
def __init__(self):
super().__init__()
self.shape("circle")
self.pu()
self.shapesize(stretch_wid=0.5, stretch_len=0.5) # default 20by20에서 0.5를 입력하였으니, 10by10 크기가 됨
self.color("white")
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)
*snake.py
from turtle import Turtle
import random
STARTING_POSITIONS = [(0, 0), (-20, 0), (-40, 0)]
COLOR = ["red", "orange", "yellow", "green", "blue", "navy", "purple"]
MOVING_DISTANCE = 20
UP = 90
DOWN = 270
LEFT = 180
RIGHT = 0
class Snake:
def __init__(self):
self.turtles = []
self.create_snake()
self.head = self.turtles[0]
self.color_index = 0
def create_snake(self):
for pos in STARTING_POSITIONS:
self.add_segment(pos)
def add_segment(self, pos):
i = random.randint(0, 6)
new_turtle = Turtle(shape="square")
new_turtle.color(COLOR[i])
new_turtle.pu()
new_turtle.setpos(pos)
self.turtles.append(new_turtle)
def reset(self):
for tut in self.turtles:
tut.goto(1000, 1000)
self.turtles.clear()
self.create_snake()
self.head = self.turtles[0]
def extend(self):
# add a new segment to the snake.
self.add_segment(self.turtles[-1].position())
def move(self):
for tut_num in range(len(self.turtles) - 1, 0, -1): # (start=2, stop=0, step=-1)
new_x = self.turtles[tut_num - 1].xcor()
new_y = self.turtles[tut_num - 1].ycor()
self.turtles[tut_num].goto(new_x, new_y)
self.head.fd(MOVING_DISTANCE)
def up(self):
if self.head.heading() != DOWN:
self.head.setheading(UP)
def down(self):
if self.head.heading() != UP:
self.head.setheading(DOWN)
def left(self):
if self.head.heading() != RIGHT:
self.head.setheading(LEFT)
def right(self):
if self.head.heading() != LEFT:
self.head.setheading(RIGHT)
*data.txt
0