TIL: Python Basics Day 20 - Snake Game (first 3 steps)

이다연·2020년 12월 30일
0

Udemy Python Course

목록 보기
20/64

Project: Snake Game (3 steps)

Create the body

Move the snake

Control the snake

from turtle import Screen, Turtle
import time
from snake import Snake

screen = Screen()
screen.setup(width= 600, height=600)
screen.bgcolor("black")
screen.title("Snaky Game")
screen.tracer(0)

segments = []

for turtle_index in range(3):
    new_segment = Turtle(shape="square")
    new_segment.color("white")
    new_segment.penup()

    new_segment.goto(x=0 + turtle_index * -20, y=0)
    segments.append(new_segment)
#2. move the snake

game_is_on = True
while game_is_on:
    screen.update()
    time.sleep(1)
#snake move its body overlaping itself(2nd part's position becomes 3rd part's new position)
    for seg_num in range(len(segments) - 1, 0 , -1):
        new_x = segments[seg_num - 1].xcor()
        new_y = segments[seg_num - 1].ycor()
        segments[seg_num].goto(new_x, new_y)
    segments[0].forward(20)

#3. Control the snake

snake = Snake()
snake.move()

screen.exitonclick()
profile
Dayeon Lee | Django & Python Web Developer

2개의 댓글

comment-user-thumbnail
2023년 11월 30일

This Python snippet lays the foundation for a classic Snake Game, marking the initial steps—creating the body, moving the snake, and controlling its motion. Using Turtle graphics, it establishes the snake's segments and initiates its movement across the screen. The code's loop structure updates the screen and controls the snake's motion, making each segment follow the one before it. To enhance gameplay, introducing controls for user interaction will be crucial, allowing players to steer the snake. This project taps into coding for entertainment, blending gaming concepts with programming, a fusion that connects technology enthusiasts with fun lifestyle topics like gaming and creativity.

Play Summoners War on PC - Games.lol

답글 달기
comment-user-thumbnail
2024년 1월 29일

The Snake Game, a classic in the realm of online entertainment, has evolved since its inception. From its humble beginnings to the contemporary versions, enthusiasts can now enjoy a myriad of adaptations. One notable instance is the rendition hosted on the domain xn--o80bl8jezb35e91unugksh.com, offering a unique twist to the timeless gameplay. This domain reflects the game's enduring popularity, showcasing the creativity and diversity within the gaming community as it continues to captivate players worldwide.

답글 달기