0

I am trying to check in def_2_is_apple if the variable APPLE is on the game board in def_1_print_game_board but if i try it like that i only get that i need to define row and column in def_2

def_2_is_apple is a bool and should be set to True if the variable APPLE is in def_1_print_game_board


import random

BOARD_WIDTH = 8
BOARD_HEIGHT = 8
SNAKE = \[""\]
ORIENTATION = 4
APPLE = "A0"
APPLE_LIVES = 12
APPLE_GOT_EATEN = False
LIVES = 3
SCORE = 0
BIGGER_SNAKE = False


def _2_is_apple(row, column):
    global BOARD_HEIGHT
    global BOARD_WIDTH

    for row in range(BOARD_HEIGHT):
        for column in range(BOARD_WIDTH):
            if APPLE == f"{chr(row + 65)}{column}":
                return True
            else:
                return False
pass



def _1_print_game_board():
    global Board_HEIGHT
    global Board_WIDTH
     # print the game field

    print(f"Lives: {LIVES} - Apple Lives: {APPLE_LIVES} - Score: {SCORE}")
    print("----------------------------")
    for i in range(BOARD_HEIGHT):
        print(f"{chr(i + 65)} |", end="")
        for j in range(BOARD_WIDTH):
            if APPLE == f"{chr(i + 65)}{j}":
                print(" O ", end="")
            elif _3_is_snake == 1:
                print(" + ", end="")
            elif _3_is_snake == 2:
                print(" ∧ ", end="")
            elif _3_is_snake == 3:
                print(" < ", end="")
            elif _3_is_snake == 4:
                print(" v ", end="")
            elif _3_is_snake == 5:
                print(" > ", end="")
            else:
                print("   ", end="")
        print("|", end="")
        print()
    print("----------------------------")
    print("    0  1  2  3  4  5  6  7")
    pass


def main():
      #call the functions in the right order
    _1_print_game_board()
    _2_is_apple()
    _3_is_snake
    _4_move_snake()
    _5_detect_collision()
    _6_spawn_apple()
pass


if __name__ == '__main__':
    main()

tried deleting the row and column from def_2 and letting it empty but the functions seems not to intergate with def_1

3
  • Ok, from I understand from your (very messy, sorry to say it) code, your function _2_is_apple shouldn't have arguments, so it should be defined with : def _2_is_apple(): (by the way, you don't need the global Board_HEIGHT and global Board_WIDTH statements unless you want to MODIFY their values from within the function (which you obviously don't)). Commented Oct 28, 2022 at 13:21
  • Furthermore, in it's current state, it will only return True after the FIRST test, so when APPLE="A0" and False for any other legal position ("G5") for example; to fix this, remove the "else:" line, and unindent "return False" to position it at the same level as "for row..." (so it's only executed if no legal position returns True). Commented Oct 28, 2022 at 13:27
  • Have you noticed that _2_is_apple() always returns True and wondered why that might be? Commented Oct 28, 2022 at 13:29

1 Answer 1

1

To sum up what I said in my comments, here is a correct code for the _2_is_apple function (independently of the rest of the code):

def _2_is_apple():
    for row in range(BOARD_HEIGHT):
        for column in range(BOARD_WIDTH):
            if APPLE == f"{chr(row + 65)}{column}":
                return True
    return False
            
APPLE = "A0"            
print(_2_is_apple())
# True
APPLE = "G5"
print(_2_is_apple())
# True
APPLE = "B9"
print(_2_is_apple())
# False
APPLE = "Z1"
print(_2_is_apple())
# False
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for letting me know:) yea that fixed everything. Also cleaned the code a bit looks better now
Nice; have fun on your Python learning/game building endeavour :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.