Skip to content

Commit

Permalink
Take off unnecessary comments to snakegame.py
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinB1lo authored Sep 25, 2024
1 parent 553cbc4 commit 3aa78d4
Showing 1 changed file with 4 additions and 21 deletions.
25 changes: 4 additions & 21 deletions snakegame.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ def __init__(self):
def next_turn(snake, food):
global score, direction

# Move player's snake
x, y = snake.coordinates[0]
if direction == "up":
y -= SPACE_SIZE
Expand All @@ -53,7 +52,6 @@ def next_turn(snake, food):
square = canvas.create_rectangle(x, y, x + SPACE_SIZE, y + SPACE_SIZE, fill=SNAKE_COLOR, outline='#000000')
snake.squares.insert(0, square)

# Check if the snake has eaten the food
if x == food.coordinates[0] and y == food.coordinates[1]:
score += 1
label.config(text=f"Score: {score}")
Expand All @@ -64,11 +62,9 @@ def next_turn(snake, food):
canvas.delete(snake.squares[-1])
del snake.squares[-1]

# Check for collisions (walls or self-collision)
if check_collisions(snake):
game_over()
else:
# Schedule the next turn
window.after(SPEED, next_turn, snake, food)


Expand Down Expand Up @@ -103,74 +99,61 @@ def game_over():
canvas.create_text(canvas.winfo_width() / 2, canvas.winfo_height() / 2 - 40,
font=('consolas', 70), text="GAME OVER", fill='red', tag='gameover')

# Create "Play Again" button
play_again_button = Button(window, text="Play Again", command=reset_game, font=('consolas', 20))
play_again_button.pack()

# Position the button below the "GAME OVER" message
canvas.create_window(canvas.winfo_width() / 2, canvas.winfo_height() / 2 + 40, window=play_again_button)


def reset_game():
global score, snake, food, direction

# Clear the canvas and reset the game variables
canvas.delete(ALL)

# Reset score and direction
score = 0
label.config(text=f"Score: {score}")

# Reinitialize the snake and food
snake = Snake()
food = Food()
direction = 'right'

# Start the game again
next_turn(snake, food)

# Remove the "Play Again" button
for widget in window.pack_slaves():
if isinstance(widget, Button):
widget.pack_forget()


# Set up the window
window = Tk()
window.title("SnakeGame")

# Set the window geometry to match the game area, ensuring it matches the canvas size
window.geometry(f"{GAME_WIDTH+4}x{GAME_HEIGHT+4}") # Extra space for padding or borders
window.geometry(f"{GAME_WIDTH+4}x{GAME_HEIGHT+4}")
window.resizable(False, False)

# Set up the score

score = 0
direction = "right"
label = Label(window, text="Score:{}".format(score), font=("Arial", 20))
label.pack()

# Create the game canvas with the exact dimensions
canvas = Canvas(window, bg=BACKGROUND_COLOR, height=GAME_HEIGHT, width=GAME_WIDTH)
canvas.pack()

# Center the window on the screen
window.update()
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()
x = int((screen_width / 2) - (GAME_WIDTH / 2))
y = int((screen_height / 2) - (GAME_HEIGHT / 2))
window.geometry(f"{GAME_WIDTH+4}x{GAME_HEIGHT+4}+{x}+{y}")

# Bind the arrow keys to control the snake
window.bind('<Left>', lambda event: change_direction("left"))
window.bind('<Right>', lambda event: change_direction("right"))
window.bind('<Up>', lambda event: change_direction("up"))
window.bind('<Down>', lambda event: change_direction("down"))

# Initialize the snake and food, and start the game
snake = Snake()
food = Food()
next_turn(snake, food)

# Start the game loop
window.mainloop()

window.mainloop()

0 comments on commit 3aa78d4

Please sign in to comment.