Python Game Programming Tutorial: Snake Game Part 1 - playproduction.de

Python Game Programming Tutorial: Snake Game Part 1

TokyoEdtech
Views: 269522
Like: 3247
NEED HELP?
🆘 Watch this first and then let me know in the comments below:

❤️❤️ SHOW SOME LOVE AND SUPPORT THE CHANNEL ❤️❤️

Click Join and Become a Channel Member Today!
Channel members can get preferential comment replies, early access to new content, members only live streams, and access to my private Discord.
❤️

Amazon Affiliate Links
💻 My Laptop (Asus Zenbook 13):
🎙My New Microphone (Blue Yeti Pro):
🎙My Old Microphone (Blue Snowball):
🎶My USB Interface (Focusrite Scarlett Solo):

Other Affiliate Links
⭐ Kite is a free AI-powered coding assistant that will help you code faster and smarter. The Kite plugin integrates with all the top editors and IDEs to give you smart completions and documentation while you’re typing.

LINKS
🗄️ GITHUB:
💬 Follow me on Twitter:
📜 Subscribe to my Newsletter:
📝 Check out my Blog:
⬇️ Download Geany Editor:

LEARN MORE PYTHON
➡️Space Invaders:
➡️Snake Game:
➡️Pong:
➡️Space War:
➡️Intro to Python (for Java Coders):
➡️Space Arena – The Ultimate Python Turtle Graphics Game Tutorial:

LEARN MORE JAVA
➡️Basic Java for Beginners:
➡️Intro to AP Computer Science A:

#Python #Tutorial #Beginner

