r/pythonhelp • • Jun 26 '23

python snake game game height not defined?

I am making a snake game,but it gives me a name error that reads "line 56, in <module>
y = int((screen_height/2) - (window_height/2))
NameError: name 'window_height' is not defined"

here is my code:

from tkinter import *
import random
GAME_WIDTH = 700
GAME_HEIGHT = 700
SPEED = 50
SPACE_SIZE = 50
BODY_PARTS = 3
SNAKE_COLOR = "#00ff00"
FOOD_COLOR = "#ff00ff"
BACKGROUND_COLOR = "#000000"
class Snake:
pass
class food:
pass
def next_turn():
pass
def change_direction(new_direction) :
pass
def check_collisions():
pass
def game_over():
pass
window = Tk ()
window.title("snake game")
score = 0
direction = 'down'
label = Label(window, text="score{}".format(score),font=('consolas', 40))
label.pack()
canvas = Canvas(window, bg=BACKGROUND_COLOR, height=GAME_HEIGHT, width= GAME_WIDTH)
canvas.pack()
window.update()
window_width = window.winfo_width()
window.height = window.winfo_height()
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()
x = int((screen_width/2) - (window_width/2))
y = int((screen_height/2) - (window_height/2))
window.geometry(f"{window_width}x{window_height } + {x} +{y}")
window.mainloop()

0 Upvotes

1 comment sorted by

View all comments

•

u/AutoModerator Jun 26 '23

Note:

  • This sub went private for a few days recently in solidarity with other subs who are hoping to get Reddit to reconsider some changes that they have proposed. These changes will affect the Reddit API and many third-party apps that access Reddit. If you are not already aware of the proposed changes, please read up on the topic and the ongoing protest.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.