|
| 1 | +import turtle |
| 2 | +import pandas |
| 3 | + |
| 4 | +# constant in the game |
| 5 | +BLANK_STATES_IMAGE_WIDTH = 725 |
| 6 | +BLANK_STATES_IMAGE_HEIGHT = 495 |
| 7 | +STATE_FONT = ("Verdana", 7, "normal") |
| 8 | +WELL_DONE_FONT = ("Verdana", 40, "normal") |
| 9 | +WELL_DONE_X_SPOT = 0 |
| 10 | +WELL_DONE_Y_SPOT = 0 |
| 11 | +ALIGN = 'center' |
| 12 | +NUM_STATES = 50 |
| 13 | +X_COLUMN = 1 |
| 14 | +Y_COLUMN = 2 |
| 15 | +FIRST_USER_SCORE = 0 |
| 16 | +WELL_DONE_TEXT = "well done!!" |
| 17 | + |
| 18 | +pen = turtle.Turtle() |
| 19 | +screen = turtle.Screen() |
| 20 | +screen.title("liron us states game") |
| 21 | +image = "blank_states_img.gif" |
| 22 | +screen.addshape(image) |
| 23 | +turtle.shape(image) |
| 24 | +screen.setup(width=BLANK_STATES_IMAGE_WIDTH, height=BLANK_STATES_IMAGE_HEIGHT) |
| 25 | +# making data frame |
| 26 | +data = pandas.read_csv("50_states.csv") |
| 27 | +# making the state list and lower all the items |
| 28 | +data_state_list = data.state.to_list() |
| 29 | +data_state_list = [each_state.lower() for each_state in data_state_list] |
| 30 | + |
| 31 | + |
| 32 | +def main(): |
| 33 | + user_need_to_learn_list = [] |
| 34 | + user_score = FIRST_USER_SCORE |
| 35 | + user_right_answer = [] |
| 36 | + print("welcome to liron us state game!!! if you done with the game enter 'Exit' ") |
| 37 | + while user_score < NUM_STATES: |
| 38 | + user_answer = screen.textinput(title=f"{user_score}/50", prompt="what's another state name?") |
| 39 | + if user_answer == "Exit": |
| 40 | + user_need_to_learn_list = [state for state in data.state if str(state).lower() not in user_right_answer] |
| 41 | + data_frame_user_need_to_learn = pandas.DataFrame(user_need_to_learn_list) |
| 42 | + data_frame_user_need_to_learn.to_csv("need_to_learn.csv") |
| 43 | + break |
| 44 | + for index, state in enumerate(data.state): |
| 45 | + if str(user_answer).lower() == str(data.iloc[index, 0]).lower() and str( |
| 46 | + user_answer).lower() not in user_right_answer: |
| 47 | + write_on_screen(data.iloc[index, X_COLUMN], data.iloc[index, Y_COLUMN], user_answer.lower(), STATE_FONT) |
| 48 | + user_score += 1 |
| 49 | + user_right_answer.append(user_answer) |
| 50 | + # looping for all the states and checking if the user was right |
| 51 | + |
| 52 | + if user_score == NUM_STATES: |
| 53 | + write_on_screen(WELL_DONE_X_SPOT, WELL_DONE_Y_SPOT, WELL_DONE_TEXT, WELL_DONE_FONT) |
| 54 | + turtle.mainloop() |
| 55 | + |
| 56 | + |
| 57 | +def write_on_screen(x_loc: int, y_loc: int, text: str, font: tuple) -> None: |
| 58 | + """ |
| 59 | + update the score of the users and show to screen |
| 60 | + Parameters: x_loc: int - the x coordinate of text, y_loc: int - the y coordinate of text |
| 61 | + text: str - the text data, font: tuple - the font style |
| 62 | + Returns: None |
| 63 | + """ |
| 64 | + pen.penup() |
| 65 | + pen.hideturtle() |
| 66 | + pen.color("black") |
| 67 | + pen.goto(x_loc, y_loc) |
| 68 | + pen.write(text, font=font, align=ALIGN) |
| 69 | + |
| 70 | + |
| 71 | +if __name__ == "__main__": |
| 72 | + main() |
0 commit comments