首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我怎样才能防止每次添加一个片段时,我的蛇游戏就会慢下来?

我怎样才能防止每次添加一个片段时,我的蛇游戏就会慢下来?
EN

Stack Overflow用户
提问于 2022-01-09 20:21:48
回答 1查看 121关注 0票数 2

我在Python 3中用海龟模块编写了一个简单的蛇游戏,每次蛇吃东西的时候都会慢一点,最终游戏是不可能玩的。

我想使速度恒定和足够快,你可以玩游戏,而不生气,因为蛇是移动的超级慢,但我不知道如何。

这是我到目前为止的代码,我还有一些其他的事情要做,但忽略那些。

代码语言:javascript
复制
#------------------------------------------SETUP------------------------------------------
#import statements
import turtle
import random

#variables
score = 0
up = True
down = False 
left = False
right = False
high_score=0

#background
wn = turtle.Screen()
wn.title("Snake Game")
wn.bgcolor("black")
wn.setup(width = 600, height =600) #set height and width of the screen

#create snake head
head = turtle.Turtle()
head.shape("square")
head.color("green")
head.penup()
head.goto(0,0)

#food
food = turtle.Turtle()
food.shape("square")
food.color("red")
food.penup()
food.speed = "fastest"
food.goto(0,100)

#segments
segments = []
segment_locationsy=[]
segment_locationsx=[]

#create pen to write score
pen = turtle.Turtle()
pen.speed(0)
pen.shape("square")
pen.color("green")
pen.penup()
pen.hideturtle()
pen.goto(0,260)
pen.pendown()

pen.write(pen.write("PRESS SPACE TO START" , align = "center", font=("Courier", 24, "normal")))
#------------------------------------------FUNCTIONS------------------------------------------
# define a function for each direction
#TODO: FIX REPETITIVENESS
def go_up():
    global up,down,left,right
    if (down != True):
        up = True
        down = False
        left = False
        right = False
def go_down():
    global up,down,left,right
    if (up != True):
        up = False
        down = True
        left = False
        right = False
def go_right():
    global up,down,left,right
    if (left != True):
        up = False
        down = False
        left = False
        right = True
def go_left():
    global up,down,left,right
    if (right != True):
        up = False
        down = False
        left = True
        right = False

#create a function to make the turtle move
def move():
    if up:
        head.sety(head.ycor()+5)
    if down:
        head.sety(head.ycor()-5)  
    if left: 
        head.setx(head.xcor()-5)
    if right:
        head.setx(head.xcor()+5)

#function to reset the food location
def reset_food():
    new_x = random.randint(-280,280)
    new_y = random.randint(-280,280)
    if (head.distance(new_x,new_y)<20): #make sure that apple doesn't overlap with the snake
        reset_food()
    for i in segments:
        if ((head.distance(i)<20)):
            reset_food()
    food.hideturtle()
    food.goto(new_x,new_y)
    food.showturtle()
#function to add to the score
def add_score():
    global score
    global high_score
    score = score + 1
    if (score >high_score):
        high_score = high_score + 1
    pen.clear()
    pen.write("Score:" + str(score) + " High score:" + str(high_score) , align = "center", font=("Courier", 24, "normal"))
    

def start_game():
    global up, down, left, right
    global score
    pen.goto(0,260)
    food.goto(0,100)
    score = 0
    up = True
    down = False 
    left = False
    right = False
    pen.clear()
    game_go = True
    segments = []
    segment_locationsy=[]
    segment_locationsx=[]
    head.goto(0,0)
    # call function every time screen is updated
    var = 0
    while game_go:
        wn.update()
        move()
        #assign a key for each function
        wn.listen()
        wn.onkeypress(go_up, "i")
        wn.onkeypress(go_down, "k")
        wn.onkeypress(go_left, "j")
        wn.onkeypress(go_right, "l")
        #reset snakehead when it touches the border, stop movement and reset direction, clear segments
        if (head.xcor() > 290) or (head.xcor() < -290) or (head.ycor() > 290) or (head.ycor() < -290):
            game_go = False
        # add a segment every time it touches food
        if (head.distance(food)<=15):
            segment = turtle.Turtle("square")
            segment.color("green")
            segment.hideturtle()
            segment.penup()
            segment.goto(head.xcor(),head.ycor())
            segment.showturtle()
            segment.speed("fastest")
            segments.append(segment)
            reset_food()
            add_score()
        #segments follow along
        segment_locationsy.append(head.ycor())
        segment_locationsx.append(head.xcor())
        var2 = 1
        for i in segments:
            i.setx(segment_locationsx[var-5*var2])
            i.sety(segment_locationsy[var-5*var2])
            if (head.distance(i) < 15): #snake dies if it touches itself
                game_go=False
            var2+=1
        var+=1


        #write game over
    pen.clear()
    pen.pu()
    pen.sety(pen.ycor()-50)
    pen.write("GAME OVER" , align = "center", font=("Courier", 24, "normal"))
    pen.pu()
    pen.sety(pen.ycor()-50)
    pen.write("press space to restart" , align = "center", font=("Courier", 24, "normal"))

    for i in segments:
        i.clear
        i.hideturtle()
    

#------------------------------------------RUN GAME------------------------------------------
while True:
    wn.listen()
    wn.onkeypress(start_game,"space")
    wn.update()

我已经尝试过查找这个问题,但只是变得更加混乱,无法在我的代码中实现任何东西(如果有人回答这个问题,请包括一个初学者友好的解释,如何解决这个问题,谢谢!)

EN

回答 1

Stack Overflow用户

发布于 2022-01-09 23:14:22

嗯,更多的片段意味着更多的时间来进行运动,我会尝试而不是移动所有的分段,把最后一段移到前面。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70645190

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档