티스토리 뷰
* 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.20220302(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.clear()
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.game_over()
game_is_on = False
for tut in snake.turtles[1:]: # slicing
if snake.head.distance(tut) < 10:
game_is_on = False
scoreboard.game_over()
screen.exitonclick()
* 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 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)
* 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)
* scoreboard.py
from turtle import Turtle
ALIGNMENT = "center"
FONT = ("Arial", 24, "normal")
class Scoreboard(Turtle):
def __init__(self):
super().__init__()
self.yumyum = 0
self.goto(0, 265)
self.color("white")
self.pu()
self.hideturtle()
self.score()
def game_over(self):
self.goto(0, 0)
self.write("GAME OVER", align=ALIGNMENT, font=FONT)
def score(self):
self.write(f"Score: {self.yumyum}", align=ALIGNMENT, font=FONT)
self.yumyum += 1
'Test > Python(20220101~)' 카테고리의 다른 글
Days23. Turtle crossing 게임 (0) | 2022.03.05 |
---|---|
Days22. Pong 게임 (0) | 2022.03.02 |
Days21. 뱀게임7 - Slicing (0) | 2022.03.02 |
Days21. 뱀게임6 - 꼬리를 물면 GAME OVER (0) | 2022.03.02 |
Days21. 뱀게임5 - 벽에 닿으면 종료, food를 먹으면 길어지기 (0) | 2022.03.02 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 변수화
- 대소문자
- LIST
- dp-2
- artandculture
- powershell
- Join
- virt-sysprep
- 3par
- exadata
- vmware
- fromkeys
- insert
- sysprep
- oracle
- 스토리지
- storage
- 차집합
- EXA
- 부동없이
- vmware.powercli
- dp-1
- cloud-init
- 중복제거
- powercli
- dezoomify
- 읽어오기
- set()
- 배열
- 정렬
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
글 보관함