251 Comments

  1. It's a great tutorial. I like your videos and tutorials very much

  2. I just love ur effort and would like to say

    Thanks a ton my hero!!!😃😃😃😃

  3. should i even write the green colour script at the top

  4. I tryed turning tracer on and Python has stoped responding what could be the problem

  5. Green color window is opening but there is no snake y?

  6. Good 👍.
    Can you make snakes and ladders game please.

  7. in your next video of making game try to make it on windows 7 python 3.8.6
    plz i T WOULD BE REALLY HELP FULL

  8. Sir I ran the code ….my green screen came but it vanished quickly

  9. Hi sir, I need your help for some program. can you please support

  10. Dude you should write a book on turtle module it would be so helpful i bet you could make some good money…. coding books most of the time are like 30 bucks (united states dollars)

  11. hello sir, I ran the code but i didnt get the 600×600 size of the screen and instead it was a full screen and I couldnt reduce the size of screen even if I take values less than 600.

  12. I'm not getting the title in output. What should i do?

  13. You could use try and except to stop the "window.update() cannot be terminated" message, it looks some thing like this…

    try:
    window.update()
    except:
    break

  14. i need the code to copy paste where is he?????????

  15. did you know that when you make the setup for the screen you don't need to type width or height you just need to type in how high and wide it's going to be.

  16. AttributeError: 'tuple' object has no attribute 'append'

  17. here is the code import turtle

    import time

    import random

    delay = 0.1

    # Score

    score = 0

    high_score = 0

    # Set up the screen

    wn = turtle.Screen()

    wn.title("Snake Game by @TokyoEdTech")

    wn.bgcolor("green")

    wn.setup(width=600, height=600)

    wn.tracer(0) # Turns off the screen updates

    # Snake head

    head = turtle.Turtle()

    head.speed(0)

    head.shape("square")

    head.color("black")

    head.penup()

    head.goto(0,0)

    head.direction = "stop"

    # Snake food

    food = turtle.Turtle()

    food.speed(0)

    food.shape("circle")

    food.color("red")

    food.penup()

    food.goto(0,100)

    segments = []

    # Pen

    pen = turtle.Turtle()

    pen.speed(0)

    pen.shape("square")

    pen.color("white")

    pen.penup()

    pen.hideturtle()

    pen.goto(0, 260)

    pen.write("Score: 0 High Score: 0", align="center", font=("Courier", 24, "normal"))

    # Functions

    def go_up():

    if head.direction != "down":

    head.direction = "up"

    def go_down():

    if head.direction != "up":

    head.direction = "down"

    def go_left():

    if head.direction != "right":

    head.direction = "left"

    def go_right():

    if head.direction != "left":

    head.direction = "right"

    def move():

    if head.direction == "up":

    y = head.ycor()

    head.sety(y + 20)

    if head.direction == "down":

    y = head.ycor()

    head.sety(y – 20)

    if head.direction == "left":

    x = head.xcor()

    head.setx(x – 20)

    if head.direction == "right":

    x = head.xcor()

    head.setx(x + 20)

    # Keyboard bindings

    wn.listen()

    wn.onkeypress(go_up, "w")

    wn.onkeypress(go_down, "s")

    wn.onkeypress(go_left, "a")

    wn.onkeypress(go_right, "d")

    # Main game loop

    while True:

    wn.update()

    # Check for a collision with the border

    if head.xcor()>290 or head.xcor()<-290 or head.ycor()>290 or head.ycor()<-290:

    time.sleep(1)

    head.goto(0,0)

    head.direction = "stop"

    # Hide the segments

    for segment in segments:

    segment.goto(1000, 1000)

    # Clear the segments list

    segments.clear()

    # Reset the score

    score = 0

    # Reset the delay

    delay = 0.1

    pen.clear()

    pen.write("Score: {} High Score: {}".format(score, high_score), align="center", font=("Courier", 24, "normal"))

    # Check for a collision with the food

    if head.distance(food) < 20:

    # Move the food to a random spot

    x = random.randint(-290, 290)

    y = random.randint(-290, 290)

    food.goto(x,y)

    # Add a segment

    new_segment = turtle.Turtle()

    new_segment.speed(0)

    new_segment.shape("square")

    new_segment.color("grey")

    new_segment.penup()

    segments.append(new_segment)

    # Shorten the delay

    delay -= 0.001

    # Increase the score

    score += 10

    if score > high_score:

    high_score = score

    pen.clear()

    pen.write("Score: {} High Score: {}".format(score, high_score), align="center", font=("Courier", 24, "normal"))

    # Move the end segments first in reverse order

    for index in range(len(segments)-1, 0, -1):

    x = segments[index-1].xcor()

    y = segments[index-1].ycor()

    segments[index].goto(x, y)

    # Move segment 0 to where the head is

    if len(segments) > 0:

    x = head.xcor()

    y = head.ycor()

    segments[0].goto(x,y)

    move()

    # Check for head collision with the body segments

    for segment in segments:

    if segment.distance(head) < 20:

    time.sleep(1)

    head.goto(0,0)

    head.direction = "stop"

    # Hide the segments

    for segment in segments:

    segment.goto(1000, 1000)

    # Clear the segments list

    segments.clear()

    # Reset the score

    score = 0

    # Reset the delay

    delay = 0.1

    # Update the score display

    pen.clear()

    pen.write("Score: {} High Score: {}".format(score, high_score), align="center", font=("Courier", 24, "normal"))

    time.sleep(delay)

    wn.mainloop()

  18. i am trying this code on vs code but it is not showing the output then what should i do

  19. can you teach us how to add pause and quit button in the game

  20. i wrote all the code down pycharm had the little tick to show everything was correct then i clicked run and chose the file i wanted to run but it still ran another file instead do you know what happened

  21. hi i absolutely love your videos and I have followed so many tutorials now, I was just wondering if you could make a tutorial on how to make a simple clicker game (like cookie clicker)

  22. Hi, can you do a version where the food does not spawn under the snake itself?

  23. wn.mainloop() does not work for me. It says, "AttributeError: '_Screen' object has no attribute 'mainloop'"

    do you know how to fix this?

  24. ok, so i wanted a tutorial on a cookie clicker because I wanted to watch your channel more because it is really easy to follow, but I have to pay to watch it. why………

  25. line 8
    NameError: name 'screen' is not defined
    What can I doooo ?

  26. My code works, but the screen is only staying up for a millisecond.
    What do I do?

  27. 'python is not responding' how can i fix it?thank you

  28. Hi! so I was wondering what program you used to make the game. I'm pretty new at coding and I use repl.it. If we used different programs, the code would stay the same right? Thanks 😊 And by the way, your tutorials are great!

  29. hi! is there a way i can detect what the score is, im trying to see when the score reaches certent numbers

  30. File "c:Users*****DesktopPyGamemain.py", line 8, in <module>

    wn = turtle.screen

    AttributeError: module 'turtle' has no attribute 'screen'

    plz plz plz help anyone

  31. I just completed and coded this entire tutorial series. This is one of the better Snake Game tutorials on YouTube – You did an excellent job explaining the logic behind the code in each video!

  32. found you from free code channel. the guy who was teaching snake there was completely useless in teaching and i got the concepts of the pong so easily due to your teaching methods. thanks again for uploading these kind of videos of aspiring software engineers like us. really appreciate your time and effort in doing so.

  33. Attribute error: Module ‘Turtle’ has no attribute ‘shape’

    Help please

  34. tq so much for dis …… how to solve this error :
    Traceback (most recent call last):

    File "<stdin>", line 1, in <module>

    NameError: name 'wn' is not defined

  35. Thank you for these awesome tutorials. Is there a way to display a grid where the snake is moving?

  36. Hey TokyoEdtech, I dont know why, but no window is shown 🙁

    # Simple Snake Game in Python

    import turtle

    # Set up Screen
    wn = turtle.Screen()
    wn.title("Snake by Louisa")
    wn.bgcolor("green")
    wn.setup(600, 600)
    wn.tracer(0)

    wn.mainloop()

Leave a Reply

Your email address will not be published.