藏不住Python龟

41zrol4v  于 2023-04-10  发布在  Python
关注(0)|答案(1)|浏览(61)

在我的代码中,在函数body_collision中,我想在碰撞的情况下隐藏蛇,这样它就发生在窗口的中间,我仍然可以读取Game Over单词和总分,但不知道如何做到这一点。

import random
from turtle import Turtle, Screen, xcor
import time

total_score = 0

# create screen

my_screen = Screen()
my_screen.setup(width=600, height=600)
my_screen.bgcolor("black")
my_screen.title("My Snake Game")
my_screen.tracer(0)

turtle_position = [(0, 0), (-20, 0), (-40, 0)]
turtle_body = []

# create and positioning random food
def food():
    random_x = random.randint(-280, 280)
    random_y = random.randint(-280, 280)
    food = Turtle()
    food.shape("circle")
    food.shapesize(0.8)
    food.color("yellow")
    food.penup()
    food.goto(random_x, random_y)
    return food

# create and positioning snake body parts
def create_snake():
    for position in turtle_position:
        my_snake = Turtle()
        my_snake.shape("square")
        my_snake.color("white")
        my_snake.penup()
        turtle_body.append(my_snake)
        my_snake.goto(position)

def move_up():
    if turtle_body[0].heading() == 270:
        pass
    else:
        turtle_body[0].setheading(90)

def move_down():
    if turtle_body[0].heading() == 90:
        pass
    else:
        turtle_body[0].setheading(270)

def move_left():
    if turtle_body[0].heading() == 0:
        pass
    else:
        turtle_body[0].setheading(180)

def move_right():
    if turtle_body[0].heading() == 180:
        pass
    else:
        turtle_body[0].setheading(0)

# listen to key press
my_screen.onkeypress(move_up, "Up")
my_screen.onkeypress(move_down, "Down")
my_screen.onkeypress(move_right, "Right")
my_screen.onkeypress(move_left, "Left")
my_screen.listen()

# move snake part to next position
def move_snake_part():
    for turtle_part in range(len(turtle_body) - 1, 0, -1):
        new_x = turtle_body[turtle_part - 1].xcor()
        new_y = turtle_body[turtle_part - 1].ycor()
        turtle_body[turtle_part].goto(new_x, new_y)

# move food to new random position when eaten

def move_food(food):
    random_x = random.randint(-280, 280)
    random_y = random.randint(-280, 280)
    food.goto(random_x, random_y)
    return food

# create new snake body part
def add_body_part():
    new_segment = Turtle()
    new_segment.shape("square")
    new_segment.color("white")
    new_segment.penup()
    turtle_body.append(new_segment)
    segment_x = turtle_body[len(turtle_body) - 2].xcor()
    segment_y = turtle_body[len(turtle_body) - 2].ycor()
    new_segment.goto(segment_x, segment_y)

# create and initialize the snake
create_snake()
food_object = food()

# start game
game_is_on = True

# detect collision with body
def body_collision(is_running):
    for turtle_part in turtle_body[1:]:
        if turtle_body[0].distance(turtle_part) < 8:
            is_running = False
            # create a Turtle object for the "game over" message
            game_over = Turtle()
            game_over.color("white")
            game_over.hideturtle()
            game_over.write("GAME OVER", align="center", font=("Arial", 24, "bold"))
            # call show_score
            show_score(total_score)
            for segment in turtle_body:
                segment.hideturtle()

    return is_running

# calculate total score using "score" as placeholder for the variable total_score
def calc_score(score):
    score += 1
    return score

# show total_score
def show_score(score):
    score_print = Turtle()
    score_print.hideturtle()
    score_print.penup()
    score_print.goto(0, -50)
    score_print.color("white")
    score_print.write(f"Total Score: {score}", align="center", font=("Arial", 20, "normal"))

# detect collision with walls
def walls_collision(is_running):
    snake_head = turtle_body[0]
    if snake_head.xcor() > 280 or snake_head.xcor() < -290:
        is_running = False
        # create a Turtle object for the "game over" message
        game_over = Turtle()
        game_over.color("white")
        game_over.hideturtle()
        game_over.write("GAME OVER", align="center", font=("Arial", 24, "bold"))
        # call show_score
        show_score(total_score)
    elif snake_head.ycor() > 290 or snake_head.ycor() < -280:
        is_running = False
        # create a Turtle object for the "game over" message
        game_over = Turtle()
        game_over.color("white")
        game_over.hideturtle()
        game_over.write("GAME OVER", align="center", font=("Arial", 24, "bold"))
        # call show_score
        show_score(total_score)
    return is_running

while game_is_on:
    my_screen.update()
    time.sleep(0.05)

    move_snake_part()
    turtle_body[0].forward(10)

    if turtle_body[0].distance(food_object.position()) < 15:
        move_food(food_object)
        # calc_score(total_score) returns the updated value for total score variable and saves it into total_score
        # which is now updated with the actual points
        total_score = calc_score(total_score)
        add_body_part()

    game_is_on = body_collision(game_is_on)
    game_is_on = walls_collision(game_is_on)

my_screen.exitonclick()

我希望这条蛇在碰撞时会消失

afdcj2ne

afdcj2ne1#

hideturtle()方法用来隐藏乌龟。你可以用它来让蛇消失。你还必须使用my_screen.update()更新pygame屏幕。你必须修改你的body_collision函数:

def body_collision(is_running):
    for turtle_part in turtle_body[1:]:
        if turtle_body[0].distance(turtle_part) < 8:
            is_running = False
            # create a Turtle object for the "game over" message
            game_over = Turtle()
            game_over.color("white")
            game_over.hideturtle()
            game_over.write("GAME OVER", align="center", font=("Arial", 24, "bold"))
            # call show_score
            show_score(total_score)
            for segment in turtle_body:
                segment.hideturtle()

            break #Break out of the for loop to prevent the the snake from disappearing multiple times

            my_screen.update() #Update the screen to apply changes

    return is_running

相关问题