파이썬으로 추억의 게임 만들기(snake)
(pip install freegames로 라이브러리 먼저 설치해야 합니다.)
from turtle import *
from random import randrange
from freegames import square, vector
food = vector(0, 0)
snake = [vector(10, 0)]
aim = vector(0, -10)
def change(x, y):
aim.x = x
aim.y = y
def inside(head):
return -200 < head.x < 190 and -200 < head.y < 190
def move():
head = snake[-1].copy()
head.move(aim)
if not inside(head) or head in snake:
square(head.x, head.y, 9, 'red')
update()
return
snake.append(head)
if head == food:
print('Snake:', len(snake))
food.x = randrange(-15, 15) * 10
food.y = randrange(-15, 15) * 10
else:
snake.pop(0)
clear()
for body in snake:
square(body.x, body.y, 9, 'black')
square(food.x, food.y, 9, 'green')
update()
ontimer(move, 100)
setup(420, 420, 370, 0)
hideturtle()
tracer(False)
listen()
onkey(lambda: change(10, 0), 'Right')
onkey(lambda: change(-10, 0), 'Left')
onkey(lambda: change(0, 10), 'Up')
onkey(lambda: change(0, -10), 'Down')
move()
done()
엔지니어 퓔이 묻어있는 스크립트입니다. 깔끔! 간결하게! 한눈에 들어오지만 함수를 다 뜯어봐야 속을 알 수 있는 코드입니다. 소스인 듯 소스 아닌 스네이크 코드!!! 해당 파이썬 소스 파일과 홈페이지 링크 올려놓습니다.
Grant Jenks Official Website. listen | learn | think | solve
Grant Jenks Official Website: listen | learn | think | solve. Sole Proprietor at Grant Jenks Enterprises: Software and Services.
www.grantjenks.com