我正在开发一款蛇的游戏,我写了很少的代码行,但是蛇的速度太快了,我想把速度降到正常,怎么做?在python中

gfttwv5a  于 2023-01-08  发布在  Python
关注(0)|答案(1)|浏览(133)

在python中

import time
from turtle import Turtle, Screen

# screen height and width
height = 600
width = 600

# snake initial positinn
x_coordinate = 0
y_coordinate = 0

game_on = True
screen = Screen()
screen.bgcolor("black")
screen.setup(height=height, width=width)
screen.title("Snake Game")
full_snake = []
screen.tracer(0)

# create a snake 3X3
for _ in range(3):
    snake = Turtle("square")
    snake.color("white")
    snake.penup()
    snake.goto(x_coordinate, y_coordinate)
    x_coordinate -= 20
    full_snake.append(snake)
snake_head = full_snake[0]


# function to operate the snake
def up():
    if snake_head.heading() != 270:
        snake_head.setheading(90)

def down():
    if snake_head.heading() != 90:
        snake_head.setheading(270)

def right():
    if snake_head.heading() != 180:
        snake_head.setheading(0)

def left():
    if snake_head.heading() != 0:
        snake_head.setheading(180)

screen.listen()
screen.onkey(up, "w")
screen.onkey(down, "s")
screen.onkey(right, "d")
screen.onkey(left, "a")

# function for snakes part to attached
def attached():
    for i in range(len(full_snake) - 1, 0, -1):
        new_x = full_snake[i - 1].xcor()
        new_y = full_snake[i - 1].ycor()
        full_snake[i].goto(new_x, new_y)
    snake_head.fd(20)

# to move the snake
while game_on:
    snake_head.speed(1)
    screen.update()
    time.sleep(0.1)
    for snake in full_snake:
        attached()

screen.exitonclick()
    • 在这段代码中snake的速度比较快,我想把速度降到正常的地方,我可以放置speed()方法或者Turtle模块中的任何方法从python中得到预期的输出:在不分离其他两个块的情况下,蛇形管的速度正常**............................................................................................................ ..................................................................................................................................................................................................
7kjnsjlb

7kjnsjlb1#

您可以尝试使用ontimer()方法来控制snake的移动。

import time
from turtle import Turtle, Screen

# screen height and width
height = 600
width = 600

# snake initial position
x_coordinate = 0
y_coordinate = 0

game_on = True
screen = Screen()
screen.bgcolor("black")
screen.setup(height=height, width=width)
screen.title("Snake Game")
full_snake = []
screen.tracer(0)

# create a snake 3X3
for _ in range(3):
    snake = Turtle("square")
    snake.color("white")
    snake.penup()
    snake.goto(x_coordinate, y_coordinate)
    x_coordinate -= 20
    full_snake.append(snake)
snake_head = full_snake[0]

# function to operate the snake
def up():
    if snake_head.heading() != 270:
        snake_head.setheading(90)

def down():
    if snake_head.heading() != 90:
        snake_head.setheading(270)

def right():
    if snake_head.heading() != 180:
        snake_head.setheading(0)

def left():
    if snake_head.heading() != 0:
        snake_head.setheading(180)

screen.listen()
screen.onkey(up, "w")
screen.onkey(down, "s")
screen.onkey(right, "d")
screen.onkey(left, "a")

# function for snakes part to attached
def attached():
    for i in range(len(full_snake) - 1, 0, -1):
        new_x = full_snake[i - 1].xcor()
        new_y = full_snake[i - 1].ycor()
        full_snake[i].goto(new_x, new_y)
    snake_head.fd(20)

# New function to move the snake
def move_snake():
    attached()
    screen.update()
    #timer to recursively call the move_snake function every 500 milliseconds
    screen.ontimer(move_snake, 500)  # adapt to your desired speed (the lower the faster)

# start moving the snake
move_snake()

screen.exitonclick()

相关问题