Created
February 13, 2023 14:42
-
-
Save horstjens/830eab4b9b93d89ebb8ccb74fb3d3ace to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import flet as ft | |
import random | |
class Game: | |
versuch = 1 | |
log = [] | |
def main(page: ft.Page): | |
secret = random.randint(1,1000) | |
page.title = "Zahlenratespiel" | |
page.vertical_alignment = ft.MainAxisAlignment.CENTER | |
page.scroll = ft.ScrollMode.AUTO | |
txt_number = ft.TextField(value="0", | |
text_align=ft.TextAlign.CENTER, width=100) | |
label_versuch = ft.Text("Versuch 1:") | |
#label_log = ft.Text("Make your first guess!", | |
# width = 700, | |
# height = 100, | |
# text_align=ft.TextAlign.CENTER, | |
# overflow = ft.TextOverflow.ELLIPSIS,) | |
label_log = ft.ListView(expand=1, spacing=10, padding=20, | |
auto_scroll=False) | |
def minus_click(e): | |
txt_number.value = str(int(txt_number.value) - 1) | |
page.update() | |
def plus_click(e): | |
txt_number.value = str(int(txt_number.value) + 1) | |
page.update() | |
def guess(e): | |
zahl = int(txt_number.value) | |
if zahl == secret: | |
meldung = "Correct" | |
elif zahl < secret: | |
meldung = f"{zahl} ist zu klein" | |
else: | |
meldung = f"{zahl} ist zu groß" | |
page.title = meldung | |
logtext = f"Versuch {Game.versuch}: {meldung}" | |
#txt_number.value="" | |
Game.versuch += 1 | |
label_versuch.value = f"Versuch {Game.versuch}:" | |
label_log.controls.insert(0,ft.Text(logtext,text_align=ft.TextAlign.CENTER)) | |
page.update() | |
page.add( | |
ft.Row( | |
[label_versuch,], | |
alignment=ft.MainAxisAlignment.CENTER, | |
), | |
ft.Row( | |
[ft.IconButton(ft.icons.REMOVE, on_click=minus_click), | |
txt_number, | |
ft.IconButton(ft.icons.ADD, on_click=plus_click), | |
ft.ElevatedButton("OK", on_click=guess), | |
], alignment=ft.MainAxisAlignment.CENTER, | |
), | |
ft.Row([label_log],alignment=ft.MainAxisAlignment.CENTER, ), | |
) | |
ft.app(target=main) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment