Home > Net >  How to add code based on count of something when i dont know the exact count
How to add code based on count of something when i dont know the exact count

Time:07-12

I´m trying to make a snake game without graphics to train with lists. Game pretty much works but I need to find a way how to make the snake longer when he eats food. Well I know how to do it manually, write the same thing 100 times with different numbers and use if, but how to automate it? I know there is some better way to code this game, but is it possible to do it with this and just change something? I tried to do it with for x in range, but didn´t work out, maybe I did it wrong I don´t know.

def snake():
    y = 5 #starting point on Y axis
    x = 9 #starting point on X axis
    length = 10 #length of the map
    width = 10 #width of the map
    food = [(2, 3), (4, 5)] #coordinates of food
    coordinates = [(y, x)] #where head of the snake currently is
    food2 = [(y, x)] #current position   position of food
    fcount = 0 #number of ate food
    erase = 0
    way = 1

    while way != "stop":
        way = input("Choose your way, use wasd: ")
        if way == "s":
            y = y   1
        elif way == "w":
            y = y - 1
        elif way == "d":
            x = x   1
        elif way == "a":
            x = x - 1


        coordinates.append([(y, x)])
        
        food2.append((y, x))

        if y < 0:
            raise ValueError("This doesn´t work")
        elif x < 0:
            raise ValueError("This doesnt´t work")
        
        if food2[-1] in food:
            fcount = fcount   1



        for g in range(length):
            
            for z in range(width):
                position = (g, z)

                
                    
                if position in coordinates[-1]:
                    print("x", end = " ")
                elif position in coordinates[-2]: #I need to make something like this for all the food therecould be
                    if fcount > 0:
                        print("x", end = " ")
                        erase = erase   1
                    else:
                        print(".", end = " ")

                elif position in food:
                    print("?", end = " ")
                else:
                    print(".", end = " ")
                

                while erase == 1:
                    if fcount == 1:
                        food.remove(food2[-1])
                        erase = erase   1
                        break
                    break
                    
            print()

    print(fcount)

print(snake())

CodePudding user response:

There are some other problems that I see, but if you only want to find a way to grow a snake in, as you say, 'auotomatic' mode, you can try calling print('x') when position in coordinates[-1-fcount:].

I have slightly modified your code to show how this can be done. However, as I said, I see other problems, and when someone tries to solve them, it may turn out that the way to grow a snake can be done differently.

def snake():
    y = 5 #starting point on Y axis
    x = 9 #starting point on X axis
    length = 10 #length of the map
    width = 10 #width of the map
    food = [(2, 3), (4, 5)] #coordinates of food
    coordinates = [(y, x)] #where head of the snake currently is
    fcount = 0 #number of ate food
    way = 1

    while way != "stop":
        way = input("Choose your way, use wasd: ")
        if way == "s":
            y = y   1
        elif way == "w":
            y = y - 1
        elif way == "d":
            x = x   1
        elif way == "a":
            x = x - 1

        coordinates.append((y, x))
                
        if (y,x) in food:
            fcount = fcount   1
            food.remove((y,x))
            
        for g in range(length):
            for z in range(width):
                position = (g, z)
                if position in coordinates[-1-fcount:]:
                    print("x", end = " ")
                elif position in food:
                    print("?", end = " ")
                else:
                    print(".", end = " ")                    
            print()


snake()
  • Related