pull down to refresh

deleted by author
Sorry, I don't know how to format the text. Some comments have become headlines. How can I differentiate between text and code when inserting a post?
reply
reply
code
Thanks, but it only works when I copy it. I don't know what character it is or how to type it. When I type an apostrophe, it doesn't work.
reply
The character is a backtick: https://en.wikipedia.org/wiki/Backtick
reply
Thanks, it's working for me now, but it's weird. VSCode uses an apostrophe for the same purpose, which doesn't work here, and the bactick that works here doesn't work in VSCode.
reply
So again and better:
from turtle import Turtle, Screen
import time
import random

score = 0
highest_score = 0

# Vytvoří screen
screen = Screen()
screen.bgcolor("black")
screen.title("Vítejte ve Snake Game!")
screen.setup(width=600, height=600)
screen.tracer(False)

# Hadí hlava
head = Turtle("square")
head.color("green")
head.speed(0)
head.penup()
head.goto(0, 0)
head.direction = "stop"

# Krmení pro hada
apple = Turtle("circle")
apple.color("red")
apple.penup()
x = random.randint(-280, 280)
y = random.randint(-280, 280)
apple.goto(x, y)

# Lišta se skóre
score_sign = Turtle()
score_sign.color("light green")
score_sign.speed()
score_sign.penup()
score_sign.hideturtle()
score_sign.goto(0, 265)
score_sign.write("Skóre: 0 Nejvyžší skóre: 0", align="center", font=("Arial", 18))


# Tělo hada
body_parts = []


# Funkce
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 == "right":
        x = head.xcor()   
        head.setx(x + 20)

    if head.direction == "left":
        x = head.xcor()
        head.setx(x - 20)

def move_up():
    if head.direction != "down":
        head.direction = "up"
    
def move_down():
    if head.direction != "up":
        head.direction = "down"

def move_right():
    if head.direction != "left":
        head.direction = "right"

def move_left():
    if head.direction != "right":
        head.direction = "left"

# Kliknutí na klávesy
screen.listen()
screen.onkeypress(move_up, "w")
screen.onkeypress(move_down, "s")
screen.onkeypress(move_right, "d")
screen.listen()
screen.onkeypress(move_left, "a")


# Hlavní cyklus
while True:
    screen.update()

    # kontrola kolize s plátnem
    if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290:
        time.sleep(2)
        head.goto(0, 0)
        head.direction = "Stop"

        for one_body_part in body_parts:
            one_body_part.goto(1000, 1000)
        # vyčistí list s tělem hada
        body_parts.clear()
        score = 0
        score_sign.clear()
        score_sign.write(f"Skóre: {score} Nejvyžší skóre: {highest_score}", align="center", font=("Arial", 18))


    # Sežrání krmení
    if head.distance(apple) < 20:
        x = random.randint(-280, 280)
        y = random.randint(-280, 280)
        apple.goto(x, y)

        # Přidání části těla
        new_body_part = Turtle("square")
        new_body_part.speed(0)
        new_body_part.color("light green")
        new_body_part.penup()
        body_parts.append(new_body_part)

        # Zvýšení skóre
        score += 10

        if highest_score < score:
            highest_score = score            
        score_sign.clear()
        score_sign.write(f"Skóre: {score} Nejvyžší skóre: {highest_score}", align="center", font=("Arial", 18))

    # Skládání částí těla za sebe 
    for index in range(len(body_parts) -1, 0, -1):
        x = body_parts[index - 1].xcor()
        y = body_parts[index - 1].ycor()
        body_parts[index].goto(x, y)

    # Zařazení 1. části těla za hlavu
    if len(body_parts) > 0:
        x = head.xcor()
        y = head.ycor()
        body_parts[0].goto(x, y)

    
            
    move()

    # Kontrola kolize hlavy s tělem hada
    for one_body_part in body_parts:
        if one_body_part.distance(head) < 20:
            time.sleep(2)
            head.goto(0, 0)
            head.direction = "stop"
            for one_body_part in body_parts:
                one_body_part.goto(1000, 1000)
            # vyčistí list s tělem hada
            body_parts.clear()
            score = 0
            score_sign.clear()
            score_sign.write(f"Skóre: {score} Nejvyžší skóre: {highest_score}", align="center", font=("Arial", 18))


    time.sleep(0.1)
reply
I'm impressed. I managed to code half a 1970s pong game and couldn't finish it. The ball is still banging off the paddle.
reply
I programmed it according to the instructions. I couldn't have done it myself. Pong is a good idea for another learning project ;o)
reply
Me too! There's no way I could do it from scratch. I still got lost somehow. Then I got lazy. There is a good pong tutorial on youtube, but I don't have the link.
reply