Created
December 6, 2023 22:18
-
-
Save Cistress/e20485f8900e4c1d51a8ca74082a5692 to your computer and use it in GitHub Desktop.
So I have been trying to create a login interface on my snake game project. To do that I use customtkinter. I have successfully created the UI and the main window, the only problem is I do not why "while root.login_boxes.login_result:" at main.py line 55 would not be executed. I have tested the value of "login_result" and it was true when I gave…
This file contains hidden or 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
| 10 |
This file contains hidden or 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
| from turtle import Turtle | |
| import random | |
| class Food(Turtle): | |
| def __init__(self): | |
| super().__init__() | |
| self.shape("circle") | |
| self.penup() | |
| # normally circle is set to 20x20 pixel | |
| self.shapesize(stretch_wid= 0.5, stretch_len= 0.5) | |
| self.color("blue") | |
| self.speed("fastest") | |
| self.refresh() | |
| def refresh(self): | |
| self.goto(x=random.randint(-280, 280), y=random.randint(-280, 280)) |
This file contains hidden or 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 customtkinter as ctk | |
| class login_interface(ctk.CTkFrame): | |
| def __init__(self,master, ): | |
| super().__init__(master) | |
| # accounts | |
| self.accounts = { | |
| "Timothy": "12345", | |
| "Kamil": "54321", | |
| "Jonas": "67890" | |
| } | |
| # User entry | |
| self.username = ctk.CTkLabel(self, text="Username").grid(row=1, column=0, pady=10) | |
| self.username_entry = ctk.CTkEntry(self) | |
| self.username_entry.grid(row=1, column=1, pady=10) | |
| # Password entry | |
| self.password = ctk.CTkLabel(self, text="Password").grid(row=2, column=0, pady=10) | |
| self.password_entry = ctk.CTkEntry(self) | |
| self.password_entry.grid(row=2, column=1, pady=10) | |
| # log in button | |
| self.button = ctk.CTkButton(self, text="Log in", command= self.perform) | |
| self.button.grid(row=3, column=1, padx=10, pady=10) | |
| self.login_result = False | |
| def perform(self): | |
| entered_username = self.username_entry.get() | |
| entered_password = self.password_entry.get() | |
| if entered_username in self.accounts and self.accounts[entered_username] == entered_password: | |
| self.login_result = True | |
| print("Log in successful") | |
| return self.login_result | |
| else: | |
| self.login_result = False | |
| print("Log in unsuccessful") | |
| return self.login_result | |
| class App(ctk.CTk): | |
| def __init__(self): | |
| super().__init__() | |
| self.title("my app") | |
| self.geometry("240x165") | |
| self.login_boxes = login_interface(self) | |
| self.login_boxes.grid(row=0, column=0, padx=10, pady=(10, 0), sticky="nsew") | |
| self.mainloop() |
This file contains hidden or 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
| # Turtle is gray out because it is not used in anywhere in the script | |
| from turtle import Turtle,Screen | |
| import time | |
| from Day20_Snake import Snake | |
| from Day20_Food_Class import Food | |
| from Day20_Scoreboard import scoreboard | |
| from Day20_Log_in_interface import login_interface, App | |
| screen = Screen() | |
| screen.setup(width= 600, height= 600) | |
| screen.bgcolor("black") | |
| screen.title("Snake Game") | |
| screen.tracer(0) | |
| # instances | |
| snake = Snake() | |
| food = Food() | |
| scoreboard = scoreboard() | |
| root = App() | |
| login = login_interface | |
| if login.login_result: | |
| print("12345") | |
| # if : | |
| # root.destroy() | |
| # if root.login_boxes.login_result: | |
| # root.destroy() | |
| # controller | |
| screen.listen() | |
| screen.onkey(snake.up, "Up") | |
| screen.onkey(snake.down, "Down") | |
| screen.onkey(snake.left, "Left") | |
| screen.onkey(snake.right, "Right") | |
| # The computer didn't run the part below | |
| # ----------------------------------------# | |
| # Check if login is successful | |
| # if not login.perform(): | |
| # main_window = App() | |
| # else: | |
| # # Handle unsuccessful login (e.g., exit the game or show a message) | |
| # main_window.quit() | |
| # print("Login successful. Exiting the game.") | |
| # quit() | |
| # ----------------------------------------# | |
| # Run the Tkinter event loop | |
| while root.login_boxes.login_result: | |
| print("GAME ON") | |
| screen.update() | |
| time.sleep(0.1) # update what happens in the code with a 0,1 second interval | |
| # step into the position of the block ahead | |
| snake.move() | |
| # Detect collision with food. | |
| if snake.head.distance(food) < 15: | |
| food.refresh() | |
| snake.extend() | |
| scoreboard.increase_score() | |
| # Detect collision with wall | |
| if snake.head.xcor() > 290 or snake.head.xcor() < -290 or snake.head.ycor() > 290 or snake.head.ycor() < -290: | |
| scoreboard.reset() | |
| snake.reset() | |
| #Detect collision with the body | |
| for elements in snake.segments[1:]: | |
| if snake.head.distance(elements) < 10: | |
| scoreboard.reset() | |
| snake.reset() | |
| #-------- super class -------- | |
| #class Fish(Animal): | |
| # def __init__(self): | |
| # super().__init__() | |
| #------- example ---------- | |
| # class Animal: | |
| # def __int__(self): | |
| # self.num_eyes = 2 | |
| # | |
| # def breathe(self): | |
| # print("Inhale, exhale.") | |
| # | |
| # class Fish(Animal): | |
| # def __init__(self): | |
| # super().__init__() | |
| # | |
| # def swim(self): | |
| # print("moving in water.") | |
| screen.exitonclick() |
This file contains hidden or 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
| from turtle import Turtle | |
| alignment = "center" | |
| font = ("Arial", 20, "normal") | |
| with open("data.txt") as file: | |
| high_score = int(file.read()) | |
| class scoreboard(Turtle): | |
| def __init__(self): | |
| super().__init__() | |
| # The default color of Turtle is black | |
| self.color("white") | |
| # scores | |
| self.current_score = 0 | |
| self.high_score = high_score | |
| # move to the position that we would like | |
| self.penup() | |
| self.goto(0,270) | |
| self.update_scoreboard() | |
| # Without hiding the turtle, it will only show the turtle | |
| self.hideturtle() | |
| # def game_over(self): | |
| # self.goto(0,0) | |
| # self.write(arg= "Game Over", align=alignment, font=font) | |
| def update_scoreboard(self): | |
| self.clear() | |
| self.write(arg=f"Score: {self.current_score} Highest Score: {self.high_score}", align="center", font=("Arial", 20, "normal")) | |
| def increase_score(self): | |
| self.current_score += 1 | |
| self.update_scoreboard() | |
| def reset(self): | |
| if self.current_score > self.high_score: | |
| self.high_score = self.current_score | |
| with open("data.txt", mode = "w") as file: | |
| file.write(str(self.high_score)) | |
| # self.high_score = file.read() | |
| self.current_score = 0 | |
| self.update_scoreboard() |
This file contains hidden or 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
| from turtle import Turtle,Screen | |
| movement = 20 | |
| up = 90 | |
| down = 270 | |
| left = 180 | |
| right = 0 | |
| class Snake: | |
| def __init__(self): | |
| # using a list to create our snake at the end | |
| self.segments = [] | |
| self.create_snake() | |
| self.head = self.segments[0] | |
| def create_snake(self): | |
| for _ in range(3): | |
| x = 0 | |
| new_segment = Turtle() | |
| new_segment.color("white") | |
| new_segment.shape("square") | |
| new_segment.penup() | |
| new_segment.setpos(x = x,y = 0) | |
| self.segments.append(new_segment) | |
| x += 20 | |
| def extend(self): | |
| new_segment = Turtle() | |
| new_segment.color("white") | |
| new_segment.shape("square") | |
| new_segment.penup() | |
| new_segment.setpos(self.segments[-1].position()) | |
| self.segments.append(new_segment) | |
| def move(self): | |
| for elements in range(len(self.segments) - 1, 0, -1): | |
| new_x = self.segments[elements - 1].xcor() | |
| new_y = self.segments[elements - 1].ycor() | |
| self.segments[elements].goto(new_x, new_y) | |
| self.segments[0].forward(movement) | |
| def reset(self): | |
| for _ in self.segments: | |
| _.goto(x = 1000, y = 1000) | |
| self.segments.clear() | |
| self.create_snake() | |
| self.head = self.segments[0] | |
| def control(self): | |
| screen = Screen() | |
| def up(self): | |
| if self.head.heading() != down: | |
| self.head.setheading(up) | |
| def right(self): | |
| if self.head.heading() != left: | |
| self.head.setheading(right) | |
| def left(self): | |
| if self.head.heading() != right: | |
| self.head.setheading(left) | |
| def down(self): | |
| if self.head.heading() != up: | |
| self.head.setheading(down) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment